-
-
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.
Merge pull request #105 from prisma-idb/104-atomic-operators
104 atomic operators
- Loading branch information
Showing
16 changed files
with
693 additions
and
26 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
77 changes: 77 additions & 0 deletions
77
packages/generator/src/fileCreators/idb-utils/updateHandlers/BigIntHandler.ts
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,77 @@ | ||
import { Model } from "src/fileCreators/types"; | ||
import type { SourceFile } from "ts-morph"; | ||
|
||
export function addBigIntUpdateHandler(utilsFile: SourceFile, models: readonly Model[]) { | ||
const bigIntFields = models.flatMap(({ fields }) => fields).filter((field) => field.type === "BigInt"); | ||
if (bigIntFields.length === 0) return; | ||
|
||
let updateOperationType = "undefined | bigint | number"; | ||
let fieldType = "bigint"; | ||
|
||
const nonNullableBigIntFieldPresent = bigIntFields.some(({ isRequired }) => isRequired); | ||
const nullableBigIntFieldPresent = bigIntFields.some(({ isRequired }) => !isRequired); | ||
|
||
if (nonNullableBigIntFieldPresent) { | ||
updateOperationType += " | Prisma.BigIntFieldUpdateOperationsInput"; | ||
} | ||
if (nullableBigIntFieldPresent) { | ||
updateOperationType += " | null | Prisma.NullableBigIntFieldUpdateOperationsInput"; | ||
fieldType += " | null"; | ||
} | ||
|
||
utilsFile.addFunction({ | ||
name: "handleBigIntUpdateField", | ||
isExported: true, | ||
typeParameters: [{ name: "T" }, { name: "R", constraint: `Prisma.Result<T, object, "findFirstOrThrow">` }], | ||
parameters: [ | ||
{ name: "record", type: `R` }, | ||
{ name: "fieldName", type: "keyof R" }, | ||
{ | ||
name: "bigIntUpdate", | ||
type: updateOperationType, | ||
}, | ||
], | ||
statements: (writer) => { | ||
writer | ||
.writeLine(`if (bigIntUpdate === undefined) return;`) | ||
.write(`if (typeof bigIntUpdate === "bigint" || typeof bigIntUpdate === "number"`) | ||
.conditionalWrite(nullableBigIntFieldPresent, ` || bigIntUpdate === null`) | ||
.writeLine(`)`) | ||
.block(() => { | ||
if (nullableBigIntFieldPresent) { | ||
writer.writeLine( | ||
`(record[fieldName] as ${fieldType}) = bigIntUpdate === null ? null : BigInt(bigIntUpdate);`, | ||
); | ||
} else { | ||
writer.writeLine(`(record[fieldName] as ${fieldType}) = BigInt(bigIntUpdate);`); | ||
} | ||
}) | ||
.writeLine(`else if (bigIntUpdate.set !== undefined)`) | ||
.block(() => { | ||
if (nullableBigIntFieldPresent) { | ||
writer.writeLine( | ||
`(record[fieldName] as ${fieldType}) = bigIntUpdate.set === null ? null : BigInt(bigIntUpdate.set);`, | ||
); | ||
} else { | ||
writer.writeLine(`(record[fieldName] as ${fieldType}) = BigInt(bigIntUpdate.set);`); | ||
} | ||
}) | ||
.writeLine(`else if (bigIntUpdate.increment !== undefined && record[fieldName] !== null)`) | ||
.block(() => { | ||
writer.writeLine(`(record[fieldName] as bigint) += BigInt(bigIntUpdate.increment);`); | ||
}) | ||
.writeLine(`else if (bigIntUpdate.decrement !== undefined && record[fieldName] !== null)`) | ||
.block(() => { | ||
writer.writeLine(`(record[fieldName] as bigint) -= BigInt(bigIntUpdate.decrement);`); | ||
}) | ||
.writeLine(`else if (bigIntUpdate.multiply !== undefined && record[fieldName] !== null)`) | ||
.block(() => { | ||
writer.writeLine(`(record[fieldName] as bigint) *= BigInt(bigIntUpdate.multiply);`); | ||
}) | ||
.writeLine(`else if (bigIntUpdate.divide !== undefined && record[fieldName] !== null)`) | ||
.block(() => { | ||
writer.writeLine(`(record[fieldName] as bigint) /= BigInt(bigIntUpdate.divide);`); | ||
}); | ||
}, | ||
}); | ||
} |
65 changes: 65 additions & 0 deletions
65
packages/generator/src/fileCreators/idb-utils/updateHandlers/FloatHandler.ts
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,65 @@ | ||
import type { Model } from "src/fileCreators/types"; | ||
import type { SourceFile } from "ts-morph"; | ||
|
||
export function addFloatUpdateHandler(utilsFile: SourceFile, models: readonly Model[]) { | ||
const floatFields = models.flatMap(({ fields }) => fields).filter((field) => field.type === "Float"); | ||
if (floatFields.length === 0) return; | ||
|
||
let updateOperationType = "undefined | number"; | ||
let fieldType = "number"; | ||
|
||
const nonNullableFloatFieldPresent = floatFields.some(({ isRequired }) => isRequired); | ||
const nullableFloatFieldPresent = floatFields.some(({ isRequired }) => !isRequired); | ||
|
||
if (nonNullableFloatFieldPresent) { | ||
updateOperationType += " | Prisma.FloatFieldUpdateOperationsInput"; | ||
} | ||
if (nullableFloatFieldPresent) { | ||
updateOperationType += " | null | Prisma.NullableFloatFieldUpdateOperationsInput"; | ||
fieldType += " | null"; | ||
} | ||
|
||
utilsFile.addFunction({ | ||
name: "handleFloatUpdateField", | ||
isExported: true, | ||
typeParameters: [{ name: "T" }, { name: "R", constraint: `Prisma.Result<T, object, "findFirstOrThrow">` }], | ||
parameters: [ | ||
{ name: "record", type: `R` }, | ||
{ name: "fieldName", type: "keyof R" }, | ||
{ | ||
name: "floatUpdate", | ||
type: updateOperationType, | ||
}, | ||
], | ||
statements: (writer) => { | ||
writer | ||
.writeLine(`if (floatUpdate === undefined) return;`) | ||
.write(`if (typeof floatUpdate === "number"`) | ||
.conditionalWrite(nullableFloatFieldPresent, ` || floatUpdate === null`) | ||
.writeLine(`)`) | ||
.block(() => { | ||
writer.writeLine(`(record[fieldName] as ${fieldType}) = floatUpdate;`); | ||
}) | ||
.writeLine(`else if (floatUpdate.set !== undefined)`) | ||
.block(() => { | ||
writer.writeLine(`(record[fieldName] as ${fieldType}) = floatUpdate.set;`); | ||
}) | ||
.writeLine(`else if (floatUpdate.increment !== undefined && record[fieldName] !== null)`) | ||
.block(() => { | ||
writer.writeLine(`(record[fieldName] as number) += floatUpdate.increment;`); | ||
}) | ||
.writeLine(`else if (floatUpdate.decrement !== undefined && record[fieldName] !== null)`) | ||
.block(() => { | ||
writer.writeLine(`(record[fieldName] as number) -= floatUpdate.decrement;`); | ||
}) | ||
.writeLine(`else if (floatUpdate.multiply !== undefined && record[fieldName] !== null)`) | ||
.block(() => { | ||
writer.writeLine(`(record[fieldName] as number) *= floatUpdate.multiply;`); | ||
}) | ||
.writeLine(`else if (floatUpdate.divide !== undefined && record[fieldName] !== null)`) | ||
.block(() => { | ||
writer.writeLine(`(record[fieldName] as number) /= floatUpdate.divide;`); | ||
}); | ||
}, | ||
}); | ||
} |
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
Oops, something went wrong.