├── .env ├── .gitignore ├── README.md ├── __tests__ ├── posts.test.ts └── users.test.ts ├── docker-compose.yml ├── jest.config.js ├── package-lock.json ├── package.json ├── prisma ├── migrations │ ├── 20200604200835-initial │ │ ├── README.md │ │ ├── schema.prisma │ │ └── steps.json │ └── migrate.lock ├── prisma-test-environment.js ├── schema.prisma └── seed.ts └── tsconfig.json /.env: -------------------------------------------------------------------------------- 1 | POSTGRES_URL=postgresql://prisma:hilly-sand-pit@localhost:54320/prisma?schema=prisma-pg-jest -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | postgres/ 3 | prisma/migrations/dev/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Prisma + Postgres + Jest 2 | 3 | This example showcases how you can configure your Jest environment in order to perform integration tests against applications utilising Prisma and Postgres. 4 | 5 | Each test suite will create a temporary schema within the database, allowing concurrent execution of them. The temporary schema is subsequently cleaned up after each test suite has completed. 6 | 7 | ## Setup 8 | 9 | Clone the repo. 10 | 11 | ```bash 12 | git clone https://github.com/ctrlplusb/prisma-pg-jest 13 | ``` 14 | 15 | Install the dependencies. 16 | 17 | ```bash 18 | cd prisma-pg-jest 19 | npm install 20 | ``` 21 | 22 | ## Running the database 23 | 24 | A `docker-compose.yml` file has been created to represent the Postgres database that we will use for local development. 25 | 26 | You will need [Docker](https://docs.docker.com/v17.09/engine/installation/) installed. 27 | 28 | To start the database run the following command: 29 | 30 | ```bash 31 | npm run db:start 32 | ``` 33 | 34 | Once you are finished developing you can stop the db by running the following command: 35 | 36 | ```bash 37 | npm run db:stop 38 | ``` 39 | 40 | This project has been configured to run with the following Postgres configuration. You can modify these to suit your needs by editing the [`docker-compose.yml`](docker-compose.yml) file. 41 | 42 | ``` 43 | - HOST=localhost 44 | - PORT=54320 45 | - POSTGRES_USER=prisma 46 | - POSTGRES_PASSWORD=hilly-sand-pit 47 | - POSTGRES_DB=prisma 48 | ``` 49 | 50 | ## Jest Configuration 51 | 52 | We have configured Jest to execute with a custom test environment. See the [`prisma/prisma-test-environment.js](prisma/prisma-test-environment.js) file for more details. 53 | 54 | This custom environment ensures that each test suite getting executed will have a unique schema created for them against the running Postgres database. The migrations will then be executed against them, via the `prisma migrate up --experimental` command, ensuring that the latest model has been applied to the schema. 55 | 56 | ## Running your tests 57 | 58 | Ensure that your local Postgres is running. 59 | 60 | ```bash 61 | npm run db:start 62 | ``` 63 | 64 | Then execute the Jest tests via the following command: 65 | 66 | ```bash 67 | npm run test 68 | ``` 69 | -------------------------------------------------------------------------------- /__tests__/posts.test.ts: -------------------------------------------------------------------------------- 1 | import { PrismaClient } from "@prisma/client"; 2 | 3 | const client = new PrismaClient(); 4 | 5 | afterAll(async () => { 6 | await client.disconnect(); 7 | }); 8 | 9 | it("are by default set as published", async () => { 10 | // ACT 11 | const post = await client.post.create({ 12 | data: { 13 | title: "foo", 14 | author: { 15 | create: { 16 | email: "foo@bar.com", 17 | }, 18 | }, 19 | }, 20 | }); 21 | 22 | // ASSERT 23 | expect(post.published).toBeTruthy(); 24 | }); 25 | -------------------------------------------------------------------------------- /__tests__/users.test.ts: -------------------------------------------------------------------------------- 1 | import { PrismaClient } from "@prisma/client"; 2 | 3 | const client = new PrismaClient(); 4 | 5 | afterAll(async () => { 6 | await client.disconnect(); 7 | }); 8 | 9 | it("cannot create a user with an email address that is already in user", async () => { 10 | // ARRANGE 11 | await client.user.create({ 12 | data: { 13 | email: "foo@bar.com", 14 | }, 15 | }); 16 | 17 | // ACT + ASSERT 18 | expect( 19 | client.user.create({ 20 | data: { 21 | email: "foo@bar.com", 22 | }, 23 | }) 24 | ).rejects.toThrow(); 25 | }); 26 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: "3" 2 | services: 3 | db: 4 | image: "postgres:11.6" 5 | container_name: "prisma-pg" 6 | environment: 7 | - POSTGRES_USER=prisma 8 | - POSTGRES_PASSWORD=hilly-sand-pit 9 | - POSTGRES_DB=prisma 10 | ports: 11 | - "54320:5432" 12 | volumes: 13 | - ./postgres/data:/var/lib/postgresql/data 14 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | const path = require("path"); 2 | 3 | module.exports = { 4 | preset: "ts-jest", 5 | testEnvironment: path.join(__dirname, "./prisma/prisma-test-environment.js") 6 | }; 7 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "private": true, 3 | "name": "prisma-pg-jest", 4 | "license": "MIT", 5 | "scripts": { 6 | "db:start": "docker-compose up -d", 7 | "db:stop": "docker-compose down", 8 | "generate": "dotenv prisma generate", 9 | "migrate:save": "dotenv prisma migrate save -- --experimental", 10 | "migrate:up": "dotenv prisma migrate up -- --experimental", 11 | "postinstall": "npm run generate", 12 | "seed": "ts-node prisma/seed", 13 | "test": "jest" 14 | }, 15 | "dependencies": { 16 | "@prisma/client": "^2.0.0-beta.8" 17 | }, 18 | "devDependencies": { 19 | "@prisma/cli": "^2.0.0-beta.8", 20 | "@types/jest": "^25.2.3", 21 | "dotenv-cli": "^3.1.0", 22 | "jest": "^26.0.1", 23 | "jest-environment-node": "^26.0.1", 24 | "nanoid": "^3.1.9", 25 | "pg": "^8.2.1", 26 | "ts-jest": "^26.1.0", 27 | "ts-node": "^8.10.2", 28 | "typescript": "^3.9.3" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /prisma/migrations/20200604200835-initial/README.md: -------------------------------------------------------------------------------- 1 | # Migration `20200604200835-initial` 2 | 3 | This migration has been generated by Sean Matheson at 6/4/2020, 8:08:35 PM. 4 | You can check out the [state of the schema](./schema.prisma) after the migration. 5 | 6 | ## Database Steps 7 | 8 | ```sql 9 | CREATE TABLE "prisma-pg-jest"."User" ( 10 | "email" text NOT NULL ,"id" text NOT NULL ,"name" text , 11 | PRIMARY KEY ("id")) 12 | 13 | CREATE TABLE "prisma-pg-jest"."Post" ( 14 | "authorId" text NOT NULL ,"content" text ,"createdAt" timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,"id" text NOT NULL ,"published" boolean NOT NULL DEFAULT true,"title" text NOT NULL ,"updatedAt" timestamp(3) NOT NULL , 15 | PRIMARY KEY ("id")) 16 | 17 | CREATE TABLE "prisma-pg-jest"."Category" ( 18 | "id" text NOT NULL ,"name" text NOT NULL , 19 | PRIMARY KEY ("id")) 20 | 21 | CREATE TABLE "prisma-pg-jest"."_CategoryToPost" ( 22 | "A" text NOT NULL ,"B" text NOT NULL ) 23 | 24 | CREATE UNIQUE INDEX "User.email" ON "prisma-pg-jest"."User"("email") 25 | 26 | CREATE UNIQUE INDEX "_CategoryToPost_AB_unique" ON "prisma-pg-jest"."_CategoryToPost"("A","B") 27 | 28 | CREATE INDEX "_CategoryToPost_B_index" ON "prisma-pg-jest"."_CategoryToPost"("B") 29 | 30 | ALTER TABLE "prisma-pg-jest"."Post" ADD FOREIGN KEY ("authorId")REFERENCES "prisma-pg-jest"."User"("id") ON DELETE CASCADE ON UPDATE CASCADE 31 | 32 | ALTER TABLE "prisma-pg-jest"."_CategoryToPost" ADD FOREIGN KEY ("A")REFERENCES "prisma-pg-jest"."Category"("id") ON DELETE CASCADE ON UPDATE CASCADE 33 | 34 | ALTER TABLE "prisma-pg-jest"."_CategoryToPost" ADD FOREIGN KEY ("B")REFERENCES "prisma-pg-jest"."Post"("id") ON DELETE CASCADE ON UPDATE CASCADE 35 | ``` 36 | 37 | ## Changes 38 | 39 | ```diff 40 | diff --git schema.prisma schema.prisma 41 | migration ..20200604200835-initial 42 | --- datamodel.dml 43 | +++ datamodel.dml 44 | @@ -1,0 +1,33 @@ 45 | +generator client { 46 | + provider = "prisma-client-js" 47 | +} 48 | + 49 | +datasource db { 50 | + provider = "postgresql" 51 | + url = env("POSTGRES_URL") 52 | +} 53 | + 54 | +model User { 55 | + id String @default(cuid()) @id 56 | + email String @unique 57 | + name String? 58 | + posts Post[] 59 | +} 60 | + 61 | +model Post { 62 | + id String @default(cuid()) @id 63 | + createdAt DateTime @default(now()) 64 | + updatedAt DateTime @updatedAt 65 | + published Boolean @default(true) 66 | + title String 67 | + content String? 68 | + author User @relation(fields: [authorId], references: [id]) 69 | + authorId String 70 | + categories Category[] 71 | +} 72 | + 73 | +model Category { 74 | + id String @default(cuid()) @id 75 | + name String 76 | + posts Post[] 77 | +} 78 | ``` 79 | 80 | 81 | -------------------------------------------------------------------------------- /prisma/migrations/20200604200835-initial/schema.prisma: -------------------------------------------------------------------------------- 1 | generator client { 2 | provider = "prisma-client-js" 3 | } 4 | 5 | datasource db { 6 | provider = "postgresql" 7 | url = "***" 8 | } 9 | 10 | model User { 11 | id String @default(cuid()) @id 12 | email String @unique 13 | name String? 14 | posts Post[] 15 | } 16 | 17 | model Post { 18 | id String @default(cuid()) @id 19 | createdAt DateTime @default(now()) 20 | updatedAt DateTime @updatedAt 21 | published Boolean @default(true) 22 | title String 23 | content String? 24 | author User @relation(fields: [authorId], references: [id]) 25 | authorId String 26 | categories Category[] 27 | } 28 | 29 | model Category { 30 | id String @default(cuid()) @id 31 | name String 32 | posts Post[] 33 | } 34 | -------------------------------------------------------------------------------- /prisma/migrations/20200604200835-initial/steps.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.3.14-fixed", 3 | "steps": [ 4 | { 5 | "tag": "CreateSource", 6 | "source": "db" 7 | }, 8 | { 9 | "tag": "CreateArgument", 10 | "location": { 11 | "tag": "Source", 12 | "source": "db" 13 | }, 14 | "argument": "provider", 15 | "value": "\"postgresql\"" 16 | }, 17 | { 18 | "tag": "CreateArgument", 19 | "location": { 20 | "tag": "Source", 21 | "source": "db" 22 | }, 23 | "argument": "url", 24 | "value": "env(\"POSTGRES_URL\")" 25 | }, 26 | { 27 | "tag": "CreateModel", 28 | "model": "User" 29 | }, 30 | { 31 | "tag": "CreateField", 32 | "model": "User", 33 | "field": "id", 34 | "type": "String", 35 | "arity": "Required" 36 | }, 37 | { 38 | "tag": "CreateDirective", 39 | "location": { 40 | "path": { 41 | "tag": "Field", 42 | "model": "User", 43 | "field": "id" 44 | }, 45 | "directive": "default" 46 | } 47 | }, 48 | { 49 | "tag": "CreateArgument", 50 | "location": { 51 | "tag": "Directive", 52 | "path": { 53 | "tag": "Field", 54 | "model": "User", 55 | "field": "id" 56 | }, 57 | "directive": "default" 58 | }, 59 | "argument": "", 60 | "value": "cuid()" 61 | }, 62 | { 63 | "tag": "CreateDirective", 64 | "location": { 65 | "path": { 66 | "tag": "Field", 67 | "model": "User", 68 | "field": "id" 69 | }, 70 | "directive": "id" 71 | } 72 | }, 73 | { 74 | "tag": "CreateField", 75 | "model": "User", 76 | "field": "email", 77 | "type": "String", 78 | "arity": "Required" 79 | }, 80 | { 81 | "tag": "CreateDirective", 82 | "location": { 83 | "path": { 84 | "tag": "Field", 85 | "model": "User", 86 | "field": "email" 87 | }, 88 | "directive": "unique" 89 | } 90 | }, 91 | { 92 | "tag": "CreateField", 93 | "model": "User", 94 | "field": "name", 95 | "type": "String", 96 | "arity": "Optional" 97 | }, 98 | { 99 | "tag": "CreateField", 100 | "model": "User", 101 | "field": "posts", 102 | "type": "Post", 103 | "arity": "List" 104 | }, 105 | { 106 | "tag": "CreateModel", 107 | "model": "Post" 108 | }, 109 | { 110 | "tag": "CreateField", 111 | "model": "Post", 112 | "field": "id", 113 | "type": "String", 114 | "arity": "Required" 115 | }, 116 | { 117 | "tag": "CreateDirective", 118 | "location": { 119 | "path": { 120 | "tag": "Field", 121 | "model": "Post", 122 | "field": "id" 123 | }, 124 | "directive": "default" 125 | } 126 | }, 127 | { 128 | "tag": "CreateArgument", 129 | "location": { 130 | "tag": "Directive", 131 | "path": { 132 | "tag": "Field", 133 | "model": "Post", 134 | "field": "id" 135 | }, 136 | "directive": "default" 137 | }, 138 | "argument": "", 139 | "value": "cuid()" 140 | }, 141 | { 142 | "tag": "CreateDirective", 143 | "location": { 144 | "path": { 145 | "tag": "Field", 146 | "model": "Post", 147 | "field": "id" 148 | }, 149 | "directive": "id" 150 | } 151 | }, 152 | { 153 | "tag": "CreateField", 154 | "model": "Post", 155 | "field": "createdAt", 156 | "type": "DateTime", 157 | "arity": "Required" 158 | }, 159 | { 160 | "tag": "CreateDirective", 161 | "location": { 162 | "path": { 163 | "tag": "Field", 164 | "model": "Post", 165 | "field": "createdAt" 166 | }, 167 | "directive": "default" 168 | } 169 | }, 170 | { 171 | "tag": "CreateArgument", 172 | "location": { 173 | "tag": "Directive", 174 | "path": { 175 | "tag": "Field", 176 | "model": "Post", 177 | "field": "createdAt" 178 | }, 179 | "directive": "default" 180 | }, 181 | "argument": "", 182 | "value": "now()" 183 | }, 184 | { 185 | "tag": "CreateField", 186 | "model": "Post", 187 | "field": "updatedAt", 188 | "type": "DateTime", 189 | "arity": "Required" 190 | }, 191 | { 192 | "tag": "CreateDirective", 193 | "location": { 194 | "path": { 195 | "tag": "Field", 196 | "model": "Post", 197 | "field": "updatedAt" 198 | }, 199 | "directive": "updatedAt" 200 | } 201 | }, 202 | { 203 | "tag": "CreateField", 204 | "model": "Post", 205 | "field": "published", 206 | "type": "Boolean", 207 | "arity": "Required" 208 | }, 209 | { 210 | "tag": "CreateDirective", 211 | "location": { 212 | "path": { 213 | "tag": "Field", 214 | "model": "Post", 215 | "field": "published" 216 | }, 217 | "directive": "default" 218 | } 219 | }, 220 | { 221 | "tag": "CreateArgument", 222 | "location": { 223 | "tag": "Directive", 224 | "path": { 225 | "tag": "Field", 226 | "model": "Post", 227 | "field": "published" 228 | }, 229 | "directive": "default" 230 | }, 231 | "argument": "", 232 | "value": "true" 233 | }, 234 | { 235 | "tag": "CreateField", 236 | "model": "Post", 237 | "field": "title", 238 | "type": "String", 239 | "arity": "Required" 240 | }, 241 | { 242 | "tag": "CreateField", 243 | "model": "Post", 244 | "field": "content", 245 | "type": "String", 246 | "arity": "Optional" 247 | }, 248 | { 249 | "tag": "CreateField", 250 | "model": "Post", 251 | "field": "author", 252 | "type": "User", 253 | "arity": "Required" 254 | }, 255 | { 256 | "tag": "CreateDirective", 257 | "location": { 258 | "path": { 259 | "tag": "Field", 260 | "model": "Post", 261 | "field": "author" 262 | }, 263 | "directive": "relation" 264 | } 265 | }, 266 | { 267 | "tag": "CreateArgument", 268 | "location": { 269 | "tag": "Directive", 270 | "path": { 271 | "tag": "Field", 272 | "model": "Post", 273 | "field": "author" 274 | }, 275 | "directive": "relation" 276 | }, 277 | "argument": "fields", 278 | "value": "[authorId]" 279 | }, 280 | { 281 | "tag": "CreateArgument", 282 | "location": { 283 | "tag": "Directive", 284 | "path": { 285 | "tag": "Field", 286 | "model": "Post", 287 | "field": "author" 288 | }, 289 | "directive": "relation" 290 | }, 291 | "argument": "references", 292 | "value": "[id]" 293 | }, 294 | { 295 | "tag": "CreateField", 296 | "model": "Post", 297 | "field": "authorId", 298 | "type": "String", 299 | "arity": "Required" 300 | }, 301 | { 302 | "tag": "CreateField", 303 | "model": "Post", 304 | "field": "categories", 305 | "type": "Category", 306 | "arity": "List" 307 | }, 308 | { 309 | "tag": "CreateModel", 310 | "model": "Category" 311 | }, 312 | { 313 | "tag": "CreateField", 314 | "model": "Category", 315 | "field": "id", 316 | "type": "String", 317 | "arity": "Required" 318 | }, 319 | { 320 | "tag": "CreateDirective", 321 | "location": { 322 | "path": { 323 | "tag": "Field", 324 | "model": "Category", 325 | "field": "id" 326 | }, 327 | "directive": "default" 328 | } 329 | }, 330 | { 331 | "tag": "CreateArgument", 332 | "location": { 333 | "tag": "Directive", 334 | "path": { 335 | "tag": "Field", 336 | "model": "Category", 337 | "field": "id" 338 | }, 339 | "directive": "default" 340 | }, 341 | "argument": "", 342 | "value": "cuid()" 343 | }, 344 | { 345 | "tag": "CreateDirective", 346 | "location": { 347 | "path": { 348 | "tag": "Field", 349 | "model": "Category", 350 | "field": "id" 351 | }, 352 | "directive": "id" 353 | } 354 | }, 355 | { 356 | "tag": "CreateField", 357 | "model": "Category", 358 | "field": "name", 359 | "type": "String", 360 | "arity": "Required" 361 | }, 362 | { 363 | "tag": "CreateField", 364 | "model": "Category", 365 | "field": "posts", 366 | "type": "Post", 367 | "arity": "List" 368 | } 369 | ] 370 | } -------------------------------------------------------------------------------- /prisma/migrations/migrate.lock: -------------------------------------------------------------------------------- 1 | # Prisma Migrate lockfile v1 2 | 3 | 20200604200835-initial -------------------------------------------------------------------------------- /prisma/prisma-test-environment.js: -------------------------------------------------------------------------------- 1 | /* eslint-disable @typescript-eslint/no-var-requires */ 2 | 3 | const { Client } = require("pg"); 4 | const NodeEnvironment = require("jest-environment-node"); 5 | const { nanoid } = require("nanoid"); 6 | const util = require("util"); 7 | const exec = util.promisify(require("child_process").exec); 8 | 9 | const prismaBinary = "./node_modules/.bin/prisma2"; 10 | 11 | class PrismaTestEnvironment extends NodeEnvironment { 12 | constructor(config) { 13 | super(config); 14 | 15 | // Generate a unique schema identifier for this test context 16 | this.schema = `test_${nanoid()}`; 17 | 18 | // Generate the pg connection string for the test schema 19 | this.connectionString = `postgresql://prisma:hilly-sand-pit@localhost:54320/prisma?schema=${this.schema}`; 20 | } 21 | 22 | async setup() { 23 | // Set the required environment variable to contain the connection string 24 | // to our database test schema 25 | process.env.POSTGRES_URL = this.connectionString; 26 | this.global.process.env.POSTGRES_URL = this.connectionString; 27 | 28 | // Run the migrations to ensure our schema has the required structure 29 | await exec(`${prismaBinary} migrate up --experimental`); 30 | 31 | return super.setup(); 32 | } 33 | 34 | async teardown() { 35 | // Drop the schema after the tests have completed 36 | const client = new Client({ 37 | connectionString: this.connectionString, 38 | }); 39 | await client.connect(); 40 | await client.query(`DROP SCHEMA IF EXISTS "${this.schema}" CASCADE`); 41 | await client.end(); 42 | } 43 | } 44 | 45 | module.exports = PrismaTestEnvironment; 46 | -------------------------------------------------------------------------------- /prisma/schema.prisma: -------------------------------------------------------------------------------- 1 | generator client { 2 | provider = "prisma-client-js" 3 | } 4 | 5 | datasource db { 6 | provider = "postgresql" 7 | url = env("POSTGRES_URL") 8 | } 9 | 10 | model User { 11 | id String @default(cuid()) @id 12 | email String @unique 13 | name String? 14 | posts Post[] 15 | } 16 | 17 | model Post { 18 | id String @default(cuid()) @id 19 | createdAt DateTime @default(now()) 20 | updatedAt DateTime @updatedAt 21 | published Boolean @default(true) 22 | title String 23 | content String? 24 | author User @relation(fields: [authorId], references: [id]) 25 | authorId String 26 | categories Category[] 27 | } 28 | 29 | model Category { 30 | id String @default(cuid()) @id 31 | name String 32 | posts Post[] 33 | } 34 | -------------------------------------------------------------------------------- /prisma/seed.ts: -------------------------------------------------------------------------------- 1 | import { PrismaClient } from "@prisma/client"; 2 | 3 | const client = new PrismaClient(); 4 | 5 | // A `main` function so that we can use async/await 6 | async function main() { 7 | // Seed the database with users and posts 8 | const user1 = await client.user.create({ 9 | data: { 10 | email: "alice@prisma.io", 11 | name: "Alice", 12 | posts: { 13 | create: { 14 | title: "Watch the talks from Prisma Day 2019", 15 | content: "https://www.prisma.io/blog/z11sg6ipb3i1/", 16 | published: true, 17 | }, 18 | }, 19 | }, 20 | include: { 21 | posts: true, 22 | }, 23 | }); 24 | const user2 = await client.user.create({ 25 | data: { 26 | email: "bob@prisma.io", 27 | name: "Bob", 28 | posts: { 29 | create: [ 30 | { 31 | title: "Subscribe to GraphQL Weekly for community news", 32 | content: "https://graphqlweekly.com/", 33 | published: true, 34 | }, 35 | { 36 | title: "Follow Prisma on Twitter", 37 | content: "https://twitter.com/prisma/", 38 | published: false, 39 | }, 40 | ], 41 | }, 42 | }, 43 | include: { 44 | posts: true, 45 | }, 46 | }); 47 | console.log( 48 | `Created users: ${user1.name} (${user1.posts.length} post) and (${user2.posts.length} posts) ` 49 | ); 50 | 51 | // Retrieve all published posts 52 | const allPosts = await client.post.findMany({ 53 | where: { published: true }, 54 | }); 55 | console.log(`Retrieved all published posts: `, allPosts); 56 | 57 | // Create a new post (written by an already existing user with email alice@prisma.io) 58 | const newPost = await client.post.create({ 59 | data: { 60 | title: "Join the Prisma Slack community", 61 | content: "http://slack.prisma.io", 62 | published: false, 63 | author: { 64 | connect: { 65 | email: "alice@prisma.io", 66 | }, 67 | }, 68 | }, 69 | }); 70 | console.log(`Created a new post: `, newPost); 71 | 72 | // Publish the new post 73 | const updatedPost = await client.post.update({ 74 | where: { 75 | id: newPost.id, 76 | }, 77 | data: { 78 | published: true, 79 | }, 80 | }); 81 | console.log(`Published the newly created post: `, updatedPost); 82 | 83 | // Retrieve all posts by user with email alice@prisma.io 84 | const postsByUser = await client.user 85 | .findOne({ 86 | where: { 87 | email: "alice@prisma.io", 88 | }, 89 | }) 90 | .posts(); 91 | console.log(`Retrieved all posts from a specific user: `, postsByUser); 92 | } 93 | 94 | main() 95 | .catch((e) => console.error(e)) 96 | .finally(async () => { 97 | await client.disconnect(); 98 | }); 99 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "sourceMap": true, 4 | "outDir": "dist", 5 | "strict": true, 6 | "lib": ["esnext", "dom"], 7 | "esModuleInterop": true 8 | } 9 | } 10 | --------------------------------------------------------------------------------