bun-query-builder 0.2.48 → 0.2.49
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 +33 -10
- package/dist/drivers/mysql.d.ts +4 -1
- package/dist/src/index.js +32 -9
- package/package.json +1 -1
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
|
-
|
|
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
|
|
18665
|
-
const
|
|
18666
|
-
|
|
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
|
|
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 = []) {
|
|
@@ -32570,7 +32593,7 @@ function getPrefix() {
|
|
|
32570
32593
|
}
|
|
32571
32594
|
var prefix = getPrefix();
|
|
32572
32595
|
// package.json
|
|
32573
|
-
var version2 = "0.2.
|
|
32596
|
+
var version2 = "0.2.49";
|
|
32574
32597
|
|
|
32575
32598
|
// bin/cli.ts
|
|
32576
32599
|
init_actions();
|
package/dist/drivers/mysql.d.ts
CHANGED
|
@@ -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
|
|
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
|
-
|
|
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
|
|
18664
|
-
const
|
|
18665
|
-
|
|
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
|
|
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 = []) {
|
package/package.json
CHANGED