bun-query-builder 0.2.46 → 0.2.48

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
@@ -15600,6 +15600,10 @@ function createQueryBuilder(state) {
15600
15600
  const newBase = fromIdx !== -1 ? `${baseText2.slice(0, fromIdx)}, ${frag}${baseText2.slice(fromIdx)}` : `${baseText2}, ${frag}`;
15601
15601
  return makeSub(newBase, terms, params, tail);
15602
15602
  },
15603
+ $call(callback) {
15604
+ const next = callback(this);
15605
+ return next ?? this;
15606
+ },
15603
15607
  where(expr, op, value) {
15604
15608
  return withTerm("AND", expr, op, value);
15605
15609
  },
@@ -18631,21 +18635,48 @@ class MySQLDriver {
18631
18635
  createEnumType(_enumTypeName, _values) {
18632
18636
  return "";
18633
18637
  }
18638
+ foreignKeyClauses(table) {
18639
+ return table.columns.filter((column) => column.references).map((column) => {
18640
+ const reference = column.references;
18641
+ const name = `${table.table}_${column.name}_fk`;
18642
+ let clause = `CONSTRAINT ${this.quoteIdentifier(name)} FOREIGN KEY (${this.quoteIdentifier(column.name)}) REFERENCES ${this.quoteIdentifier(reference.table)}(${this.quoteIdentifier(reference.column)})`;
18643
+ if (reference.onDelete)
18644
+ clause += ` ON DELETE ${reference.onDelete.toUpperCase()}`;
18645
+ if (reference.onUpdate)
18646
+ clause += ` ON UPDATE ${reference.onUpdate.toUpperCase()}`;
18647
+ return clause;
18648
+ });
18649
+ }
18634
18650
  createTable(table) {
18635
- const columns = table.columns.map((c) => this.renderColumn(c)).join(`,
18636
- `);
18651
+ const parts = [
18652
+ ...table.columns.map((c) => this.renderColumn(c)),
18653
+ ...this.foreignKeyClauses(table)
18654
+ ];
18637
18655
  return `CREATE TABLE IF NOT EXISTS ${this.quoteIdentifier(table.table)} (
18638
- ${columns}
18639
- );`;
18656
+ ${parts.join(`,
18657
+ `)}
18658
+ ) DEFAULT CHARSET=utf8mb4;`;
18659
+ }
18660
+ static KEY_PREFIX_CHARACTERS = 255;
18661
+ needsKeyPrefix(column) {
18662
+ const rendered = this.getColumnType(column).toLowerCase();
18663
+ if (rendered.includes("text") || rendered.includes("blob") || rendered === "json")
18664
+ return true;
18665
+ const width = rendered.match(/^(?:var)?char\((\d+)\)$/)?.[1];
18666
+ return width !== undefined && Number(width) >= 768;
18640
18667
  }
18641
- createIndex(tableName, index) {
18668
+ createIndex(tableName, index, columns) {
18642
18669
  if (index.where) {
18643
18670
  throw new Error(`[migrations] Partial indexes (CompositeIndex.where) are not supported on MySQL. Index '${index.name}' on table '${tableName}' uses WHERE clause: ${index.where}`);
18644
18671
  }
18645
18672
  const kind = index.type === "unique" ? "UNIQUE " : "";
18646
18673
  const idxName = qualifiedIndexName(tableName, index.name);
18647
- const columns = index.columns.map((c) => this.quoteIdentifier(c)).join(", ");
18648
- return `CREATE ${kind}INDEX ${this.quoteIdentifier(idxName)} ON ${this.quoteIdentifier(tableName)} (${columns});`;
18674
+ const keyParts = index.columns.map((name) => {
18675
+ const column = columns?.find((one) => one.name === name);
18676
+ const quoted = this.quoteIdentifier(name);
18677
+ return column && this.needsKeyPrefix(column) ? `${quoted}(${MySQLDriver.KEY_PREFIX_CHARACTERS})` : quoted;
18678
+ }).join(", ");
18679
+ return `CREATE ${kind}INDEX ${this.quoteIdentifier(idxName)} ON ${this.quoteIdentifier(tableName)} (${keyParts});`;
18649
18680
  }
18650
18681
  addForeignKey(tableName, columnName, refTable, refColumn, onDelete, onUpdate, existingConstraintNames = []) {
18651
18682
  const fkName = `${tableName}_${columnName}_fk`;
@@ -18757,14 +18788,6 @@ ${sql};` : `${sql};`;
18757
18788
  if (defaultValue) {
18758
18789
  parts.push(defaultValue);
18759
18790
  }
18760
- if (column.references) {
18761
- let reference = `REFERENCES ${this.quoteIdentifier(column.references.table)}(${this.quoteIdentifier(column.references.column)})`;
18762
- if (column.references.onDelete)
18763
- reference += ` ON DELETE ${column.references.onDelete.toUpperCase()}`;
18764
- if (column.references.onUpdate)
18765
- reference += ` ON UPDATE ${column.references.onUpdate.toUpperCase()}`;
18766
- parts.push(reference);
18767
- }
18768
18791
  return parts.join(" ");
18769
18792
  }
18770
18793
  }
@@ -19024,11 +19047,11 @@ var init_singlestore = __esm(() => {
19024
19047
  addForeignKey() {
19025
19048
  return "";
19026
19049
  }
19027
- createIndex(tableName, index) {
19050
+ createIndex(tableName, index, columns) {
19028
19051
  if (index.where) {
19029
19052
  throw new Error(`[migrations] Partial indexes (CompositeIndex.where) are not supported on SingleStore. Index '${index.name}' on table '${tableName}' uses WHERE clause: ${index.where}`);
19030
19053
  }
19031
- return super.createIndex(tableName, index);
19054
+ return super.createIndex(tableName, index, columns);
19032
19055
  }
19033
19056
  };
19034
19057
  });
@@ -19052,6 +19075,9 @@ var init_vitess = __esm(() => {
19052
19075
  const { references: _references, ...withoutReference } = column;
19053
19076
  return super.renderColumn(withoutReference);
19054
19077
  }
19078
+ foreignKeyClauses(table) {
19079
+ return this.sharded ? [] : super.foreignKeyClauses(table);
19080
+ }
19055
19081
  createTable(table) {
19056
19082
  return super.createTable(table);
19057
19083
  }
@@ -29714,7 +29740,7 @@ function generateSql(plan, opts = {}) {
29714
29740
  const createTableStatement = driver.createTable(t2);
29715
29741
  tableStatements.push(createTableStatement);
29716
29742
  for (const idx of t2.indexes)
29717
- tableStatements.push(driver.createIndex(t2.table, idx));
29743
+ tableStatements.push(driver.createIndex(t2.table, idx, t2.columns));
29718
29744
  if (tableIndex === tables.length - 1) {
29719
29745
  for (const deferredTable of tables) {
29720
29746
  for (const column of deferredTable.columns) {
@@ -30049,7 +30075,7 @@ function generateDiffOperations(previous, next, opts = {}) {
30049
30075
  const createTableStatement = driver.createTable(t2);
30050
30076
  tableStatements.push(createTableStatement);
30051
30077
  for (const idx of t2.indexes) {
30052
- const createIndexStatement = driver.createIndex(t2.table, idx);
30078
+ const createIndexStatement = driver.createIndex(t2.table, idx, t2.columns);
30053
30079
  tableStatements.push(createIndexStatement);
30054
30080
  operations.push({ kind: "create_index", table: t2.table, column: idx.name, destructive: false, sql: createIndexStatement });
30055
30081
  }
@@ -30253,7 +30279,7 @@ function generateDiffOperations(previous, next, opts = {}) {
30253
30279
  for (const key of Object.keys(currIdx)) {
30254
30280
  if (!prevIdx[key]) {
30255
30281
  const idx = currIdx[key];
30256
- const createIndexStatement = driver.createIndex(curr.table, idx);
30282
+ const createIndexStatement = driver.createIndex(curr.table, idx, curr.columns);
30257
30283
  tableChanges.push(createIndexStatement);
30258
30284
  chunks.push(createIndexStatement);
30259
30285
  operations.push({ kind: "create_index", table: curr.table, column: idx.name, destructive: false, sql: createIndexStatement });
@@ -32544,7 +32570,7 @@ function getPrefix() {
32544
32570
  }
32545
32571
  var prefix = getPrefix();
32546
32572
  // package.json
32547
- var version2 = "0.2.46";
32573
+ var version2 = "0.2.48";
32548
32574
 
32549
32575
  // bin/cli.ts
32550
32576
  init_actions();
@@ -3,7 +3,7 @@ import type { DateBucketGrain } from './postgres';
3
3
  export declare interface DialectDriver {
4
4
  createEnumType: (enumTypeName: string, values: string[]) => string
5
5
  createTable: (table: TablePlan) => string
6
- createIndex: (tableName: string, index: IndexPlan) => string
6
+ createIndex: (tableName: string, index: IndexPlan, columns?: readonly ColumnPlan[]) => string
7
7
  addForeignKey: (tableName: string, columnName: string, refTable: string, refColumn: string, onDelete?: string, onUpdate?: string, existingConstraintNames?: string[]) => string
8
8
  addColumn: (tableName: string, column: ColumnPlan) => string
9
9
  modifyColumn: (tableName: string, column: ColumnPlan) => string
@@ -25,8 +25,11 @@ export declare class MySQLDriver implements DialectDriver {
25
25
  protected getAutoIncrementClause(column: ColumnPlan): string;
26
26
  protected getDefaultValue(column: ColumnPlan): string;
27
27
  createEnumType(_enumTypeName: string, _values: string[]): string;
28
+ protected foreignKeyClauses(table: TablePlan): string[];
28
29
  createTable(table: TablePlan): string;
29
- createIndex(tableName: string, index: IndexPlan): string;
30
+ protected static readonly KEY_PREFIX_CHARACTERS: 255;
31
+ protected needsKeyPrefix(column: ColumnPlan): boolean;
32
+ createIndex(tableName: string, index: IndexPlan, columns?: readonly ColumnPlan[]): string;
30
33
  addForeignKey(tableName: string, columnName: string, refTable: string, refColumn: string, onDelete?: string, onUpdate?: string, existingConstraintNames?: string[]): string;
31
34
  addColumn(tableName: string, column: ColumnPlan): string;
32
35
  modifyColumn(tableName: string, column: ColumnPlan): string;
@@ -9,7 +9,7 @@ export declare const DATE_BUCKET_FORMAT: 'YYYY-MM-DDTHH:MM:SS.000Z';
9
9
  export declare interface DialectDriver {
10
10
  createEnumType: (enumTypeName: string, values: string[]) => string
11
11
  createTable: (table: TablePlan) => string
12
- createIndex: (tableName: string, index: IndexPlan) => string
12
+ createIndex: (tableName: string, index: IndexPlan, columns?: readonly ColumnPlan[]) => string
13
13
  addForeignKey: (tableName: string, columnName: string, refTable: string, refColumn: string, onDelete?: string, onUpdate?: string, existingConstraintNames?: string[]) => string
14
14
  addColumn: (tableName: string, column: ColumnPlan) => string
15
15
  modifyColumn: (tableName: string, column: ColumnPlan) => string
@@ -1,5 +1,5 @@
1
1
  import { MySQLDriver } from './mysql';
2
- import type { IndexPlan, TablePlan } from '../migrations';
2
+ import type { ColumnPlan, IndexPlan, TablePlan } from '../migrations';
3
3
  /**
4
4
  * SingleStore DDL driver.
5
5
  *
@@ -24,5 +24,5 @@ import type { IndexPlan, TablePlan } from '../migrations';
24
24
  export declare class SingleStoreDriver extends MySQLDriver {
25
25
  createTable(table: TablePlan): string;
26
26
  addForeignKey(): string;
27
- createIndex(tableName: string, index: IndexPlan): string;
27
+ createIndex(tableName: string, index: IndexPlan, columns?: readonly ColumnPlan[]): string;
28
28
  }
@@ -4,7 +4,7 @@ import type { DateBucketGrain } from './postgres';
4
4
  export declare interface DialectDriver {
5
5
  createEnumType: (enumTypeName: string, values: string[]) => string
6
6
  createTable: (table: TablePlan) => string
7
- createIndex: (tableName: string, index: IndexPlan) => string
7
+ createIndex: (tableName: string, index: IndexPlan, columns?: readonly ColumnPlan[]) => string
8
8
  addForeignKey: (tableName: string, columnName: string, refTable: string, refColumn: string, onDelete?: string, onUpdate?: string) => string
9
9
  addColumn: (tableName: string, column: ColumnPlan) => string
10
10
  modifyColumn: (tableName: string, column: ColumnPlan) => string
@@ -35,6 +35,7 @@ export declare class VitessDriver extends MySQLDriver {
35
35
  constructor(sharded?: boolean);
36
36
  protected getAutoIncrementClause(column: ColumnPlan): string;
37
37
  protected renderColumn(column: ColumnPlan): string;
38
+ protected foreignKeyClauses(table: TablePlan): string[];
38
39
  createTable(table: TablePlan): string;
39
40
  addForeignKey(...args: Parameters<MySQLDriver['addForeignKey']>): string;
40
41
  createMigrationsTable(): string;
package/dist/src/index.js CHANGED
@@ -15599,6 +15599,10 @@ function createQueryBuilder(state) {
15599
15599
  const newBase = fromIdx !== -1 ? `${baseText2.slice(0, fromIdx)}, ${frag}${baseText2.slice(fromIdx)}` : `${baseText2}, ${frag}`;
15600
15600
  return makeSub(newBase, terms, params, tail);
15601
15601
  },
15602
+ $call(callback) {
15603
+ const next = callback(this);
15604
+ return next ?? this;
15605
+ },
15602
15606
  where(expr, op, value) {
15603
15607
  return withTerm("AND", expr, op, value);
15604
15608
  },
@@ -18630,21 +18634,48 @@ class MySQLDriver {
18630
18634
  createEnumType(_enumTypeName, _values) {
18631
18635
  return "";
18632
18636
  }
18637
+ foreignKeyClauses(table) {
18638
+ return table.columns.filter((column) => column.references).map((column) => {
18639
+ const reference = column.references;
18640
+ const name = `${table.table}_${column.name}_fk`;
18641
+ let clause = `CONSTRAINT ${this.quoteIdentifier(name)} FOREIGN KEY (${this.quoteIdentifier(column.name)}) REFERENCES ${this.quoteIdentifier(reference.table)}(${this.quoteIdentifier(reference.column)})`;
18642
+ if (reference.onDelete)
18643
+ clause += ` ON DELETE ${reference.onDelete.toUpperCase()}`;
18644
+ if (reference.onUpdate)
18645
+ clause += ` ON UPDATE ${reference.onUpdate.toUpperCase()}`;
18646
+ return clause;
18647
+ });
18648
+ }
18633
18649
  createTable(table) {
18634
- const columns = table.columns.map((c) => this.renderColumn(c)).join(`,
18635
- `);
18650
+ const parts = [
18651
+ ...table.columns.map((c) => this.renderColumn(c)),
18652
+ ...this.foreignKeyClauses(table)
18653
+ ];
18636
18654
  return `CREATE TABLE IF NOT EXISTS ${this.quoteIdentifier(table.table)} (
18637
- ${columns}
18638
- );`;
18655
+ ${parts.join(`,
18656
+ `)}
18657
+ ) DEFAULT CHARSET=utf8mb4;`;
18658
+ }
18659
+ static KEY_PREFIX_CHARACTERS = 255;
18660
+ needsKeyPrefix(column) {
18661
+ const rendered = this.getColumnType(column).toLowerCase();
18662
+ if (rendered.includes("text") || rendered.includes("blob") || rendered === "json")
18663
+ return true;
18664
+ const width = rendered.match(/^(?:var)?char\((\d+)\)$/)?.[1];
18665
+ return width !== undefined && Number(width) >= 768;
18639
18666
  }
18640
- createIndex(tableName, index) {
18667
+ createIndex(tableName, index, columns) {
18641
18668
  if (index.where) {
18642
18669
  throw new Error(`[migrations] Partial indexes (CompositeIndex.where) are not supported on MySQL. Index '${index.name}' on table '${tableName}' uses WHERE clause: ${index.where}`);
18643
18670
  }
18644
18671
  const kind = index.type === "unique" ? "UNIQUE " : "";
18645
18672
  const idxName = qualifiedIndexName(tableName, index.name);
18646
- const columns = index.columns.map((c) => this.quoteIdentifier(c)).join(", ");
18647
- return `CREATE ${kind}INDEX ${this.quoteIdentifier(idxName)} ON ${this.quoteIdentifier(tableName)} (${columns});`;
18673
+ const keyParts = index.columns.map((name) => {
18674
+ const column = columns?.find((one) => one.name === name);
18675
+ const quoted = this.quoteIdentifier(name);
18676
+ return column && this.needsKeyPrefix(column) ? `${quoted}(${MySQLDriver.KEY_PREFIX_CHARACTERS})` : quoted;
18677
+ }).join(", ");
18678
+ return `CREATE ${kind}INDEX ${this.quoteIdentifier(idxName)} ON ${this.quoteIdentifier(tableName)} (${keyParts});`;
18648
18679
  }
18649
18680
  addForeignKey(tableName, columnName, refTable, refColumn, onDelete, onUpdate, existingConstraintNames = []) {
18650
18681
  const fkName = `${tableName}_${columnName}_fk`;
@@ -18756,14 +18787,6 @@ ${sql};` : `${sql};`;
18756
18787
  if (defaultValue) {
18757
18788
  parts.push(defaultValue);
18758
18789
  }
18759
- if (column.references) {
18760
- let reference = `REFERENCES ${this.quoteIdentifier(column.references.table)}(${this.quoteIdentifier(column.references.column)})`;
18761
- if (column.references.onDelete)
18762
- reference += ` ON DELETE ${column.references.onDelete.toUpperCase()}`;
18763
- if (column.references.onUpdate)
18764
- reference += ` ON UPDATE ${column.references.onUpdate.toUpperCase()}`;
18765
- parts.push(reference);
18766
- }
18767
18790
  return parts.join(" ");
18768
18791
  }
18769
18792
  }
@@ -19023,11 +19046,11 @@ var init_singlestore = __esm(() => {
19023
19046
  addForeignKey() {
19024
19047
  return "";
19025
19048
  }
19026
- createIndex(tableName, index) {
19049
+ createIndex(tableName, index, columns) {
19027
19050
  if (index.where) {
19028
19051
  throw new Error(`[migrations] Partial indexes (CompositeIndex.where) are not supported on SingleStore. Index '${index.name}' on table '${tableName}' uses WHERE clause: ${index.where}`);
19029
19052
  }
19030
- return super.createIndex(tableName, index);
19053
+ return super.createIndex(tableName, index, columns);
19031
19054
  }
19032
19055
  };
19033
19056
  });
@@ -19051,6 +19074,9 @@ var init_vitess = __esm(() => {
19051
19074
  const { references: _references, ...withoutReference } = column;
19052
19075
  return super.renderColumn(withoutReference);
19053
19076
  }
19077
+ foreignKeyClauses(table) {
19078
+ return this.sharded ? [] : super.foreignKeyClauses(table);
19079
+ }
19054
19080
  createTable(table) {
19055
19081
  return super.createTable(table);
19056
19082
  }
@@ -29713,7 +29739,7 @@ function generateSql(plan, opts = {}) {
29713
29739
  const createTableStatement = driver.createTable(t2);
29714
29740
  tableStatements.push(createTableStatement);
29715
29741
  for (const idx of t2.indexes)
29716
- tableStatements.push(driver.createIndex(t2.table, idx));
29742
+ tableStatements.push(driver.createIndex(t2.table, idx, t2.columns));
29717
29743
  if (tableIndex === tables.length - 1) {
29718
29744
  for (const deferredTable of tables) {
29719
29745
  for (const column of deferredTable.columns) {
@@ -30048,7 +30074,7 @@ function generateDiffOperations(previous, next, opts = {}) {
30048
30074
  const createTableStatement = driver.createTable(t2);
30049
30075
  tableStatements.push(createTableStatement);
30050
30076
  for (const idx of t2.indexes) {
30051
- const createIndexStatement = driver.createIndex(t2.table, idx);
30077
+ const createIndexStatement = driver.createIndex(t2.table, idx, t2.columns);
30052
30078
  tableStatements.push(createIndexStatement);
30053
30079
  operations.push({ kind: "create_index", table: t2.table, column: idx.name, destructive: false, sql: createIndexStatement });
30054
30080
  }
@@ -30252,7 +30278,7 @@ function generateDiffOperations(previous, next, opts = {}) {
30252
30278
  for (const key of Object.keys(currIdx)) {
30253
30279
  if (!prevIdx[key]) {
30254
30280
  const idx = currIdx[key];
30255
- const createIndexStatement = driver.createIndex(curr.table, idx);
30281
+ const createIndexStatement = driver.createIndex(curr.table, idx, curr.columns);
30256
30282
  tableChanges.push(createIndexStatement);
30257
30283
  chunks.push(createIndexStatement);
30258
30284
  operations.push({ kind: "create_index", table: curr.table, column: idx.name, destructive: false, sql: createIndexStatement });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bun-query-builder",
3
3
  "type": "module",
4
- "version": "0.2.46",
4
+ "version": "0.2.48",
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",