Skip to content

Commit

Permalink
Optimize Performance (#7)
Browse files Browse the repository at this point in the history
* Memoize names generation

* Parallelize model creations

* Run formatter optionally

* Update readme

---------

Co-authored-by: farreldarian <[email protected]>
  • Loading branch information
fdarian and farreldarian authored Dec 31, 2023
1 parent 9b77b16 commit a127ffe
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 87 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ generator drizzle {
provider = "prisma-generator-drizzle"
// Optional: specify the output directory
// output = "../models"
// Optional: run prettier
// prettier = "prettier"
}
```

Expand Down
Binary file modified bun.lockb
Binary file not shown.
3 changes: 3 additions & 0 deletions packages/generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ generator drizzle {
provider = "prisma-generator-drizzle"
// Optional: specify the output directory
// output = "../models"
// Optional: run prettier
// prettier = "prettier"
}
```

Expand Down
3 changes: 1 addition & 2 deletions packages/generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
"@prisma/sdk": "4.0.0",
"fp-ts": "^2.16.1",
"lodash": "^4.17.21",
"pluralize": "^8.0.0",
"prettier": "3.1.1"
"pluralize": "^8.0.0"
},
"devDependencies": {
"@types/lodash": "^4.14.202",
Expand Down
83 changes: 41 additions & 42 deletions packages/generator/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
generatorHandler,
GeneratorOptions,
} from '@prisma/generator-helper'
import { execSync } from 'child_process'
import { map, reduce } from 'fp-ts/lib/Array'
import { pipe } from 'fp-ts/lib/function'
import fs from 'fs'
Expand All @@ -23,7 +24,6 @@ import { isRelationField } from './lib/prisma-helpers/field'
import { getModelModuleName } from './lib/prisma-helpers/model'
import { ImportValue, namedImport, NamedImport } from './lib/syntaxes/imports'
import { createModule, Module } from './lib/syntaxes/module'
import { writeFileSafely } from './utils/writeFileSafely'

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

Expand All @@ -48,71 +48,70 @@ generatorHandler({
if (!basePath) throw new Error('No output path specified')

fs.existsSync(basePath) && fs.rmSync(basePath, { recursive: true })
fs.mkdirSync(basePath, { recursive: true })

if (adapter.extraModules) {
await Promise.all(
adapter.extraModules.map(async (module) => {
const moduleCreation = logger.createTask()
await writeModule(basePath, module)
moduleCreation.end(`◟ ${module.name}.ts`)
})
)
}
adapter.extraModules?.forEach((module) => {
const moduleCreation = logger.createTask()
writeModule(basePath, module)
moduleCreation.end(`◟ ${module.name}.ts`)
})

for await (const prismaEnum of options.dmmf.datamodel.enums) {
options.dmmf.datamodel.enums.forEach((prismaEnum) => {
const enumCreation = logger.createTask()

const enumModule = createModule({
name: getEnumModuleName(prismaEnum),
declarations: [generateEnumDeclaration(adapter, prismaEnum)],
})
await writeModule(basePath, enumModule)
writeModule(basePath, enumModule)

enumCreation.end(`◟ ${enumModule.name}.ts`)
}
return enumModule
})

let implicitModels: DMMF.Model[] = []
let models: ModelModule[] = []
for await (const model of options.dmmf.datamodel.models) {
let modelModules = options.dmmf.datamodel.models.map((model) => {
const modelCreation = logger.createTask()

const modelModule = createModelModule({
adapter,
model,
datamodel: options.dmmf.datamodel,
})
await writeModule(basePath, modelModule)

models.push(modelModule)
implicitModels = implicitModels.concat(modelModule.implicit ?? [])
writeModule(basePath, modelModule)

modelCreation.end(`◟ ${modelModule.name}.ts`)
}

const implicitModelModules = await Promise.all(
implicitModels
.reduce(deduplicateModels, [] as DMMF.Model[])
.map(async (model) => {
const modelCreation = logger.createTask()

const modelModule = createModelModule({
adapter,
model,
datamodel: options.dmmf.datamodel,
})
await writeModule(basePath, modelModule)

modelCreation.end(`◟ ${modelModule.name}.ts`)
return modelModule
return modelModule
})

const implicitModelModules = modelModules
.flatMap((module) => module.implicit)
.reduce(deduplicateModels, [] as DMMF.Model[])
.map((model) => {
const modelCreation = logger.createTask()

const modelModule = createModelModule({
adapter,
model,
datamodel: options.dmmf.datamodel,
})
)
models = models.concat(implicitModelModules)
writeModule(basePath, modelModule)

modelCreation.end(`◟ ${modelModule.name}.ts`)
return modelModule
})
modelModules = modelModules.concat(implicitModelModules)

const schemaModule = createModule({
name: 'schema',
declarations: [generateSchemaDeclaration(models)],
declarations: [generateSchemaDeclaration(modelModules)],
})
await writeModule(basePath, schemaModule)
writeModule(basePath, schemaModule)

const formatter = options.generator.config['formatter']
if (formatter === 'prettier') {
execSync(`prettier --write ${basePath}`, { stdio: 'inherit' })
}
},
})

Expand Down Expand Up @@ -149,9 +148,9 @@ export function reduceImports(imports: ImportValue[]) {
]
}

async function writeModule(basePath: string, module: Module) {
function writeModule(basePath: string, module: Module) {
const writeLocation = path.join(basePath, `${module.name}.ts`)
await writeFileSafely(writeLocation, module.code)
fs.writeFileSync(writeLocation, module.code)
}

function getAdapter(options: GeneratorOptions) {
Expand Down
10 changes: 5 additions & 5 deletions packages/generator/src/lib/prisma-helpers/enums.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { DMMF } from '@prisma/generator-helper'
import { camelCase, kebabCase } from 'lodash'
import { camelCase, kebabCase, memoize } from 'lodash'

export function getEnumVarName(prismaEnum: DMMF.DatamodelEnum) {
export const getEnumVarName = memoize((prismaEnum: DMMF.DatamodelEnum) => {
return `${camelCase(prismaEnum.name)}Enum`
}
})

export function getEnumModuleName(prismaEnum: DMMF.DatamodelEnum) {
export const getEnumModuleName = memoize((prismaEnum: DMMF.DatamodelEnum) => {
return kebabCase(getEnumVarName(prismaEnum))
}
})
10 changes: 5 additions & 5 deletions packages/generator/src/lib/prisma-helpers/model.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { DMMF } from '@prisma/generator-helper'
import { camelCase, kebabCase } from 'lodash'
import { camelCase, kebabCase, memoize } from 'lodash'
import pluralize from 'pluralize'

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

export function getModelModuleName(model: DMMF.Model | string) {
export const getModelModuleName = memoize((model: DMMF.Model | string) => {
return kebabCase(pluralize(typeof model === 'string' ? model : model.name))
}
})
22 changes: 0 additions & 22 deletions packages/generator/src/utils/formatFile.ts

This file was deleted.

11 changes: 0 additions & 11 deletions packages/generator/src/utils/writeFileSafely.ts

This file was deleted.

0 comments on commit a127ffe

Please sign in to comment.