Skip to content

Commit

Permalink
Allow disable relational query generation (#8)
Browse files Browse the repository at this point in the history
* Add isRelationalQueryEnabled helper

* Refactor to context

* Disable relationalQuery generation when disabled

* Update readme

---------

Co-authored-by: farreldarian <[email protected]>
  • Loading branch information
fdarian and farreldarian authored Jan 2, 2024
1 parent d0fa833 commit d44b481
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 36 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```

Expand Down
10 changes: 8 additions & 2 deletions packages/generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```

Expand Down
64 changes: 32 additions & 32 deletions packages/generator/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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')
Expand Down Expand Up @@ -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`)
Expand All @@ -90,23 +92,21 @@ 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`)
return modelModule
})
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') {
Expand Down Expand Up @@ -176,22 +176,22 @@ function ifExists<T>(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),
Expand Down
9 changes: 9 additions & 0 deletions packages/generator/src/lib/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Dictionary } from '@prisma/sdk'

export type Config = Dictionary<string | string[] | undefined>

export function isRelationalQueryEnabled(config: Config) {
const value = config['relationalQuery']
if (value === 'false') return false
return true
}
9 changes: 9 additions & 0 deletions packages/generator/src/lib/context.ts
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit d44b481

Please sign in to comment.