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

Add option to add js extensions to generated ts files #128

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ npm install -g yarn

imports = null // (default) will import the referenced file in generated schemas to be used via imports.someExportedVariable

// https://devblogs.microsoft.com/typescript/announcing-typescript-4-7-beta/#ecmascript-module-support-in-node-js
includeJSExtension = true // includes the .js extension on relative import/exports to comply with ESM in TS syntax requirements ()
// includeJSExtension = false // (default) leaves relative import/exports extensionless

// https://www.prisma.io/docs/concepts/components/prisma-client/working-with-fields/working-with-json-fields#filtering-by-null-values
prismaJsonNullability = true // (default) uses prisma's scheme for JSON field nullability
// prismaJsonNullability = false // allows null assignment to optional JSON fields
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const configSchema = z.object({
modelCase: z.enum(['PascalCase', 'camelCase']).default('PascalCase'),
useDecimalJs: configBoolean.default('false'),
imports: z.string().optional(),
includeJSExtension: configBoolean.default('false'),
prismaJsonNullability: configBoolean.default('true'),
})

Expand Down
8 changes: 5 additions & 3 deletions src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const writeImportsForModel = (
if (filteredFields.length > 0) {
importList.push({
kind: StructureKind.ImportDeclaration,
moduleSpecifier: './index',
moduleSpecifier: `./index${jsExt(config)}`,
namedImports: Array.from(
new Set(
filteredFields.flatMap((f) => [
Expand Down Expand Up @@ -239,10 +239,12 @@ export const populateModelFile = (
generateRelatedSchemaForModel(model, sourceFile, config, prismaOptions)
}

export const generateBarrelFile = (models: DMMF.Model[], indexFile: SourceFile) => {
export const generateBarrelFile = (models: DMMF.Model[], indexFile: SourceFile, config: Config) => {
models.forEach((model) =>
indexFile.addExportDeclaration({
moduleSpecifier: `./${model.name.toLowerCase()}`,
moduleSpecifier: `./${model.name.toLowerCase()}${jsExt(config)}`
})
)
}

const jsExt = (config: Config) => config.includeJSExtension ? '.js' : ''
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ generatorHandler({
{ overwrite: true }
)

generateBarrelFile(models, indexFile)
generateBarrelFile(models, indexFile, config)

indexFile.formatText({
indentSize: 2,
Expand Down
3 changes: 2 additions & 1 deletion src/test/functional/driver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const ftForDir = (dir: string) => async () => {

const indexFile = project.createSourceFile(`${outputPath}/index.ts`, {}, { overwrite: true })

generateBarrelFile(dmmf.datamodel.models, indexFile)
generateBarrelFile(dmmf.datamodel.models, indexFile, config)

indexFile.formatText({
indentSize: 2,
Expand Down Expand Up @@ -105,6 +105,7 @@ describe('Functional Tests', () => {
test.concurrent('Config', ftForDir('config'))
test.concurrent('Docs', ftForDir('docs'))
test.concurrent('Different Client Path', ftForDir('different-client-path'))
test.concurrent('JS Extension', ftForDir('js-extension'))
test.concurrent('Recursive Schema', ftForDir('recursive'))
test.concurrent('relationModel = false', ftForDir('relation-false'))
test.concurrent('Relation - 1 to 1', ftForDir('relation-1to1'))
Expand Down
10 changes: 10 additions & 0 deletions src/test/functional/js-extension/expected/document.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as z from "zod"

export const DocumentModel = z.object({
id: z.string(),
filename: z.string(),
author: z.string(),
contents: z.string(),
created: z.date(),
updated: z.date(),
})
1 change: 1 addition & 0 deletions src/test/functional/js-extension/expected/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./document.js"
12 changes: 12 additions & 0 deletions src/test/functional/js-extension/prisma/.client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Model Document
*
*/
export type Document = {
id: string
filename: string
author: string
contents: string
created: Date
updated: Date
}
28 changes: 28 additions & 0 deletions src/test/functional/js-extension/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

datasource db {
provider = "postgresql"
url = ""
}

generator client {
provider = "prisma-client-js"
output = ".client"
}

generator zod {
provider = "zod-prisma"
output = "../actual/"
includeJSExtension = "true"
}

model Document {
id String @id @default(cuid())
filename String @unique
author String
contents String

created DateTime @default(now())
updated DateTime @default(now())
}