Skip to content

Prisma ​

Swap the notebook's repository for Prisma without changing its routes. This recipe uses Prisma 6.12.0 and SQLite.

Schema and generated client ​

prisma
generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "sqlite"
  url      = env("PRISMA_DATABASE_URL")
}

model Note {
  id   Int    @id @default(autoincrement())
  text String
}

From the repository root:

sh
PRISMA_DATABASE_URL=file:./notebook.db npx prisma generate --schema docs/samples/prisma/schema.prisma
PRISMA_DATABASE_URL=file:./notebook.db npx prisma db push --schema docs/samples/prisma/schema.prisma

db push is for a disposable development database. Use Prisma migrations and migrate deploy for a reviewed production schema. Relative SQLite URLs in the CLI resolve relative to the schema file.

Match the application's contract ​

ts
import { PrismaClient } from "@prisma/client";
import type { NoteStore } from "../tutorial/store.js";

export async function openPrismaStore(url: string) {
  const prisma = new PrismaClient({ datasources: { db: { url } } });
  await prisma.$connect();
  return {
    async list() {
      return prisma.note.findMany({ orderBy: { id: "asc" } });
    },
    async add(text: string) {
      return prisma.$transaction((tx) => tx.note.create({ data: { text } }));
    },
    async close() {
      await prisma.$disconnect();
    },
  } satisfies NoteStore;
}

Pass the returned repository to createNotebook(store) from the tutorial. Use an absolute file: URL if the application working directory differs from the schema directory. Create one client for the app, not one per handler.

Prisma owns pooling, parameterization, and transactions; Brevity owns the HTTP response. The async close() must be awaited by your entry point after the server stops accepting/drains requests.

Run npm run docs:check to generate the client and test the SQLite repository. Prisma 7 hasn't been checked with this recipe.

Small core. Build your way.