Integrate Prisma with Platformatic DB
Prisma is an open-source ORM for Node.js and TypeScript. It is used as an alternative to writing SQL, or using another database access tool such as SQL query builders (like knex.js) or ORMs (like TypeORM and Sequelize). Prisma currently supports PostgreSQL, MySQL, SQL Server, SQLite, MongoDB, and CockroachDB.
Prisma can be used with JavaScript or TypeScript, and provides a level to type-safety that goes beyond the guarantees made by other ORMs in the TypeScript ecosystem. You can find an in-depth comparison of Prisma against other ORMs here.
If you want to get a quick overview of how Prisma works, you can follow the Quickstart or read the Introduction in the Prisma documentation.
How Prisma can improve your workflow with Platformatic DB
While Platformatic speeds up development of your REST and GraphQL APIs, Prisma can complement the workflow in several ways:
- Provides an intuitive data modeling language
- Provides auto-generated and customizable SQL migrations
- Provides type-safety and auto-completion for your database queries
You can learn more about why Prisma and Platformatic are a great match this article.
Prerequisites
To follow along with this guide, you will need to have the following:
- Node.js >= v18.8.0 or >= v20.6.0
- npm v7 or later
- A code editor, for example Visual Studio Code
- A Platformatic DB project
Setup Prisma
Install the Prisma CLI and the db-diff
development dependencies in your project:
npm install --save-dev prisma @ruheni/db-diff
Next, initialize Prisma in your project
npx prisma init
This command does the following:
- Creates a new directory called
prisma
which contains a file calledschema.prisma
. This file defines your database connection and the Prisma Client generator. - Creates a
.env
file at the root of your project if it doesn't exist. This defines your environment variables (used for your database connection).
You can specify your preferred database provider using the --datasource-provider
flag, followed by the name of the provider:
npx prisma init --datasource-provider postgresql # or sqlite, mysql, sqlserver, cockroachdb
Prisma uses the DATABASE_URL
environment variable to connect to your database to sync your database and Prisma schema. It also uses the variable to connect to your database to run your Prisma Client queries.
If you're using PostgreSQL, MySQL, SQL Server, or CockroachDB, ensure that the DATABASE_URL
used by Prisma is the same as the one used by Platformatic DB project. If you're using SQLite, refer to the Using Prisma with SQLite section.
If you have an existing project, refer to the Adding Prisma to an existing Platformatic DB project section. If you're adding Prisma to a new project, refer to the Adding Prisma to a new project.
Adding Prisma to an existing project
If you have an existing Platformatic DB project, you can introspect your database and generate the data model in your Prisma schema with the following command:
npx prisma db pull
The command will introspect your database and generate the data model
Next, add the @@ignore
attribute to the versions
model to exclude it from the Prisma Client API:
model versions {
version BigInt @id
name String?
md5 String?
run_at DateTime? @db.Timestamptz(6)
+ @@ignore
}
To learn how you can evolve your database schema, you can jump to the Evolving your database schema section.
Adding Prisma to a new project
Define a Post
model with the following fields at the end of your schema.prisma
file:
model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
viewCount Int @default(0)
createdAt DateTime @default(now())
@@map("posts")
}
The snippet above defines a Post
model with the following fields and properties:
id
: An auto-incrementing integer that will be the primary key for the model.title
: A non-nullableString
field.content
: A nullableString
field.published
: ABoolean
field with a default value of false.viewCount
: AnInt
field with a default value of 0.createdAt
: ADateTime
field with a timestamp of when the value is created as its default value.
By default, Prisma maps the model name and its format to the table name — which is also used in Prisma Client. Platformatic DB uses a snake casing and pluralized table names to map your table names to the generated API. The @@map()
attribute in the Prisma schema allows you to define the name and format of your table names to be used in your database. You can also use the @map()
attribute to define the format for field names to be used in your database. Refer to the Foreign keys and table names naming conventions section to learn how you can automate formatting foreign keys and table names.
Next, run the following command to generate an up and down migration:
npx db-diff
The previous command will generate both an up
and down
migration based on your schema. The generated migration is stored in your ./migrations
directory. If you are currently using a different path to store the migration, you can provide the --migrations-dir
flag followed by the path.
You can then apply the generated migration using the Platformatic DB CLI:
npx platformatic db migrations apply
Platformatic uses Postgrator to run migrations. Postgrator creates a table in the database called versions
to track the applied migrations. Since the versions
table is not yet captured in the Prisma schema, run the following command to introspect the database and populate it with the missing model:
npx prisma db pull
Introspecting the database to populate the model prevents including the versions
table in the generated down migrations.
Your Prisma schema should now contain a versions
model that is similar to this one (it will vary depending on the database system you're using):
model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
viewCount Int @default(0)
createdAt DateTime @default(now())
@@map("posts")
}
+model versions {
+ version BigInt @id
+ name String?
+ md5 String?
+ run_at DateTime? @db.Timestamptz(6)
+}
Add the @@ignore
attribute function to the model to exclude it from the Prisma Client API:
model versions {
version BigInt @id
name String?
md5 String?
run_at DateTime? @db.Timestamptz(6)
+ @@ignore
}
Evolving your database schema
Update the data model in your Prisma schema by adding a model or a field:
// based on the schema in the "Adding Prisma to a new project" section
+model User {
+ id Int @id @default(autoincrement())
+ email String @unique
+ name String?
+ posts Post[]
+
+ @@map("users")
+}
model Post {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
title String
content String?
published Boolean @default(false)
viewCount Int @default(0)
+ author User? @relation(fields: [authorId], references: [id])
+ authorId Int? @map("author_id")
@@map("posts")
}
Next, use the @ruheni/db-diff
CLI tool to generate up
and down
migrations:
npx db-diff
This command will generate up and down migrations based off of your Prisma schema. If you are currently using a different path to store the migration, you can provide the --migrations-dir
flag followed by the path.
Next, apply the generated migration using the Platformatic CLI:
npx platformatic db migrations apply
And you're done!