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

feat: add support for class prefix and postfix #25

Open
wants to merge 1 commit 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.
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 @@ -258,6 +258,10 @@ export class ProductDto extends IntersectionType(
- makes index file, default value is **true**
- _separateRelationFields_
- Puts relational fields into different file for each model. This way the class will match the object returned by a Prisma query, default value is **false**
- _classPrefix_
- Prefix for generated classes, default value is **''**
- _classPostfix_
- Postfix for generated classes, default value is **''**

### **How it works?**

Expand Down
2 changes: 2 additions & 0 deletions prisma/postgresql.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ generator prismaClassGenerator {
output = "../src/_gen/prisma-class"
dryRun = "false"
separateRelationFields = "false"
classPrefix = ""
classPostfix = ""
}

enum ProductType {
Expand Down
7 changes: 6 additions & 1 deletion src/components/class.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@ export class ClassComponent extends BaseComponent implements Echoable {
relationTypes?: string[]
enumTypes?: string[] = []
extra?: string = ''
classPrefix?: string
classPostfix?: string

echo = () => {
const fieldContent = this.fields.map((_field) => _field.echo())

const classPrefix = this.classPrefix ?? ''
const classPostfix = this.classPostfix ?? ''
let str = CLASS_TEMPLATE.replace(
'#!{DECORATORS}',
this.echoDecorators(),
)
.replace('#!{NAME}', `${this.name}`)
.replace('#!{NAME}', `${classPrefix}${this.name}${classPostfix}`)
.replace('#!{FIELDS}', fieldContent.join('\r\n'))
.replace('#!{EXTRA}', this.extra)

Expand Down
10 changes: 9 additions & 1 deletion src/convertor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,15 @@ export class PrismaConvertor {
if (postfix) {
className += postfix
}
const classComponent = new ClassComponent({ name: className })
const classPrefix = this._config.classPrefix
const classPostfix = this._config.classPostfix

const classComponent = new ClassComponent({
name: className,
classPrefix,
classPostfix,
})

/** relation & enums */
const relationTypes = uniquify(
model.fields
Expand All @@ -229,6 +236,7 @@ export class PrismaConvertor {
return true
})
.map((field) => this.convertField(field))

classComponent.relationTypes =
extractRelationFields === false ? [] : relationTypes

Expand Down
13 changes: 12 additions & 1 deletion src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ export const PrismaClassGeneratorOptions = {
desc: 'use undefined default',
defaultValue: false,
},
classPrefix: {
desc: 'set class prefix',
defaultValue: '',
},
classPostfix: {
desc: 'set class postfix',
defaultValue: '',
},
} as const

export type PrismaClassGeneratorOptionsKeys =
Expand Down Expand Up @@ -152,12 +160,15 @@ export class PrismaClassGenerator {
fileRow.write(config.dryRun)
})
if (config.makeIndexFile) {
const classPrefix = config.classPrefix ?? ''
const classPostfix = config.classPostfix ?? ''

const indexFilePath = path.resolve(output, 'index.ts')
const imports = files.map(
(fileRow) =>
new ImportComponent(
getRelativeTSPath(indexFilePath, fileRow.getPath()),
fileRow.prismaClass.name,
`${classPrefix}${fileRow.prismaClass.name}${classPostfix}`,
),
)

Expand Down