Skip to content
This repository has been archived by the owner on Jan 5, 2025. It is now read-only.

Commit

Permalink
Merge branch 'release/1.3'
Browse files Browse the repository at this point in the history
* release/1.3:
  bumps version to 1.3.0
  feat: adds `reExport` option
  • Loading branch information
benkroeger committed Jul 30, 2021
2 parents 82ae413 + 7cc67db commit ec3e8b2
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 9 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ generator nestjsDto {
output = "../src/generated/nestjs-dto"
outputToNestJsResourceStructure = "false"
exportRelationModifierClasses = "true"
reExport = "false"
createDtoPrefix = "Create"
updateDtoPrefix = "Update"
dtoSuffix = "Dto"
Expand All @@ -44,6 +45,7 @@ All parameters are optional.
- [`output`]: (default: `"../src/generated/nestjs-dto"`) - output path relative to your `schema.prisma` file
- [`outputToNestJsResourceStructure`]: (default: `"false"`) - writes `dto`s and `entities` to subfolders aligned with [NestJS CRUD generator](https://docs.nestjs.com/recipes/crud-generator). Resource module name is derived from lower-cased model name in `schema.prisma`
- [`exportRelationModifierClasses`]: (default: `"true"`) - Should extra classes generated for relationship field operations on DTOs be exported?
- [`reExport`]: (default: `false`) - Should an index.ts be created for every folder?
- [`createDtoPrefix`]: (default: `"Create"`) - phrase to prefix every `CreateDTO` class with
- [`updateDtoPrefix`]: (default: `"Update"`) - phrase to prefix every `UpdateDTO` class with
- [`dtoSuffix`]: (default: `"Dto"`) - phrase to suffix every `CreateDTO` and `UpdateDTO` class with
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@vegardit/prisma-generator-nestjs-dto",
"description": "Generates DTO and Entity classes from Prisma Schema for NestJS",
"version": "1.2.2",
"version": "1.3.0",
"license": "Apache-2.0",
"author": {
"name": "Benjamin Kroeger",
Expand Down
3 changes: 2 additions & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ datasource db {
generator nestgraphql {
provider = "node -r ts-node/register/transpile-only src/cli.ts"
output = "../src/@generated/prisma-nestjs-graphql"
outputToNestJsResourceStructure = "true"
outputToNestJsResourceStructure = "false"
exportRelationModifierClasses = "true"
reExport = "true"
createDtoPrefix = "Create"
updateDtoPrefix = "Update"
dtoSuffix = "Dto"
Expand Down
8 changes: 6 additions & 2 deletions src/generator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { DTO_IGNORE_MODEL } from './annotations';
import { isAnnotatedWith } from './field-classifiers';

import type { DMMF } from '@prisma/generator-helper';
import { Model } from './types';
import { Model, WriteableFileSpecs } from './types';

interface RunParam {
output: string;
Expand All @@ -28,7 +28,11 @@ interface RunParam {
entityPrefix: string;
entitySuffix: string;
}
export const run = ({ output, dmmf, ...options }: RunParam) => {
export const run = ({
output,
dmmf,
...options
}: RunParam): WriteableFileSpecs[] => {
const {
exportRelationModifierClasses,
outputToNestJsResourceStructure,
Expand Down
5 changes: 5 additions & 0 deletions src/generator/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,8 @@ export interface ModelParams {
update: UpdateDtoParams;
entity: EntityParams;
}

export type WriteableFileSpecs = {
fileName: string;
content: string;
};
34 changes: 30 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { parseEnvValue } from '@prisma/sdk';
import { run } from './generator';

import type { GeneratorOptions } from '@prisma/generator-helper';
import type { WriteableFileSpecs } from './generator/types';

export const stringToBoolean = (input: string, defaultValue = false) => {
if (input === 'true') {
Expand Down Expand Up @@ -43,6 +44,12 @@ export const generate = (options: GeneratorOptions) => {
false,
);

const reExport = stringToBoolean(
options.generator.config.reExport,
// using `true` as default value would be a breaking change
false,
);

const results = run({
output,
dmmf: options.dmmf,
Expand All @@ -56,11 +63,30 @@ export const generate = (options: GeneratorOptions) => {
entitySuffix,
});

const indexCollections: Record<string, WriteableFileSpecs> = {};

if (reExport) {
results.forEach(({ fileName }) => {
const dirName = path.dirname(fileName);

const { [dirName]: fileSpec } = indexCollections;
indexCollections[dirName] = {
fileName: fileSpec?.fileName || path.join(dirName, 'index.ts'),
content: [
fileSpec?.content || '',
`export * from './${path.basename(fileName, '.ts')}';`,
].join('\n'),
};
});
}

return Promise.all(
results.map(async ({ fileName, content }) => {
await makeDir(path.dirname(fileName));
return fs.writeFile(fileName, content);
}),
results
.concat(Object.values(indexCollections))
.map(async ({ fileName, content }) => {
await makeDir(path.dirname(fileName));
return fs.writeFile(fileName, content);
}),
);
};

Expand Down

0 comments on commit ec3e8b2

Please sign in to comment.