@typesql/prisma
v4.0.1
Published
Use a Prisma schema as a TypeSQL schema: convert the generated client's types, or generate an exact schema with the typesql-prisma generator.
Maintainers
Readme
@typesql/prisma
Write TypeSQL queries against your Prisma schema, and run them through Prisma Client.
Install
pnpm add @typesql/core @typesql/prismaRequires @prisma/client 7 or later, with the postgresql or sqlite provider.
Usage
Add the generator to schema.prisma:
generator typesql {
provider = "typesql-prisma"
output = "../src/db/typesql.ts"
}Every prisma generate then writes a DB type with the database's own table and column names, native types, defaults and enums. Pass it to createSql with your Prisma Client:
import { createSql } from "@typesql/prisma";
import { postgres } from "@typesql/core/postgres";
import type { DB } from "./db/typesql";
const sql = createSql<DB>(postgres, prisma);
const users = await sql("SELECT id, display_name FROM users WHERE email = $1", email).execute();
await sql.transaction(async (tx) => {
await tx("DELETE FROM posts WHERE author_id = $1", 1).execute();
});Open an example in the TypeScript Playground to hover the types and edit the SQL, with nothing to install.
Good to know:
- Rows are typed the way Prisma's raw queries return them:
int8(socount(*)) isbigint,numericisDecimal. uuid(),cuid(),nanoid(),ulid()and@updatedAtare filled in by Prisma Client, not the database, so a SQLINSERTmust set them.- Without the generator,
createSql(postgres, prisma)reads the types Prisma Client already generates. They don't record@mapnames or native types, so you may need overrides.
SQLite
For a SQLite database, pass sqlite from @typesql/core/sqlite instead: createSql(sqlite, db). The schema is converted to SQLite types; see SQLite.
Documentation
See From Kysely, Drizzle or Prisma for the generator's options, what it emits and overrides, and Type mapping for the row types.
