bun-query-builder 0.2.48 → 0.2.50

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
@@ -18658,12 +18658,39 @@ class MySQLDriver {
18658
18658
  ) DEFAULT CHARSET=utf8mb4;`;
18659
18659
  }
18660
18660
  static KEY_PREFIX_CHARACTERS = 255;
18661
- needsKeyPrefix(column) {
18661
+ static MAX_KEY_BYTES = 3072;
18662
+ static BYTES_PER_CHARACTER = 4;
18663
+ keyPartWidth(column) {
18662
18664
  const rendered = this.getColumnType(column).toLowerCase();
18663
18665
  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;
18666
+ return { bytes: Number.POSITIVE_INFINITY, indexableWhole: false };
18667
+ const characters = rendered.match(/^(?:var)?char\((\d+)\)$/)?.[1];
18668
+ if (characters !== undefined)
18669
+ return { bytes: Number(characters) * MySQLDriver.BYTES_PER_CHARACTER, indexableWhole: true };
18670
+ return { bytes: 8, indexableWhole: true };
18671
+ }
18672
+ keyParts(tableName, index, columns) {
18673
+ const parts = index.columns.map((name) => {
18674
+ const column = columns?.find((one) => one.name === name);
18675
+ const width = column ? this.keyPartWidth(column) : { bytes: 0, indexableWhole: true };
18676
+ const prefixed = !width.indexableWhole;
18677
+ return {
18678
+ name,
18679
+ prefixed,
18680
+ bytes: prefixed ? MySQLDriver.KEY_PREFIX_CHARACTERS * MySQLDriver.BYTES_PER_CHARACTER : width.bytes,
18681
+ narrowable: !width.indexableWhole || width.bytes > MySQLDriver.KEY_PREFIX_CHARACTERS * MySQLDriver.BYTES_PER_CHARACTER
18682
+ };
18683
+ });
18684
+ const total = () => parts.reduce((sum, part) => sum + part.bytes, 0);
18685
+ while (total() > MySQLDriver.MAX_KEY_BYTES) {
18686
+ const widest = parts.filter((part) => part.narrowable && !part.prefixed).sort((a, b) => b.bytes - a.bytes)[0];
18687
+ if (!widest) {
18688
+ throw new Error(`[migrations] Index '${index.name}' on table '${tableName}' needs ${total()} bytes of key, over MySQL's ${MySQLDriver.MAX_KEY_BYTES}. ` + `Every column in it is already at its ${MySQLDriver.KEY_PREFIX_CHARACTERS}-character prefix, so shorten a column or drop one from the index.`);
18689
+ }
18690
+ widest.prefixed = true;
18691
+ widest.bytes = MySQLDriver.KEY_PREFIX_CHARACTERS * MySQLDriver.BYTES_PER_CHARACTER;
18692
+ }
18693
+ return parts.map((part) => part.prefixed ? `${this.quoteIdentifier(part.name)}(${MySQLDriver.KEY_PREFIX_CHARACTERS})` : this.quoteIdentifier(part.name));
18667
18694
  }
18668
18695
  createIndex(tableName, index, columns) {
18669
18696
  if (index.where) {
@@ -18671,11 +18698,7 @@ class MySQLDriver {
18671
18698
  }
18672
18699
  const kind = index.type === "unique" ? "UNIQUE " : "";
18673
18700
  const idxName = qualifiedIndexName(tableName, index.name);
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(", ");
18701
+ const keyParts = this.keyParts(tableName, index, columns).join(", ");
18679
18702
  return `CREATE ${kind}INDEX ${this.quoteIdentifier(idxName)} ON ${this.quoteIdentifier(tableName)} (${keyParts});`;
18680
18703
  }
18681
18704
  addForeignKey(tableName, columnName, refTable, refColumn, onDelete, onUpdate, existingConstraintNames = []) {
@@ -30394,6 +30417,8 @@ function satisfies(declared, actual, dialect) {
30394
30417
  if ((declared === "date" || declared === "json") && actual === "text")
30395
30418
  return true;
30396
30419
  }
30420
+ if (isMysqlLike(dialect) && declared === "boolean" && actual === "integer")
30421
+ return true;
30397
30422
  if (declared === "varchar" && actual === "text")
30398
30423
  return true;
30399
30424
  if (declared === "integer" && actual === "fractional")
@@ -32570,7 +32595,7 @@ function getPrefix() {
32570
32595
  }
32571
32596
  var prefix = getPrefix();
32572
32597
  // package.json
32573
- var version2 = "0.2.48";
32598
+ var version2 = "0.2.50";
32574
32599
 
32575
32600
  // bin/cli.ts
32576
32601
  init_actions();
@@ -28,7 +28,10 @@ export declare class MySQLDriver implements DialectDriver {
28
28
  protected foreignKeyClauses(table: TablePlan): string[];
29
29
  createTable(table: TablePlan): string;
30
30
  protected static readonly KEY_PREFIX_CHARACTERS: 255;
31
- protected needsKeyPrefix(column: ColumnPlan): boolean;
31
+ protected static readonly MAX_KEY_BYTES: 3072;
32
+ protected static readonly BYTES_PER_CHARACTER: 4;
33
+ protected keyPartWidth(column: ColumnPlan): { bytes: number, indexableWhole: boolean };
34
+ protected keyParts(tableName: string, index: IndexPlan, columns?: readonly ColumnPlan[]): string[];
32
35
  createIndex(tableName: string, index: IndexPlan, columns?: readonly ColumnPlan[]): string;
33
36
  addForeignKey(tableName: string, columnName: string, refTable: string, refColumn: string, onDelete?: string, onUpdate?: string, existingConstraintNames?: string[]): string;
34
37
  addColumn(tableName: string, column: ColumnPlan): string;
package/dist/src/index.js CHANGED
@@ -18657,12 +18657,39 @@ class MySQLDriver {
18657
18657
  ) DEFAULT CHARSET=utf8mb4;`;
18658
18658
  }
18659
18659
  static KEY_PREFIX_CHARACTERS = 255;
18660
- needsKeyPrefix(column) {
18660
+ static MAX_KEY_BYTES = 3072;
18661
+ static BYTES_PER_CHARACTER = 4;
18662
+ keyPartWidth(column) {
18661
18663
  const rendered = this.getColumnType(column).toLowerCase();
18662
18664
  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;
18665
+ return { bytes: Number.POSITIVE_INFINITY, indexableWhole: false };
18666
+ const characters = rendered.match(/^(?:var)?char\((\d+)\)$/)?.[1];
18667
+ if (characters !== undefined)
18668
+ return { bytes: Number(characters) * MySQLDriver.BYTES_PER_CHARACTER, indexableWhole: true };
18669
+ return { bytes: 8, indexableWhole: true };
18670
+ }
18671
+ keyParts(tableName, index, columns) {
18672
+ const parts = index.columns.map((name) => {
18673
+ const column = columns?.find((one) => one.name === name);
18674
+ const width = column ? this.keyPartWidth(column) : { bytes: 0, indexableWhole: true };
18675
+ const prefixed = !width.indexableWhole;
18676
+ return {
18677
+ name,
18678
+ prefixed,
18679
+ bytes: prefixed ? MySQLDriver.KEY_PREFIX_CHARACTERS * MySQLDriver.BYTES_PER_CHARACTER : width.bytes,
18680
+ narrowable: !width.indexableWhole || width.bytes > MySQLDriver.KEY_PREFIX_CHARACTERS * MySQLDriver.BYTES_PER_CHARACTER
18681
+ };
18682
+ });
18683
+ const total = () => parts.reduce((sum, part) => sum + part.bytes, 0);
18684
+ while (total() > MySQLDriver.MAX_KEY_BYTES) {
18685
+ const widest = parts.filter((part) => part.narrowable && !part.prefixed).sort((a, b) => b.bytes - a.bytes)[0];
18686
+ if (!widest) {
18687
+ throw new Error(`[migrations] Index '${index.name}' on table '${tableName}' needs ${total()} bytes of key, over MySQL's ${MySQLDriver.MAX_KEY_BYTES}. ` + `Every column in it is already at its ${MySQLDriver.KEY_PREFIX_CHARACTERS}-character prefix, so shorten a column or drop one from the index.`);
18688
+ }
18689
+ widest.prefixed = true;
18690
+ widest.bytes = MySQLDriver.KEY_PREFIX_CHARACTERS * MySQLDriver.BYTES_PER_CHARACTER;
18691
+ }
18692
+ return parts.map((part) => part.prefixed ? `${this.quoteIdentifier(part.name)}(${MySQLDriver.KEY_PREFIX_CHARACTERS})` : this.quoteIdentifier(part.name));
18666
18693
  }
18667
18694
  createIndex(tableName, index, columns) {
18668
18695
  if (index.where) {
@@ -18670,11 +18697,7 @@ class MySQLDriver {
18670
18697
  }
18671
18698
  const kind = index.type === "unique" ? "UNIQUE " : "";
18672
18699
  const idxName = qualifiedIndexName(tableName, index.name);
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(", ");
18700
+ const keyParts = this.keyParts(tableName, index, columns).join(", ");
18678
18701
  return `CREATE ${kind}INDEX ${this.quoteIdentifier(idxName)} ON ${this.quoteIdentifier(tableName)} (${keyParts});`;
18679
18702
  }
18680
18703
  addForeignKey(tableName, columnName, refTable, refColumn, onDelete, onUpdate, existingConstraintNames = []) {
@@ -30393,6 +30416,8 @@ function satisfies(declared, actual, dialect) {
30393
30416
  if ((declared === "date" || declared === "json") && actual === "text")
30394
30417
  return true;
30395
30418
  }
30419
+ if (isMysqlLike(dialect) && declared === "boolean" && actual === "integer")
30420
+ return true;
30396
30421
  if (declared === "varchar" && actual === "text")
30397
30422
  return true;
30398
30423
  if (declared === "integer" && actual === "fractional")
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bun-query-builder",
3
3
  "type": "module",
4
- "version": "0.2.48",
4
+ "version": "0.2.50",
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",