Skip to content

Commit

Permalink
Refactor defineTableVar out
Browse files Browse the repository at this point in the history
  • Loading branch information
farreldarian committed Dec 26, 2023
1 parent 196850b commit 9fd9754
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 30 deletions.
31 changes: 4 additions & 27 deletions packages/generator/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { createValue, IValue } from './lib/value/createValue'
import { pipe } from 'fp-ts/lib/function'
import { render } from './lib/value/utils'
import { Adapter, mysqlAdapter, pgAdapter } from './lib/adapter/adapter'
import { or } from 'fp-ts/lib/Refinement'
import { defineBigint } from './lib/adapter/columns/defineBigint'
import { IColumnValue } from './lib/adapter/base/defineColumn'
import { defineBoolean } from './lib/adapter/columns/defineBoolean'
Expand All @@ -35,6 +34,7 @@ import {
} from './lib/value/types/import'
import { defineVar } from './lib/value/types/defineVar'
import { useVar } from './lib/value/types/useVar'
import { defineTableVar } from './lib/adapter/vars/defineTableVar'

const { version } = require('../package.json')

Expand Down Expand Up @@ -155,29 +155,6 @@ function reduceImports(imports: ImportValue[]) {
]
}

function defineTableVar(adapter: Adapter, model: DMMF.Model) {
const fields = model.fields
.filter(pipe(isKind('scalar'), or(isKind('enum'))))
.map(getField(adapter))
const name = getModelVarName(model)

return createValue({
name,
imports: [
namedImport([adapter.functions.table], adapter.module),
...fields.flatMap((field) => field.imports),
],
render: defineVar(
name,
adapter.table(
model.name,
fields.map((field) => [field.field, field])
),
{ export: true }
).render,
})
}

function defineTableRelationsVar(
tableVarName: string,
fields: DMMFRelationField[]
Expand Down Expand Up @@ -226,7 +203,7 @@ function getAdapter(options: GeneratorOptions) {
})()
}

function getField(adapter: Adapter) {
export function getField(adapter: Adapter) {
return function (field: DMMF.Field): IColumnValue {
if (field.kind === 'enum') {
return defineEnum(adapter, field)
Expand Down Expand Up @@ -313,11 +290,11 @@ function getEnumModuleName(prismaEnum: DMMF.DatamodelEnum) {
}
// #endregion

function isKind(kind: DMMF.FieldKind) {
export function isKind(kind: DMMF.FieldKind) {
return (field: DMMF.Field): field is DMMF.Field => field.kind === kind
}

function getModelVarName(model: DMMF.Model | string) {
export function getModelVarName(model: DMMF.Model | string) {
return camelCase(pluralize(typeof model === 'string' ? model : model.name))
}

Expand Down
3 changes: 0 additions & 3 deletions packages/generator/src/lib/adapter/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ function createAdapter<TName extends string>(input: {
module: input.module,
functions,
definition: input.definition,
table(name: string, fields: Entry[]) {
return v.func(input.functions.table, [v.string(name), v.object(fields)])
},
}
}

Expand Down
32 changes: 32 additions & 0 deletions packages/generator/src/lib/adapter/vars/defineTableVar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { DMMF } from '@prisma/generator-helper'
import { createValue } from '../../value/createValue'
import { pipe } from 'fp-ts/lib/function'
import { Adapter } from '../adapter'
import { or } from 'fp-ts/lib/Refinement'
import { namedImport } from '../../value/types/import'
import { defineVar } from '../../value/types/defineVar'
import { isKind, getField, getModelVarName } from '../../../generator'
import { v } from 'src/lib/value'

export function defineTableVar(adapter: Adapter, model: DMMF.Model) {
const fields = model.fields
.filter(pipe(isKind('scalar'), or(isKind('enum'))))
.map(getField(adapter))
const name = getModelVarName(model)

return createValue({
name,
imports: [
namedImport([adapter.functions.table], adapter.module),
...fields.flatMap((field) => field.imports),
],
render: defineVar(
name,
v.func(adapter.functions.table, [
v.string(model.name),
v.object(fields.map((field) => [field.field, field])),
]),
{ export: true }
).render,
})
}

0 comments on commit 9fd9754

Please sign in to comment.