From d44b4817cf0749b09151cf1b17ee57c257fcd5f5 Mon Sep 17 00:00:00 2001 From: Farrel Darian <62016900+farreldarian@users.noreply.github.com> Date: Tue, 2 Jan 2024 11:25:36 +0700 Subject: [PATCH] Allow disable relational query generation (#8) * Add isRelationalQueryEnabled helper * Refactor to context * Disable relationalQuery generation when disabled * Update readme --------- Co-authored-by: farreldarian --- README.md | 10 ++++- packages/generator/README.md | 10 ++++- packages/generator/src/generator.ts | 64 +++++++++++++-------------- packages/generator/src/lib/config.ts | 9 ++++ packages/generator/src/lib/context.ts | 9 ++++ 5 files changed, 66 insertions(+), 36 deletions(-) create mode 100644 packages/generator/src/lib/config.ts create mode 100644 packages/generator/src/lib/context.ts diff --git a/README.md b/README.md index 3a781e4..e59912d 100644 --- a/README.md +++ b/README.md @@ -31,11 +31,17 @@ npm install drizzle-orm ```prisma generator drizzle { provider = "prisma-generator-drizzle" - // Optional: specify the output directory + + // # Optionals: + + // - Specify the output directory // output = "../models" - // Optional: run prettier + // - Run prettier // prettier = "prettier" + + // - Disable relational query + // relationalQuery = false } ``` diff --git a/packages/generator/README.md b/packages/generator/README.md index 3a781e4..e59912d 100644 --- a/packages/generator/README.md +++ b/packages/generator/README.md @@ -31,11 +31,17 @@ npm install drizzle-orm ```prisma generator drizzle { provider = "prisma-generator-drizzle" - // Optional: specify the output directory + + // # Optionals: + + // - Specify the output directory // output = "../models" - // Optional: run prettier + // - Run prettier // prettier = "prettier" + + // - Disable relational query + // relationalQuery = false } ``` diff --git a/packages/generator/src/generator.ts b/packages/generator/src/generator.ts index d2b2e28..ad806c9 100644 --- a/packages/generator/src/generator.ts +++ b/packages/generator/src/generator.ts @@ -17,7 +17,8 @@ import { generateTableRelationsDeclaration } from './lib/adapter/declarations/ge import { mysqlAdapter } from './lib/adapter/providers/mysql' import { postgresAdapter } from './lib/adapter/providers/postgres' import { sqliteAdapter } from './lib/adapter/providers/sqlite' -import { Adapter } from './lib/adapter/types' +import { isRelationalQueryEnabled } from './lib/config' +import { Context } from './lib/context' import { logger } from './lib/logger' import { getEnumModuleName } from './lib/prisma-helpers/enums' import { isRelationField } from './lib/prisma-helpers/field' @@ -43,6 +44,11 @@ generatorHandler({ throw new Error('Only one datasource is supported') const adapter = getAdapter(options) + const ctx: Context = { + adapter, + config: options.generator.config, + datamodel: options.dmmf.datamodel, + } const basePath = options.generator.output?.value if (!basePath) throw new Error('No output path specified') @@ -72,11 +78,7 @@ generatorHandler({ let modelModules = options.dmmf.datamodel.models.map((model) => { const modelCreation = logger.createTask() - const modelModule = createModelModule({ - adapter, - model, - datamodel: options.dmmf.datamodel, - }) + const modelModule = createModelModule({ model, ctx }) writeModule(basePath, modelModule) modelCreation.end(`◟ ${modelModule.name}.ts`) @@ -90,11 +92,7 @@ generatorHandler({ .map((model) => { const modelCreation = logger.createTask() - const modelModule = createModelModule({ - adapter, - model, - datamodel: options.dmmf.datamodel, - }) + const modelModule = createModelModule({ model, ctx }) writeModule(basePath, modelModule) modelCreation.end(`◟ ${modelModule.name}.ts`) @@ -102,11 +100,13 @@ generatorHandler({ }) modelModules = modelModules.concat(implicitModelModules) - const schemaModule = createModule({ - name: 'schema', - declarations: [generateSchemaDeclaration(modelModules)], - }) - writeModule(basePath, schemaModule) + if (isRelationalQueryEnabled(options.generator.config)) { + const schemaModule = createModule({ + name: 'schema', + declarations: [generateSchemaDeclaration(modelModules)], + }) + writeModule(basePath, schemaModule) + } const formatter = options.generator.config['formatter'] if (formatter === 'prettier') { @@ -176,22 +176,22 @@ function ifExists(value: T | null | undefined): T[] { return [value] } -function createModelModule(input: { - model: DMMF.Model - datamodel: DMMF.Datamodel - adapter: Adapter -}) { - const tableVar = generateTableDeclaration(input.adapter, input.model) - - const relationalFields = input.model.fields.filter(isRelationField) - const relationsVar = isEmpty(relationalFields) - ? null - : generateTableRelationsDeclaration({ - model: input.model, - tableVarName: tableVar.name, - fields: relationalFields, - datamodel: input.datamodel, - }) +function createModelModule(input: { model: DMMF.Model; ctx: Context }) { + const tableVar = generateTableDeclaration(input.ctx.adapter, input.model) + + const relationsVar = (() => { + if (!isRelationalQueryEnabled(input.ctx.config)) return null + + const relationalFields = input.model.fields.filter(isRelationField) + if (isEmpty(relationalFields)) return null + + return generateTableRelationsDeclaration({ + model: input.model, + tableVarName: tableVar.name, + fields: relationalFields, + datamodel: input.ctx.datamodel, + }) + })() return createModule({ name: getModelModuleName(input.model), diff --git a/packages/generator/src/lib/config.ts b/packages/generator/src/lib/config.ts new file mode 100644 index 0000000..8beb78a --- /dev/null +++ b/packages/generator/src/lib/config.ts @@ -0,0 +1,9 @@ +import { Dictionary } from '@prisma/sdk' + +export type Config = Dictionary + +export function isRelationalQueryEnabled(config: Config) { + const value = config['relationalQuery'] + if (value === 'false') return false + return true +} diff --git a/packages/generator/src/lib/context.ts b/packages/generator/src/lib/context.ts new file mode 100644 index 0000000..7e7b88f --- /dev/null +++ b/packages/generator/src/lib/context.ts @@ -0,0 +1,9 @@ +import { DMMF } from '@prisma/generator-helper' +import { Adapter } from './adapter/types' +import { Config } from './config' + +export type Context = { + adapter: Adapter + config: Config + datamodel: DMMF.Datamodel +}