bun-query-builder 0.2.31 → 0.2.33

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
@@ -12641,6 +12641,61 @@ function renderInPredicate(column, values, negated, placeholders) {
12641
12641
  return negated ? TRUE_PREDICATE : FALSE_PREDICATE;
12642
12642
  return `${column} ${negated ? "NOT IN" : "IN"} (${placeholders})`;
12643
12643
  }
12644
+ function toDialectPlaceholders(sql, dialect) {
12645
+ if (dialect !== "postgres")
12646
+ return sql;
12647
+ let out = "";
12648
+ let index = 0;
12649
+ let position = 0;
12650
+ while (position < sql.length) {
12651
+ const char = sql[position];
12652
+ if (char === "'") {
12653
+ const end = closingQuote(sql, position, "'");
12654
+ out += sql.slice(position, end);
12655
+ position = end;
12656
+ continue;
12657
+ }
12658
+ if (char === '"') {
12659
+ const end = closingQuote(sql, position, '"');
12660
+ out += sql.slice(position, end);
12661
+ position = end;
12662
+ continue;
12663
+ }
12664
+ if (char === "$") {
12665
+ const tag = sql.slice(position).match(/^\$[A-Z_a-z]\w*\$|^\$\$/)?.[0];
12666
+ if (tag) {
12667
+ const close = sql.indexOf(tag, position + tag.length);
12668
+ const end = close === -1 ? sql.length : close + tag.length;
12669
+ out += sql.slice(position, end);
12670
+ position = end;
12671
+ continue;
12672
+ }
12673
+ }
12674
+ if (char === "?") {
12675
+ index++;
12676
+ out += `$${index}`;
12677
+ position++;
12678
+ continue;
12679
+ }
12680
+ out += char;
12681
+ position++;
12682
+ }
12683
+ return out;
12684
+ }
12685
+ function closingQuote(sql, start, quote) {
12686
+ let position = start + 1;
12687
+ while (position < sql.length) {
12688
+ if (sql[position] === quote) {
12689
+ if (sql[position + 1] === quote) {
12690
+ position += 2;
12691
+ continue;
12692
+ }
12693
+ return position + 1;
12694
+ }
12695
+ position++;
12696
+ }
12697
+ return sql.length;
12698
+ }
12644
12699
  var FALSE_PREDICATE = "1 = 0", TRUE_PREDICATE = "1 = 1";
12645
12700
 
12646
12701
  // src/client.ts
@@ -28919,11 +28974,12 @@ function detectTypeFromValidationRule(rule) {
28919
28974
  if (ruleObj.name) {
28920
28975
  const name = String(ruleObj.name).toLowerCase();
28921
28976
  switch (name) {
28922
- case "string":
28923
- case "text": {
28977
+ case "string": {
28924
28978
  const maxValue = extractMaxFromRules(ruleObj);
28925
28979
  return maxValue && maxValue > MAX_BOUNDED_VARCHAR_LENGTH ? "text" : "string";
28926
28980
  }
28981
+ case "text":
28982
+ return "text";
28927
28983
  case "integer":
28928
28984
  case "int":
28929
28985
  return "integer";
@@ -32133,7 +32189,7 @@ function getPrefix() {
32133
32189
  }
32134
32190
  var prefix = getPrefix();
32135
32191
  // package.json
32136
- var version2 = "0.2.31";
32192
+ var version2 = "0.2.33";
32137
32193
 
32138
32194
  // bin/cli.ts
32139
32195
  init_actions();
package/dist/index.d.ts CHANGED
@@ -50,3 +50,4 @@ export * from './types';
50
50
  export { type ModelDefinition, defineModel } from './model';
51
51
  // Explicit re-exports for model registry functions
52
52
  export { getModel, getAllModels, getModelRegistry, hasModel, clearModelRegistry, registerModel } from './model';
53
+ export { toDialectPlaceholders } from './sql-fragments';
@@ -1,3 +1,4 @@
1
+ import type { SupportedDialect } from './types';
1
2
  /**
2
3
  * Render a WHERE body (without the `WHERE` keyword) from an ordered term list.
3
4
  *
@@ -32,6 +33,25 @@ export declare function renderWhereTerms(terms: readonly WhereTerm[]): string;
32
33
  * failure as the `IN ()` bug itself.
33
34
  */
34
35
  export declare function renderInPredicate(column: string, values: any[], negated: boolean, placeholders: string): string;
36
+ /**
37
+ * Rewrite `?` placeholders into the numbered form Postgres requires.
38
+ *
39
+ * Raw SQL written with `?` runs on SQLite and MySQL and is a syntax error on
40
+ * Postgres, which reports it as a problem with whatever token follows: a WHERE
41
+ * built as `project_id = ? AND …` is rejected with `syntax error at or near
42
+ * "AND"`, which sends the reader looking at the wrong part of the query. An
43
+ * application that developed against SQLite therefore finds out at the point
44
+ * its raw queries first meet Postgres, and the error does not name the cause.
45
+ *
46
+ * Only unquoted placeholders are rewritten. A `?` inside a string literal, a
47
+ * quoted identifier, or a dollar-quoted block is data and is left exactly as
48
+ * written — the naive global replace is the reason this needs to be a function
49
+ * with tests rather than one line at a call site.
50
+ *
51
+ * Dialects that already take `?` get the string back unchanged, so this is safe
52
+ * to apply unconditionally.
53
+ */
54
+ export declare function toDialectPlaceholders(sql: string, dialect: SupportedDialect): string;
35
55
  /**
36
56
  * Constant predicates, for the cases where a collection is empty.
37
57
  *
package/dist/src/index.js CHANGED
@@ -12640,6 +12640,61 @@ function renderInPredicate(column, values, negated, placeholders) {
12640
12640
  return negated ? TRUE_PREDICATE : FALSE_PREDICATE;
12641
12641
  return `${column} ${negated ? "NOT IN" : "IN"} (${placeholders})`;
12642
12642
  }
12643
+ function toDialectPlaceholders(sql, dialect) {
12644
+ if (dialect !== "postgres")
12645
+ return sql;
12646
+ let out = "";
12647
+ let index = 0;
12648
+ let position = 0;
12649
+ while (position < sql.length) {
12650
+ const char = sql[position];
12651
+ if (char === "'") {
12652
+ const end = closingQuote(sql, position, "'");
12653
+ out += sql.slice(position, end);
12654
+ position = end;
12655
+ continue;
12656
+ }
12657
+ if (char === '"') {
12658
+ const end = closingQuote(sql, position, '"');
12659
+ out += sql.slice(position, end);
12660
+ position = end;
12661
+ continue;
12662
+ }
12663
+ if (char === "$") {
12664
+ const tag = sql.slice(position).match(/^\$[A-Z_a-z]\w*\$|^\$\$/)?.[0];
12665
+ if (tag) {
12666
+ const close = sql.indexOf(tag, position + tag.length);
12667
+ const end = close === -1 ? sql.length : close + tag.length;
12668
+ out += sql.slice(position, end);
12669
+ position = end;
12670
+ continue;
12671
+ }
12672
+ }
12673
+ if (char === "?") {
12674
+ index++;
12675
+ out += `$${index}`;
12676
+ position++;
12677
+ continue;
12678
+ }
12679
+ out += char;
12680
+ position++;
12681
+ }
12682
+ return out;
12683
+ }
12684
+ function closingQuote(sql, start, quote) {
12685
+ let position = start + 1;
12686
+ while (position < sql.length) {
12687
+ if (sql[position] === quote) {
12688
+ if (sql[position + 1] === quote) {
12689
+ position += 2;
12690
+ continue;
12691
+ }
12692
+ return position + 1;
12693
+ }
12694
+ position++;
12695
+ }
12696
+ return sql.length;
12697
+ }
12643
12698
  var FALSE_PREDICATE = "1 = 0", TRUE_PREDICATE = "1 = 1";
12644
12699
 
12645
12700
  // src/client.ts
@@ -28918,11 +28973,12 @@ function detectTypeFromValidationRule(rule) {
28918
28973
  if (ruleObj.name) {
28919
28974
  const name = String(ruleObj.name).toLowerCase();
28920
28975
  switch (name) {
28921
- case "string":
28922
- case "text": {
28976
+ case "string": {
28923
28977
  const maxValue = extractMaxFromRules(ruleObj);
28924
28978
  return maxValue && maxValue > MAX_BOUNDED_VARCHAR_LENGTH ? "text" : "string";
28925
28979
  }
28980
+ case "text":
28981
+ return "text";
28926
28982
  case "integer":
28927
28983
  case "int":
28928
28984
  return "integer";
@@ -30068,6 +30124,7 @@ export {
30068
30124
  toTableName,
30069
30125
  toModelInstance,
30070
30126
  toDynamoDBItem,
30127
+ toDialectPlaceholders,
30071
30128
  tinker,
30072
30129
  tableNameFor,
30073
30130
  tableInfo,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bun-query-builder",
3
3
  "type": "module",
4
- "version": "0.2.31",
4
+ "version": "0.2.33",
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",