tort6334
tort6334
Explore posts from servers
PPrisma
Created by tort6334 on 8/5/2024 in #help-and-questions
Alternative to solving optimistic concurrency issue without version field
In the following page there is a suggestion how to solve concurrency issue by adding a version field and ask about it in the where clause when updating a model:
const userEmail = '[email protected]'
const movieName = 'Hidden Figures'

// Find the first available seat
// availableSeat.version might be 0
const availableSeat = await client.seat.findFirst({
where: {
Movie: {
name: movieName,
},
claimedBy: null,
},
})

if (!availableSeat) {
throw new Error(`Oh no! ${movieName} is all booked.`)
}

// Only mark the seat as claimed if the availableSeat.version
// matches the version we're updating. Additionally, increment the
// version when we perform this update so all other clients trying
// to book this same seat will have an outdated version.
const seats = await client.seat.updateMany({
data: {
claimedBy: userEmail,
version: {
increment: 1,
},
},
where: {
id: availableSeat.id,
version: availableSeat.version, // This version field is the key; only claim seat if in-memory version matches database version, indicating that the field has not been updated
},
})

if (seats.count === 0) {
throw new Error(`That seat is already booked! Please try again.`)
}
const userEmail = '[email protected]'
const movieName = 'Hidden Figures'

// Find the first available seat
// availableSeat.version might be 0
const availableSeat = await client.seat.findFirst({
where: {
Movie: {
name: movieName,
},
claimedBy: null,
},
})

if (!availableSeat) {
throw new Error(`Oh no! ${movieName} is all booked.`)
}

// Only mark the seat as claimed if the availableSeat.version
// matches the version we're updating. Additionally, increment the
// version when we perform this update so all other clients trying
// to book this same seat will have an outdated version.
const seats = await client.seat.updateMany({
data: {
claimedBy: userEmail,
version: {
increment: 1,
},
},
where: {
id: availableSeat.id,
version: availableSeat.version, // This version field is the key; only claim seat if in-memory version matches database version, indicating that the field has not been updated
},
})

if (seats.count === 0) {
throw new Error(`That seat is already booked! Please try again.`)
}
Reference: https://www.prisma.io/docs/orm/prisma-client/queries/transactions#optimistic-concurrency-control what I want to ask, is if instead of doing:
where: {
id: availableSeat.id,
version: availableSeat.version, // This version field is the key; only claim seat if in-memory version matches database version, indicating that the field has not been updated
},
where: {
id: availableSeat.id,
version: availableSeat.version, // This version field is the key; only claim seat if in-memory version matches database version, indicating that the field has not been updated
},
I could have asked if claimedBy is still null, will it do the same trick? so the solution would be like so:
where: {
id: availableSeat.id,
claimedBy: null
},
where: {
id: availableSeat.id,
claimedBy: null
},
The only disadvantage compared to version is that if there were many swaps between claimedby and null, i would have not known about it, but that's fine by me I guess?
1 replies
PPrisma
Created by tort6334 on 7/24/2024 in #help-and-questions
Solve optimistic concurrency issue without adding a `version` field
Question In the following page there is a suggestion how to solve concurrency issue by adding a version field and ask about it in the where clause when updating a model:
const userEmail = '[email protected]'
const movieName = 'Hidden Figures'

// Find the first available seat
// availableSeat.version might be 0
const availableSeat = await client.seat.findFirst({
where: {
Movie: {
name: movieName,
},
claimedBy: null,
},
})

if (!availableSeat) {
throw new Error(`Oh no! ${movieName} is all booked.`)
}

// Only mark the seat as claimed if the availableSeat.version
// matches the version we're updating. Additionally, increment the
// version when we perform this update so all other clients trying
// to book this same seat will have an outdated version.
const seats = await client.seat.updateMany({
data: {
claimedBy: userEmail,
version: {
increment: 1,
},
},
where: {
id: availableSeat.id,
version: availableSeat.version, // This version field is the key; only claim seat if in-memory version matches database version, indicating that the field has not been updated
},
})

if (seats.count === 0) {
throw new Error(`That seat is already booked! Please try again.`)
}
const userEmail = '[email protected]'
const movieName = 'Hidden Figures'

// Find the first available seat
// availableSeat.version might be 0
const availableSeat = await client.seat.findFirst({
where: {
Movie: {
name: movieName,
},
claimedBy: null,
},
})

if (!availableSeat) {
throw new Error(`Oh no! ${movieName} is all booked.`)
}

// Only mark the seat as claimed if the availableSeat.version
// matches the version we're updating. Additionally, increment the
// version when we perform this update so all other clients trying
// to book this same seat will have an outdated version.
const seats = await client.seat.updateMany({
data: {
claimedBy: userEmail,
version: {
increment: 1,
},
},
where: {
id: availableSeat.id,
version: availableSeat.version, // This version field is the key; only claim seat if in-memory version matches database version, indicating that the field has not been updated
},
})

if (seats.count === 0) {
throw new Error(`That seat is already booked! Please try again.`)
}
Reference: https://www.prisma.io/docs/orm/prisma-client/queries/transactions#optimistic-concurrency-control what I want to ask, is if instead of doing:
where: {
id: availableSeat.id,
version: availableSeat.version, // This version field is the key; only claim seat if in-memory version matches database version, indicating that the field has not been updated
},
where: {
id: availableSeat.id,
version: availableSeat.version, // This version field is the key; only claim seat if in-memory version matches database version, indicating that the field has not been updated
},
I could have asked if claimedBy is still null, will it do the same trick? so the solution would be like so:
where: {
id: availableSeat.id,
claimedBy: null
},
where: {
id: availableSeat.id,
claimedBy: null
},
The only disadvantage compared to version is that if there were many swaps between claimedby and null, i would have not known about it, but that's fine by me I guess?
3 replies
PPrisma
Created by tort6334 on 6/27/2024 in #help-and-questions
How can I use Prisma with Kubernetes? Any example
I couldn't find a single local development Kubernetes example with Prisma, and like other tools like Skaffold. How can I do that? Prisma is an interesting case because it requires also post processing of migration, deployment and creation of the schemas.
2 replies
PPrisma
Created by tort6334 on 5/27/2024 in #help-and-questions
Any expected SDL-First generator with CRUD abilities?
Hi guys, I really like Prisma and I want to use it with GraphQL, the thing is, I want to generate automatically all CRUD operations based on my schema, and the only valid option right now is to use TypeGraphQL, and I prefer to use Schema First language, will there be a generator for that?
12 replies