Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: local enums def file #111

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 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 @@ -46,13 +46,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 @@ -246,3 +245,17 @@ export const generateBarrelFile = (models: DMMF.Model[], indexFile: SourceFile)
})
)
}

export const generateEnumsFile = (enums: DMMF.DatamodelEnum[], enumsFile: SourceFile) => {
enums.forEach(({ name, values }) => {
const members = values.map(({ name: memberName }) => {
return { name: memberName, value: memberName }
})
enumsFile
.addEnum({
name,
members,
})
.setIsExported(true)
})
}
19 changes: 18 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { version } from '../package.json'
import { generatorHandler } from '@prisma/generator-helper'
import { SemicolonPreference } from 'typescript'
import { configSchema, PrismaOptions } from './config'
import { populateModelFile, generateBarrelFile } from './generator'
import { populateModelFile, generateBarrelFile, generateEnumsFile } from './generator'
import { Project } from 'ts-morph'

generatorHandler({
Expand All @@ -19,6 +19,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 @@ -69,6 +70,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()
},
})
18 changes: 17 additions & 1 deletion src/test/functional/driver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import path from 'path'
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 @@ -65,6 +65,22 @@ const ftForDir = (dir: string) => async () => {

expect(actualIndexContents).toStrictEqual(expectedIndexContents)

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/functional/imports/expected/document.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as z from "zod"
import { Status } from "../prisma/.client"
import { Status } from "./enums"

export const DocumentModel = z.object({
id: z.string(),
Expand Down
5 changes: 5 additions & 0 deletions src/test/functional/imports/expected/enums.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum Status {
draft = "draft",
live = "live",
archived = "archived"
}
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 "zod";\nimport { UserType } from "@prisma/client";\n'
'import * as z from "zod";\nimport { UserType } from "./enums";\n'
)
})
})