ReviewOS

also looking at this

stacks/bun-query-builder

fix(client)!: bind db.raw and db.sql fragment values or refuse them

#1152
Merged glennmichael123 wants to merge fix/db-sql-raw-bindings into main
16 files +455 -118
docs/features/transactions.mdmodified+7-5
Changes to docs/features/transactions.md
@@ -251,12 +251,12 @@ async function transferFunds(fromAccount: number, toAccount: number, amount: num
251251 }
252252
253253 await tx.updateTable('accounts')
254 .set({ balance: db.sql`balance - ${amount}` })
254 .set({ balance: db.raw('balance - ?', [amount]) })
255255 .where({ id: fromAccount })
256256 .execute()
257257
258258 await tx.updateTable('accounts')
259 .set({ balance: db.sql`balance + ${amount}` })
259 .set({ balance: db.raw('balance + ?', [amount]) })
260260 .where({ id: toAccount })
261261 .execute()
262262 }, { isolation: 'serializable', retries: 3 })
@@ -288,7 +288,7 @@ async function highContentionOperation(data: any) {
288288 return await db.transaction(async (tx) => {
289289 // Operations that might conflict with other transactions
290290 await tx.updateTable('counters')
291 .set({ value: db.sql`value + 1` })
291 .set({ value: db.raw('value + 1') })
292292 .where({ name: 'page_views' })
293293 .execute()
294294 }, {
@@ -318,7 +318,7 @@ async function processOrder(orderData: any) {
318318
319319 // Update inventory
320320 await tx.updateTable('products')
321 .set({ stock_quantity: db.sql`stock_quantity - ${orderData.quantity}` })
321 .set({ stock_quantity: db.raw('stock_quantity - ?', [orderData.quantity]) })
322322 .where({ id: orderData.product_id })
323323 .execute()
324324
@@ -448,11 +448,13 @@ await db.transaction(async (tx) => {
448448### Serializable reads with retries
449449
450450```ts
451import { raw } from 'bun-query-builder'
452
451453await db.transaction(async (tx) => {
452454 const totals = await tx
453455 .selectFrom('orders')
454456 .groupBy('customer_id')
455 .selectRaw(db.sql`SUM(total) as total`)
457 .selectRaw(raw`SUM(total) as total`)
456458 .execute()
457459 // ...work with totals
458460}, { isolation: 'serializable', retries: 2 })