bun-query-builder 0.2.45 → 0.2.46

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/bin/cli.js CHANGED
@@ -14944,6 +14944,10 @@ function createQueryBuilder(state) {
14944
14944
  built = null;
14945
14945
  return this;
14946
14946
  },
14947
+ $call(callback) {
14948
+ callback(this);
14949
+ return this;
14950
+ },
14947
14951
  union(other) {
14948
14952
  appendSetOp("UNION", other);
14949
14953
  return this;
@@ -26792,6 +26796,18 @@ class ModelQueryBuilder {
26792
26796
  return "";
26793
26797
  return this._trashed === "only" ? `${SOFT_DELETE_COLUMN} IS NOT NULL` : `${SOFT_DELETE_COLUMN} IS NULL`;
26794
26798
  }
26799
+ assertNoUnappliedClauses(method) {
26800
+ const ignored = [];
26801
+ if (this._limit !== undefined)
26802
+ ignored.push("limit()");
26803
+ if (this._offset !== undefined)
26804
+ ignored.push("offset()");
26805
+ if (this._orderBy.length > 0)
26806
+ ignored.push("orderBy()");
26807
+ if (ignored.length > 0) {
26808
+ throw new TypeError(`[orm] ${method}() cannot apply ${ignored.join(", ")} \u2014 an UPDATE/DELETE takes no ORDER BY or LIMIT on every supported dialect. ` + `Previously these were ignored, so the statement affected every matching row. Narrow it with where() instead.`);
26809
+ }
26810
+ }
26795
26811
  composeWhere(params) {
26796
26812
  const userClause = this._wheres.length > 0 ? this.buildWhereClauses(params) : "";
26797
26813
  const sd = this.softDeleteClause();
@@ -27041,6 +27057,7 @@ class ModelQueryBuilder {
27041
27057
  }
27042
27058
  async increment(column, amount = 1) {
27043
27059
  assertValidIdentifier(column, "increment(column)");
27060
+ this.assertNoUnappliedClauses("increment");
27044
27061
  const exec = getExecutor();
27045
27062
  const params = [amount];
27046
27063
  const incCol = sqlColumn(column);
@@ -27049,9 +27066,9 @@ class ModelQueryBuilder {
27049
27066
  sql2 += `, updated_at = ?`;
27050
27067
  params.push(formatNow());
27051
27068
  }
27052
- if (this._wheres.length > 0) {
27053
- const clauses = this.buildWhereClauses(params);
27054
- sql2 += ` WHERE ${clauses}`;
27069
+ const whereBody = this.composeWhere(params);
27070
+ if (whereBody) {
27071
+ sql2 += ` WHERE ${whereBody}`;
27055
27072
  }
27056
27073
  return (await exec.run(sql2, params)).changes;
27057
27074
  }
@@ -27150,15 +27167,18 @@ class ModelQueryBuilder {
27150
27167
  return Number(await this.aggregate("SUM", column) ?? 0) || 0;
27151
27168
  }
27152
27169
  async delete() {
27170
+ this.assertNoUnappliedClauses("delete");
27153
27171
  const exec = getExecutor();
27154
27172
  const params = [];
27155
27173
  let sql2 = `DELETE FROM ${this._definition.table}`;
27156
- if (this._wheres.length > 0) {
27157
- sql2 += ` WHERE ${this.buildWhereClauses(params)}`;
27174
+ const whereBody = this.composeWhere(params);
27175
+ if (whereBody) {
27176
+ sql2 += ` WHERE ${whereBody}`;
27158
27177
  }
27159
27178
  return (await exec.run(sql2, params)).changes;
27160
27179
  }
27161
27180
  async update(data) {
27181
+ this.assertNoUnappliedClauses("update");
27162
27182
  const exec = getExecutor();
27163
27183
  const entries = Object.entries(data);
27164
27184
  const sets = entries.map(([k2]) => `${sqlColumn(k2)} = ?`).join(", ");
@@ -27167,8 +27187,9 @@ class ModelQueryBuilder {
27167
27187
  params.push(formatNow());
27168
27188
  }
27169
27189
  let sql2 = `UPDATE ${this._definition.table} SET ${sets}${timestampsEnabled(this._definition) ? ", updated_at = ?" : ""}`;
27170
- if (this._wheres.length > 0) {
27171
- sql2 += ` WHERE ${this.buildWhereClauses(params)}`;
27190
+ const whereBody = this.composeWhere(params);
27191
+ if (whereBody) {
27192
+ sql2 += ` WHERE ${whereBody}`;
27172
27193
  }
27173
27194
  return (await exec.run(sql2, params)).changes;
27174
27195
  }
@@ -32523,7 +32544,7 @@ function getPrefix() {
32523
32544
  }
32524
32545
  var prefix = getPrefix();
32525
32546
  // package.json
32526
- var version2 = "0.2.45";
32547
+ var version2 = "0.2.46";
32527
32548
 
32528
32549
  // bin/cli.ts
32529
32550
  init_actions();
package/dist/client.d.ts CHANGED
@@ -180,6 +180,7 @@ export declare interface BaseSelectQueryBuilder<DB extends AnyDatabaseSchema, TT
180
180
  }
181
181
  selectAll?: () => SelectQueryBuilder<DB, TTable, DB[TTable]['columns'], TJoined>
182
182
  orderByRaw: (fragment: SqlFragment) => SelectQueryBuilder<DB, TTable, TSelected, TJoined>
183
+ $call: (callback: (query: SelectQueryBuilder<DB, TTable, TSelected, TJoined>) => unknown) => SelectQueryBuilder<DB, TTable, TSelected, TJoined>
183
184
  union: (other: { toSQL: () => string }) => SelectQueryBuilder<DB, TTable, TSelected, TJoined>
184
185
  unionAll: (other: { toSQL: () => string }) => SelectQueryBuilder<DB, TTable, TSelected, TJoined>
185
186
  forPage: (page: number, perPage: number) => SelectQueryBuilder<DB, TTable, TSelected, TJoined>
package/dist/src/index.js CHANGED
@@ -14943,6 +14943,10 @@ function createQueryBuilder(state) {
14943
14943
  built = null;
14944
14944
  return this;
14945
14945
  },
14946
+ $call(callback) {
14947
+ callback(this);
14948
+ return this;
14949
+ },
14946
14950
  union(other) {
14947
14951
  appendSetOp("UNION", other);
14948
14952
  return this;
@@ -26791,6 +26795,18 @@ class ModelQueryBuilder {
26791
26795
  return "";
26792
26796
  return this._trashed === "only" ? `${SOFT_DELETE_COLUMN} IS NOT NULL` : `${SOFT_DELETE_COLUMN} IS NULL`;
26793
26797
  }
26798
+ assertNoUnappliedClauses(method) {
26799
+ const ignored = [];
26800
+ if (this._limit !== undefined)
26801
+ ignored.push("limit()");
26802
+ if (this._offset !== undefined)
26803
+ ignored.push("offset()");
26804
+ if (this._orderBy.length > 0)
26805
+ ignored.push("orderBy()");
26806
+ if (ignored.length > 0) {
26807
+ throw new TypeError(`[orm] ${method}() cannot apply ${ignored.join(", ")} \u2014 an UPDATE/DELETE takes no ORDER BY or LIMIT on every supported dialect. ` + `Previously these were ignored, so the statement affected every matching row. Narrow it with where() instead.`);
26808
+ }
26809
+ }
26794
26810
  composeWhere(params) {
26795
26811
  const userClause = this._wheres.length > 0 ? this.buildWhereClauses(params) : "";
26796
26812
  const sd = this.softDeleteClause();
@@ -27040,6 +27056,7 @@ class ModelQueryBuilder {
27040
27056
  }
27041
27057
  async increment(column, amount = 1) {
27042
27058
  assertValidIdentifier(column, "increment(column)");
27059
+ this.assertNoUnappliedClauses("increment");
27043
27060
  const exec = getExecutor();
27044
27061
  const params = [amount];
27045
27062
  const incCol = sqlColumn(column);
@@ -27048,9 +27065,9 @@ class ModelQueryBuilder {
27048
27065
  sql2 += `, updated_at = ?`;
27049
27066
  params.push(formatNow());
27050
27067
  }
27051
- if (this._wheres.length > 0) {
27052
- const clauses = this.buildWhereClauses(params);
27053
- sql2 += ` WHERE ${clauses}`;
27068
+ const whereBody = this.composeWhere(params);
27069
+ if (whereBody) {
27070
+ sql2 += ` WHERE ${whereBody}`;
27054
27071
  }
27055
27072
  return (await exec.run(sql2, params)).changes;
27056
27073
  }
@@ -27149,15 +27166,18 @@ class ModelQueryBuilder {
27149
27166
  return Number(await this.aggregate("SUM", column) ?? 0) || 0;
27150
27167
  }
27151
27168
  async delete() {
27169
+ this.assertNoUnappliedClauses("delete");
27152
27170
  const exec = getExecutor();
27153
27171
  const params = [];
27154
27172
  let sql2 = `DELETE FROM ${this._definition.table}`;
27155
- if (this._wheres.length > 0) {
27156
- sql2 += ` WHERE ${this.buildWhereClauses(params)}`;
27173
+ const whereBody = this.composeWhere(params);
27174
+ if (whereBody) {
27175
+ sql2 += ` WHERE ${whereBody}`;
27157
27176
  }
27158
27177
  return (await exec.run(sql2, params)).changes;
27159
27178
  }
27160
27179
  async update(data) {
27180
+ this.assertNoUnappliedClauses("update");
27161
27181
  const exec = getExecutor();
27162
27182
  const entries = Object.entries(data);
27163
27183
  const sets = entries.map(([k2]) => `${sqlColumn(k2)} = ?`).join(", ");
@@ -27166,8 +27186,9 @@ class ModelQueryBuilder {
27166
27186
  params.push(formatNow());
27167
27187
  }
27168
27188
  let sql2 = `UPDATE ${this._definition.table} SET ${sets}${timestampsEnabled(this._definition) ? ", updated_at = ?" : ""}`;
27169
- if (this._wheres.length > 0) {
27170
- sql2 += ` WHERE ${this.buildWhereClauses(params)}`;
27189
+ const whereBody = this.composeWhere(params);
27190
+ if (whereBody) {
27191
+ sql2 += ` WHERE ${whereBody}`;
27171
27192
  }
27172
27193
  return (await exec.run(sql2, params)).changes;
27173
27194
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bun-query-builder",
3
3
  "type": "module",
4
- "version": "0.2.45",
4
+ "version": "0.2.46",
5
5
  "description": "A simple yet performant query builder for TypeScript. Built with Bun.",
6
6
  "author": "Chris Breuer <chris@stacksjs.org>",
7
7
  "license": "MIT",