Next.js Discord

Discord Forum

node:sqlite - in memory DB is not updating

Unanswered
Eastern Carpenter bee posted this in #help-forum
Open in Discord
Eastern Carpenter beeOP
, ?so when i am not using react server actions and doing DB modifications with next api route, sqlite DB dont get updated, but with server actions DB is getting updated. What can be the reason for this?

import {DatabaseSync} from "node:sqlite";

const database = new DatabaseSync(":memory")

database.exec(`
  CREATE TABLE data(
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    title TEXT,
    price INTEGER,
    description TEXT
  ) 
`)

export async function addProduct(title, price, description) {
  const db = database.prepare("INSERT INTO data(title, name, description) VALUES (?, ?, ?)")
  return  db.run(title, price, description)
}

9 Replies

Whats your next version?
And share code for your api route
@Anay-208 | Ping in replies Whats your next version?
Eastern Carpenter beeOP
next js 15
import { addProduct } from "@db";

export async function POST(request: Request) {
  const body = await request.json();
  const { title, price, description } = body;
  const product = await addProduct(title, parseInt(price), description);
  return new Response(JSON.stringify(product), {
    headers: { "Content-Type": "application/json" },
  });
}
"use client";

import { useRouter } from "next/navigation";
import { useState } from "react";

export default function CreateProduct() {
  const [title, setTitle] = useState("");
  const [price, setPrice] = useState("");
  const [description, setDescription] = useState("");
  const [loading, setLoading] = useState(false);

  const router = useRouter();

  const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
    e.preventDefault();
    setLoading(true);
    try {
      const response = await fetch("/react-form/api", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ title, price, description }),
      });
      if (response.ok) {
        router.push("/products-db");
      }
    } catch (error) {
      console.error("Error:", error);
    } finally {
      setLoading(false);
    }
  };

  return (
    <form onSubmit={handleSubmit} className="p-4 space-y-4 max-w-96">
      <label className="text-white">
        Title
        <input
          type="text"
          className="block w-full p-2 text-black border rounded"
          name="title"
          onChange={(e) => setTitle(e.target.value)}
        />
      </label>
      <label className="text-white">
        Price
        <input
          type="number"
          className="block w-full p-2 text-black border rounded"
          name="price"
          onChange={(e) => setPrice(e.target.value)}
        />
      </label>
      <label className="text-white">
        Description
        <textarea
          className="block w-full p-2 text-black border rounded"
          name="description"
          onChange={(e) => setDescription(e.target.value)}
        />
      </label>
      <button
        type="submit"
        className="block w-full p-2 text-white bg-blue-500 rounded disabled:bg-gray-500"
        disabled={loading}
      >
        {loading ? "Submitting..." : "Submit"}
      </button>
    </form>
  );
}
when using actions and when doing it manually too
I'm not sure if it'd help, but can you try deleting .next folder