Skip to content

Commit

Permalink
transaction alternative approaches (#3)
Browse files Browse the repository at this point in the history
Co-authored-by: Kent C. Dodds <[email protected]>
  • Loading branch information
rtbenfield and kentcdodds committed Jul 21, 2023
1 parent c7c0fe8 commit 663b0d2
Showing 1 changed file with 16 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,28 +96,22 @@ export async function action({ request, params }: DataFunctionArgs) {
if (image.file.size > 0) {
console.log('file has been uploaded')
const blob = Buffer.from(await image.file.arrayBuffer())
if (image.id) {
const newId = cuid()
await $prisma.image.update({
select: { id: true },
where: { id: image.id },
data: {
id: newId,
altText: image.altText,
file: { update: { contentType: image.file.type, blob: blob } },
},
})
return { id: newId }
} else {
const i = await $prisma.image.create({
select: { id: true },
data: {
altText: image.altText,
file: { create: { contentType: image.file.type, blob } },
},
})
return i
}
return await $prisma.image.upsert({
// use a fake ID as a fallback to meet the `where` requirements
// without matching any records
where: { id: image.id ?? '__does_not_exist__' },
select: { id: true },
create: {
altText: image.altText,
file: { create: { contentType: image.file.type, blob } },
},
update: {
// update the id since it is used for caching
id: cuid(),
altText: image.altText,
file: { update: { contentType: image.file.type, blob: blob } },
},
})
} else if (image.id) {
return await $prisma.image.update({
select: { id: true },
Expand Down

0 comments on commit 663b0d2

Please sign in to comment.