|
| 1 | +import { loadSchema } from '@zenstackhq/testtools'; |
| 2 | + |
| 3 | +describe('issue 2175', () => { |
| 4 | + it('regression standard generator', async () => { |
| 5 | + await loadSchema( |
| 6 | + ` |
| 7 | + model User { |
| 8 | + id Int @id @default(autoincrement()) |
| 9 | + email String @unique |
| 10 | + posts Post[] |
| 11 | + } |
| 12 | +
|
| 13 | + model Post { |
| 14 | + id Int @id @default(autoincrement()) |
| 15 | + title String |
| 16 | + author User? @relation(fields: [authorId], references: [id]) |
| 17 | + authorId Int? @default(auth().id) |
| 18 | + } |
| 19 | + |
| 20 | + `, |
| 21 | + { |
| 22 | + compile: true, |
| 23 | + extraSourceFiles: [ |
| 24 | + { |
| 25 | + name: 'main.ts', |
| 26 | + content: ` |
| 27 | +import { PrismaClient } from "@prisma/client"; |
| 28 | +import { enhance } from ".zenstack/enhance"; |
| 29 | +
|
| 30 | +const prisma = new PrismaClient(); |
| 31 | +const prismaExtended = prisma.$extends({ |
| 32 | + model: { |
| 33 | + user: { |
| 34 | + async signUp(email: string) { |
| 35 | + return prisma.user.create({ data: { email } }); |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | +}); |
| 40 | +
|
| 41 | +const dbExtended = enhance(prismaExtended); |
| 42 | +
|
| 43 | +async function main() { |
| 44 | + const newUser = await dbExtended.user.signUp("[email protected]"); |
| 45 | + console.log(newUser); |
| 46 | +} |
| 47 | +
|
| 48 | +main(); |
| 49 | +`, |
| 50 | + }, |
| 51 | + ], |
| 52 | + } |
| 53 | + ); |
| 54 | + }); |
| 55 | + |
| 56 | + it('regression new generator', async () => { |
| 57 | + await loadSchema( |
| 58 | + ` |
| 59 | + datasource db { |
| 60 | + provider = "sqlite" |
| 61 | + url = "file:./test.db" |
| 62 | + } |
| 63 | +
|
| 64 | + generator client { |
| 65 | + provider = "prisma-client" |
| 66 | + output = "../generated/prisma" |
| 67 | + moduleFormat = "cjs" |
| 68 | + } |
| 69 | +
|
| 70 | + model User { |
| 71 | + id Int @id @default(autoincrement()) |
| 72 | + email String @unique |
| 73 | + posts Post[] |
| 74 | + } |
| 75 | +
|
| 76 | + model Post { |
| 77 | + id Int @id @default(autoincrement()) |
| 78 | + title String |
| 79 | + author User? @relation(fields: [authorId], references: [id]) |
| 80 | + authorId Int? @default(auth().id) |
| 81 | + } |
| 82 | + |
| 83 | + `, |
| 84 | + { |
| 85 | + addPrelude: false, |
| 86 | + compile: true, |
| 87 | + extraSourceFiles: [ |
| 88 | + { |
| 89 | + name: 'main.ts', |
| 90 | + content: ` |
| 91 | +import { PrismaClient } from "./generated/prisma/client"; |
| 92 | +import { enhance } from "./generated/zenstack/enhance"; |
| 93 | +
|
| 94 | +const prisma = new PrismaClient(); |
| 95 | +const prismaExtended = prisma.$extends({ |
| 96 | + model: { |
| 97 | + user: { |
| 98 | + async signUp(email: string) { |
| 99 | + return prisma.user.create({ data: { email } }); |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | +}); |
| 104 | +
|
| 105 | +const dbExtended = enhance(prismaExtended); |
| 106 | +
|
| 107 | +async function main() { |
| 108 | + const newUser = await dbExtended.user.signUp("[email protected]"); |
| 109 | + console.log(newUser); |
| 110 | +} |
| 111 | +
|
| 112 | +main(); |
| 113 | +`, |
| 114 | + }, |
| 115 | + ], |
| 116 | + output: './generated/zenstack', |
| 117 | + prismaLoadPath: './generated/prisma/client', |
| 118 | + } |
| 119 | + ); |
| 120 | + }); |
| 121 | +}); |
0 commit comments