diff --git a/packages/generator/src/generator.ts b/packages/generator/src/generator.ts index f7c4dba..6faac28 100644 --- a/packages/generator/src/generator.ts +++ b/packages/generator/src/generator.ts @@ -14,7 +14,6 @@ import { type ModelModule, generateModelModules, } from './lib/adapter/modules/model' -import type { Context } from './lib/context' import { logger } from './lib/logger' import { type ImportValue, @@ -52,20 +51,16 @@ generatorHandler({ logger.log('Generating drizzle schema...') const adapter = await getAdapter(options) - const ctx: Context = { - adapter, - datamodel: options.dmmf.datamodel, - } const modules: GeneratedModules = { extras: adapter.extraModules, - enums: generateEnumModules(options, adapter), - models: generateModelModules(options, ctx), + enums: generateEnumModules(adapter), + models: generateModelModules(adapter), } if (isRelationalQueryEnabled()) { - const relational = generateRelationalModules(modules, ctx) - const implicit = generateImplicitModules(relational, ctx) + const relational = generateRelationalModules(modules.models) + const implicit = generateImplicitModules(adapter, relational) const schema = generateSchemaModule({ ...modules, relational: relational, diff --git a/packages/generator/src/lib/adapter/modules/enums.ts b/packages/generator/src/lib/adapter/modules/enums.ts index aa4e9ae..e56d108 100644 --- a/packages/generator/src/lib/adapter/modules/enums.ts +++ b/packages/generator/src/lib/adapter/modules/enums.ts @@ -1,14 +1,11 @@ -import type { GeneratorOptions } from '@prisma/generator-helper' import { getEnumModuleName } from '~/lib/prisma-helpers/enums' import { createModule } from '~/lib/syntaxes/module' import { generateEnumDeclaration } from '../declarations/generateEnumDeclaration' import type { Adapter } from '../types' +import { getGenerator } from '~/shared/generator-context' -export function generateEnumModules( - options: GeneratorOptions, - adapter: Adapter -) { - return (options.dmmf.datamodel.enums ?? []).map((prismaEnum) => +export function generateEnumModules(adapter: Adapter) { + return (getGenerator().dmmf.datamodel.enums ?? []).map((prismaEnum) => createModule({ name: getEnumModuleName(prismaEnum), declarations: [generateEnumDeclaration(adapter, prismaEnum)], diff --git a/packages/generator/src/lib/adapter/modules/model.ts b/packages/generator/src/lib/adapter/modules/model.ts index a754f04..0ece2f8 100644 --- a/packages/generator/src/lib/adapter/modules/model.ts +++ b/packages/generator/src/lib/adapter/modules/model.ts @@ -1,16 +1,17 @@ -import type { DMMF, GeneratorOptions } from '@prisma/generator-helper' -import type { Context } from '../../context' +import type { DMMF } from '@prisma/generator-helper' import { getModelModuleName } from '../../prisma-helpers/model' import { createModule } from '../../syntaxes/module' import { generateTableDeclaration } from '../declarations/generateTableDeclaration' +import { getGenerator } from '~/shared/generator-context' +import type { Adapter } from '../types' -export function generateModelModules(options: GeneratorOptions, ctx: Context) { - return options.dmmf.datamodel.models.map(createModelModule(ctx)) +export function generateModelModules(adapter: Adapter) { + return getGenerator().dmmf.datamodel.models.map(createModelModule(adapter)) } -export function createModelModule(ctx: Context) { +export function createModelModule(adapter: Adapter) { return (model: DMMF.Model) => { - const tableVar = generateTableDeclaration(ctx.adapter, model) + const tableVar = generateTableDeclaration(adapter, model) return createModule({ name: getModelModuleName(model), diff --git a/packages/generator/src/lib/adapter/modules/relational.ts b/packages/generator/src/lib/adapter/modules/relational.ts index 0be7d61..7075a37 100644 --- a/packages/generator/src/lib/adapter/modules/relational.ts +++ b/packages/generator/src/lib/adapter/modules/relational.ts @@ -1,4 +1,3 @@ -import type { Context } from '../../context' import { isEmpty } from 'lodash' import { isRelationField } from '~/lib/prisma-helpers/field' import { createModule, type Module } from '~/lib/syntaxes/module' @@ -8,6 +7,8 @@ import type { BaseGeneratedModules } from './sets/base-generated-modules' import { deduplicateModels } from '~/generator' import type { DMMF } from '@prisma/generator-helper' import { generateSchemaDeclaration } from '../declarations/generateSchemaDeclaration' +import { getGenerator } from '~/shared/generator-context' +import type { Adapter } from '../types' export type RelationalModuleSet = { relational: RelationalModule[] @@ -15,33 +16,27 @@ export type RelationalModuleSet = { implicitRelational: Module[] } -export function generateRelationalModules( - modules: BaseGeneratedModules, - ctx: Context -) { - return modules.models.flatMap((modelModule) => { - const relationalModule = createRelationalModule({ ctx, modelModule }) +export function generateRelationalModules(modelModules: ModelModule[]) { + return modelModules.flatMap((modelModule) => { + const relationalModule = createRelationalModule(modelModule) if (relationalModule == null) return [] return relationalModule }) } -export function createRelationalModule(input: { - modelModule: ModelModule - ctx: Context -}) { - const { model } = input.modelModule +export function createRelationalModule(modelModule: ModelModule) { + const { model } = modelModule const relationalFields = model.fields.filter(isRelationField) if (isEmpty(relationalFields)) return undefined const declaration = generateTableRelationsDeclaration({ fields: relationalFields, - modelModule: input.modelModule, - datamodel: input.ctx.datamodel, + modelModule: modelModule, + datamodel: getGenerator().dmmf.datamodel, }) return createModule({ - name: `${input.modelModule.name}-relations`, + name: `${modelModule.name}-relations`, declarations: [declaration], implicit: declaration.implicit, }) @@ -52,16 +47,16 @@ export type RelationalModule = NonNullable< > export function generateImplicitModules( - relationalModules: RelationalModule[], - ctx: Context + adapter: Adapter, + relationalModules: RelationalModule[] ) { const models = relationalModules .flatMap((module) => module.implicit) .reduce(deduplicateModels, [] as DMMF.Model[]) - .map(createModelModule(ctx)) + .map(createModelModule(adapter)) const relational = models.flatMap((modelModule) => { - const relationalModule = createRelationalModule({ ctx, modelModule }) + const relationalModule = createRelationalModule(modelModule) if (relationalModule == null) return [] return relationalModule }) diff --git a/packages/generator/src/lib/context.ts b/packages/generator/src/lib/context.ts deleted file mode 100644 index 030146d..0000000 --- a/packages/generator/src/lib/context.ts +++ /dev/null @@ -1,7 +0,0 @@ -import type { DMMF } from '@prisma/generator-helper' -import type { Adapter } from './adapter/types' - -export type Context = { - adapter: Adapter - datamodel: DMMF.Datamodel -} diff --git a/packages/generator/src/shared/generator-context/index.ts b/packages/generator/src/shared/generator-context/index.ts index aae2a4b..670f9c3 100644 --- a/packages/generator/src/shared/generator-context/index.ts +++ b/packages/generator/src/shared/generator-context/index.ts @@ -5,6 +5,8 @@ import { resolveModuleResolution } from './module-resolution' type Generator = { outputBasePath: string moduleResolution?: string + // + dmmf: GeneratorOptions['dmmf'] config: Config } @@ -18,6 +20,8 @@ export function initializeGenerator(options: GeneratorOptions) { const context: Generator = { moduleResolution: config.moduleResolution ?? resolveModuleResolution(), outputBasePath: getBasePath(options), + // + dmmf: options.dmmf, config, } generator_ = context