Skip to content
Merged
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
26 changes: 17 additions & 9 deletions packages/sdk/src/ts-schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -618,17 +618,18 @@ export class TsSchemaGenerator {
ts.factory.createPropertyAssignment(
field.name,
ts.factory.createObjectLiteralExpression([
ts.factory.createPropertyAssignment(
'type',
ts.factory.createStringLiteral(field.type.type!),
),
ts.factory.createPropertyAssignment('type', this.generateFieldTypeLiteral(field)),
]),
),
);
}
}

// model-level id and unique

// it's possible to have the same set of fields in both `@@id` and `@@unique`
// so we need to deduplicate them
const seenKeys = new Set<string>();
for (const attr of dm.attributes) {
if (attr.decl.$refText === '@@id' || attr.decl.$refText === '@@unique') {
const fieldNames = this.getReferenceNames(attr.args[0]!.value);
Expand All @@ -643,15 +644,17 @@ export class TsSchemaGenerator {
ts.factory.createPropertyAssignment(
fieldNames[0]!,
ts.factory.createObjectLiteralExpression([
ts.factory.createPropertyAssignment(
'type',
ts.factory.createStringLiteral(fieldDef.type.type!),
),
ts.factory.createPropertyAssignment('type', this.generateFieldTypeLiteral(fieldDef)),
]),
),
);
} else {
// multi-field unique
const key = fieldNames.join('_');
if (seenKeys.has(key)) {
continue;
}
seenKeys.add(key);
properties.push(
ts.factory.createPropertyAssignment(
fieldNames.join('_'),
Expand All @@ -663,7 +666,7 @@ export class TsSchemaGenerator {
ts.factory.createObjectLiteralExpression([
ts.factory.createPropertyAssignment(
'type',
ts.factory.createStringLiteral(fieldDef.type.type!),
this.generateFieldTypeLiteral(fieldDef),
),
]),
);
Expand All @@ -678,6 +681,11 @@ export class TsSchemaGenerator {
return ts.factory.createObjectLiteralExpression(properties, true);
}

private generateFieldTypeLiteral(field: DataModelField): ts.Expression {
invariant(field.type.type || field.type.reference, 'Field type must be a primitive or reference');
return ts.factory.createStringLiteral(field.type.type ?? field.type.reference!.$refText);
}

private createEnumObject(e: Enum) {
return ts.factory.createObjectLiteralExpression(
e.fields.map((field) =>
Expand Down