Skip to content
This repository has been archived by the owner on Nov 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2 from risenforces/feat/local-enums
Browse files Browse the repository at this point in the history
Add local enums
  • Loading branch information
Evgeny Zakharov authored Jun 15, 2022
2 parents c17c9f5 + 21a0dec commit 0aaad25
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 7 deletions.
23 changes: 20 additions & 3 deletions src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const writeImportsForModel = (
model: DMMF.Model,
sourceFile: SourceFile,
config: Config,
{ schemaPath, outputPath, clientPath }: PrismaOptions
{ schemaPath, outputPath }: PrismaOptions
) => {
const { relatedModelName } = useModelNames(config)
const importList: ImportDeclarationStructure[] = [
Expand Down Expand Up @@ -49,13 +49,12 @@ export const writeImportsForModel = (

const enumFields = model.fields.filter((f) => f.kind === 'enum')
const relationFields = model.fields.filter((f) => f.kind === 'object')
const relativePath = path.relative(outputPath, clientPath)

if (enumFields.length > 0) {
importList.push({
kind: StructureKind.ImportDeclaration,
isTypeOnly: enumFields.length === 0,
moduleSpecifier: dotSlash(relativePath),
moduleSpecifier: dotSlash('enums'),
namedImports: enumFields.map((f) => f.type),
})
}
Expand Down Expand Up @@ -237,3 +236,21 @@ export const generateBarrelFile = (
})
)
}

export const generateEnumsFile = (
enums: DMMF.DatamodelEnum[],
enumsFile: SourceFile
) => {
for (const { name, values } of enums) {
const members = values.map(({ name: memberName }) => {
return { name: memberName, value: memberName }
})

enumsFile
.addEnum({
name,
members,
})
.setIsExported(true)
}
}
23 changes: 22 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { generatorHandler } from '@prisma/generator-helper'
import { Project } from 'ts-morph'
import { SemicolonPreference } from 'typescript'
import { configSchema, PrismaOptions } from './config'
import { populateModelFile, generateBarrelFile } from './generator'
import {
populateModelFile,
generateBarrelFile,
generateEnumsFile,
} from './generator'

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

Expand All @@ -18,6 +22,7 @@ generatorHandler({
const project = new Project()

const models = options.dmmf.datamodel.models
const enums = options.dmmf.datamodel.enums

const { schemaPath } = options
const outputPath = options.generator.output!.value
Expand Down Expand Up @@ -68,6 +73,22 @@ generatorHandler({
})
})

if (enums.length > 0) {
const enumsFile = project.createSourceFile(
`${outputPath}/enums.ts`,
{},
{ overwrite: true }
)

generateEnumsFile(enums, enumsFile)

enumsFile.formatText({
indentSize: 2,
convertTabsToSpaces: true,
semicolons: SemicolonPreference.Remove,
})
}

return project.save()
},
})
2 changes: 1 addition & 1 deletion src/test/functional/__snapshots__/driver.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export * from \\"./spreadsheet\\"
exports[`Functional Tests Imports: imports - document 1`] = `
"import * as z from \\"nestjs-zod/z\\"
import { Status } from \\"../prisma/.client\\"
import { Status } from \\"./enums\\"
export const DocumentModel = z.object({
id: z.string(),
Expand Down
22 changes: 21 additions & 1 deletion src/test/functional/driver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { readFile } from 'fs-extra'
import { Project } from 'ts-morph'
import { SemicolonPreference } from 'typescript'
import { configSchema, PrismaOptions } from '../../config'
import { populateModelFile, generateBarrelFile } from '../../generator'
import {
populateModelFile,
generateBarrelFile,
generateEnumsFile,
} from '../../generator'

jest.setTimeout(10000)

Expand Down Expand Up @@ -68,6 +72,22 @@ const ftForDir = (dir: string) => async () => {

expect(actualIndexContents).toMatchSnapshot(`${dir} - Index file`)

if (dmmf.datamodel.enums.length > 0) {
const enumsFile = project.createSourceFile(
`${actualDir}/enums.ts`,
{},
{ overwrite: true }
)

generateEnumsFile(dmmf.datamodel.enums, enumsFile)

enumsFile.formatText({
indentSize: 2,
convertTabsToSpaces: true,
semicolons: SemicolonPreference.Remove,
})
}

await Promise.all(
dmmf.datamodel.models.map(async (model) => {
const sourceFile = project.createSourceFile(
Expand Down
2 changes: 1 addition & 1 deletion src/test/regressions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('Regression Tests', () => {
writeImportsForModel(model, testFile, config, prismaOptions)

expect(testFile.print()).toBe(
'import * as z from "nestjs-zod/z";\nimport { UserType } from "@prisma/client";\n'
'import * as z from "nestjs-zod/z";\nimport { UserType } from "./enums";\n'
)
})
})

0 comments on commit 0aaad25

Please sign in to comment.