Skip to content

Commit

Permalink
Map Prisma's Json type to the right PG type
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-dp committed Nov 28, 2023
1 parent 80c1dc1 commit 73c3766
Showing 1 changed file with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ function pgType(field: ExtendedDMMFField, modelName: string): string {
const prismaType = field.type
const attributes = field.attributes
switch (prismaType) {
// BigInt, Boolean, Bytes, DateTime, Decimal, Float, Int, JSON, String
case 'String':
return stringToPg(attributes)
case 'Int':
Expand All @@ -125,12 +124,22 @@ function pgType(field: ExtendedDMMFField, modelName: string): string {
case 'Float':
return 'FLOAT8'
case 'Json':
return 'JSON'
return jsonToPg(attributes)
default:
return 'UNRECOGNIZED PRISMA TYPE'
}
}

function jsonToPg(attributes: Array<Attribute>) {
const pgTypeAttribute = attributes.find((a) => a.type.startsWith('@db'))
if (pgTypeAttribute && pgTypeAttribute.type === '@db.Json') {
return 'JSON'
} else {
// default mapping for Prisma's `Json` type is PG's JSONB
return 'JSONB'
}
}

function dateTimeToPg(
attributes: Array<Attribute>,
field: string,
Expand Down

0 comments on commit 73c3766

Please sign in to comment.