Skip to content

Commit

Permalink
fix: assign default value to field which uses auth() in @default
Browse files Browse the repository at this point in the history
…when generating logical schema
  • Loading branch information
ymc9 committed Jan 14, 2025
1 parent 1a9cf5a commit 3b183e0
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion packages/schema/src/plugins/prisma/schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
} from '@zenstackhq/language/ast';
import { getIdFields } from '@zenstackhq/sdk';
import { getPrismaVersion } from '@zenstackhq/sdk/prisma';
import { match } from 'ts-pattern';
import { match, P } from 'ts-pattern';

import { DELEGATE_AUX_RELATION_PREFIX, PRISMA_MINIMUM_VERSION } from '@zenstackhq/runtime';
import {
Expand Down Expand Up @@ -869,9 +869,34 @@ export class PrismaSchemaGenerator {
const docs = [...field.comments, ...this.getCustomAttributesAsComments(field)];
const result = model.addField(field.name, type, attributes, docs, addToFront);

if (this.mode === 'logical') {
if (field.attributes.some((attr) => isDefaultWithAuth(attr))) {
// field has `@default` with `auth()`, turn it into a dummy default value, and the
// real default value setting is handled outside Prisma
this.setDummyDefault(result, field);
}
}

return result;
}

private setDummyDefault(result: ModelField, field: DataModelField) {
const dummyDefaultValue = match(field.type.type)
.with('String', () => new AttributeArgValue('String', ''))
.with(P.union('Int', 'BigInt', 'Float', 'Decimal'), () => new AttributeArgValue('Number', '0'))
.with('Boolean', () => new AttributeArgValue('Boolean', 'false'))
.with('DateTime', () => new AttributeArgValue('FunctionCall', new PrismaFunctionCall('now')))
.with('Json', () => new AttributeArgValue('String', '{}'))
.with('Bytes', () => new AttributeArgValue('String', ''))
.otherwise(() => {
throw new PluginError(name, `Unsupported field type with default value: ${field.type.type}`);
});

result.attributes.push(
new PrismaFieldAttribute('@default', [new PrismaAttributeArg(undefined, dummyDefaultValue)])
);
}

private ensureSupportingTypeDefFields(zmodel: Model) {
const dsProvider = getDataSourceProvider(zmodel);
if (dsProvider && !PROVIDERS_SUPPORTING_TYPEDEF_FIELDS.includes(dsProvider)) {
Expand Down

0 comments on commit 3b183e0

Please sign in to comment.