Skip to content

Commit 546ba8e

Browse files
authored
fix: prisma schema generation issue with default() using auth() (#278)
* fix: prisma schema generation issue with `default()` using `auth()` fixes #274 * update * addressing pr comment
1 parent 056029e commit 546ba8e

File tree

4 files changed

+43
-20
lines changed

4 files changed

+43
-20
lines changed

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import {
2020
isDataModel,
2121
isInvocationExpr,
2222
isLiteralExpr,
23-
isModel,
2423
isNullExpr,
2524
isReferenceExpr,
2625
isStringLiteral,
@@ -31,7 +30,7 @@ import {
3130
StringLiteral,
3231
type AstNode,
3332
} from '@zenstackhq/language/ast';
34-
import { getAllAttributes, getAllFields, isDelegateModel } from '@zenstackhq/language/utils';
33+
import { getAllAttributes, getAllFields, isAuthInvocation, isDelegateModel } from '@zenstackhq/language/utils';
3534
import { AstUtils } from 'langium';
3635
import { match } from 'ts-pattern';
3736
import { ModelUtils, ZModelCodeGenerator } from '..';
@@ -242,8 +241,8 @@ export class PrismaSchemaGenerator {
242241

243242
const attributes = field.attributes
244243
.filter((attr) => this.isPrismaAttribute(attr))
245-
// `@default` with calling functions from plugin is handled outside Prisma
246-
.filter((attr) => !this.isDefaultWithPluginInvocation(attr))
244+
// `@default` using `auth()` is handled outside Prisma
245+
.filter((attr) => !this.isDefaultWithAuthInvocation(attr))
247246
.filter(
248247
(attr) =>
249248
// when building physical schema, exclude `@default` for id fields inherited from delegate base
@@ -260,7 +259,7 @@ export class PrismaSchemaGenerator {
260259
return result;
261260
}
262261

263-
private isDefaultWithPluginInvocation(attr: DataFieldAttribute) {
262+
private isDefaultWithAuthInvocation(attr: DataFieldAttribute) {
264263
if (attr.decl.ref?.name !== '@default') {
265264
return false;
266265
}
@@ -270,12 +269,7 @@ export class PrismaSchemaGenerator {
270269
return false;
271270
}
272271

273-
return AstUtils.streamAst(expr).some((node) => isInvocationExpr(node) && this.isFromPlugin(node.function.ref));
274-
}
275-
276-
private isFromPlugin(node: AstNode | undefined) {
277-
const model = AstUtils.getContainerOfType(node, isModel);
278-
return !!model && !!model.$document && model.$document.uri.path.endsWith('plugin.zmodel');
272+
return AstUtils.streamAst(expr).some(isAuthInvocation);
279273
}
280274

281275
private isInheritedFromDelegate(field: DataField, contextModel: DataModel) {

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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
execSync('node node_modules/@zenstackhq/cli/dist/index.js migrate dev --name init', { cwd: dir });
27+
});

0 commit comments

Comments
 (0)