Archived. This repository is an unmaintained sample from a 2023 blog series. Its last update was January 2024 and its dependencies are out of date, including known security advisories. Do not use it in production. It is kept read-only so the article links below keep working.
Article series:
- Part 1: Why Prisma and Platformatic are a great match
- Part 2: Friendly Data Modeling & Auto-generated, Editable Migrations for Platformatic with Prisma
- Part 3: Extend your Platformatic API with Prisma Client
Clone the repository
git clone git@github.com:ruheni/prisma-platformatic.git
Install dependencies
cd prisma-platformatic
npm i
Start up the container with docker compose:
docker-compose up -d
Create a .env file at the root of your project directory:
cp .env.example .env
Open up the schema.prisma file. The schema contains two models: User and Post. The User and Post model have a 1-n relationship.
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
title String
content String?
published Boolean @default(false)
viewCount Int @default(0)
author User? @relation(fields: [authorId], references: [id])
authorId Int?
}Generate a migration using @ruheni/db-diff:
npx db-diff
The command will generate an up and a down migration, i.e. 001.do.sql and 001.undo.sql, which are Postgrator-compatible.
@ruheni/db-diffis a helper library built on top of the Prisma CLI for auto-generating, versioning, fully-customizable database migrations that are Postgrator-compatible. By default, it generates bothupanddownmigrations. Check out the README to learn more about it.
Apply the migration:
npx platformatic db migrate
Under the hood, Platformatic will create a versions table in the database. The versions table is used by Platformatic(that uses Postgrator under the hood) to track the applied migrations to your database. The next time you run npx db-diff, it will generate SQL that will prompt you to drop the table from your database. This is an undesirable behavior. Therefore, add the following snippet to your Prisma schema file to prevent this from happening:
// used by [postgrator](https://github.com/rickbergfalk/postgrator) to keep track of applied migrations
model versions {
version BigInt @id
name String?
md5 String?
run_at DateTime? @db.Timestamptz(6)
@@ignore
}Start-up Platformatic DB:
npm run dev
Explore the GraphQL API on http://localhost:3042/graphiql. Explore the REST API documentation at http://localhost:3042/documentation.