Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions packages/sdk/src/prisma/prisma-schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
isDataModel,
isInvocationExpr,
isLiteralExpr,
isModel,
isNullExpr,
isReferenceExpr,
isStringLiteral,
Expand All @@ -31,7 +30,7 @@ import {
StringLiteral,
type AstNode,
} from '@zenstackhq/language/ast';
import { getAllAttributes, getAllFields, isDelegateModel } from '@zenstackhq/language/utils';
import { getAllAttributes, getAllFields, isAuthInvocation, isDelegateModel } from '@zenstackhq/language/utils';
import { AstUtils } from 'langium';
import { match } from 'ts-pattern';
import { ModelUtils, ZModelCodeGenerator } from '..';
Expand Down Expand Up @@ -242,8 +241,8 @@ export class PrismaSchemaGenerator {

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

private isDefaultWithPluginInvocation(attr: DataFieldAttribute) {
private isDefaultWithAuthInvocation(attr: DataFieldAttribute) {
if (attr.decl.ref?.name !== '@default') {
return false;
}
Expand All @@ -270,12 +269,7 @@ export class PrismaSchemaGenerator {
return false;
}

return AstUtils.streamAst(expr).some((node) => isInvocationExpr(node) && this.isFromPlugin(node.function.ref));
}

private isFromPlugin(node: AstNode | undefined) {
const model = AstUtils.getContainerOfType(node, isModel);
return !!model && !!model.$document && model.$document.uri.path.endsWith('plugin.zmodel');
return AstUtils.streamAst(expr).some(isAuthInvocation);
}

private isInheritedFromDelegate(field: DataField, contextModel: DataModel) {
Expand Down
6 changes: 5 additions & 1 deletion packages/testtools/src/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'node:fs';
import path from 'node:path';
import tmp from 'tmp';

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

fs.mkdirSync(path.join(workDir, 'node_modules'));
Expand Down Expand Up @@ -63,5 +63,9 @@ export function createTestProject() {
),
);

if (zmodelContent) {
fs.writeFileSync(path.join(workDir, 'schema.zmodel'), zmodelContent);
}

return workDir;
}
14 changes: 6 additions & 8 deletions tests/regression/test/issue-204/regression.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { describe, it } from 'vitest';
import { it } from 'vitest';
import { type Configuration, ShirtColor } from './models';

describe('Issue 204 regression tests', () => {
it('tests issue 204', () => {
const config: Configuration = { teamColors: [ShirtColor.Black, ShirtColor.Blue] };
console.log(config.teamColors?.[0]);
const config1: Configuration = {};
console.log(config1);
});
it('tests issue 204', () => {
const config: Configuration = { teamColors: [ShirtColor.Black, ShirtColor.Blue] };
console.log(config.teamColors?.[0]);
const config1: Configuration = {};
console.log(config1);
});
27 changes: 27 additions & 0 deletions tests/regression/test/issue-274/regression.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { createTestProject } from '@zenstackhq/testtools';
import { execSync } from 'child_process';
import { it } from 'vitest';

it('tests issue 274', async () => {
const dir = await createTestProject(`

datasource db {
provider = 'sqlite'
url = "file:./test.db"
}

model Comment {
id String @id
author User? @relation(fields: [authorId], references: [id])
authorId String? @default(auth().id)
}

model User {
id String @id
email String
comments Comment[]
}
`);

execSync('node node_modules/@zenstackhq/cli/dist/index.js migrate dev --name init', { cwd: dir });
});