Skip to content

Commit

Permalink
remove the need to use another context
Browse files Browse the repository at this point in the history
  • Loading branch information
farreldarian committed Apr 6, 2024
1 parent ae27865 commit 3df79e0
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 47 deletions.
13 changes: 4 additions & 9 deletions packages/generator/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 3 additions & 6 deletions packages/generator/src/lib/adapter/modules/enums.ts
Original file line number Diff line number Diff line change
@@ -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)],
Expand Down
13 changes: 7 additions & 6 deletions packages/generator/src/lib/adapter/modules/model.ts
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
33 changes: 14 additions & 19 deletions packages/generator/src/lib/adapter/modules/relational.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -8,40 +7,36 @@ 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[]
implicitModels: ModelModule[]
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,
})
Expand All @@ -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
})
Expand Down
7 changes: 0 additions & 7 deletions packages/generator/src/lib/context.ts

This file was deleted.

4 changes: 4 additions & 0 deletions packages/generator/src/shared/generator-context/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { resolveModuleResolution } from './module-resolution'
type Generator = {
outputBasePath: string
moduleResolution?: string
//
dmmf: GeneratorOptions['dmmf']
config: Config
}

Expand All @@ -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
Expand Down

0 comments on commit 3df79e0

Please sign in to comment.