-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔧 Implement convertToPostgresColumnType function for PostgreSQL type …
…conversion and update parser to utilize it
1 parent
6d26866
commit 7085005
Showing
4 changed files
with
106 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@liam-hq/db-structure": patch | ||
"@liam-hq/cli": patch | ||
--- | ||
|
||
🔧 Implement convertToPostgresColumnType function for PostgreSQL type conversion and update parser to utilize it |
84 changes: 84 additions & 0 deletions
84
frontend/packages/db-structure/src/parser/prisma/convertToPostgresColumnType.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import type { DMMF } from '@prisma/generator-helper' | ||
|
||
// ref: https://www.prisma.io/docs/orm/reference/prisma-schema-reference#model-field-scalar-types | ||
export function convertToPostgresColumnType( | ||
type: string, | ||
nativeType: DMMF.Field['nativeType'], | ||
defaultValue: DMMF.Field['default'] | null, | ||
): string { | ||
if (nativeType) { | ||
const [nativeTypeName, nativeTypeArgs] = nativeType | ||
|
||
// If the default value includes 'autoincrement()', return the appropriate serial type | ||
if ( | ||
typeof defaultValue === 'string' && | ||
defaultValue.includes('autoincrement()') | ||
) { | ||
switch (nativeTypeName) { | ||
case 'Int': | ||
return 'serial' | ||
case 'SmallInt': | ||
return 'smallserial' | ||
case 'BigInt': | ||
return 'bigserial' | ||
default: | ||
return nativeTypeName.toLowerCase() | ||
} | ||
} | ||
|
||
// If nativeType has arguments, format it as 'type(args)' | ||
// For example, when `price Decimal @db.Decimal(10, 2)`, type should be Decimal(10, 2) | ||
if (nativeTypeArgs.length > 0) { | ||
return `${nativeTypeName.toLowerCase()}(${nativeTypeArgs.join(',')})` | ||
} | ||
|
||
// Special case for 'DoublePrecision' to return 'double precision' with a space | ||
if (nativeTypeName === 'DoublePrecision') { | ||
return 'double precision' | ||
} | ||
return nativeTypeName.toLowerCase() | ||
} | ||
|
||
// If nativeType is not provided, use the Prisma field type to determine the PostgreSQL column type | ||
if ( | ||
typeof defaultValue === 'string' && | ||
defaultValue.includes('autoincrement()') | ||
) { | ||
switch (type) { | ||
case 'Int': | ||
return 'serial' | ||
case 'BigInt': | ||
return 'bigserial' | ||
default: | ||
return type.toLowerCase() | ||
} | ||
} | ||
|
||
// Special case for 'uuid' default value | ||
if (typeof defaultValue === 'string' && defaultValue.includes('uuid')) { | ||
return 'uuid' | ||
} | ||
|
||
switch (type) { | ||
case 'String': | ||
return 'text' | ||
case 'Boolean': | ||
return 'boolean' | ||
case 'Int': | ||
return 'integer' | ||
case 'BigInt': | ||
return 'bigint' | ||
case 'Float': | ||
return 'double precision' | ||
case 'DateTime': | ||
return 'timestamp(3)' | ||
case 'Json': | ||
return 'jsonb' | ||
case 'Decimal': | ||
return 'decimal(65,30)' | ||
case 'Bytes': | ||
return 'bytea' | ||
default: | ||
return type | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters