also looking at this
fix(client)!: bind db.raw and db.sql fragment values or refuse them
#1152
16 files
+455
-118
| @@ -251,12 +251,12 @@ async function transferFunds(fromAccount: number, toAccount: number, amount: num | ||
| 251 | 251 | } |
| 252 | 252 | |
| 253 | 253 | await tx.updateTable('accounts') |
| 254 | .set({ balance: db.sql`balance - ${amount}` }) | |
| 254 | .set({ balance: db.raw('balance - ?', [amount]) }) | |
| 255 | 255 | .where({ id: fromAccount }) |
| 256 | 256 | .execute() |
| 257 | 257 | |
| 258 | 258 | await tx.updateTable('accounts') |
| 259 | .set({ balance: db.sql`balance + ${amount}` }) | |
| 259 | .set({ balance: db.raw('balance + ?', [amount]) }) | |
| 260 | 260 | .where({ id: toAccount }) |
| 261 | 261 | .execute() |
| 262 | 262 | }, { isolation: 'serializable', retries: 3 }) |
| @@ -288,7 +288,7 @@ async function highContentionOperation(data: any) { | ||
| 288 | 288 | return await db.transaction(async (tx) => { |
| 289 | 289 | // Operations that might conflict with other transactions |
| 290 | 290 | await tx.updateTable('counters') |
| 291 | .set({ value: db.sql`value + 1` }) | |
| 291 | .set({ value: db.raw('value + 1') }) | |
| 292 | 292 | .where({ name: 'page_views' }) |
| 293 | 293 | .execute() |
| 294 | 294 | }, { |
| @@ -318,7 +318,7 @@ async function processOrder(orderData: any) { | ||
| 318 | 318 | |
| 319 | 319 | // Update inventory |
| 320 | 320 | await tx.updateTable('products') |
| 321 | .set({ stock_quantity: db.sql`stock_quantity - ${orderData.quantity}` }) | |
| 321 | .set({ stock_quantity: db.raw('stock_quantity - ?', [orderData.quantity]) }) | |
| 322 | 322 | .where({ id: orderData.product_id }) |
| 323 | 323 | .execute() |
| 324 | 324 | |
| @@ -448,11 +448,13 @@ await db.transaction(async (tx) => { | ||
| 448 | 448 | ### Serializable reads with retries |
| 449 | 449 | |
| 450 | 450 | ```ts |
| 451 | import { raw } from 'bun-query-builder' | |
| 452 | ||
| 451 | 453 | await db.transaction(async (tx) => { |
| 452 | 454 | const totals = await tx |
| 453 | 455 | .selectFrom('orders') |
| 454 | 456 | .groupBy('customer_id') |
| 455 | .selectRaw(db.sql`SUM(total) as total`) | |
| 457 | .selectRaw(raw`SUM(total) as total`) | |
| 456 | 458 | .execute() |
| 457 | 459 | // ...work with totals |
| 458 | 460 | }, { isolation: 'serializable', retries: 2 }) |