Skip to content

Commit

Permalink
Merge pull request #48 from farreldarian/customizable-date
Browse files Browse the repository at this point in the history
Allow custom dateMode
  • Loading branch information
fdarian authored Apr 9, 2024
2 parents 2f15dac + 814c448 commit 0f038b1
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
5 changes: 3 additions & 2 deletions packages/generator/src/lib/adapter/providers/mysql.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { camelCase, kebabCase } from 'lodash'
import { getDbName } from '~/lib/prisma-helpers/getDbName'
import { namedImport } from '~/lib/syntaxes/imports'
import { createModule } from '~/lib/syntaxes/module'
import { getGenerator } from '~/shared/generator-context'
import { createAdapter } from '../adapter'
import { createField, hasDefault, isDefaultFunc } from '../fields/createField'
import { createModule } from '~/lib/syntaxes/module'

const coreModule = 'drizzle-orm/mysql-core'

Expand Down Expand Up @@ -96,7 +97,7 @@ export const mysqlAdapter = createAdapter({
return createField({
field,
imports: [namedImport(['datetime'], coreModule)],
func: `datetime('${getDbName(field)}', { mode: 'date', fsp: 3 })`,
func: `datetime('${getDbName(field)}', { mode: '${getGenerator().dateMode}', fsp: 3 })`, // biome-ignore format: keep one line
// https://github.com/drizzle-team/drizzle-orm/issues/921
onDefault: (field) => {
if (
Expand Down
9 changes: 4 additions & 5 deletions packages/generator/src/lib/adapter/providers/postgres.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { camelCase, kebabCase } from 'lodash'
import { getDbName } from '~/lib/prisma-helpers/getDbName'
import { namedImport } from '~/lib/syntaxes/imports'
import { createModule } from '~/lib/syntaxes/module'
import { getGenerator } from '~/shared/generator-context'
import { createAdapter } from '../adapter'
import {
type CreateFieldInput,
createField as baseCreateField,
hasDefault,
isDefaultFunc,
} from '../fields/createField'
import { createModule } from '~/lib/syntaxes/module'

const coreModule = 'drizzle-orm/pg-core'

Expand Down Expand Up @@ -104,9 +105,7 @@ export const postgresAdapter = createAdapter({
return createField({
field,
imports: [namedImport(['timestamp'], coreModule)],
func: `timestamp('${getDbName(
field
)}', { mode: 'date', precision: 3 })`,
func: `timestamp('${getDbName(field)}', { mode: '${getGenerator().dateMode}', precision: 3 })`, // biome-ignore format: keep one line
})
},
// https://orm.drizzle.team/docs/column-types/pg/#decimal
Expand All @@ -132,7 +131,7 @@ export const postgresAdapter = createAdapter({
isDefaultFunc(field) &&
field.default.name === 'autoincrement'
? // https://arc.net/l/quote/mpimqrfn
'serial'
'serial'
: 'integer'

return createField({
Expand Down
7 changes: 6 additions & 1 deletion packages/generator/src/lib/adapter/providers/sqlite.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getDbName } from '~/lib/prisma-helpers/getDbName'
import { namedImport } from '~/lib/syntaxes/imports'
import { createModule } from '~/lib/syntaxes/module'
import { getGenerator } from '~/shared/generator-context'
import { createAdapter } from '../adapter'
import { createField, hasDefault, isDefaultFunc } from '../fields/createField'

Expand Down Expand Up @@ -99,10 +100,14 @@ export const sqliteAdapter = createAdapter({
// Prisma: https://arc.net/l/quote/grwnsumx
// Drizzle: https://arc.net/l/quote/fpupjigo
DateTime(field) {
if (getGenerator().dateMode !== 'date') {
throw new Error('Only dateMode `date` is supported for sqlite')
}

return createField({
field,
imports: [namedImport(['integer'], coreModule)],
func: `integer('${getDbName(field)}', { mode: 'timestamp' })`,
func: `integer('${getDbName(field)}', { mode: 'timestamp' })`, // biome-ignore format: keep one line
})
},
// Prisma: https://arc.net/l/quote/sgkjrpxh
Expand Down
5 changes: 5 additions & 0 deletions packages/generator/src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
object,
optional,
safeParse,
union,
} from 'valibot'
import { ModuleResolution } from '~/shared/generator-context/module-resolution'
import { BooleanInStr, withDefault } from './valibot-schema'
Expand All @@ -16,6 +17,10 @@ const Config = object({
moduleResolution: optional(ModuleResolution),
verbose: optional(BooleanInStr),
formatter: optional(literal('prettier')),
dateMode: withDefault(
optional(union([literal('string'), literal('date')])),
'date'
),
})
export type Config = Output<typeof Config>

Expand Down
2 changes: 2 additions & 0 deletions packages/generator/src/shared/generator-context/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Output = {
type Generator = {
moduleResolution?: string
output: Output
dateMode: Config['dateMode']
//
dmmf: GeneratorOptions['dmmf']
config: Config
Expand All @@ -26,6 +27,7 @@ export function initializeGenerator(options: GeneratorOptions) {
const context: Generator = {
moduleResolution: config.moduleResolution ?? resolveModuleResolution(),
output,
dateMode: config.dateMode,
//
dmmf: options.dmmf,
config,
Expand Down

0 comments on commit 0f038b1

Please sign in to comment.