From 6078eb9187ef134fd8e711d5a0dd6430d5f5cee0 Mon Sep 17 00:00:00 2001 From: Roman_Vasilev Date: Sun, 28 Feb 2021 15:24:38 +0400 Subject: [PATCH] chore!: Renamed config option `atomicNumberOperation` to `noAtomicOperations` BREAKING CHANGE: Made all options which mutates/renames types are `false` --- .cz-config.js | 11 +- README.md | 41 +-- Taskfile | 14 + package.json | 1 + prisma/schema.prisma | 7 +- .../dummy/dummy-avg-aggregate.input.ts | 15 + .../dummy/dummy-avg-aggregate.output.ts | 15 + .../dummy/dummy-count-aggregate.input.ts | 15 + .../dummy/dummy-count-aggregate.output.ts | 15 + .../dummy/dummy-create-many.input.ts | 17 +- src/@generated/dummy/dummy-create.input.ts | 17 +- src/@generated/dummy/dummy-group-by.output.ts | 17 +- .../dummy/dummy-max-aggregate.input.ts | 15 + .../dummy/dummy-max-aggregate.output.ts | 17 +- .../dummy/dummy-min-aggregate.input.ts | 15 + .../dummy/dummy-min-aggregate.output.ts | 17 +- src/@generated/dummy/dummy-order-by.input.ts | 15 + .../dummy/dummy-scalar-field.enum.ts | 3 + ...ummy-scalar-where-with-aggregates.input.ts | 18 ++ .../dummy/dummy-sum-aggregate.input.ts | 15 + .../dummy/dummy-sum-aggregate.output.ts | 17 +- .../dummy/dummy-unchecked-create.input.ts | 17 +- .../dummy-unchecked-update-many.input.ts | 18 ++ .../dummy/dummy-unchecked-update.input.ts | 18 ++ .../dummy/dummy-update-many-mutation.input.ts | 18 ++ src/@generated/dummy/dummy-update.input.ts | 18 ++ src/@generated/dummy/dummy-where.input.ts | 18 ++ src/@generated/dummy/dummy.model.ts | 17 +- .../float-field-update-operations.input.ts | 29 ++ src/@generated/prisma/float-filter.input.ts | 46 +++ .../float-with-aggregates-filter.input.ts | 73 +++++ ...sted-float-with-aggregates-filter.input.ts | 72 +++++ src/generate.spec.ts | 273 ++++++++++++++++-- src/generate.ts | 36 ++- src/handlers/index.ts | 21 -- src/handlers/no-atomic-operations.ts | 34 +++ src/helpers/create-config.spec.ts | 4 +- src/helpers/create-config.ts | 8 +- src/index.ts | 7 +- 39 files changed, 943 insertions(+), 101 deletions(-) create mode 100644 src/@generated/prisma/float-field-update-operations.input.ts create mode 100644 src/@generated/prisma/float-filter.input.ts create mode 100644 src/@generated/prisma/float-with-aggregates-filter.input.ts create mode 100644 src/@generated/prisma/nested-float-with-aggregates-filter.input.ts delete mode 100644 src/handlers/index.ts create mode 100644 src/handlers/no-atomic-operations.ts diff --git a/.cz-config.js b/.cz-config.js index bee8336e..90275a41 100644 --- a/.cz-config.js +++ b/.cz-config.js @@ -10,8 +10,7 @@ module.exports = { }, { value: 'refactor', - name: - 'refactor: A code change that neither fixes a bug nor adds a feature', + name: 'refactor: A code change that neither fixes a bug nor adds a feature', }, { value: 'perf', @@ -61,14 +60,12 @@ module.exports = { body: 'Provide a LONGER description of the change (optional). Use "|" to break new line:\n', breaking: 'List any BREAKING CHANGES (optional):\n', - footer: - 'List any ISSUES CLOSED by this change (optional). E.g.: #31, #34:\n', - confirmCommit: - 'Are you sure you want to proceed with the commit above?', + footer: 'List any ISSUES CLOSED by this change (optional). E.g.: #31, #34:\n', + confirmCommit: 'Are you sure you want to proceed with the commit above?', }, allowCustomScopes: true, - allowBreakingChanges: ['feat', 'fix'], + allowBreakingChanges: ['feat', 'fix', 'refactor', 'chore'], // skip any questions you want skipQuestions: ['body'], diff --git a/README.md b/README.md index ae546706..871bb682 100644 --- a/README.md +++ b/README.md @@ -34,26 +34,29 @@ npx prisma generate ## Generator options - `output` Output folder relative to this schema file -- `outputFilePattern` File pattern (default: `{feature}/{name}.{type}.ts`) +- `outputFilePattern` File pattern + Type: `string` + Default: `{feature}/{name}.{type}.ts` Possible tokens: - - `{feature}` - model name in dashed case or 'prisma' if unknown - - `{name}` - dashed-case name of model/input/arg without suffix - - `{type}` - short type name (model, input, args, output) - - `{plural.type}` - plural short type name (models, inputs, enums) -- `combineScalarFilters` - Combine nested/nullable scalar filters to single - (default: `true`) -- `noAtomicNumberOperations` - Remove atomic number operations, - `true` (default), `false` -- `reExportAll` - create `index.ts` files for each directory with re-export, - `false` - disabled (default), `true` - enabled -- `renameZooTypes` - Remove plague words from type names ('nullabe', 'nested') - `true` - remove (default), `false` - do not remove -- `types_*` - [flatten](https://github.com/hughsk/flat) map of types - - - `types_{type}_fieldType` - TypeScript type name - - `types_{type}_fieldModule` - Module to import - - `types_{type}_graphqlType` - GraphQL type name - - `types_{type}_graphqlModule` - Module to import + - `{feature}` Model name in dashed case or 'prisma' if unknown + - `{name}` Dashed-case name of model/input/arg without suffix + - `{type}` Short type name (model, input, args, output) + - `{plural.type}` Plural short type name (models, inputs, enums) +- `combineScalarFilters` Combine nested/nullable scalar filters to single + Type: `boolean` + Default: `true` +- `noAtomicOperations` Remove input types for atomic operations + Type: `boolean` + Default: `false` +- `reExportAll` Create `index.ts` files for each directory with re-export + Type: `boolean` + Default: `false` +- `types_*` - Map prisma types in [flatten](https://github.com/hughsk/flat) style + + - `types_{type}_fieldType` TypeScript field type name + - `types_{type}_fieldModule` Module to import + - `types_{type}_graphqlType` GraphQL type name + - `types_{type}_graphqlModule` Module to import Where `{type}` is prisma type in schema diff --git a/Taskfile b/Taskfile index 892086fd..af80f870 100644 --- a/Taskfile +++ b/Taskfile @@ -2,6 +2,20 @@ PATH="$PWD/node_modules/.bin":$PATH set -e +compatibilty_check() { + rm -rf src/@generated && npm run prisma:g && npm run tscheck + + # Enable noAtomicOperations + sed -i 's/noAtomicOperations = false/noAtomicOperations = true/g' prisma/schema.prisma + rm -rf src/@generated && npm run prisma:g && npm run tscheck + sed -i 's/noAtomicOperations = true/noAtomicOperations = false/g' prisma/schema.prisma + + # Enable combineScalarFilters + sed -i 's/combineScalarFilters = false/combineScalarFilters = true/g' prisma/schema.prisma + rm -rf src/@generated && npm run prisma:g && npm run tscheck + sed -i 's/combineScalarFilters = true/combineScalarFilters = false/g' prisma/schema.prisma +} + build() { set -x rm -rf dist diff --git a/package.json b/package.json index a5eddb05..7d88c601 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "regen": "rm -rf src/@generated && npm run prisma:g && npm run lint:src:fix && npm run format", "example": "ts-node-dev src/example/main.ts", "clean_cache": "rm -rf node_modules/.cache", + "compatibilty_check": "sh Taskfile compatibilty_check", "commit": "cz" }, "config": { diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 15623a84..38040054 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -11,9 +11,9 @@ generator client { generator nestgraphql { provider = "node -r ts-node/register/transpile-only src/index.ts" output = "../src/@generated" - renameZooTypes = true + noAtomicOperations = false combineScalarFilters = true - noAtomicNumberOperations = true + renameZooTypes = false } model User { @@ -74,6 +74,9 @@ enum Role { model Dummy { id String @id + floaty Float + int Int? + float Float? bytes Bytes? decimal Decimal? bigInt BigInt? diff --git a/src/@generated/dummy/dummy-avg-aggregate.input.ts b/src/@generated/dummy/dummy-avg-aggregate.input.ts index f20d4f1d..1617fe96 100644 --- a/src/@generated/dummy/dummy-avg-aggregate.input.ts +++ b/src/@generated/dummy/dummy-avg-aggregate.input.ts @@ -2,6 +2,21 @@ import { Field, InputType } from '@nestjs/graphql'; @InputType() export class DummyAvgAggregateInput { + @Field(() => Boolean, { + nullable: true, + }) + floaty?: true; + + @Field(() => Boolean, { + nullable: true, + }) + int?: true; + + @Field(() => Boolean, { + nullable: true, + }) + float?: true; + @Field(() => Boolean, { nullable: true, }) diff --git a/src/@generated/dummy/dummy-avg-aggregate.output.ts b/src/@generated/dummy/dummy-avg-aggregate.output.ts index a87d90a0..ed6d3e9f 100644 --- a/src/@generated/dummy/dummy-avg-aggregate.output.ts +++ b/src/@generated/dummy/dummy-avg-aggregate.output.ts @@ -2,6 +2,21 @@ import { Field, Float, ObjectType } from '@nestjs/graphql'; @ObjectType() export class DummyAvgAggregate { + @Field(() => Float, { + nullable: false, + }) + floaty!: number; + + @Field(() => Float, { + nullable: true, + }) + int?: number; + + @Field(() => Float, { + nullable: true, + }) + float?: number; + @Field(() => String, { nullable: true, }) diff --git a/src/@generated/dummy/dummy-count-aggregate.input.ts b/src/@generated/dummy/dummy-count-aggregate.input.ts index 323fd58d..09a6b45f 100644 --- a/src/@generated/dummy/dummy-count-aggregate.input.ts +++ b/src/@generated/dummy/dummy-count-aggregate.input.ts @@ -7,6 +7,21 @@ export class DummyCountAggregateInput { }) id?: true; + @Field(() => Boolean, { + nullable: true, + }) + floaty?: true; + + @Field(() => Boolean, { + nullable: true, + }) + int?: true; + + @Field(() => Boolean, { + nullable: true, + }) + float?: true; + @Field(() => Boolean, { nullable: true, }) diff --git a/src/@generated/dummy/dummy-count-aggregate.output.ts b/src/@generated/dummy/dummy-count-aggregate.output.ts index 73d3191d..0efa14e7 100644 --- a/src/@generated/dummy/dummy-count-aggregate.output.ts +++ b/src/@generated/dummy/dummy-count-aggregate.output.ts @@ -7,6 +7,21 @@ export class DummyCountAggregate { }) id?: number; + @Field(() => Int, { + nullable: false, + }) + floaty!: number; + + @Field(() => Int, { + nullable: true, + }) + int?: number; + + @Field(() => Int, { + nullable: true, + }) + float?: number; + @Field(() => Int, { nullable: true, }) diff --git a/src/@generated/dummy/dummy-create-many.input.ts b/src/@generated/dummy/dummy-create-many.input.ts index 5fae7107..86ed8daf 100644 --- a/src/@generated/dummy/dummy-create-many.input.ts +++ b/src/@generated/dummy/dummy-create-many.input.ts @@ -1,4 +1,4 @@ -import { Field, InputType } from '@nestjs/graphql'; +import { Field, Float, InputType, Int } from '@nestjs/graphql'; import { GraphQLJSON } from 'graphql-type-json'; @InputType() @@ -8,6 +8,21 @@ export class DummyCreateManyInput { }) id!: string; + @Field(() => Float, { + nullable: false, + }) + floaty!: number; + + @Field(() => Int, { + nullable: true, + }) + int?: number; + + @Field(() => Float, { + nullable: true, + }) + float?: number; + @Field(() => String, { nullable: true, }) diff --git a/src/@generated/dummy/dummy-create.input.ts b/src/@generated/dummy/dummy-create.input.ts index 71ddca5c..804aa885 100644 --- a/src/@generated/dummy/dummy-create.input.ts +++ b/src/@generated/dummy/dummy-create.input.ts @@ -1,4 +1,4 @@ -import { Field, InputType } from '@nestjs/graphql'; +import { Field, Float, InputType, Int } from '@nestjs/graphql'; import { GraphQLJSON } from 'graphql-type-json'; @InputType() @@ -8,6 +8,21 @@ export class DummyCreateInput { }) id!: string; + @Field(() => Float, { + nullable: false, + }) + floaty!: number; + + @Field(() => Int, { + nullable: true, + }) + int?: number; + + @Field(() => Float, { + nullable: true, + }) + float?: number; + @Field(() => String, { nullable: true, }) diff --git a/src/@generated/dummy/dummy-group-by.output.ts b/src/@generated/dummy/dummy-group-by.output.ts index 654746d8..5ca8b59a 100644 --- a/src/@generated/dummy/dummy-group-by.output.ts +++ b/src/@generated/dummy/dummy-group-by.output.ts @@ -1,4 +1,4 @@ -import { Field, ObjectType } from '@nestjs/graphql'; +import { Field, Float, Int, ObjectType } from '@nestjs/graphql'; import { GraphQLJSON } from 'graphql-type-json'; import { DummyAvgAggregate } from './dummy-avg-aggregate.output'; @@ -14,6 +14,21 @@ export class DummyGroupBy { }) id!: string; + @Field(() => Float, { + nullable: false, + }) + floaty!: number; + + @Field(() => Int, { + nullable: true, + }) + int?: number; + + @Field(() => Float, { + nullable: true, + }) + float?: number; + @Field(() => String, { nullable: true, }) diff --git a/src/@generated/dummy/dummy-max-aggregate.input.ts b/src/@generated/dummy/dummy-max-aggregate.input.ts index 9bfbee88..4454403b 100644 --- a/src/@generated/dummy/dummy-max-aggregate.input.ts +++ b/src/@generated/dummy/dummy-max-aggregate.input.ts @@ -7,6 +7,21 @@ export class DummyMaxAggregateInput { }) id?: true; + @Field(() => Boolean, { + nullable: true, + }) + floaty?: true; + + @Field(() => Boolean, { + nullable: true, + }) + int?: true; + + @Field(() => Boolean, { + nullable: true, + }) + float?: true; + @Field(() => Boolean, { nullable: true, }) diff --git a/src/@generated/dummy/dummy-max-aggregate.output.ts b/src/@generated/dummy/dummy-max-aggregate.output.ts index cc138858..a7412bac 100644 --- a/src/@generated/dummy/dummy-max-aggregate.output.ts +++ b/src/@generated/dummy/dummy-max-aggregate.output.ts @@ -1,4 +1,4 @@ -import { Field, ObjectType } from '@nestjs/graphql'; +import { Field, Float, Int, ObjectType } from '@nestjs/graphql'; @ObjectType() export class DummyMaxAggregate { @@ -7,6 +7,21 @@ export class DummyMaxAggregate { }) id?: string; + @Field(() => Float, { + nullable: false, + }) + floaty!: number; + + @Field(() => Int, { + nullable: true, + }) + int?: number; + + @Field(() => Float, { + nullable: true, + }) + float?: number; + @Field(() => String, { nullable: true, }) diff --git a/src/@generated/dummy/dummy-min-aggregate.input.ts b/src/@generated/dummy/dummy-min-aggregate.input.ts index d1d84daa..654d0b0b 100644 --- a/src/@generated/dummy/dummy-min-aggregate.input.ts +++ b/src/@generated/dummy/dummy-min-aggregate.input.ts @@ -7,6 +7,21 @@ export class DummyMinAggregateInput { }) id?: true; + @Field(() => Boolean, { + nullable: true, + }) + floaty?: true; + + @Field(() => Boolean, { + nullable: true, + }) + int?: true; + + @Field(() => Boolean, { + nullable: true, + }) + float?: true; + @Field(() => Boolean, { nullable: true, }) diff --git a/src/@generated/dummy/dummy-min-aggregate.output.ts b/src/@generated/dummy/dummy-min-aggregate.output.ts index 13c8ef5a..b206334c 100644 --- a/src/@generated/dummy/dummy-min-aggregate.output.ts +++ b/src/@generated/dummy/dummy-min-aggregate.output.ts @@ -1,4 +1,4 @@ -import { Field, ObjectType } from '@nestjs/graphql'; +import { Field, Float, Int, ObjectType } from '@nestjs/graphql'; @ObjectType() export class DummyMinAggregate { @@ -7,6 +7,21 @@ export class DummyMinAggregate { }) id?: string; + @Field(() => Float, { + nullable: false, + }) + floaty!: number; + + @Field(() => Int, { + nullable: true, + }) + int?: number; + + @Field(() => Float, { + nullable: true, + }) + float?: number; + @Field(() => String, { nullable: true, }) diff --git a/src/@generated/dummy/dummy-order-by.input.ts b/src/@generated/dummy/dummy-order-by.input.ts index 09ae05f2..a6364ddb 100644 --- a/src/@generated/dummy/dummy-order-by.input.ts +++ b/src/@generated/dummy/dummy-order-by.input.ts @@ -9,6 +9,21 @@ export class DummyOrderByInput { }) id?: SortOrder; + @Field(() => SortOrder, { + nullable: true, + }) + floaty?: SortOrder; + + @Field(() => SortOrder, { + nullable: true, + }) + int?: SortOrder; + + @Field(() => SortOrder, { + nullable: true, + }) + float?: SortOrder; + @Field(() => SortOrder, { nullable: true, }) diff --git a/src/@generated/dummy/dummy-scalar-field.enum.ts b/src/@generated/dummy/dummy-scalar-field.enum.ts index 2f31cf5f..6049ccdf 100644 --- a/src/@generated/dummy/dummy-scalar-field.enum.ts +++ b/src/@generated/dummy/dummy-scalar-field.enum.ts @@ -2,6 +2,9 @@ import { registerEnumType } from '@nestjs/graphql'; export enum DummyScalarFieldEnum { id = 'id', + floaty = 'floaty', + int = 'int', + float = 'float', bytes = 'bytes', decimal = 'decimal', bigInt = 'bigInt', diff --git a/src/@generated/dummy/dummy-scalar-where-with-aggregates.input.ts b/src/@generated/dummy/dummy-scalar-where-with-aggregates.input.ts index e02f534b..5df81106 100644 --- a/src/@generated/dummy/dummy-scalar-where-with-aggregates.input.ts +++ b/src/@generated/dummy/dummy-scalar-where-with-aggregates.input.ts @@ -3,6 +3,9 @@ import { Field, InputType } from '@nestjs/graphql'; import { BigIntNullableWithAggregatesFilter } from '../prisma/big-int-nullable-with-aggregates-filter.input'; import { BytesNullableWithAggregatesFilter } from '../prisma/bytes-nullable-with-aggregates-filter.input'; import { DecimalNullableWithAggregatesFilter } from '../prisma/decimal-nullable-with-aggregates-filter.input'; +import { FloatNullableWithAggregatesFilter } from '../prisma/float-nullable-with-aggregates-filter.input'; +import { FloatWithAggregatesFilter } from '../prisma/float-with-aggregates-filter.input'; +import { IntNullableWithAggregatesFilter } from '../prisma/int-nullable-with-aggregates-filter.input'; import { JsonNullableWithAggregatesFilter } from '../prisma/json-nullable-with-aggregates-filter.input'; import { StringWithAggregatesFilter } from '../prisma/string-with-aggregates-filter.input'; @@ -28,6 +31,21 @@ export class DummyScalarWhereWithAggregatesInput { }) id?: StringWithAggregatesFilter; + @Field(() => FloatWithAggregatesFilter, { + nullable: true, + }) + floaty?: FloatWithAggregatesFilter; + + @Field(() => IntNullableWithAggregatesFilter, { + nullable: true, + }) + int?: IntNullableWithAggregatesFilter; + + @Field(() => FloatNullableWithAggregatesFilter, { + nullable: true, + }) + float?: FloatNullableWithAggregatesFilter; + @Field(() => BytesNullableWithAggregatesFilter, { nullable: true, }) diff --git a/src/@generated/dummy/dummy-sum-aggregate.input.ts b/src/@generated/dummy/dummy-sum-aggregate.input.ts index 4110972f..f3675867 100644 --- a/src/@generated/dummy/dummy-sum-aggregate.input.ts +++ b/src/@generated/dummy/dummy-sum-aggregate.input.ts @@ -2,6 +2,21 @@ import { Field, InputType } from '@nestjs/graphql'; @InputType() export class DummySumAggregateInput { + @Field(() => Boolean, { + nullable: true, + }) + floaty?: true; + + @Field(() => Boolean, { + nullable: true, + }) + int?: true; + + @Field(() => Boolean, { + nullable: true, + }) + float?: true; + @Field(() => Boolean, { nullable: true, }) diff --git a/src/@generated/dummy/dummy-sum-aggregate.output.ts b/src/@generated/dummy/dummy-sum-aggregate.output.ts index addb7954..8f4e2ef4 100644 --- a/src/@generated/dummy/dummy-sum-aggregate.output.ts +++ b/src/@generated/dummy/dummy-sum-aggregate.output.ts @@ -1,7 +1,22 @@ -import { Field, ObjectType } from '@nestjs/graphql'; +import { Field, Float, Int, ObjectType } from '@nestjs/graphql'; @ObjectType() export class DummySumAggregate { + @Field(() => Float, { + nullable: false, + }) + floaty!: number; + + @Field(() => Int, { + nullable: true, + }) + int?: number; + + @Field(() => Float, { + nullable: true, + }) + float?: number; + @Field(() => String, { nullable: true, }) diff --git a/src/@generated/dummy/dummy-unchecked-create.input.ts b/src/@generated/dummy/dummy-unchecked-create.input.ts index bb35b99c..856f7b57 100644 --- a/src/@generated/dummy/dummy-unchecked-create.input.ts +++ b/src/@generated/dummy/dummy-unchecked-create.input.ts @@ -1,4 +1,4 @@ -import { Field, InputType } from '@nestjs/graphql'; +import { Field, Float, InputType, Int } from '@nestjs/graphql'; import { GraphQLJSON } from 'graphql-type-json'; @InputType() @@ -8,6 +8,21 @@ export class DummyUncheckedCreateInput { }) id!: string; + @Field(() => Float, { + nullable: false, + }) + floaty!: number; + + @Field(() => Int, { + nullable: true, + }) + int?: number; + + @Field(() => Float, { + nullable: true, + }) + float?: number; + @Field(() => String, { nullable: true, }) diff --git a/src/@generated/dummy/dummy-unchecked-update-many.input.ts b/src/@generated/dummy/dummy-unchecked-update-many.input.ts index 7dd79762..be3af7e6 100644 --- a/src/@generated/dummy/dummy-unchecked-update-many.input.ts +++ b/src/@generated/dummy/dummy-unchecked-update-many.input.ts @@ -1,9 +1,12 @@ import { Field, InputType } from '@nestjs/graphql'; import { GraphQLJSON } from 'graphql-type-json'; +import { FloatFieldUpdateOperationsInput } from '../prisma/float-field-update-operations.input'; import { NullableBigIntFieldUpdateOperationsInput } from '../prisma/nullable-big-int-field-update-operations.input'; import { NullableBytesFieldUpdateOperationsInput } from '../prisma/nullable-bytes-field-update-operations.input'; import { NullableDecimalFieldUpdateOperationsInput } from '../prisma/nullable-decimal-field-update-operations.input'; +import { NullableFloatFieldUpdateOperationsInput } from '../prisma/nullable-float-field-update-operations.input'; +import { NullableIntFieldUpdateOperationsInput } from '../prisma/nullable-int-field-update-operations.input'; import { StringFieldUpdateOperationsInput } from '../prisma/string-field-update-operations.input'; @InputType() @@ -13,6 +16,21 @@ export class DummyUncheckedUpdateManyInput { }) id?: StringFieldUpdateOperationsInput; + @Field(() => FloatFieldUpdateOperationsInput, { + nullable: true, + }) + floaty?: FloatFieldUpdateOperationsInput; + + @Field(() => NullableIntFieldUpdateOperationsInput, { + nullable: true, + }) + int?: NullableIntFieldUpdateOperationsInput; + + @Field(() => NullableFloatFieldUpdateOperationsInput, { + nullable: true, + }) + float?: NullableFloatFieldUpdateOperationsInput; + @Field(() => NullableBytesFieldUpdateOperationsInput, { nullable: true, }) diff --git a/src/@generated/dummy/dummy-unchecked-update.input.ts b/src/@generated/dummy/dummy-unchecked-update.input.ts index 833f0b05..642c5643 100644 --- a/src/@generated/dummy/dummy-unchecked-update.input.ts +++ b/src/@generated/dummy/dummy-unchecked-update.input.ts @@ -1,9 +1,12 @@ import { Field, InputType } from '@nestjs/graphql'; import { GraphQLJSON } from 'graphql-type-json'; +import { FloatFieldUpdateOperationsInput } from '../prisma/float-field-update-operations.input'; import { NullableBigIntFieldUpdateOperationsInput } from '../prisma/nullable-big-int-field-update-operations.input'; import { NullableBytesFieldUpdateOperationsInput } from '../prisma/nullable-bytes-field-update-operations.input'; import { NullableDecimalFieldUpdateOperationsInput } from '../prisma/nullable-decimal-field-update-operations.input'; +import { NullableFloatFieldUpdateOperationsInput } from '../prisma/nullable-float-field-update-operations.input'; +import { NullableIntFieldUpdateOperationsInput } from '../prisma/nullable-int-field-update-operations.input'; import { StringFieldUpdateOperationsInput } from '../prisma/string-field-update-operations.input'; @InputType() @@ -13,6 +16,21 @@ export class DummyUncheckedUpdateInput { }) id?: StringFieldUpdateOperationsInput; + @Field(() => FloatFieldUpdateOperationsInput, { + nullable: true, + }) + floaty?: FloatFieldUpdateOperationsInput; + + @Field(() => NullableIntFieldUpdateOperationsInput, { + nullable: true, + }) + int?: NullableIntFieldUpdateOperationsInput; + + @Field(() => NullableFloatFieldUpdateOperationsInput, { + nullable: true, + }) + float?: NullableFloatFieldUpdateOperationsInput; + @Field(() => NullableBytesFieldUpdateOperationsInput, { nullable: true, }) diff --git a/src/@generated/dummy/dummy-update-many-mutation.input.ts b/src/@generated/dummy/dummy-update-many-mutation.input.ts index 1625d577..6203bb44 100644 --- a/src/@generated/dummy/dummy-update-many-mutation.input.ts +++ b/src/@generated/dummy/dummy-update-many-mutation.input.ts @@ -1,9 +1,12 @@ import { Field, InputType } from '@nestjs/graphql'; import { GraphQLJSON } from 'graphql-type-json'; +import { FloatFieldUpdateOperationsInput } from '../prisma/float-field-update-operations.input'; import { NullableBigIntFieldUpdateOperationsInput } from '../prisma/nullable-big-int-field-update-operations.input'; import { NullableBytesFieldUpdateOperationsInput } from '../prisma/nullable-bytes-field-update-operations.input'; import { NullableDecimalFieldUpdateOperationsInput } from '../prisma/nullable-decimal-field-update-operations.input'; +import { NullableFloatFieldUpdateOperationsInput } from '../prisma/nullable-float-field-update-operations.input'; +import { NullableIntFieldUpdateOperationsInput } from '../prisma/nullable-int-field-update-operations.input'; import { StringFieldUpdateOperationsInput } from '../prisma/string-field-update-operations.input'; @InputType() @@ -13,6 +16,21 @@ export class DummyUpdateManyMutationInput { }) id?: StringFieldUpdateOperationsInput; + @Field(() => FloatFieldUpdateOperationsInput, { + nullable: true, + }) + floaty?: FloatFieldUpdateOperationsInput; + + @Field(() => NullableIntFieldUpdateOperationsInput, { + nullable: true, + }) + int?: NullableIntFieldUpdateOperationsInput; + + @Field(() => NullableFloatFieldUpdateOperationsInput, { + nullable: true, + }) + float?: NullableFloatFieldUpdateOperationsInput; + @Field(() => NullableBytesFieldUpdateOperationsInput, { nullable: true, }) diff --git a/src/@generated/dummy/dummy-update.input.ts b/src/@generated/dummy/dummy-update.input.ts index 8ba97263..907759ad 100644 --- a/src/@generated/dummy/dummy-update.input.ts +++ b/src/@generated/dummy/dummy-update.input.ts @@ -1,9 +1,12 @@ import { Field, InputType } from '@nestjs/graphql'; import { GraphQLJSON } from 'graphql-type-json'; +import { FloatFieldUpdateOperationsInput } from '../prisma/float-field-update-operations.input'; import { NullableBigIntFieldUpdateOperationsInput } from '../prisma/nullable-big-int-field-update-operations.input'; import { NullableBytesFieldUpdateOperationsInput } from '../prisma/nullable-bytes-field-update-operations.input'; import { NullableDecimalFieldUpdateOperationsInput } from '../prisma/nullable-decimal-field-update-operations.input'; +import { NullableFloatFieldUpdateOperationsInput } from '../prisma/nullable-float-field-update-operations.input'; +import { NullableIntFieldUpdateOperationsInput } from '../prisma/nullable-int-field-update-operations.input'; import { StringFieldUpdateOperationsInput } from '../prisma/string-field-update-operations.input'; @InputType() @@ -13,6 +16,21 @@ export class DummyUpdateInput { }) id?: StringFieldUpdateOperationsInput; + @Field(() => FloatFieldUpdateOperationsInput, { + nullable: true, + }) + floaty?: FloatFieldUpdateOperationsInput; + + @Field(() => NullableIntFieldUpdateOperationsInput, { + nullable: true, + }) + int?: NullableIntFieldUpdateOperationsInput; + + @Field(() => NullableFloatFieldUpdateOperationsInput, { + nullable: true, + }) + float?: NullableFloatFieldUpdateOperationsInput; + @Field(() => NullableBytesFieldUpdateOperationsInput, { nullable: true, }) diff --git a/src/@generated/dummy/dummy-where.input.ts b/src/@generated/dummy/dummy-where.input.ts index 40e207ae..ea440108 100644 --- a/src/@generated/dummy/dummy-where.input.ts +++ b/src/@generated/dummy/dummy-where.input.ts @@ -3,6 +3,9 @@ import { Field, InputType } from '@nestjs/graphql'; import { BigIntNullableFilter } from '../prisma/big-int-nullable-filter.input'; import { BytesNullableFilter } from '../prisma/bytes-nullable-filter.input'; import { DecimalNullableFilter } from '../prisma/decimal-nullable-filter.input'; +import { FloatFilter } from '../prisma/float-filter.input'; +import { FloatNullableFilter } from '../prisma/float-nullable-filter.input'; +import { IntNullableFilter } from '../prisma/int-nullable-filter.input'; import { JsonNullableFilter } from '../prisma/json-nullable-filter.input'; import { StringFilter } from '../prisma/string-filter.input'; @@ -28,6 +31,21 @@ export class DummyWhereInput { }) id?: StringFilter; + @Field(() => FloatFilter, { + nullable: true, + }) + floaty?: FloatFilter; + + @Field(() => IntNullableFilter, { + nullable: true, + }) + int?: IntNullableFilter; + + @Field(() => FloatNullableFilter, { + nullable: true, + }) + float?: FloatNullableFilter; + @Field(() => BytesNullableFilter, { nullable: true, }) diff --git a/src/@generated/dummy/dummy.model.ts b/src/@generated/dummy/dummy.model.ts index 08d66031..564fe9b8 100644 --- a/src/@generated/dummy/dummy.model.ts +++ b/src/@generated/dummy/dummy.model.ts @@ -1,4 +1,4 @@ -import { Field, ID, ObjectType } from '@nestjs/graphql'; +import { Field, Float, ID, Int, ObjectType } from '@nestjs/graphql'; import { GraphQLJSON } from 'graphql-type-json'; @ObjectType() @@ -8,6 +8,21 @@ export class Dummy { }) id!: string; + @Field(() => Float, { + nullable: false, + }) + floaty!: number; + + @Field(() => Int, { + nullable: true, + }) + int?: number; + + @Field(() => Float, { + nullable: true, + }) + float?: number; + @Field(() => String, { nullable: true, }) diff --git a/src/@generated/prisma/float-field-update-operations.input.ts b/src/@generated/prisma/float-field-update-operations.input.ts new file mode 100644 index 00000000..9a7ec942 --- /dev/null +++ b/src/@generated/prisma/float-field-update-operations.input.ts @@ -0,0 +1,29 @@ +import { Field, Float, InputType } from '@nestjs/graphql'; + +@InputType() +export class FloatFieldUpdateOperationsInput { + @Field(() => Float, { + nullable: true, + }) + set?: number; + + @Field(() => Float, { + nullable: true, + }) + increment?: number; + + @Field(() => Float, { + nullable: true, + }) + decrement?: number; + + @Field(() => Float, { + nullable: true, + }) + multiply?: number; + + @Field(() => Float, { + nullable: true, + }) + divide?: number; +} diff --git a/src/@generated/prisma/float-filter.input.ts b/src/@generated/prisma/float-filter.input.ts new file mode 100644 index 00000000..c163d02d --- /dev/null +++ b/src/@generated/prisma/float-filter.input.ts @@ -0,0 +1,46 @@ +import { Field, Float, InputType } from '@nestjs/graphql'; + +import { NestedFloatFilter } from './nested-float-filter.input'; + +@InputType() +export class FloatFilter { + @Field(() => Float, { + nullable: true, + }) + equals?: number; + + @Field(() => [Float], { + nullable: true, + }) + in?: Array; + + @Field(() => [Float], { + nullable: true, + }) + notIn?: Array; + + @Field(() => Float, { + nullable: true, + }) + lt?: number; + + @Field(() => Float, { + nullable: true, + }) + lte?: number; + + @Field(() => Float, { + nullable: true, + }) + gt?: number; + + @Field(() => Float, { + nullable: true, + }) + gte?: number; + + @Field(() => NestedFloatFilter, { + nullable: true, + }) + not?: NestedFloatFilter; +} diff --git a/src/@generated/prisma/float-with-aggregates-filter.input.ts b/src/@generated/prisma/float-with-aggregates-filter.input.ts new file mode 100644 index 00000000..23769149 --- /dev/null +++ b/src/@generated/prisma/float-with-aggregates-filter.input.ts @@ -0,0 +1,73 @@ +import { Field, Float, InputType } from '@nestjs/graphql'; + +import { NestedFloatFilter } from './nested-float-filter.input'; +import { NestedFloatWithAggregatesFilter } from './nested-float-with-aggregates-filter.input'; +import { NestedIntFilter } from './nested-int-filter.input'; + +@InputType() +export class FloatWithAggregatesFilter { + @Field(() => Float, { + nullable: true, + }) + equals?: number; + + @Field(() => [Float], { + nullable: true, + }) + in?: Array; + + @Field(() => [Float], { + nullable: true, + }) + notIn?: Array; + + @Field(() => Float, { + nullable: true, + }) + lt?: number; + + @Field(() => Float, { + nullable: true, + }) + lte?: number; + + @Field(() => Float, { + nullable: true, + }) + gt?: number; + + @Field(() => Float, { + nullable: true, + }) + gte?: number; + + @Field(() => NestedFloatWithAggregatesFilter, { + nullable: true, + }) + not?: NestedFloatWithAggregatesFilter; + + @Field(() => NestedIntFilter, { + nullable: true, + }) + count?: NestedIntFilter; + + @Field(() => NestedFloatFilter, { + nullable: true, + }) + avg?: NestedFloatFilter; + + @Field(() => NestedFloatFilter, { + nullable: true, + }) + sum?: NestedFloatFilter; + + @Field(() => NestedFloatFilter, { + nullable: true, + }) + min?: NestedFloatFilter; + + @Field(() => NestedFloatFilter, { + nullable: true, + }) + max?: NestedFloatFilter; +} diff --git a/src/@generated/prisma/nested-float-with-aggregates-filter.input.ts b/src/@generated/prisma/nested-float-with-aggregates-filter.input.ts new file mode 100644 index 00000000..d334b593 --- /dev/null +++ b/src/@generated/prisma/nested-float-with-aggregates-filter.input.ts @@ -0,0 +1,72 @@ +import { Field, Float, InputType } from '@nestjs/graphql'; + +import { NestedFloatFilter } from './nested-float-filter.input'; +import { NestedIntFilter } from './nested-int-filter.input'; + +@InputType() +export class NestedFloatWithAggregatesFilter { + @Field(() => Float, { + nullable: true, + }) + equals?: number; + + @Field(() => [Float], { + nullable: true, + }) + in?: Array; + + @Field(() => [Float], { + nullable: true, + }) + notIn?: Array; + + @Field(() => Float, { + nullable: true, + }) + lt?: number; + + @Field(() => Float, { + nullable: true, + }) + lte?: number; + + @Field(() => Float, { + nullable: true, + }) + gt?: number; + + @Field(() => Float, { + nullable: true, + }) + gte?: number; + + @Field(() => NestedFloatWithAggregatesFilter, { + nullable: true, + }) + not?: NestedFloatWithAggregatesFilter; + + @Field(() => NestedIntFilter, { + nullable: true, + }) + count?: NestedIntFilter; + + @Field(() => NestedFloatFilter, { + nullable: true, + }) + avg?: NestedFloatFilter; + + @Field(() => NestedFloatFilter, { + nullable: true, + }) + sum?: NestedFloatFilter; + + @Field(() => NestedFloatFilter, { + nullable: true, + }) + min?: NestedFloatFilter; + + @Field(() => NestedFloatFilter, { + nullable: true, + }) + max?: NestedFloatFilter; +} diff --git a/src/generate.spec.ts b/src/generate.spec.ts index c4b1edaa..87837612 100644 --- a/src/generate.spec.ts +++ b/src/generate.spec.ts @@ -51,11 +51,9 @@ async function testGenerate(args: { }); sourceFiles = project.getSourceFiles(); + let emptyFiles: string[] = []; try { - // eslint-disable-next-line no-var - var emptyFiles = sourceFiles - .filter(s => !s.getText()) - .map(s => s.getFilePath()); + emptyFiles = sourceFiles.filter(s => !s.getText()).map(s => s.getFilePath()); expect(emptyFiles).toHaveLength(0); } catch { // eslint-disable-next-line @typescript-eslint/restrict-template-expressions @@ -541,14 +539,14 @@ describe('custom types', () => { }); sourceFile = project.getSourceFile(s => s.getFilePath().endsWith('/date-time-filter.input.ts'), - ); + )!; }); // it('^', () => console.log(sourceFile.getText())); it('property in', () => { const property = getPropertyStructure(sourceFile, 'in'); - expect(property.type).toEqual('Array | Array'); + expect(property?.type).toEqual('Array | Array'); }); it('decorator type should be array date', () => { @@ -882,9 +880,21 @@ describe('model with one id string', () => { }); }); -it('smoke many', async () => { +it('generator option outputFilePattern', async () => { await testGenerate({ schema: `model User { + id Int @id + }`, + options: [`outputFilePattern = "data/{type}/{name}.ts"`], + }); + const filePaths = sourceFiles.map(s => String(s.getFilePath())); + expect(filePaths).toContainEqual(expect.stringContaining('/data/model/user.ts')); +}); + +it('several models', () => { + before(async () => { + await testGenerate({ + schema: `model User { id Int @id name String? profile Profile? @@ -897,25 +907,236 @@ it('smoke many', async () => { model Comment { id Int @id }`, + }); + }); + + it('no nullable type', () => { + for (const d of sourceFiles + .flatMap(s => s.getClasses()) + .flatMap(d => d.getProperties()) + .flatMap(p => p.getDecorators())) { + const argument = d.getCallExpression()?.getArguments()?.[0].getText(); + expect(argument).not.toContain('null'); + } + }); +}); + +describe('get rid of atomic number operations', () => { + before(async () => { + await testGenerate({ + schema: ` + model User { + id String @id + age Int + rating Float? + money Decimal? + } + `, + options: [ + `outputFilePattern = "{name}.{type}.ts"`, + `noAtomicOperations = true`, + ], + }); + }); + + it('files should not be', () => { + const filePaths = sourceFiles.map(s => s.getFilePath().slice(1)); + for (const filePath of filePaths) { + expect(filePath).not.toContain('field-update-operations'); + } + }); + + describe('user update input', () => { + before(() => { + sourceFile = project.getSourceFile(s => + s.getFilePath().endsWith('user-update.input.ts'), + )!; + }); + + // it('^', () => console.log(sourceFile.getText())); + + it('id should be regular string', () => { + expect(getPropertyStructure(sourceFile, 'id')?.type).toEqual('string'); + }); + + it('id field type should be string', () => { + expect(getFieldType(sourceFile, 'id')).toEqual('() => String'); + }); + + it('age should be regular string', () => { + expect(getPropertyStructure(sourceFile, 'age')?.type).toEqual('number'); + }); + + it('age field type should be string', () => { + expect(getFieldType(sourceFile, 'age')).toEqual('() => Int'); + }); + + it('rating should be regular string', () => { + expect(getPropertyStructure(sourceFile, 'rating')?.type).toEqual('number'); + }); + + it('rating field type should be string', () => { + expect(getFieldType(sourceFile, 'rating')).toEqual('() => Float'); + }); + }); +}); + +describe('combine scalar filters', () => { + before(async () => { + await testGenerate({ + schema: ` + model User { + id String @id + bio String? + count Int? + rating Float? + born DateTime? + humanoid Boolean? + money Decimal? + data Json? + } + `, + options: [ + `outputFilePattern = "{name}.{type}.ts"`, + `combineScalarFilters = true`, + ], + }); + }); + + it('files should not contain nested and nullable', () => { + const filePaths = sourceFiles.map(s => s.getFilePath().slice(1)); + for (const filePath of filePaths) { + expect(filePath).not.toContain('nested'); + expect(filePath).not.toContain('nullable'); + } + }); + + describe('user where input', () => { + before(() => { + sourceFile = project.getSourceFile(s => + s.getFilePath().endsWith('user-where.input.ts'), + )!; + }); + + it('count', () => { + expect(getPropertyStructure(sourceFile, 'count')?.type).toBe('IntFilter'); + }); + + it('bio', () => { + expect(getPropertyStructure(sourceFile, 'bio')?.type).toBe('StringFilter'); + }); + + it('money', () => { + expect(getPropertyStructure(sourceFile, 'money')?.type).toBe( + 'DecimalFilter', + ); + }); + + it('rating', () => { + expect(getPropertyStructure(sourceFile, 'rating')?.type).toBe( + 'FloatFilter', + ); + }); + + it('born', () => { + expect(getPropertyStructure(sourceFile, 'born')?.type).toBe( + 'DateTimeFilter', + ); + }); + + it('humanoid', () => { + expect(getPropertyStructure(sourceFile, 'humanoid')?.type).toBe( + 'BooleanFilter', + ); + }); + + // it('^', () => console.log(sourceFile.getText())); + }); + + // describe('combine filters', () => { + // it('replacement type name scalars', () => { + // expect(replacementTypeName('StringNullableFilter')).toEqual('StringFilter'); + // expect(replacementTypeName('NullableStringFilter')).toEqual('StringFilter'); + // expect(replacementTypeName('NestedStringNullableFilter')).toEqual( + // 'StringFilter', + // ); + // expect(replacementTypeName('NestedStringFilter')).toEqual('StringFilter'); + // expect(replacementTypeName('IntNullableFilter')).toEqual('IntFilter'); + // expect(replacementTypeName('NullableIntFilter')).toEqual('IntFilter'); + // expect(replacementTypeName('NestedIntNullableFilter')).toEqual('IntFilter'); + // expect(replacementTypeName('NestedIntFilter')).toEqual('IntFilter'); + // expect(replacementTypeName('FloatNullableFilter')).toEqual('FloatFilter'); + // expect(replacementTypeName('NullableFloatFilter')).toEqual('FloatFilter'); + // expect(replacementTypeName('NestedFloatNullableFilter')).toEqual( + // 'FloatFilter', + // ); + // expect(replacementTypeName('NestedFloatFilter')).toEqual('FloatFilter'); + // expect(replacementTypeName('DateTimeNullableFilter')).toEqual( + // 'DateTimeFilter', + // ); + // expect(replacementTypeName('NullableDateTimeFilter')).toEqual( + // 'DateTimeFilter', + // ); + // expect(replacementTypeName('NestedDateTimeNullableFilter')).toEqual( + // 'DateTimeFilter', + // ); + // expect(replacementTypeName('NestedDateTimeFilter')).toEqual( + // 'DateTimeFilter', + // ); + // expect(replacementTypeName('BooleanNullableFilter')).toEqual( + // 'BooleanFilter', + // ); + // expect(replacementTypeName('NullableBooleanFilter')).toEqual( + // 'BooleanFilter', + // ); + // expect(replacementTypeName('NestedBooleanNullableFilter')).toEqual( + // 'BooleanFilter', + // ); + // expect(replacementTypeName('NestedBooleanFilter')).toEqual('BooleanFilter'); + // expect(replacementTypeName('BoolNullableFilter')).toEqual('BooleanFilter'); + // expect(replacementTypeName('NestedBoolNullableFilter')).toEqual( + // 'BooleanFilter', + // ); + // expect(replacementTypeName('NestedBoolFilter')).toEqual('BooleanFilter'); + // }); + + // it('replacement type name enum', () => { + // expect(replacementTypeName('EnumRoleNullableFilter')).toEqual( + // 'EnumRoleFilter', + // ); + // }); + + // it('nullable with aggregates', () => { + // expect(replacementTypeName('StringNullableWithAggregatesFilter')).toEqual( + // 'StringWithAggregatesFilter', + // ); + // expect(replacementTypeName('NestedStringWithAggregatesFilter')).toEqual( + // 'StringWithAggregatesFilter', + // ); + // expect( + // replacementTypeName('NestedBoolNullableWithAggregatesFilter'), + // ).toEqual('BooleanWithAggregatesFilter'); + // expect(replacementTypeName('BoolNullableWithAggregatesFilter')).toEqual( + // 'BooleanWithAggregatesFilter', + // ); + // }); + // }); +}); + +it.skip('hide field', async () => { + await testGenerate({ + schema: ` + model User { + id String @id + /// @TypeGraphQL.omit(output: true) + /// Regular documentation + password String + } + `, + options: [], }); + const property = getPropertyStructure(sourceFile, 'password'); + expect(property?.decorators?.[0]?.name).toEqual('HideField'); }); -// it('hide field', async () => { -// await getResult({ -// schema: ` -// model User { -// id String @id -// /// @TypeGraphQL.omit(output: true) -// /// Regular documentation -// password String -// } -// `, -// options: [], -// }); -// const property = getStructure({ -// sourceFile, -// className: 'User', -// property: 'password', -// }); -// expect(property?.decorators?.[0]?.name).toEqual('HideField'); -// }); +// it('^', () => console.log(sourceFile.getText())); diff --git a/src/generate.ts b/src/generate.ts index ef5bd441..af18e6d3 100644 --- a/src/generate.ts +++ b/src/generate.ts @@ -1,20 +1,41 @@ +/* eslint-disable import/max-dependencies */ import { GeneratorOptions } from '@prisma/generator-helper'; import assert from 'assert'; import AwaitEventEmitter from 'await-event-emitter'; import { mapKeys } from 'lodash'; import { Project, QuoteKind } from 'ts-morph'; -import { handlers } from './handlers'; +import { argsType } from './handlers/args-type'; +import { createAggregateInput } from './handlers/create-aggregate-input'; +import { generateFiles } from './handlers/generate-files'; +import { inputType } from './handlers/input-type'; +import { modelData } from './handlers/model-data'; +import { + noAtomicBeforeGenerateFiles, + noAtomicOperations, +} from './handlers/no-atomic-operations'; +import { outputType } from './handlers/output-type'; +import { registerEnum } from './handlers/register-enum'; +import { typeNames } from './handlers/type-names'; import { createConfig } from './helpers/create-config'; import { generateFileName } from './helpers/generate-file-name'; import { DMMF, EventArguments, Model, OutputType } from './types'; export const eventEmitter = new AwaitEventEmitter(); -// eventEmitter.on('aggregateOutput', () => console.log('>>>>>>>> aggregateOutput')); -// eventEmitter.on('argsType', () => console.log('>>>>>>>>> argsType')); +// eventEmitter.on('inputType', () => console.log('>>>>>>>>> inputType')); -handlers(eventEmitter); +eventEmitter.on('model', modelData); +eventEmitter.on('enumType', registerEnum); +eventEmitter.on('outputType', outputType); +eventEmitter.on('aggregateOutput', createAggregateInput); +eventEmitter.on('inputType', inputType); +eventEmitter.on('argsType', argsType); +eventEmitter.on('generateFiles', generateFiles); +eventEmitter.on('inputType', typeNames); + +eventEmitter.on('beforeInputType', noAtomicOperations); +eventEmitter.on('beforeGenerateFiles', noAtomicBeforeGenerateFiles); export async function generate( args: GeneratorOptions & { @@ -82,12 +103,14 @@ export async function generate( for (const inputType of inputObjectTypes.prisma.concat( inputObjectTypes.model || [], )) { - await eventEmitter.emit('inputType', { + const event = { ...eventArguments, inputType, fileType: 'input', classDecoratorName: 'InputType', - }); + }; + await eventEmitter.emit('beforeInputType', event); + await eventEmitter.emit('inputType', event); } for (const outputType of queryOutputTypes) { @@ -96,6 +119,7 @@ export async function generate( } } + await eventEmitter.emit('beforeGenerateFiles', eventArguments); await eventEmitter.emit('generateFiles', eventArguments); await eventEmitter.emit('end', eventArguments); } diff --git a/src/handlers/index.ts b/src/handlers/index.ts deleted file mode 100644 index fbd96fb2..00000000 --- a/src/handlers/index.ts +++ /dev/null @@ -1,21 +0,0 @@ -import AwaitEventEmitter from 'await-event-emitter'; - -import { argsType } from './args-type'; -import { createAggregateInput } from './create-aggregate-input'; -import { generateFiles } from './generate-files'; -import { inputType } from './input-type'; -import { modelData } from './model-data'; -import { outputType } from './output-type'; -import { registerEnum } from './register-enum'; -import { typeNames } from './type-names'; - -export function handlers(emitter: AwaitEventEmitter) { - emitter.on('model', modelData); - emitter.on('enumType', registerEnum); - emitter.on('outputType', outputType); - emitter.on('aggregateOutput', createAggregateInput); - emitter.on('inputType', inputType); - emitter.on('argsType', argsType); - emitter.on('generateFiles', generateFiles); - emitter.on('inputType', typeNames); -} diff --git a/src/handlers/no-atomic-operations.ts b/src/handlers/no-atomic-operations.ts new file mode 100644 index 00000000..0da797c7 --- /dev/null +++ b/src/handlers/no-atomic-operations.ts @@ -0,0 +1,34 @@ +import { EventArguments, InputType } from '../types'; + +export function noAtomicOperations(args: EventArguments & { inputType: InputType }) { + const { config, inputType } = args; + if (!config.noAtomicOperations) { + return; + } + + for (const field of inputType.fields) { + field.inputTypes = field.inputTypes.filter(inputType => { + if (isAtomicOperation(String(inputType.type))) { + return false; + } + return true; + }); + } +} + +function isAtomicOperation(name: string) { + return name.endsWith('FieldUpdateOperationsInput'); +} + +export function noAtomicBeforeGenerateFiles(args: EventArguments) { + const { config, project } = args; + if (!config.noAtomicOperations) { + return; + } + for (const sourceFile of project.getSourceFiles()) { + const className = sourceFile.getClass(() => true)?.getName(); + if (className && isAtomicOperation(className)) { + project.removeSourceFile(sourceFile); + } + } +} diff --git a/src/helpers/create-config.spec.ts b/src/helpers/create-config.spec.ts index 3bd16242..8c4ae516 100644 --- a/src/helpers/create-config.spec.ts +++ b/src/helpers/create-config.spec.ts @@ -9,8 +9,8 @@ describe('createConfig', () => { it('createConfig default', () => { const result = createConfig({}); - expect(result.combineScalarFilters).toEqual(true); - expect(result.noAtomicNumberOperations).toEqual(true); + expect(result.combineScalarFilters).toEqual(false); + expect(result.noAtomicOperations).toEqual(false); }); it('createConfig types', () => { diff --git a/src/helpers/create-config.ts b/src/helpers/create-config.ts index f576f172..3199e34c 100644 --- a/src/helpers/create-config.ts +++ b/src/helpers/create-config.ts @@ -14,10 +14,10 @@ export function createConfig(data: Record) { config.outputFilePattern || `{feature}/{name}.{type}.ts`, ), combineScalarFilters: ['true', '1', 'on'].includes( - (config.combineScalarFilters as Nullable) ?? 'true', + (config.combineScalarFilters as Nullable) ?? 'false', ), - noAtomicNumberOperations: ['true', '1', 'on'].includes( - (config.atomicNumberOperations as Nullable) ?? 'true', + noAtomicOperations: ['true', '1', 'on'].includes( + (config.noAtomicOperations as Nullable) ?? 'false', ), types: merge( {}, @@ -34,7 +34,7 @@ export function createConfig(data: Record) { (config.reExportAll as Nullable) ?? 'false', ), renameZooTypes: ['true', '1', 'on'].includes( - (config.renameZooTypes as Nullable) ?? 'true', + (config.renameZooTypes as Nullable) ?? 'false', ), }; } diff --git a/src/index.ts b/src/index.ts index c599add4..ff0fc8bc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,15 +1,10 @@ import { generatorHandler, GeneratorOptions } from '@prisma/generator-helper'; import { generate } from './generate'; -import { createConfig } from './helpers/create-config'; generatorHandler({ async onGenerate(options: GeneratorOptions) { - const config = createConfig(options.generator.config); - await generate({ - ...options, - config, - }); + await generate(options); }, onManifest() { return {