Update with array of objects

type StoreSpecialHours = {
storeID: StoreID;
day: Day;
dayOpen: DayOpen;
dayClose: DayClose;
}

openingHours: StoreSpecialHours[],
const storeHours = await db
.update(storespecialhours)
.set(openingHours)
.where(
and(
eq(storespecialhours.storeID, openingHours.storeID),
eq(storespecialhours.day, openingHours.day),
),
)
.returning({
storeID: storespecialhours.storeID,
day: storespecialhours.day,
dayOpen: storespecialhours.dayOpen,
dayClose: storespecialhours.dayClose,
})
type StoreSpecialHours = {
storeID: StoreID;
day: Day;
dayOpen: DayOpen;
dayClose: DayClose;
}

openingHours: StoreSpecialHours[],
const storeHours = await db
.update(storespecialhours)
.set(openingHours)
.where(
and(
eq(storespecialhours.storeID, openingHours.storeID),
eq(storespecialhours.day, openingHours.day),
),
)
.returning({
storeID: storespecialhours.storeID,
day: storespecialhours.day,
dayOpen: storespecialhours.dayOpen,
dayClose: storespecialhours.dayClose,
})
How do I do an update with an array of object where I update multiple fields in the object. The examples I find are just updating one field in the row.
3 Replies
rphlmr ⚡
rphlmr ⚡5mo ago
Do you have a little example to share with https://drizzle.run ? If you mean updating for each openingHour in openingHours, you need to loop over your array, in a transaction (to be sure that all updates apply, https://orm.drizzle.team/docs/transactions). If you are using SQLite you can use batch (https://orm.drizzle.team/docs/batch-api)
AndréLB
AndréLBOP5mo ago
Thanks! That worked!
Want results from more Discord servers?
Add your server