sqlite-zod-orm 3.18.0 → 3.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +28 -0
- package/package.json +1 -1
- package/src/builder.ts +50 -0
package/dist/index.js
CHANGED
|
@@ -4572,6 +4572,34 @@ class QueryBuilder {
|
|
|
4572
4572
|
const aggSql = selectSql.replace(/^SELECT .+? FROM/, `SELECT ${groupCols}, COUNT(*) as count FROM`);
|
|
4573
4573
|
return this.executor(aggSql, params, true);
|
|
4574
4574
|
}
|
|
4575
|
+
updateAll(data) {
|
|
4576
|
+
const { sql: selectSql, params } = compileIQO(this.tableName, this.iqo);
|
|
4577
|
+
const whereMatch = selectSql.match(/WHERE (.+?)(?:\s+ORDER|\s+LIMIT|\s+GROUP|\s+HAVING|$)/s);
|
|
4578
|
+
const wherePart = whereMatch ? whereMatch[1] : "1=1";
|
|
4579
|
+
const setClauses = [];
|
|
4580
|
+
const setParams = [];
|
|
4581
|
+
for (const [col, val] of Object.entries(data)) {
|
|
4582
|
+
setClauses.push(`"${col}" = ?`);
|
|
4583
|
+
if (val !== null && val !== undefined && typeof val === "object" && !(val instanceof Buffer) && !(val instanceof Date)) {
|
|
4584
|
+
setParams.push(JSON.stringify(val));
|
|
4585
|
+
} else {
|
|
4586
|
+
setParams.push(val);
|
|
4587
|
+
}
|
|
4588
|
+
}
|
|
4589
|
+
const updateSql = `UPDATE "${this.tableName}" SET ${setClauses.join(", ")} WHERE ${wherePart}`;
|
|
4590
|
+
this.executor(updateSql, [...setParams, ...params], true);
|
|
4591
|
+
const result = this.executor(`SELECT changes() as c`, [], true);
|
|
4592
|
+
return result[0]?.c ?? 0;
|
|
4593
|
+
}
|
|
4594
|
+
deleteAll() {
|
|
4595
|
+
const { sql: selectSql, params } = compileIQO(this.tableName, this.iqo);
|
|
4596
|
+
const whereMatch = selectSql.match(/WHERE (.+?)(?:\s+ORDER|\s+LIMIT|\s+GROUP|\s+HAVING|$)/s);
|
|
4597
|
+
const wherePart = whereMatch ? whereMatch[1] : "1=1";
|
|
4598
|
+
const deleteSql = `DELETE FROM "${this.tableName}" WHERE ${wherePart}`;
|
|
4599
|
+
this.executor(deleteSql, params, true);
|
|
4600
|
+
const result = this.executor(`SELECT changes() as c`, [], true);
|
|
4601
|
+
return result[0]?.c ?? 0;
|
|
4602
|
+
}
|
|
4575
4603
|
then(onfulfilled, onrejected) {
|
|
4576
4604
|
try {
|
|
4577
4605
|
const result = this.all();
|
package/package.json
CHANGED
package/src/builder.ts
CHANGED
|
@@ -465,6 +465,56 @@ export class QueryBuilder<T extends Record<string, any>, TResult extends Record<
|
|
|
465
465
|
return this.executor(aggSql, params, true) as any;
|
|
466
466
|
}
|
|
467
467
|
|
|
468
|
+
// ---------- Batch Mutations ----------
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* Update all rows matching the current query's WHERE conditions.
|
|
472
|
+
* Returns the number of affected rows.
|
|
473
|
+
* ```ts
|
|
474
|
+
* db.users.select().where({ role: 'guest' }).updateAll({ role: 'member' })
|
|
475
|
+
* ```
|
|
476
|
+
*/
|
|
477
|
+
updateAll(data: Partial<T>): number {
|
|
478
|
+
const { sql: selectSql, params } = compileIQO(this.tableName, this.iqo);
|
|
479
|
+
// Extract WHERE clause from compiled SELECT
|
|
480
|
+
const whereMatch = selectSql.match(/WHERE (.+?)(?:\s+ORDER|\s+LIMIT|\s+GROUP|\s+HAVING|$)/s);
|
|
481
|
+
const wherePart = whereMatch ? whereMatch[1] : '1=1';
|
|
482
|
+
|
|
483
|
+
const setClauses: string[] = [];
|
|
484
|
+
const setParams: any[] = [];
|
|
485
|
+
for (const [col, val] of Object.entries(data)) {
|
|
486
|
+
setClauses.push(`"${col}" = ?`);
|
|
487
|
+
if (val !== null && val !== undefined && typeof val === 'object' && !(val instanceof Buffer) && !(val instanceof Date)) {
|
|
488
|
+
setParams.push(JSON.stringify(val));
|
|
489
|
+
} else {
|
|
490
|
+
setParams.push(val);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
const updateSql = `UPDATE "${this.tableName}" SET ${setClauses.join(', ')} WHERE ${wherePart}`;
|
|
495
|
+
this.executor(updateSql, [...setParams, ...params], true);
|
|
496
|
+
// Return affected rows via changes()
|
|
497
|
+
const result = this.executor(`SELECT changes() as c`, [], true);
|
|
498
|
+
return (result[0] as any)?.c ?? 0;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/**
|
|
502
|
+
* Delete all rows matching the current query's WHERE conditions.
|
|
503
|
+
* Returns the number of deleted rows.
|
|
504
|
+
* ```ts
|
|
505
|
+
* db.users.select().where({ role: 'guest' }).deleteAll()
|
|
506
|
+
* ```
|
|
507
|
+
*/
|
|
508
|
+
deleteAll(): number {
|
|
509
|
+
const { sql: selectSql, params } = compileIQO(this.tableName, this.iqo);
|
|
510
|
+
const whereMatch = selectSql.match(/WHERE (.+?)(?:\s+ORDER|\s+LIMIT|\s+GROUP|\s+HAVING|$)/s);
|
|
511
|
+
const wherePart = whereMatch ? whereMatch[1] : '1=1';
|
|
512
|
+
|
|
513
|
+
const deleteSql = `DELETE FROM "${this.tableName}" WHERE ${wherePart}`;
|
|
514
|
+
this.executor(deleteSql, params, true);
|
|
515
|
+
const result = this.executor(`SELECT changes() as c`, [], true);
|
|
516
|
+
return (result[0] as any)?.c ?? 0;
|
|
517
|
+
}
|
|
468
518
|
|
|
469
519
|
|
|
470
520
|
// ---------- Thenable (async/await support) ----------
|