Skip to content

Commit efa1768

Browse files
committed
fix: prisma schema generation issue with default() using auth()
fixes #274
1 parent b6e16ff commit efa1768

File tree

4 files changed

+46
-14
lines changed

4 files changed

+46
-14
lines changed

packages/sdk/src/prisma/prisma-schema-generator.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
InvocationExpr,
1919
isArrayExpr,
2020
isDataModel,
21+
isExpression,
2122
isInvocationExpr,
2223
isLiteralExpr,
2324
isModel,
@@ -31,7 +32,7 @@ import {
3132
StringLiteral,
3233
type AstNode,
3334
} from '@zenstackhq/language/ast';
34-
import { getAllAttributes, getAllFields, isDelegateModel } from '@zenstackhq/language/utils';
35+
import { getAllAttributes, getAllFields, isAuthInvocation, isDelegateModel } from '@zenstackhq/language/utils';
3536
import { AstUtils } from 'langium';
3637
import { match } from 'ts-pattern';
3738
import { ModelUtils, ZModelCodeGenerator } from '..';
@@ -242,8 +243,8 @@ export class PrismaSchemaGenerator {
242243

243244
const attributes = field.attributes
244245
.filter((attr) => this.isPrismaAttribute(attr))
245-
// `@default` with calling functions from plugin is handled outside Prisma
246-
.filter((attr) => !this.isDefaultWithPluginInvocation(attr))
246+
// `@default` using `auth()` is handled outside Prisma
247+
.filter((attr) => !this.isDefaultWithAuthInvocation(attr))
247248
.filter(
248249
(attr) =>
249250
// when building physical schema, exclude `@default` for id fields inherited from delegate base
@@ -260,7 +261,7 @@ export class PrismaSchemaGenerator {
260261
return result;
261262
}
262263

263-
private isDefaultWithPluginInvocation(attr: DataFieldAttribute) {
264+
private isDefaultWithAuthInvocation(attr: DataFieldAttribute) {
264265
if (attr.decl.ref?.name !== '@default') {
265266
return false;
266267
}
@@ -270,7 +271,7 @@ export class PrismaSchemaGenerator {
270271
return false;
271272
}
272273

273-
return AstUtils.streamAst(expr).some((node) => isInvocationExpr(node) && this.isFromPlugin(node.function.ref));
274+
return AstUtils.streamAst(expr).some((node) => isExpression(node) && isAuthInvocation(node));
274275
}
275276

276277
private isFromPlugin(node: AstNode | undefined) {

packages/testtools/src/project.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from 'node:fs';
22
import path from 'node:path';
33
import tmp from 'tmp';
44

5-
export function createTestProject() {
5+
export function createTestProject(zmodelContent?: string) {
66
const { name: workDir } = tmp.dirSync({ unsafeCleanup: true });
77

88
fs.mkdirSync(path.join(workDir, 'node_modules'));
@@ -63,5 +63,9 @@ export function createTestProject() {
6363
),
6464
);
6565

66+
if (zmodelContent) {
67+
fs.writeFileSync(path.join(workDir, 'schema.zmodel'), zmodelContent);
68+
}
69+
6670
return workDir;
6771
}
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import { describe, it } from 'vitest';
1+
import { it } from 'vitest';
22
import { type Configuration, ShirtColor } from './models';
33

4-
describe('Issue 204 regression tests', () => {
5-
it('tests issue 204', () => {
6-
const config: Configuration = { teamColors: [ShirtColor.Black, ShirtColor.Blue] };
7-
console.log(config.teamColors?.[0]);
8-
const config1: Configuration = {};
9-
console.log(config1);
10-
});
4+
it('tests issue 204', () => {
5+
const config: Configuration = { teamColors: [ShirtColor.Black, ShirtColor.Blue] };
6+
console.log(config.teamColors?.[0]);
7+
const config1: Configuration = {};
8+
console.log(config1);
119
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { createTestProject } from '@zenstackhq/testtools';
2+
import { execSync } from 'child_process';
3+
import { it } from 'vitest';
4+
5+
it('tests issue 274', async () => {
6+
const dir = await createTestProject(`
7+
8+
datasource db {
9+
provider = 'sqlite'
10+
url = "file:./test.db"
11+
}
12+
13+
model Comment {
14+
id String @id
15+
author User? @relation(fields: [authorId], references: [id])
16+
authorId String? @default(auth().id)
17+
}
18+
19+
model User {
20+
id String @id
21+
email String
22+
comments Comment[]
23+
}
24+
`);
25+
26+
process.chdir(dir);
27+
28+
execSync('node node_modules/@zenstackhq/cli/dist/index.js migrate dev --name init');
29+
});

0 commit comments

Comments
 (0)