diff --git a/packages/cli/test/prisma-schema-gen.test.ts b/packages/cli/test/prisma-schema-gen.test.ts index 34aed5d6f..b5a8ce733 100644 --- a/packages/cli/test/prisma-schema-gen.test.ts +++ b/packages/cli/test/prisma-schema-gen.test.ts @@ -46,4 +46,49 @@ model User { expect(prismaSchemaText.includes('nanoid(12)')).toBe(true); expect(prismaSchemaText.includes('nanoid12_%s')).toBe(false); }); -}); \ No newline at end of file + + 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); + }); +}); diff --git a/packages/sdk/src/prisma/prisma-schema-generator.ts b/packages/sdk/src/prisma/prisma-schema-generator.ts index f15422678..0eefb28c8 100644 --- a/packages/sdk/src/prisma/prisma-schema-generator.ts +++ b/packages/sdk/src/prisma/prisma-schema-generator.ts @@ -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); @@ -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)),