Skip to content

Commit

Permalink
add generateFileTypes flag to output only dto or entity files #42
Browse files Browse the repository at this point in the history
  • Loading branch information
Brakebein committed Jul 31, 2024
1 parent b80eea1 commit f00410c
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ generator nestjsDto {
requiredResponseApiProperty = "true"
prettier = "false"
outputApiPropertyType = "true"
generateFileTypes = "all"
}
```

Expand All @@ -83,7 +84,8 @@ All parameters are optional.
| `definiteAssignmentAssertion = "false"` | Add a definite assignment assertion operator `!` to required fields, which is required if `strict` and/or `strictPropertyInitialization` is set `true` in your tsconfig.json's `compilerOptions`. |
| `requiredResponseApiProperty = "true"` | If `false`, add `@ApiRequired({ required: false })` to response DTO properties. Otherwise, use `required` defaults always to `true` unless field is optional. |
| `prettier = "false"` | Stylize output files with prettier. |
| `outputApiPropertyType = "true"` | Disable the type property inside @ApiProperty() |
| `outputApiPropertyType = "true"` | Disable the type property inside @ApiProperty() |
| `generateFileTypes = "all"` | `all`: generate both DTO and Entity files, `dto`: generate only DTO files, `entity`: generate only Entity files (not possible in combination with complex types) |

## Annotations

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"cleanup:generated": "rimraf src/@generated",
"pregenerate": "npm run cleanup:generated",
"generate": "npx prisma generate",
"generate:mongo": "npx prisma generate --schema ./prisma/mongodb.prisma && npm run format -- --loglevel error",
"generate:mongo": "npx prisma generate --schema ./prisma/mongodb.prisma",
"prepare": "npm run build"
},
"repository": {
Expand Down
2 changes: 2 additions & 0 deletions prisma/mongodb.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ generator nestjsDto {
classValidation = "true"
noDependencies = "false"
outputType = "class"
prettier = "true"
requiredResponseApiProperty = "false"
generateFileTypes = "dto"
}

model Product {
Expand Down
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ generator nestjsDto {
definiteAssignmentAssertion = "true"
prettier = "true"
outputApiPropertyType = "true"
generateFileTypes = "all"
}

model Product {
Expand Down
19 changes: 18 additions & 1 deletion src/generator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ interface RunParam {
requiredResponseApiProperty: boolean;
prismaClientImportPath: string;
outputApiPropertyType: boolean;
generateFileTypes: string;
}

export const run = ({
Expand All @@ -53,6 +54,7 @@ export const run = ({
requiredResponseApiProperty,
prismaClientImportPath,
outputApiPropertyType,
generateFileTypes,
...preAndSuffixes
} = options;

Expand Down Expand Up @@ -93,6 +95,12 @@ export const run = ({
},
}));

if (generateFileTypes === 'entity' && filteredTypes.length) {
throw new Error(
`Generating only Entity files while having complex types is not possible. Set 'generateFileTypes' to 'all' or 'dto'.`,
);
}

const filteredModels: Model[] = allModels
.filter((model) => !isAnnotatedWith(model, DTO_IGNORE_MODEL))
// adds `output` information for each model, so we can compute relative import paths
Expand Down Expand Up @@ -239,7 +247,16 @@ export const run = ({
}),
};

return [connectDto, createDto, updateDto, entity, plainDto];
switch (generateFileTypes) {
case 'all':
return [connectDto, createDto, updateDto, entity, plainDto];
case 'dto':
return [connectDto, createDto, updateDto, plainDto];
case 'entity':
return [entity];
default:
throw new Error(`Unknown 'generateFileTypes' value.`);
}
});

return [...typeFiles, ...modelFiles].flat();
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const generate = async (options: GeneratorOptions) => {
entitySuffix = '',
fileNamingStyle = 'camel',
outputType = 'class',
generateFileTypes = 'all',
} = options.generator.config;

const exportRelationModifierClasses = stringToBoolean(
Expand Down Expand Up @@ -169,6 +170,7 @@ export const generate = async (options: GeneratorOptions) => {
requiredResponseApiProperty,
prismaClientImportPath,
outputApiPropertyType,
generateFileTypes,
});

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

0 comments on commit f00410c

Please sign in to comment.