Skip to content

Commit

Permalink
feature: Add outputApiPropertyType setting (#43)
Browse files Browse the repository at this point in the history
Co-authored-by: Koen Lemmen <[email protected]>
  • Loading branch information
KoenLemmen and Koen Lemmen authored Jul 31, 2024
1 parent 7560d34 commit 26d6b51
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 9 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ generator nestjsDto {
definiteAssignmentAssertion = "false"
requiredResponseApiProperty = "true"
prettier = "false"
outputApiPropertyType = "true"
}
```

Expand All @@ -82,6 +83,7 @@ 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() |

## Annotations

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@brakebein/prisma-generator-nestjs-dto",
"description": "Generates DTO and Entity classes from Prisma Schema for NestJS",
"version": "1.22.0",
"version": "1.23.0",
"license": "Apache-2.0",
"author": {
"name": "Benjamin Kroeger",
Expand Down
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ generator nestjsDto {
outputType = "class"
definiteAssignmentAssertion = "true"
prettier = "true"
outputApiPropertyType = "true"
}

model Product {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,10 @@ export const computeConnectDtoParams = ({
...field,
...overrides,
},
{ default: false },
{
default: false,
type: templateHelpers.config.outputApiPropertyType,
},
);
const typeProperty = decorators.apiProperties.find(
(p) => p.name === 'type',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,14 @@ export const computeCreateDtoParams = ({
if (isAnnotatedWith(field, DTO_API_HIDDEN)) {
decorators.apiHideProperty = true;
} else {
// If outputApiPropertyType is false, make sure to set includeType false, otherwise use negated overrides.type
const includeType = templateHelpers.config.outputApiPropertyType
? !overrides.type
: false;
decorators.apiProperties = parseApiProperty(field, {
type: !overrides.type,
type: includeType,
});
if (overrides.type)
if (overrides.type && templateHelpers.config.outputApiPropertyType)
decorators.apiProperties.push({
name: 'type',
value: overrides.type,
Expand Down
5 changes: 4 additions & 1 deletion src/generator/compute-model-params/compute-entity-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,10 @@ export const computeEntityParams = ({
: false,
isNullable: !field.isRequired,
},
{ default: false },
{
default: false,
type: templateHelpers.config.outputApiPropertyType,
},
);
const typeProperty = decorators.apiProperties.find(
(p) => p.name === 'type',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ export const computePlainDtoParams = ({
: false,
isNullable: !field.isRequired,
},
{ default: false },
{
default: false,
type: templateHelpers.config.outputApiPropertyType,
},
);
const typeProperty = decorators.apiProperties.find(
(p) => p.name === 'type',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,17 +206,21 @@ export const computeUpdateDtoParams = ({
if (isAnnotatedWith(field, DTO_API_HIDDEN)) {
decorators.apiHideProperty = true;
} else {
// If outputApiPropertyType is false, make sure to set includeType false, otherwise use negated overrides.type
const includeType = templateHelpers.config.outputApiPropertyType
? !overrides.type
: false;
decorators.apiProperties = parseApiProperty(
{
...field,
...overrides,
isNullable: !field.isRequired,
},
{
type: !overrides.type,
type: includeType,
},
);
if (overrides.type)
if (overrides.type && templateHelpers.config.outputApiPropertyType)
decorators.apiProperties.push({
name: 'type',
value: overrides.type,
Expand Down
10 changes: 9 additions & 1 deletion src/generator/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,15 @@ export const generateUniqueInput = ({
}

if (!t.config.noDependencies) {
decorators.apiProperties = parseApiProperty({ ...field, ...overrides });
decorators.apiProperties = parseApiProperty(
{
...field,
...overrides,
},
{
type: t.config.outputApiPropertyType,
},
);
const typeProperty = decorators.apiProperties.find(
(p) => p.name === 'type',
);
Expand Down
3 changes: 3 additions & 0 deletions src/generator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ interface RunParam {
definiteAssignmentAssertion: boolean;
requiredResponseApiProperty: boolean;
prismaClientImportPath: string;
outputApiPropertyType: boolean;
}

export const run = ({
Expand All @@ -51,6 +52,7 @@ export const run = ({
definiteAssignmentAssertion,
requiredResponseApiProperty,
prismaClientImportPath,
outputApiPropertyType,
...preAndSuffixes
} = options;

Expand All @@ -72,6 +74,7 @@ export const run = ({
definiteAssignmentAssertion,
prismaClientImportPath,
requiredResponseApiProperty,
outputApiPropertyType,
...preAndSuffixes,
});
const allModels = dmmf.datamodel.models;
Expand Down
3 changes: 3 additions & 0 deletions src/generator/template-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ interface MakeHelpersParam {
definiteAssignmentAssertion: boolean;
requiredResponseApiProperty: boolean;
prismaClientImportPath: string;
outputApiPropertyType: boolean;
}
export const makeHelpers = ({
connectDtoPrefix,
Expand All @@ -119,6 +120,7 @@ export const makeHelpers = ({
definiteAssignmentAssertion,
requiredResponseApiProperty,
prismaClientImportPath,
outputApiPropertyType,
}: MakeHelpersParam) => {
const className = (name: string, prefix = '', suffix = '') =>
`${prefix}${transformClassNameCase(name)}${suffix}`;
Expand Down Expand Up @@ -263,6 +265,7 @@ export const makeHelpers = ({
definiteAssignmentAssertion,
requiredResponseApiProperty,
prismaClientImportPath,
outputApiPropertyType,
},
apiExtraModels,
entityName,
Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ export const generate = async (options: GeneratorOptions) => {
}
}

const outputApiPropertyType = stringToBoolean(
options.generator.config.outputApiPropertyType,
true,
);

const results = run({
output,
dmmf: options.dmmf,
Expand All @@ -163,6 +168,7 @@ export const generate = async (options: GeneratorOptions) => {
definiteAssignmentAssertion,
requiredResponseApiProperty,
prismaClientImportPath,
outputApiPropertyType,
});

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

0 comments on commit 26d6b51

Please sign in to comment.