Prisma can now generate an ESM version of the client, so let's use it with Deno.
$ deno --version
deno 2.2.12 (stable, release, x86_64-unknown-linux-gnu)
v8 13.5.212.10-rusty
typescript 5.7.3
First, install the Prisma libraries.
deno add npm:prisma npm:@prisma/client
Then run prisma init
.
deno run -A npm:prisma init --datasource-provider sqlite --generator-provider prisma-client --with-model
Open the generated schema.prisma
and set moduleFormat
to esm
.
provider
and moduleFormat
are important.
generator client {
provider = "prisma-client"
output = "../src/generated/prisma"
+ moduleFormat = "esm"
}
datasource db {
provider = "sqlite"
url = env("DATABASE_URL")
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
}
Initialize the database.
deno run -A --env-file npm:prisma migrate dev --name init
Create a script to add a User and one to list Users, then run them:
$ deno run -A --env-file src/createUser.ts
{ id: 1, email: "[email protected]", name: "user481" }
$ deno run -A --env-file src/createUser.ts
{ id: 2, email: "[email protected]", name: "user396" }
$ deno run -A --env-file src/listUsers.ts
[
{ id: 1, email: "[email protected]", name: "user481" },
{ id: 2, email: "[email protected]", name: "user396" }
]
It works without issues.
Next, try migrations by adding a Post model.
...
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
+ posts Post[]
}
+ model Post {
+ id Int @id @default(autoincrement())
+ title String
+ content String?
+ published Boolean @default(false)
+ author User @relation(fields: [authorId], references: [id])
+ authorId Int
+ }
Run the migration.
Warning
This appears to be a bug, but if client code already exists, the code generation fails.
For now, remove the existing generated code before running prisma migrate
or prisma generate
.
$ rm -rf src/generated/
$ deno run -A --env-file npm:prisma migrate dev --name add-post-model
Create a script to add a Post and one to list them, then run them:
$ deno run -A --env-file src/createPost.ts
{
id: 1,
title: "Post Title 137",
content: "This is the content of Post Title 137",
published: true,
authorId: 1
}
$ deno run -A --env-file src/createPost.ts
{
id: 2,
title: "Post Title 347",
content: "This is the content of Post Title 347",
published: true,
authorId: 1
}
$ deno run -A --env-file src/listPostsByUser.ts
[
{
id: 1,
title: "Post Title 137",
content: "This is the content of Post Title 137",
published: true,
authorId: 1
},
{
id: 2,
title: "Post Title 347",
content: "This is the content of Post Title 347",
published: true,
authorId: 1
}
]
It also runs without problems.