forked from unlight/prisma-nestjs-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Option to disable atomic number operations
- Loading branch information
Showing
10 changed files
with
234 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import { DMMF as PrismaDMMF } from '@prisma/client/runtime/dmmf-types'; | ||
|
||
type MutateFiltersOptions = { | ||
atomicNumberOperations?: boolean; | ||
combineScalarFilters?: boolean; | ||
}; | ||
|
||
export function mutateFilters(inputTypes: PrismaDMMF.InputType[], options: MutateFiltersOptions) { | ||
const mutations = [ | ||
options.combineScalarFilters && [ | ||
combineScalarFilters(inputTypes, [ | ||
'StringFilter', | ||
'NullableStringFilter', | ||
'StringNullableFilter', | ||
'NestedStringNullableFilter', | ||
'NestedStringFilter', | ||
]), | ||
combineScalarFilters(inputTypes, [ | ||
'BooleanFilter', | ||
'NullableBooleanFilter', | ||
'BoolNullableFilter', | ||
'NestedBoolNullableFilter', | ||
]), | ||
combineScalarFilters(inputTypes, [ | ||
'IntFilter', | ||
'NullableIntFilter', | ||
'IntNullableFilter', | ||
'NestedIntNullableFilter', | ||
'NestedIntFilter', | ||
]), | ||
combineScalarFilters(inputTypes, [ | ||
'FloatFilter', | ||
'NullableFloatFilter', | ||
'FloatNullableFilter', | ||
'NestedFloatNullableFilter', | ||
'NestedFloatFilter', | ||
]), | ||
combineScalarFilters(inputTypes, [ | ||
'DateTimeFilter', | ||
'NullableDateTimeFilter', | ||
'DateTimeNullableFilter', | ||
'NestedDateTimeNullableFilter', | ||
'NestedDateTimeFilter', | ||
]), | ||
combineScalarFilters(inputTypes, [ | ||
'StringFieldUpdateOperationsInput', | ||
'NullableStringFieldUpdateOperationsInput', | ||
]), | ||
combineScalarFilters(inputTypes, [ | ||
'IntFieldUpdateOperationsInput', | ||
'NullableIntFieldUpdateOperationsInput', | ||
]), | ||
combineScalarFilters(inputTypes, [ | ||
'FloatFieldUpdateOperationsInput', | ||
'NullableFloatFieldUpdateOperationsInput', | ||
]), | ||
combineScalarFilters(inputTypes, [ | ||
'BoolFieldUpdateOperationsInput', | ||
'NullableBoolFieldUpdateOperationsInput', | ||
]), | ||
combineScalarFilters(inputTypes, [ | ||
'DateTimeFieldUpdateOperationsInput', | ||
'NullableDateTimeFieldUpdateOperationsInput', | ||
]), | ||
], | ||
!options.atomicNumberOperations && | ||
noAtomicNumberOperations([ | ||
'StringFieldUpdateOperationsInput', | ||
'NullableStringFieldUpdateOperationsInput', | ||
'IntFieldUpdateOperationsInput', | ||
'NullableIntFieldUpdateOperationsInput', | ||
'FloatFieldUpdateOperationsInput', | ||
'NullableFloatFieldUpdateOperationsInput', | ||
'BoolFieldUpdateOperationsInput', | ||
'NullableBoolFieldUpdateOperationsInput', | ||
'DateTimeFieldUpdateOperationsInput', | ||
'NullableDateTimeFieldUpdateOperationsInput', | ||
]), | ||
]; | ||
|
||
return function (inputType: PrismaDMMF.InputType) { | ||
for (const mutation of mutations.filter(Boolean).flat()) { | ||
const result = mutation && mutation(inputType); | ||
if (!result) { | ||
return false; | ||
} | ||
inputType = result; | ||
} | ||
return inputType; | ||
}; | ||
} | ||
|
||
function combineScalarFilters(inputTypes: PrismaDMMF.InputType[], names: string[]) { | ||
let counter = 0; | ||
let type: PrismaDMMF.InputType | undefined; | ||
const [main] = names; | ||
for (const name of names) { | ||
type = inputTypes.find((t) => t.name === name); | ||
if (type) { | ||
type = mutateFieldsType(type, names); | ||
break; | ||
} | ||
} | ||
|
||
return (inputType: PrismaDMMF.InputType) => { | ||
if (!type) { | ||
return inputType; | ||
} | ||
if (names.includes(inputType.name)) { | ||
counter++; | ||
if (counter > 1) { | ||
return false; | ||
} | ||
inputType.name = main; | ||
inputType.fields = type.fields; | ||
return inputType; | ||
} | ||
inputType = mutateFieldsType(inputType, names); | ||
return inputType; | ||
}; | ||
|
||
function mutateFieldsType(inputType: PrismaDMMF.InputType, names: string[]) { | ||
const [main] = names; | ||
inputType.fields.forEach((field) => { | ||
field.inputType.forEach((input) => { | ||
if (names.includes(String(input.type))) { | ||
input.type = main; | ||
} | ||
}); | ||
}); | ||
return inputType; | ||
} | ||
} | ||
|
||
function noAtomicNumberOperations(names: string[]) { | ||
return (inputType: PrismaDMMF.InputType) => { | ||
if (names.includes(inputType.name)) { | ||
return false; | ||
} | ||
inputType.fields = inputType.fields.map((field) => { | ||
field.inputType = field.inputType.filter((inputType) => { | ||
return !names.includes(String(inputType.type)); | ||
}); | ||
return field; | ||
}); | ||
return inputType; | ||
}; | ||
} |
Oops, something went wrong.