-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
922 additions
and
847 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
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,114 @@ | ||
import type { ColumnToCodeConverter } from '@payloadcms/drizzle/types' | ||
|
||
export const columnToCodeConverter: ColumnToCodeConverter = ({ | ||
adapter, | ||
addImport, | ||
column, | ||
locales, | ||
}) => { | ||
let columnBuilderFn: string = column.type | ||
|
||
const columnBuilderArgsArray: string[] = [] | ||
|
||
let defaultStatement: null | string = null | ||
|
||
switch (column.type) { | ||
case 'boolean': { | ||
columnBuilderFn = 'integer' | ||
columnBuilderArgsArray.push("mode: 'boolean'") | ||
break | ||
} | ||
|
||
case 'enum': { | ||
let options: string[] | ||
if ('isLocale' in column) { | ||
options = locales | ||
} else { | ||
options = column.options | ||
} | ||
|
||
columnBuilderFn = 'text' | ||
columnBuilderArgsArray.push(`enum: [${options.map((locale) => `'${locale}'`).join(', ')}]`) | ||
|
||
break | ||
} | ||
|
||
case 'geometry': | ||
case 'jsonb': { | ||
columnBuilderFn = 'text' | ||
columnBuilderArgsArray.push("mode: 'json'") | ||
break | ||
} | ||
|
||
case 'serial': { | ||
columnBuilderFn = 'integer' | ||
break | ||
} | ||
|
||
case 'timestamp': { | ||
columnBuilderFn = 'text' | ||
defaultStatement = `default(sql\`(strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))\`)` | ||
break | ||
} | ||
|
||
case 'uuid': { | ||
columnBuilderFn = 'text' | ||
|
||
if (column.defaultRandom) { | ||
addImport('crypto', 'randomUUID') | ||
defaultStatement = `defaultFn(() => randomUUID())` | ||
} | ||
|
||
break | ||
} | ||
|
||
case 'varchar': { | ||
columnBuilderFn = 'text' | ||
break | ||
} | ||
|
||
default: { | ||
columnBuilderFn = column.type | ||
} | ||
} | ||
|
||
addImport(`${adapter.packageName}/drizzle/sqlite-core`, columnBuilderFn) | ||
|
||
let columnBuilderArgs = '' | ||
|
||
if (columnBuilderArgsArray.length) { | ||
columnBuilderArgs = `, {${columnBuilderArgsArray.join(',')}}` | ||
} | ||
|
||
let code = `${columnBuilderFn}('${column.name}'${columnBuilderArgs})` | ||
|
||
if (column.notNull) { | ||
code = `${code}.notNull()` | ||
} | ||
|
||
if (column.primaryKey) { | ||
code = `${code}.primaryKey()` | ||
} | ||
|
||
if (defaultStatement) { | ||
code = `${code}.${defaultStatement}` | ||
} else if (typeof column.default !== 'undefined') { | ||
let sanitizedDefault = column.default | ||
|
||
if (column.type === 'jsonb' || column.type === 'geometry') { | ||
sanitizedDefault = `'${JSON.stringify(column.default)}'` | ||
} else if (typeof column.default === 'string' || column.type === 'numeric') { | ||
sanitizedDefault = `'${column.default}'` | ||
} | ||
|
||
code = `${code}.default(${sanitizedDefault})` | ||
} | ||
|
||
if (column.reference) { | ||
code = `${code}.references(() => ${column.reference.table}.${column.reference.name}, { | ||
${column.reference.onDelete ? `onDelete: '${column.reference.onDelete}'` : ''} | ||
})` | ||
} | ||
|
||
return code | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import type { ColumnToCodeConverter } from '../types.js' | ||
|
||
export const columnToCodeConverter: ColumnToCodeConverter = ({ | ||
adapter, | ||
addEnum, | ||
addImport, | ||
column, | ||
}) => { | ||
let columnBuilderFn: string = column.type | ||
|
||
if (column.type === 'geometry') { | ||
columnBuilderFn = 'geometryColumn' | ||
addImport(`@payloadcms/drizzle/postgres`, columnBuilderFn) | ||
} else if (column.type === 'enum') { | ||
if ('isLocale' in column) { | ||
columnBuilderFn = `enum__locales` | ||
} else { | ||
addEnum(column.enumName, column.options) | ||
columnBuilderFn = column.enumName | ||
} | ||
} else { | ||
addImport(`${adapter.packageName}/drizzle/pg-core`, columnBuilderFn) | ||
} | ||
|
||
const columnBuilderArgsArray: string[] = [] | ||
|
||
if (column.type === 'timestamp') { | ||
columnBuilderArgsArray.push(`mode: '${column.mode}'`) | ||
if (column.withTimezone) { | ||
columnBuilderArgsArray.push('withTimezone: true') | ||
} | ||
|
||
if (typeof column.precision === 'number') { | ||
columnBuilderArgsArray.push(`precision: ${column.precision}`) | ||
} | ||
} | ||
|
||
let columnBuilderArgs = '' | ||
|
||
if (columnBuilderArgsArray.length) { | ||
columnBuilderArgs = `, {${columnBuilderArgsArray.join(',')}}` | ||
} | ||
|
||
let code = `${columnBuilderFn}('${column.name}'${columnBuilderArgs})` | ||
|
||
if (column.type === 'timestamp' && column.defaultNow) { | ||
code = `${code}.defaultNow()` | ||
} | ||
|
||
if (column.type === 'uuid' && column.defaultRandom) { | ||
code = `${code}.defaultRandom()` | ||
} | ||
|
||
if (column.notNull) { | ||
code = `${code}.notNull()` | ||
} | ||
|
||
if (column.primaryKey) { | ||
code = `${code}.primaryKey()` | ||
} | ||
|
||
if (typeof column.default !== 'undefined') { | ||
let sanitizedDefault = column.default | ||
|
||
if (column.type === 'geometry') { | ||
sanitizedDefault = `sql\`${column.default}\`` | ||
} else if (column.type === 'jsonb') { | ||
sanitizedDefault = `sql\`'${JSON.stringify(column.default)}'::jsonb\`` | ||
} else if (column.type === 'numeric') { | ||
sanitizedDefault = `sql\`${column.default}\`` | ||
} else if (typeof column.default === 'string') { | ||
sanitizedDefault = `'${column.default}'` | ||
} | ||
|
||
code = `${code}.default(${sanitizedDefault})` | ||
} | ||
|
||
if (column.reference) { | ||
code = `${code}.references(() => ${column.reference.table}.${column.reference.name}, { | ||
${column.reference.onDelete ? `onDelete: '${column.reference.onDelete}'` : ''} | ||
})` | ||
} | ||
|
||
return code | ||
} |
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
Oops, something went wrong.