Skip to content

Commit

Permalink
Refactor enum, table, and relation definition
Browse files Browse the repository at this point in the history
  • Loading branch information
farreldarian committed Dec 26, 2023
1 parent 7e976ad commit 506f66b
Show file tree
Hide file tree
Showing 13 changed files with 242 additions and 164 deletions.
347 changes: 217 additions & 130 deletions packages/generator/src/generator.ts

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions packages/generator/src/lib/adapter/base/defineColumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import { DMMF } from '@prisma/generator-helper'
import { createValue } from '../../value/createValue'
import { IChainableValue } from '../../value/types/funcValue'
import { Adapter } from '../adapter'
import { ImportValue } from 'src/lib/value/types/import'

type DefineImport = {
export type DefineImport = {
module: string
name: string
}

interface DefineColumnInput<TAdapter extends Adapter> {
adapter: TAdapter
field: DMMF.Field
imports?: DefineImport[]
imports?: ImportValue[]
columnFunc: IChainableValue
}

Expand Down Expand Up @@ -53,7 +54,7 @@ abstract class Extension {
abstract shouldChain(): boolean
abstract getFunc(): IChainableValue

getImports(): DefineImport[] {
getImports(): ImportValue[] {
return []
}
}
Expand Down Expand Up @@ -92,7 +93,7 @@ class ChainableExtension extends Extension {

getImports() {
if (!this.enabled) return []
return [{ module: this.module!, name: this.type! }]
return [v.namedImport([this.type!], this.module!)]
}
}
// #endregion
2 changes: 1 addition & 1 deletion packages/generator/src/lib/adapter/columns/defineBigint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function defineBigint(adapter: Adapter, field: DMMF.Field) {
return defineColumn({
field,
adapter,
imports: [{ module: adapter.module, name: adapter.functions.bigint }],
imports: [v.namedImport([adapter.functions.bigint], adapter.module)],
columnFunc: fieldFunc(adapter.functions.bigint, field, {
mode: v.string('bigint'),
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function defineBoolean(adapter: Adapter, field: DMMF.Field) {
return defineColumn({
field,
adapter,
imports: [{ module: adapter.module, name: adapter.functions.boolean }],
imports: [v.namedImport([adapter.functions.boolean], adapter.module)],
columnFunc: fieldFunc(adapter.functions.boolean, field),
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function defineDatetime(adapter: Adapter, field: DMMF.Field) {
return defineColumn({
field,
adapter,
imports: [{ module: adapter.module, name: adapter.functions.datetime }],
imports: [v.namedImport([adapter.functions.datetime], adapter.module)],
columnFunc: fieldFunc(adapter.functions.datetime, field, {
...adapter.definition.datetime.opts,
mode: v.string('date'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function defineDecimal(adapter: Adapter, field: DMMF.Field) {
return defineColumn({
field,
adapter,
imports: [{ module: adapter.module, name: adapter.functions.decimal }],
imports: [v.namedImport([adapter.functions.decimal], adapter.module)],
columnFunc: fieldFunc(adapter.functions.decimal, field, {
precision: v.number(65),
scale: v.number(30),
Expand Down
2 changes: 1 addition & 1 deletion packages/generator/src/lib/adapter/columns/defineEnum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function defineEnum(adapter: Adapter, field: DMMF.Field) {
return defineColumn({
field,
adapter,
imports: [{ module: `./${kebabCase(field.type)}-enum`, name: func }],
imports: [v.namedImport([func], `./${kebabCase(field.type)}-enum`)],
columnFunc: fieldFunc(func, field),
})
}
3 changes: 2 additions & 1 deletion packages/generator/src/lib/adapter/columns/defineFloat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { DMMF } from '@prisma/generator-helper'
import { defineColumn } from '../base/defineColumn'
import { Adapter } from '../adapter'
import { fieldFunc } from './fieldFunc'
import { v } from 'src/lib/value'

// https://www.prisma.io/docs/orm/reference/prisma-schema-reference#float
export function defineFloat(adapter: Adapter, field: DMMF.Field) {
return defineColumn({
field,
adapter,
imports: [{ module: adapter.module, name: adapter.functions.float }],
imports: [v.namedImport([adapter.functions.float], adapter.module)],
columnFunc: fieldFunc(adapter.functions.float, field),
})
}
2 changes: 1 addition & 1 deletion packages/generator/src/lib/adapter/columns/defineInt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function defineInt(adapter: Adapter, field: DMMF.Field) {
return defineColumn({
field,
adapter,
imports: [{ module: adapter.module, name: adapter.functions.int }],
imports: [v.namedImport([adapter.functions.int], adapter.module)],
columnFunc: fieldFunc(adapter.functions.int, field),
})
}
2 changes: 1 addition & 1 deletion packages/generator/src/lib/adapter/columns/defineJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function defineJson(adapter: Adapter, field: DMMF.Field) {
return defineColumn({
field,
adapter,
imports: [{ module: adapter.module, name: adapter.functions.json }],
imports: [v.namedImport([adapter.functions.json], adapter.module)],
columnFunc: fieldFunc(adapter.functions.json, field),
})
}
2 changes: 1 addition & 1 deletion packages/generator/src/lib/adapter/columns/defineString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function defineString(adapter: Adapter, field: DMMF.Field) {
return defineColumn({
field,
adapter,
imports: [{ module: adapter.module, name: adapter.functions.string }],
imports: [v.namedImport([adapter.functions.string], adapter.module)],
columnFunc: fieldFunc(adapter.functions.string, field),
})
}
17 changes: 0 additions & 17 deletions packages/generator/src/lib/import-builder.ts

This file was deleted.

14 changes: 10 additions & 4 deletions packages/generator/src/lib/value/types/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@ import { IValue, createValue } from '../createValue'

export function namedImportValue(names: string[], path: string) {
return createValue({
type: 'namedImport',
type: 'namedImport' as const,
names: names,
module: path,
render() {
return `import { ${names.join(', ')} } from '${path}';`
},
})
}
export type NamedImport = ReturnType<typeof namedImportValue>

export function defaultImportValue(name: string, path: string) {
return createValue({
type: 'defaultImport',
type: 'defaultImport' as const,
name,
module: path,
render() {
return `import ${name} from '${path}';`
},
Expand All @@ -20,14 +25,15 @@ export function defaultImportValue(name: string, path: string) {

export function wildcardImportValue(alias: string, path: string) {
return createValue({
type: 'wildcardImport',
type: 'wildcardImport' as const,
module: path,
render() {
return `import * as ${alias} from '${path}';`
},
})
}

export type ImportValue =
| ReturnType<typeof namedImportValue>
| NamedImport
| ReturnType<typeof defaultImportValue>
| ReturnType<typeof wildcardImportValue>

0 comments on commit 506f66b

Please sign in to comment.