Skip to content
Open
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
47 changes: 46 additions & 1 deletion packages/cli/test/prisma-schema-gen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,49 @@ model User {
expect(prismaSchemaText.includes('nanoid(12)')).toBe(true);
expect(prismaSchemaText.includes('nanoid12_%s')).toBe(false);
});
});

it('renames native type mappings to match the datasource', async () => {
const model = await loadSchema(`
datasource ds {
provider = 'postgresql'
url = env('POSTGRES_URL')
}

model User {
id String @id
string String @db.Text
boolean Boolean @db.Boolean
int Int @db.Integer
bigInt BigInt @db.BigInt
float Float @db.Real
decimal Decimal @db.Money
dateTime DateTime @db.Timestamptz(3)
json Json @db.JsonB
bytes Bytes @db.ByteA
}
`);

const generator = new PrismaSchemaGenerator(model);
const prismaSchemaText = await generator.generate();

expect(prismaSchemaText.includes('@db.Text')).toBe(false);
expect(prismaSchemaText.includes('@db.Boolean')).toBe(false);
expect(prismaSchemaText.includes('@db.Integer')).toBe(false);
expect(prismaSchemaText.includes('@db.BigInt')).toBe(false);
expect(prismaSchemaText.includes('@db.Real')).toBe(false);
expect(prismaSchemaText.includes('@db.Money')).toBe(false);
expect(prismaSchemaText.includes('@db.Timestamptz(3)')).toBe(false);
expect(prismaSchemaText.includes('@db.JsonB')).toBe(false);
expect(prismaSchemaText.includes('@db.ByteA')).toBe(false);

expect(prismaSchemaText.includes('@ds.Text')).toBe(true);
expect(prismaSchemaText.includes('@ds.Boolean')).toBe(true);
expect(prismaSchemaText.includes('@ds.Integer')).toBe(true);
expect(prismaSchemaText.includes('@ds.BigInt')).toBe(true);
expect(prismaSchemaText.includes('@ds.Real')).toBe(true);
expect(prismaSchemaText.includes('@ds.Money')).toBe(true);
expect(prismaSchemaText.includes('@ds.Timestamptz(3)')).toBe(true);
expect(prismaSchemaText.includes('@ds.JsonB')).toBe(true);
expect(prismaSchemaText.includes('@ds.ByteA')).toBe(true);
});
});
12 changes: 11 additions & 1 deletion packages/sdk/src/prisma/prisma-schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ export class PrismaSchemaGenerator {
return attr.decl.ref.attributes.some((a) => a.decl.ref?.name === '@@@prisma');
}

private isNativeTypeMappingAttribute(attr: DataFieldAttribute) {
return attr.decl.$refText.startsWith('@db.') && this.isPrismaAttribute(attr);
}

private getUnsupportedFieldType(fieldType: DataFieldType) {
if (fieldType.unsupported) {
const value = getStringLiteral(fieldType.unsupported.value);
Expand Down Expand Up @@ -337,7 +341,13 @@ export class PrismaSchemaGenerator {
}

private makeFieldAttribute(attr: DataFieldAttribute) {
const attrName = attr.decl.ref!.name;
let attrName = attr.decl.ref!.name;
if (this.isNativeTypeMappingAttribute(attr)) {
const dataSource = this.zmodel.declarations.find(isDataSource);
if (dataSource) {
attrName = attrName.replace('@db', `@${dataSource.name}`);
}
}
return new PrismaFieldAttribute(
attrName,
attr.args.map((arg) => this.makeAttributeArg(arg)),
Expand Down
Loading