-
-
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 #78 from prisma-idb/74-upsert
`upsert()`
- Loading branch information
Showing
5 changed files
with
188 additions
and
5 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
43 changes: 43 additions & 0 deletions
43
packages/generator/src/fileCreators/prisma-idb-client/classes/models/api/upsert.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,43 @@ | ||
import { getUniqueIdentifiers } from "../../../../../helpers/utils"; | ||
import { Model } from "../../../../../fileCreators/types"; | ||
import { ClassDeclaration, CodeBlockWriter } from "ts-morph"; | ||
|
||
export function addUpsertMethod(modelClass: ClassDeclaration, model: Model) { | ||
modelClass.addMethod({ | ||
name: "upsert", | ||
typeParameters: [{ name: "Q", constraint: `Prisma.Args<Prisma.${model.name}Delegate, "upsert">` }], | ||
isAsync: true, | ||
parameters: [ | ||
{ name: "query", type: "Q" }, | ||
{ name: "tx", hasQuestionToken: true, type: "IDBUtils.ReadwriteTransactionType" }, | ||
], | ||
returnType: `Promise<Prisma.Result<Prisma.${model.name}Delegate, Q, "upsert">>`, | ||
statements: (writer) => { | ||
addGetAndUpsertRecord(writer); | ||
addRefetchAndReturnRecord(writer, model); | ||
}, | ||
}); | ||
} | ||
|
||
function addGetAndUpsertRecord(writer: CodeBlockWriter) { | ||
// TODO: add nested query things to the tx as well (nested writes to other records) | ||
writer | ||
.writeLine(`tx = tx ?? this.client._db.transaction(Array.from(this._getNeededStoresForFind(query)), "readwrite");`) | ||
.writeLine(`let record = await this.findUnique({ where: query.where }, tx);`) | ||
.writeLine(`if (!record) record = await this.create({ data: query.create }, tx);`) | ||
.writeLine(`else record = await this.update({ where: query.where, data: query.update }, tx);`); | ||
} | ||
|
||
function addRefetchAndReturnRecord(writer: CodeBlockWriter, model: Model) { | ||
// TODO: composite keys | ||
const pk = JSON.parse(getUniqueIdentifiers(model)[0].keyPath)[0]; | ||
const hasRelations = model.fields.some(({ kind }) => kind === "object"); | ||
|
||
let recordFindQuery = `record = await this.findUniqueOrThrow({ where: { ${pk}: record.${pk} }, select: query.select`; | ||
if (hasRelations) recordFindQuery += ", include: query.include"; | ||
recordFindQuery += "});"; | ||
|
||
writer | ||
.writeLine(recordFindQuery) | ||
.writeLine(`return record as Prisma.Result<Prisma.${model.name}Delegate, Q, "upsert">;`); | ||
} |
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,24 @@ | ||
import { test } from "../fixtures"; | ||
import { expectQueryToSucceed } from "../queryRunnerHelper"; | ||
|
||
test("upsert_ChangeId_SuccessfullyUpdatesRecord", async ({ page }) => { | ||
await expectQueryToSucceed({ | ||
page, | ||
model: "user", | ||
operation: "upsert", | ||
query: { where: { id: 1 }, create: { id: 1, name: "John" }, update: { name: "Alice" } }, | ||
}); | ||
|
||
await expectQueryToSucceed({ | ||
page, | ||
model: "user", | ||
operation: "upsert", | ||
query: { where: { id: 1 }, create: { name: "Alice" }, update: { id: 3 } }, | ||
}); | ||
|
||
await expectQueryToSucceed({ | ||
page, | ||
model: "user", | ||
operation: "findMany", | ||
}); | ||
}); |