Skip to content

Commit

Permalink
Merge pull request #68 from fdarian/drizzle-custom-mode
Browse files Browse the repository at this point in the history
feat: `drizzle.custom.field.mode` for customizing `bigint` mode
  • Loading branch information
fdarian authored Jul 5, 2024
2 parents 1458235 + 36c20d1 commit fa88c56
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 7 deletions.
8 changes: 5 additions & 3 deletions packages/generator/src/lib/adapter/fields/createField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export type DefineImport = {
export interface CreateFieldInput {
field: DMMF.Field
imports?: ImportValue[]
func: string
func:
| string
| (<T extends Partial<Record<string, unknown>>>(opts: T) => string)
onDefault?: (
field: FieldWithDefault
) => { code: string; imports?: ImportValue[] } | undefined
Expand All @@ -30,8 +32,6 @@ export function createField(input: CreateFieldInput) {

let imports = input.imports ?? []

let func = `${input.func}`

const custom = getCustomDirective(field)
if (custom?.imports) {
imports = imports.concat(
Expand All @@ -43,6 +43,8 @@ export function createField(input: CreateFieldInput) {
)
}

let func = `${typeof input.func === 'string' ? input.func : input.func(custom?.field ?? {})}`

// .type<...>()
if (custom?.$type) {
func += `.$type<${custom.$type}>()`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,18 @@ const ImportSchema = v.object({
type: v.optional(v.boolean()),
})

const BigIntMode = v.picklist(['number', 'bigint'])
export type BigIntMode = v.InferOutput<typeof BigIntMode>

const FieldSchema = v.object({
mode: v.optional(BigIntMode),
})

const DirectiveSchema = v.object({
imports: v.optional(v.array(ImportSchema)),
$type: v.optional(v.string()),
default: v.optional(v.string()),
field: v.optional(FieldSchema),
})

class InvalidDirectiveShapeError extends Error {
Expand Down
4 changes: 3 additions & 1 deletion packages/generator/src/lib/adapter/providers/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { createModule } from '~/lib/syntaxes/module'
import { getDateMode } from '~/shared/date-mode'
import { createAdapter } from '../adapter'
import { createField, hasDefault, isDefaultFunc } from '../fields/createField'
import type { BigIntMode } from '../fields/directives/custom'

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

Expand Down Expand Up @@ -63,7 +64,8 @@ export const mysqlAdapter = createAdapter({
return createField({
field,
imports: [namedImport(['bigint'], coreModule)],
func: `bigint('${getDbName(field)}', { mode: 'bigint' })`,
func: (opts: { mode?: BigIntMode }) =>
`bigint('${getDbName(field)}', { mode: '${opts?.mode ?? 'bigint'}' })`,
onDefault(field) {
if (
field.isId &&
Expand Down
4 changes: 3 additions & 1 deletion packages/generator/src/lib/adapter/providers/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
hasDefault,
isDefaultFunc,
} from '../fields/createField'
import type { BigIntMode } from '../fields/directives/custom'

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

Expand Down Expand Up @@ -81,7 +82,8 @@ export const postgresAdapter = createAdapter({
return createField({
field,
imports: [namedImport([func], coreModule)],
func: `${func}('${getDbName(field)}', { mode: 'bigint' })`,
func: (opts: { mode?: BigIntMode }) =>
`${func}('${getDbName(field)}', { mode: '${opts?.mode ?? 'bigint'}' })`,
})
},
// https://orm.drizzle.team/docs/column-types/pg/#boolean
Expand Down
3 changes: 2 additions & 1 deletion packages/usage/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ model FieldCustomization {
/// drizzle.custom {
/// "imports": [{ "name": ["SomeBigInt"], "module": "~/tests/shared/testFieldCustomization", "type": true }],
/// "$type": "SomeBigInt",
/// "default": "() => 1n"
/// "default": "() => 1n",
/// "field": { "mode": "number" }
/// }
allFields BigInt
}
2 changes: 1 addition & 1 deletion packages/usage/tests/shared/testFieldCustomization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function testFieldCustomization({ db, schema, provider }: TestContext) {

test('should contain correct field definition', () => {
expect(content).include(
"allFields: bigint('allFields', { mode: 'bigint' }).$type<SomeBigInt>().$defaultFn(() => 1n)"
"allFields: bigint('allFields', { mode: 'number' }).$type<SomeBigInt>().$defaultFn(() => 1n)"
)
})
})
Expand Down

0 comments on commit fa88c56

Please sign in to comment.