bun-query-builder 0.1.32 → 0.1.34

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
@@ -11664,8 +11664,9 @@ async function getConfig() {
11664
11664
  alias: "qb",
11665
11665
  defaultConfig: defaultConfig4
11666
11666
  });
11667
+ setConfig(_config);
11667
11668
  }
11668
- return _config;
11669
+ return config5;
11669
11670
  }
11670
11671
  function setConfig(userConfig) {
11671
11672
  if (userConfig.dialect && _lastConfiguredDialect && userConfig.dialect !== _lastConfiguredDialect) {
@@ -12835,7 +12836,7 @@ function createQueryBuilder(state) {
12835
12836
  };
12836
12837
  const addWhereText = (prefix, clause) => {
12837
12838
  const hasWhere = SQL_PATTERNS.WHERE.test(text);
12838
- const p = hasWhere ? prefix : "WHERE";
12839
+ const p = !hasWhere ? "WHERE" : prefix === "WHERE" ? "AND" : prefix;
12839
12840
  text = `${text} ${p} ${clause}`;
12840
12841
  };
12841
12842
  const appendSetOp = (op, other) => {
@@ -13993,87 +13994,71 @@ function createQueryBuilder(state) {
13993
13994
  return this;
13994
13995
  },
13995
13996
  whereLike(column, pattern, caseSensitive = false) {
13996
- const expr = caseSensitive ? sql`${sql(String(column))} LIKE ${pattern}` : sql`LOWER(${sql(String(column))}) LIKE LOWER(${pattern})`;
13997
- built = sql`${ensureBuilt()} WHERE ${expr}`;
13998
13997
  const ph = getPlaceholder(whereParams.length + 1);
13999
13998
  addWhereText("WHERE", `${caseSensitive ? String(column) : `LOWER(${String(column)})`} LIKE ${caseSensitive ? ph : `LOWER(${ph})`}`);
14000
13999
  whereParams.push(pattern);
14000
+ built = null;
14001
14001
  return this;
14002
14002
  },
14003
14003
  whereILike(column, pattern) {
14004
14004
  const ph = getPlaceholder(whereParams.length + 1);
14005
- if (config5.dialect === "postgres") {
14006
- built = sql`${ensureBuilt()} WHERE ${sql(String(column))} ILIKE ${pattern}`;
14005
+ if (config5.dialect === "postgres")
14007
14006
  addWhereText("WHERE", `${String(column)} ILIKE ${ph}`);
14008
- } else {
14009
- const expr = sql`LOWER(${sql(String(column))}) LIKE LOWER(${pattern})`;
14010
- built = sql`${ensureBuilt()} WHERE ${expr}`;
14007
+ else
14011
14008
  addWhereText("WHERE", `LOWER(${String(column)}) LIKE LOWER(${ph})`);
14012
- }
14013
14009
  whereParams.push(pattern);
14010
+ built = null;
14014
14011
  return this;
14015
14012
  },
14016
14013
  orWhereLike(column, pattern, caseSensitive = false) {
14017
- const expr = caseSensitive ? sql`${sql(String(column))} LIKE ${pattern}` : sql`LOWER(${sql(String(column))}) LIKE LOWER(${pattern})`;
14018
- built = sql`${ensureBuilt()} OR ${expr}`;
14019
14014
  const ph = getPlaceholder(whereParams.length + 1);
14020
14015
  addWhereText("OR", `${caseSensitive ? String(column) : `LOWER(${String(column)})`} LIKE ${caseSensitive ? ph : `LOWER(${ph})`}`);
14021
14016
  whereParams.push(pattern);
14017
+ built = null;
14022
14018
  return this;
14023
14019
  },
14024
14020
  orWhereILike(column, pattern) {
14025
14021
  const ph = getPlaceholder(whereParams.length + 1);
14026
- if (config5.dialect === "postgres") {
14027
- built = sql`${ensureBuilt()} OR ${sql(String(column))} ILIKE ${pattern}`;
14022
+ if (config5.dialect === "postgres")
14028
14023
  addWhereText("OR", `${String(column)} ILIKE ${ph}`);
14029
- } else {
14030
- const expr = sql`LOWER(${sql(String(column))}) LIKE LOWER(${pattern})`;
14031
- built = sql`${ensureBuilt()} OR ${expr}`;
14024
+ else
14032
14025
  addWhereText("OR", `LOWER(${String(column)}) LIKE LOWER(${ph})`);
14033
- }
14034
14026
  whereParams.push(pattern);
14027
+ built = null;
14035
14028
  return this;
14036
14029
  },
14037
14030
  whereNotLike(column, pattern, caseSensitive = false) {
14038
- const expr = caseSensitive ? sql`${sql(String(column))} NOT LIKE ${pattern}` : sql`LOWER(${sql(String(column))}) NOT LIKE LOWER(${pattern})`;
14039
- built = sql`${ensureBuilt()} WHERE ${expr}`;
14040
14031
  const ph = getPlaceholder(whereParams.length + 1);
14041
14032
  addWhereText("WHERE", `${caseSensitive ? String(column) : `LOWER(${String(column)})`} NOT LIKE ${caseSensitive ? ph : `LOWER(${ph})`}`);
14042
14033
  whereParams.push(pattern);
14034
+ built = null;
14043
14035
  return this;
14044
14036
  },
14045
14037
  whereNotILike(column, pattern) {
14046
14038
  const ph = getPlaceholder(whereParams.length + 1);
14047
- if (config5.dialect === "postgres") {
14048
- built = sql`${ensureBuilt()} WHERE ${sql(String(column))} NOT ILIKE ${pattern}`;
14039
+ if (config5.dialect === "postgres")
14049
14040
  addWhereText("WHERE", `${String(column)} NOT ILIKE ${ph}`);
14050
- } else {
14051
- const expr = sql`LOWER(${sql(String(column))}) NOT LIKE LOWER(${pattern})`;
14052
- built = sql`${ensureBuilt()} WHERE ${expr}`;
14041
+ else
14053
14042
  addWhereText("WHERE", `LOWER(${String(column)}) NOT LIKE LOWER(${ph})`);
14054
- }
14055
14043
  whereParams.push(pattern);
14044
+ built = null;
14056
14045
  return this;
14057
14046
  },
14058
14047
  orWhereNotLike(column, pattern, caseSensitive = false) {
14059
- const expr = caseSensitive ? sql`${sql(String(column))} NOT LIKE ${pattern}` : sql`LOWER(${sql(String(column))}) NOT LIKE LOWER(${pattern})`;
14060
- built = sql`${ensureBuilt()} OR ${expr}`;
14061
14048
  const ph = getPlaceholder(whereParams.length + 1);
14062
14049
  addWhereText("OR", `${caseSensitive ? String(column) : `LOWER(${String(column)})`} NOT LIKE ${caseSensitive ? ph : `LOWER(${ph})`}`);
14063
14050
  whereParams.push(pattern);
14051
+ built = null;
14064
14052
  return this;
14065
14053
  },
14066
14054
  orWhereNotILike(column, pattern) {
14067
14055
  const ph = getPlaceholder(whereParams.length + 1);
14068
- if (config5.dialect === "postgres") {
14069
- built = sql`${ensureBuilt()} OR ${sql(String(column))} NOT ILIKE ${pattern}`;
14056
+ if (config5.dialect === "postgres")
14070
14057
  addWhereText("OR", `${String(column)} NOT ILIKE ${ph}`);
14071
- } else {
14072
- const expr = sql`LOWER(${sql(String(column))}) NOT LIKE LOWER(${pattern})`;
14073
- built = sql`${ensureBuilt()} OR ${expr}`;
14058
+ else
14074
14059
  addWhereText("OR", `LOWER(${String(column)}) NOT LIKE LOWER(${ph})`);
14075
- }
14076
14060
  whereParams.push(pattern);
14061
+ built = null;
14077
14062
  return this;
14078
14063
  },
14079
14064
  whereAny(cols, op, value) {
@@ -14673,19 +14658,21 @@ function createQueryBuilder(state) {
14673
14658
  q = sql`${q} ORDER BY ${sql(String(column))} ${direction === "asc" ? sql`ASC` : sql`DESC`} LIMIT ${perPage + 1}`;
14674
14659
  }
14675
14660
  const rows = await runWithHooks(q, "select", { signal: abortSignal, timeoutMs });
14676
- const next = rows.length > perPage ? Array.isArray(column) ? column.map((c) => rows[perPage]?.[c]) : rows[perPage]?.[column] : null;
14661
+ const hasMore = rows.length > perPage;
14677
14662
  const data = rows.slice(0, perPage);
14663
+ const lastRow = data[data.length - 1];
14664
+ const next = hasMore && lastRow ? Array.isArray(column) ? column.map((c) => lastRow[c]) : lastRow[column] : null;
14678
14665
  const prevCursor = data.length ? Array.isArray(column) ? column.map((c) => data[0]?.[c]) : data[0]?.[column] : null;
14679
14666
  return { data, meta: { perPage, nextCursor: next ?? null, prevCursor } };
14680
14667
  },
14681
14668
  async chunk(size, handler) {
14682
14669
  let page = 1;
14683
14670
  while (true) {
14684
- const { data } = await this.paginate(size, page);
14671
+ const { data, meta: meta2 } = await this.paginate(size, page);
14685
14672
  if (data.length === 0)
14686
14673
  break;
14687
14674
  await handler(data);
14688
- if (data.length < size)
14675
+ if (page >= meta2.lastPage)
14689
14676
  break;
14690
14677
  page += 1;
14691
14678
  }
@@ -30125,7 +30112,7 @@ function getPrefix() {
30125
30112
  }
30126
30113
  var prefix = getPrefix();
30127
30114
  // package.json
30128
- var version2 = "0.1.32";
30115
+ var version2 = "0.1.34";
30129
30116
 
30130
30117
  // bin/cli.ts
30131
30118
  init_actions();
package/dist/config.d.ts CHANGED
@@ -10,6 +10,26 @@ export declare function getPlaceholder(index: number): string;
10
10
  * MySQL/SQLite: ?, ?, ?
11
11
  */
12
12
  export declare function getPlaceholders(count: number, startIndex?: number): string;
13
+ /**
14
+ * Load the query-builder config from a config file (`query-builder.config.ts`,
15
+ * `.config/query-builder.ts`, etc.) and environment variables via bunfig, then
16
+ * MERGE it into the live, process-wide `config` singleton so every reader
17
+ * (dialect dispatch, placeholders, soft-deletes, the model layer, …) sees it.
18
+ *
19
+ * Call this once at application boot if you keep configuration in a file:
20
+ *
21
+ * ```ts
22
+ * import { getConfig } from 'bun-query-builder'
23
+ * await getConfig() // applies query-builder.config.ts + env to the runtime
24
+ * ```
25
+ *
26
+ * It is intentionally explicit/async: the builder otherwise runs purely off the
27
+ * synchronous `config` singleton (defaults + any `setConfig`), which keeps
28
+ * `bun --compile` and test behavior deterministic — auto-loading a file in the
29
+ * background would make early queries race the load. Previously this wrote the
30
+ * loaded config to a private `_config` variable that nothing else read, so a
31
+ * config file silently never took effect; it now routes through `setConfig`.
32
+ */
13
33
  export declare function getConfig(): Promise<QueryBuilderConfig>;
14
34
  export declare function setConfig(userConfig: Partial<QueryBuilderConfig>): void;
15
35
  export declare const defaultConfig: QueryBuilderConfig;
package/dist/src/index.js CHANGED
@@ -11664,8 +11664,9 @@ async function getConfig() {
11664
11664
  alias: "qb",
11665
11665
  defaultConfig: defaultConfig4
11666
11666
  });
11667
+ setConfig(_config);
11667
11668
  }
11668
- return _config;
11669
+ return config5;
11669
11670
  }
11670
11671
  function setConfig(userConfig) {
11671
11672
  if (userConfig.dialect && _lastConfiguredDialect && userConfig.dialect !== _lastConfiguredDialect) {
@@ -12835,7 +12836,7 @@ function createQueryBuilder(state) {
12835
12836
  };
12836
12837
  const addWhereText = (prefix, clause) => {
12837
12838
  const hasWhere = SQL_PATTERNS.WHERE.test(text);
12838
- const p = hasWhere ? prefix : "WHERE";
12839
+ const p = !hasWhere ? "WHERE" : prefix === "WHERE" ? "AND" : prefix;
12839
12840
  text = `${text} ${p} ${clause}`;
12840
12841
  };
12841
12842
  const appendSetOp = (op, other) => {
@@ -13993,87 +13994,71 @@ function createQueryBuilder(state) {
13993
13994
  return this;
13994
13995
  },
13995
13996
  whereLike(column, pattern, caseSensitive = false) {
13996
- const expr = caseSensitive ? sql`${sql(String(column))} LIKE ${pattern}` : sql`LOWER(${sql(String(column))}) LIKE LOWER(${pattern})`;
13997
- built = sql`${ensureBuilt()} WHERE ${expr}`;
13998
13997
  const ph = getPlaceholder(whereParams.length + 1);
13999
13998
  addWhereText("WHERE", `${caseSensitive ? String(column) : `LOWER(${String(column)})`} LIKE ${caseSensitive ? ph : `LOWER(${ph})`}`);
14000
13999
  whereParams.push(pattern);
14000
+ built = null;
14001
14001
  return this;
14002
14002
  },
14003
14003
  whereILike(column, pattern) {
14004
14004
  const ph = getPlaceholder(whereParams.length + 1);
14005
- if (config5.dialect === "postgres") {
14006
- built = sql`${ensureBuilt()} WHERE ${sql(String(column))} ILIKE ${pattern}`;
14005
+ if (config5.dialect === "postgres")
14007
14006
  addWhereText("WHERE", `${String(column)} ILIKE ${ph}`);
14008
- } else {
14009
- const expr = sql`LOWER(${sql(String(column))}) LIKE LOWER(${pattern})`;
14010
- built = sql`${ensureBuilt()} WHERE ${expr}`;
14007
+ else
14011
14008
  addWhereText("WHERE", `LOWER(${String(column)}) LIKE LOWER(${ph})`);
14012
- }
14013
14009
  whereParams.push(pattern);
14010
+ built = null;
14014
14011
  return this;
14015
14012
  },
14016
14013
  orWhereLike(column, pattern, caseSensitive = false) {
14017
- const expr = caseSensitive ? sql`${sql(String(column))} LIKE ${pattern}` : sql`LOWER(${sql(String(column))}) LIKE LOWER(${pattern})`;
14018
- built = sql`${ensureBuilt()} OR ${expr}`;
14019
14014
  const ph = getPlaceholder(whereParams.length + 1);
14020
14015
  addWhereText("OR", `${caseSensitive ? String(column) : `LOWER(${String(column)})`} LIKE ${caseSensitive ? ph : `LOWER(${ph})`}`);
14021
14016
  whereParams.push(pattern);
14017
+ built = null;
14022
14018
  return this;
14023
14019
  },
14024
14020
  orWhereILike(column, pattern) {
14025
14021
  const ph = getPlaceholder(whereParams.length + 1);
14026
- if (config5.dialect === "postgres") {
14027
- built = sql`${ensureBuilt()} OR ${sql(String(column))} ILIKE ${pattern}`;
14022
+ if (config5.dialect === "postgres")
14028
14023
  addWhereText("OR", `${String(column)} ILIKE ${ph}`);
14029
- } else {
14030
- const expr = sql`LOWER(${sql(String(column))}) LIKE LOWER(${pattern})`;
14031
- built = sql`${ensureBuilt()} OR ${expr}`;
14024
+ else
14032
14025
  addWhereText("OR", `LOWER(${String(column)}) LIKE LOWER(${ph})`);
14033
- }
14034
14026
  whereParams.push(pattern);
14027
+ built = null;
14035
14028
  return this;
14036
14029
  },
14037
14030
  whereNotLike(column, pattern, caseSensitive = false) {
14038
- const expr = caseSensitive ? sql`${sql(String(column))} NOT LIKE ${pattern}` : sql`LOWER(${sql(String(column))}) NOT LIKE LOWER(${pattern})`;
14039
- built = sql`${ensureBuilt()} WHERE ${expr}`;
14040
14031
  const ph = getPlaceholder(whereParams.length + 1);
14041
14032
  addWhereText("WHERE", `${caseSensitive ? String(column) : `LOWER(${String(column)})`} NOT LIKE ${caseSensitive ? ph : `LOWER(${ph})`}`);
14042
14033
  whereParams.push(pattern);
14034
+ built = null;
14043
14035
  return this;
14044
14036
  },
14045
14037
  whereNotILike(column, pattern) {
14046
14038
  const ph = getPlaceholder(whereParams.length + 1);
14047
- if (config5.dialect === "postgres") {
14048
- built = sql`${ensureBuilt()} WHERE ${sql(String(column))} NOT ILIKE ${pattern}`;
14039
+ if (config5.dialect === "postgres")
14049
14040
  addWhereText("WHERE", `${String(column)} NOT ILIKE ${ph}`);
14050
- } else {
14051
- const expr = sql`LOWER(${sql(String(column))}) NOT LIKE LOWER(${pattern})`;
14052
- built = sql`${ensureBuilt()} WHERE ${expr}`;
14041
+ else
14053
14042
  addWhereText("WHERE", `LOWER(${String(column)}) NOT LIKE LOWER(${ph})`);
14054
- }
14055
14043
  whereParams.push(pattern);
14044
+ built = null;
14056
14045
  return this;
14057
14046
  },
14058
14047
  orWhereNotLike(column, pattern, caseSensitive = false) {
14059
- const expr = caseSensitive ? sql`${sql(String(column))} NOT LIKE ${pattern}` : sql`LOWER(${sql(String(column))}) NOT LIKE LOWER(${pattern})`;
14060
- built = sql`${ensureBuilt()} OR ${expr}`;
14061
14048
  const ph = getPlaceholder(whereParams.length + 1);
14062
14049
  addWhereText("OR", `${caseSensitive ? String(column) : `LOWER(${String(column)})`} NOT LIKE ${caseSensitive ? ph : `LOWER(${ph})`}`);
14063
14050
  whereParams.push(pattern);
14051
+ built = null;
14064
14052
  return this;
14065
14053
  },
14066
14054
  orWhereNotILike(column, pattern) {
14067
14055
  const ph = getPlaceholder(whereParams.length + 1);
14068
- if (config5.dialect === "postgres") {
14069
- built = sql`${ensureBuilt()} OR ${sql(String(column))} NOT ILIKE ${pattern}`;
14056
+ if (config5.dialect === "postgres")
14070
14057
  addWhereText("OR", `${String(column)} NOT ILIKE ${ph}`);
14071
- } else {
14072
- const expr = sql`LOWER(${sql(String(column))}) NOT LIKE LOWER(${pattern})`;
14073
- built = sql`${ensureBuilt()} OR ${expr}`;
14058
+ else
14074
14059
  addWhereText("OR", `LOWER(${String(column)}) NOT LIKE LOWER(${ph})`);
14075
- }
14076
14060
  whereParams.push(pattern);
14061
+ built = null;
14077
14062
  return this;
14078
14063
  },
14079
14064
  whereAny(cols, op, value) {
@@ -14673,19 +14658,21 @@ function createQueryBuilder(state) {
14673
14658
  q = sql`${q} ORDER BY ${sql(String(column))} ${direction === "asc" ? sql`ASC` : sql`DESC`} LIMIT ${perPage + 1}`;
14674
14659
  }
14675
14660
  const rows = await runWithHooks(q, "select", { signal: abortSignal, timeoutMs });
14676
- const next = rows.length > perPage ? Array.isArray(column) ? column.map((c) => rows[perPage]?.[c]) : rows[perPage]?.[column] : null;
14661
+ const hasMore = rows.length > perPage;
14677
14662
  const data = rows.slice(0, perPage);
14663
+ const lastRow = data[data.length - 1];
14664
+ const next = hasMore && lastRow ? Array.isArray(column) ? column.map((c) => lastRow[c]) : lastRow[column] : null;
14678
14665
  const prevCursor = data.length ? Array.isArray(column) ? column.map((c) => data[0]?.[c]) : data[0]?.[column] : null;
14679
14666
  return { data, meta: { perPage, nextCursor: next ?? null, prevCursor } };
14680
14667
  },
14681
14668
  async chunk(size, handler) {
14682
14669
  let page = 1;
14683
14670
  while (true) {
14684
- const { data } = await this.paginate(size, page);
14671
+ const { data, meta: meta2 } = await this.paginate(size, page);
14685
14672
  if (data.length === 0)
14686
14673
  break;
14687
14674
  await handler(data);
14688
- if (data.length < size)
14675
+ if (page >= meta2.lastPage)
14689
14676
  break;
14690
14677
  page += 1;
14691
14678
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bun-query-builder",
3
3
  "type": "module",
4
- "version": "0.1.32",
4
+ "version": "0.1.34",
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",