bun-query-builder 0.1.49 → 0.1.51
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 +64 -13
- package/dist/schema.d.ts +2 -0
- package/dist/src/index.js +70 -19
- package/package.json +1 -2
package/dist/bin/cli.js
CHANGED
|
@@ -18078,6 +18078,14 @@ class MySQLDriver {
|
|
|
18078
18078
|
if (defaultValue) {
|
|
18079
18079
|
parts.push(defaultValue);
|
|
18080
18080
|
}
|
|
18081
|
+
if (column.references) {
|
|
18082
|
+
let reference = `REFERENCES ${this.quoteIdentifier(column.references.table)}(${this.quoteIdentifier(column.references.column)})`;
|
|
18083
|
+
if (column.references.onDelete)
|
|
18084
|
+
reference += ` ON DELETE ${column.references.onDelete.toUpperCase()}`;
|
|
18085
|
+
if (column.references.onUpdate)
|
|
18086
|
+
reference += ` ON UPDATE ${column.references.onUpdate.toUpperCase()}`;
|
|
18087
|
+
parts.push(reference);
|
|
18088
|
+
}
|
|
18081
18089
|
return parts.join(" ");
|
|
18082
18090
|
}
|
|
18083
18091
|
}
|
|
@@ -18245,6 +18253,14 @@ class PostgresDriver {
|
|
|
18245
18253
|
if (defaultValue) {
|
|
18246
18254
|
parts.push(defaultValue);
|
|
18247
18255
|
}
|
|
18256
|
+
if (column.references) {
|
|
18257
|
+
let reference = `REFERENCES ${this.quoteIdentifier(column.references.table)}(${this.quoteIdentifier(column.references.column)})`;
|
|
18258
|
+
if (column.references.onDelete)
|
|
18259
|
+
reference += ` ON DELETE ${column.references.onDelete.toUpperCase()}`;
|
|
18260
|
+
if (column.references.onUpdate)
|
|
18261
|
+
reference += ` ON UPDATE ${column.references.onUpdate.toUpperCase()}`;
|
|
18262
|
+
parts.push(reference);
|
|
18263
|
+
}
|
|
18248
18264
|
return parts.join(" ");
|
|
18249
18265
|
}
|
|
18250
18266
|
}
|
|
@@ -28014,7 +28030,8 @@ function buildMigrationPlan2(models, options) {
|
|
|
28014
28030
|
for (const attrName of Object.keys(attrs)) {
|
|
28015
28031
|
const attr = attrs[attrName];
|
|
28016
28032
|
const columnName = snakeCase(attrName);
|
|
28017
|
-
const
|
|
28033
|
+
const validatorRequired = Boolean(attr.validation?.rule?.isRequired);
|
|
28034
|
+
const isNullable = attrName === primaryKey ? false : attr.nullable ?? !(attr.required ?? validatorRequired);
|
|
28018
28035
|
let inferred;
|
|
28019
28036
|
let enumValues;
|
|
28020
28037
|
const isPk = attrName === primaryKey;
|
|
@@ -28309,7 +28326,12 @@ function generateSql(plan, opts = {}) {
|
|
|
28309
28326
|
const emit = opts.dryRun ? () => false : createMigrationFile;
|
|
28310
28327
|
const statements = [];
|
|
28311
28328
|
const driver = getDialectDriver(plan.dialect);
|
|
28312
|
-
|
|
28329
|
+
const { tables, deferredForeignKeys } = orderTablesForCreate(plan.tables, plan.dialect);
|
|
28330
|
+
for (const originalTable of tables) {
|
|
28331
|
+
const t2 = {
|
|
28332
|
+
...originalTable,
|
|
28333
|
+
columns: originalTable.columns.map((column) => deferredForeignKeys.has(foreignKeyKey(originalTable.table, column.name)) ? { ...column, references: undefined } : column)
|
|
28334
|
+
};
|
|
28313
28335
|
const tableStatements = [];
|
|
28314
28336
|
const enumTypes = new Set;
|
|
28315
28337
|
for (const c2 of t2.columns) {
|
|
@@ -28327,15 +28349,17 @@ function generateSql(plan, opts = {}) {
|
|
|
28327
28349
|
}
|
|
28328
28350
|
const createTableStatement = driver.createTable(t2);
|
|
28329
28351
|
tableStatements.push(createTableStatement);
|
|
28352
|
+
for (const idx of t2.indexes)
|
|
28353
|
+
tableStatements.push(driver.createIndex(t2.table, idx));
|
|
28330
28354
|
statements.push(...tableStatements);
|
|
28331
28355
|
const combinedStatement = tableStatements.join(`
|
|
28332
28356
|
|
|
28333
28357
|
`);
|
|
28334
28358
|
emit(combinedStatement, `create-${t2.table}-table`);
|
|
28335
28359
|
}
|
|
28336
|
-
for (const t2 of
|
|
28360
|
+
for (const t2 of tables) {
|
|
28337
28361
|
for (const c2 of t2.columns) {
|
|
28338
|
-
if (c2.references) {
|
|
28362
|
+
if (c2.references && deferredForeignKeys.has(foreignKeyKey(t2.table, c2.name))) {
|
|
28339
28363
|
const alterTableStatement = driver.addForeignKey(t2.table, c2.name, c2.references.table, c2.references.column, c2.references.onDelete, c2.references.onUpdate);
|
|
28340
28364
|
if (!alterTableStatement)
|
|
28341
28365
|
continue;
|
|
@@ -28344,13 +28368,6 @@ function generateSql(plan, opts = {}) {
|
|
|
28344
28368
|
}
|
|
28345
28369
|
}
|
|
28346
28370
|
}
|
|
28347
|
-
for (const t2 of plan.tables) {
|
|
28348
|
-
for (const idx of t2.indexes) {
|
|
28349
|
-
const createIndexStatement = driver.createIndex(t2.table, idx);
|
|
28350
|
-
statements.push(createIndexStatement);
|
|
28351
|
-
emit(createIndexStatement, `create-${idx.name}-index-in-${t2.table}`);
|
|
28352
|
-
}
|
|
28353
|
-
}
|
|
28354
28371
|
const totalChanges = migrationsCreatedCount + migrationsUpdatedCount;
|
|
28355
28372
|
if (totalChanges === 0) {
|
|
28356
28373
|
info2("-- Nothing to migrate");
|
|
@@ -28364,6 +28381,39 @@ function generateSql(plan, opts = {}) {
|
|
|
28364
28381
|
}
|
|
28365
28382
|
return statements;
|
|
28366
28383
|
}
|
|
28384
|
+
function foreignKeyKey(table, column) {
|
|
28385
|
+
return `${table}\x00${column}`;
|
|
28386
|
+
}
|
|
28387
|
+
function orderTablesForCreate(input, dialect) {
|
|
28388
|
+
if (dialect === "sqlite" || input.length < 2)
|
|
28389
|
+
return { tables: input, deferredForeignKeys: new Set };
|
|
28390
|
+
const byName = new Map(input.map((table) => [table.table, table]));
|
|
28391
|
+
const dependencies = new Map;
|
|
28392
|
+
for (const table of input) {
|
|
28393
|
+
dependencies.set(table.table, new Set(table.columns.map((column) => column.references?.table).filter((target) => Boolean(target && target !== table.table && byName.has(target)))));
|
|
28394
|
+
}
|
|
28395
|
+
const ordered = [];
|
|
28396
|
+
const emitted = new Set;
|
|
28397
|
+
while (ordered.length < input.length) {
|
|
28398
|
+
const ready = input.find((table) => !emitted.has(table.table) && [...dependencies.get(table.table) ?? []].every((target) => emitted.has(target)));
|
|
28399
|
+
if (!ready)
|
|
28400
|
+
break;
|
|
28401
|
+
ordered.push(ready);
|
|
28402
|
+
emitted.add(ready.table);
|
|
28403
|
+
}
|
|
28404
|
+
const cycleTables = new Set(input.filter((table) => !emitted.has(table.table)).map((table) => table.table));
|
|
28405
|
+
const deferredForeignKeys = new Set;
|
|
28406
|
+
for (const table of input) {
|
|
28407
|
+
if (!cycleTables.has(table.table))
|
|
28408
|
+
continue;
|
|
28409
|
+
for (const column of table.columns) {
|
|
28410
|
+
if (column.references && cycleTables.has(column.references.table) && column.references.table !== table.table)
|
|
28411
|
+
deferredForeignKeys.add(foreignKeyKey(table.table, column.name));
|
|
28412
|
+
}
|
|
28413
|
+
}
|
|
28414
|
+
ordered.push(...input.filter((table) => cycleTables.has(table.table)));
|
|
28415
|
+
return { tables: ordered, deferredForeignKeys };
|
|
28416
|
+
}
|
|
28367
28417
|
function generateSqlString(plan) {
|
|
28368
28418
|
return generateSql(plan).join(`
|
|
28369
28419
|
`);
|
|
@@ -28842,9 +28892,10 @@ class Seeder {
|
|
|
28842
28892
|
function defineSeeder(seederClass) {
|
|
28843
28893
|
return seederClass;
|
|
28844
28894
|
}
|
|
28845
|
-
|
|
28846
28895
|
// src/index.ts
|
|
28847
28896
|
var init_src2 = __esm(() => {
|
|
28897
|
+
init_model2();
|
|
28898
|
+
init_model2();
|
|
28848
28899
|
init_actions();
|
|
28849
28900
|
init_browser();
|
|
28850
28901
|
init_client();
|
|
@@ -30901,7 +30952,7 @@ function getPrefix() {
|
|
|
30901
30952
|
}
|
|
30902
30953
|
var prefix = getPrefix();
|
|
30903
30954
|
// package.json
|
|
30904
|
-
var version2 = "0.1.
|
|
30955
|
+
var version2 = "0.1.51";
|
|
30905
30956
|
|
|
30906
30957
|
// bin/cli.ts
|
|
30907
30958
|
init_actions();
|
package/dist/schema.d.ts
CHANGED
package/dist/src/index.js
CHANGED
|
@@ -18077,6 +18077,14 @@ class MySQLDriver {
|
|
|
18077
18077
|
if (defaultValue) {
|
|
18078
18078
|
parts.push(defaultValue);
|
|
18079
18079
|
}
|
|
18080
|
+
if (column.references) {
|
|
18081
|
+
let reference = `REFERENCES ${this.quoteIdentifier(column.references.table)}(${this.quoteIdentifier(column.references.column)})`;
|
|
18082
|
+
if (column.references.onDelete)
|
|
18083
|
+
reference += ` ON DELETE ${column.references.onDelete.toUpperCase()}`;
|
|
18084
|
+
if (column.references.onUpdate)
|
|
18085
|
+
reference += ` ON UPDATE ${column.references.onUpdate.toUpperCase()}`;
|
|
18086
|
+
parts.push(reference);
|
|
18087
|
+
}
|
|
18080
18088
|
return parts.join(" ");
|
|
18081
18089
|
}
|
|
18082
18090
|
}
|
|
@@ -18244,6 +18252,14 @@ class PostgresDriver {
|
|
|
18244
18252
|
if (defaultValue) {
|
|
18245
18253
|
parts.push(defaultValue);
|
|
18246
18254
|
}
|
|
18255
|
+
if (column.references) {
|
|
18256
|
+
let reference = `REFERENCES ${this.quoteIdentifier(column.references.table)}(${this.quoteIdentifier(column.references.column)})`;
|
|
18257
|
+
if (column.references.onDelete)
|
|
18258
|
+
reference += ` ON DELETE ${column.references.onDelete.toUpperCase()}`;
|
|
18259
|
+
if (column.references.onUpdate)
|
|
18260
|
+
reference += ` ON UPDATE ${column.references.onUpdate.toUpperCase()}`;
|
|
18261
|
+
parts.push(reference);
|
|
18262
|
+
}
|
|
18247
18263
|
return parts.join(" ");
|
|
18248
18264
|
}
|
|
18249
18265
|
}
|
|
@@ -28013,7 +28029,8 @@ function buildMigrationPlan2(models, options) {
|
|
|
28013
28029
|
for (const attrName of Object.keys(attrs)) {
|
|
28014
28030
|
const attr = attrs[attrName];
|
|
28015
28031
|
const columnName = snakeCase(attrName);
|
|
28016
|
-
const
|
|
28032
|
+
const validatorRequired = Boolean(attr.validation?.rule?.isRequired);
|
|
28033
|
+
const isNullable = attrName === primaryKey ? false : attr.nullable ?? !(attr.required ?? validatorRequired);
|
|
28017
28034
|
let inferred;
|
|
28018
28035
|
let enumValues;
|
|
28019
28036
|
const isPk = attrName === primaryKey;
|
|
@@ -28308,7 +28325,12 @@ function generateSql(plan, opts = {}) {
|
|
|
28308
28325
|
const emit = opts.dryRun ? () => false : createMigrationFile;
|
|
28309
28326
|
const statements = [];
|
|
28310
28327
|
const driver = getDialectDriver(plan.dialect);
|
|
28311
|
-
|
|
28328
|
+
const { tables, deferredForeignKeys } = orderTablesForCreate(plan.tables, plan.dialect);
|
|
28329
|
+
for (const originalTable of tables) {
|
|
28330
|
+
const t2 = {
|
|
28331
|
+
...originalTable,
|
|
28332
|
+
columns: originalTable.columns.map((column) => deferredForeignKeys.has(foreignKeyKey(originalTable.table, column.name)) ? { ...column, references: undefined } : column)
|
|
28333
|
+
};
|
|
28312
28334
|
const tableStatements = [];
|
|
28313
28335
|
const enumTypes = new Set;
|
|
28314
28336
|
for (const c2 of t2.columns) {
|
|
@@ -28326,15 +28348,17 @@ function generateSql(plan, opts = {}) {
|
|
|
28326
28348
|
}
|
|
28327
28349
|
const createTableStatement = driver.createTable(t2);
|
|
28328
28350
|
tableStatements.push(createTableStatement);
|
|
28351
|
+
for (const idx of t2.indexes)
|
|
28352
|
+
tableStatements.push(driver.createIndex(t2.table, idx));
|
|
28329
28353
|
statements.push(...tableStatements);
|
|
28330
28354
|
const combinedStatement = tableStatements.join(`
|
|
28331
28355
|
|
|
28332
28356
|
`);
|
|
28333
28357
|
emit(combinedStatement, `create-${t2.table}-table`);
|
|
28334
28358
|
}
|
|
28335
|
-
for (const t2 of
|
|
28359
|
+
for (const t2 of tables) {
|
|
28336
28360
|
for (const c2 of t2.columns) {
|
|
28337
|
-
if (c2.references) {
|
|
28361
|
+
if (c2.references && deferredForeignKeys.has(foreignKeyKey(t2.table, c2.name))) {
|
|
28338
28362
|
const alterTableStatement = driver.addForeignKey(t2.table, c2.name, c2.references.table, c2.references.column, c2.references.onDelete, c2.references.onUpdate);
|
|
28339
28363
|
if (!alterTableStatement)
|
|
28340
28364
|
continue;
|
|
@@ -28343,13 +28367,6 @@ function generateSql(plan, opts = {}) {
|
|
|
28343
28367
|
}
|
|
28344
28368
|
}
|
|
28345
28369
|
}
|
|
28346
|
-
for (const t2 of plan.tables) {
|
|
28347
|
-
for (const idx of t2.indexes) {
|
|
28348
|
-
const createIndexStatement = driver.createIndex(t2.table, idx);
|
|
28349
|
-
statements.push(createIndexStatement);
|
|
28350
|
-
emit(createIndexStatement, `create-${idx.name}-index-in-${t2.table}`);
|
|
28351
|
-
}
|
|
28352
|
-
}
|
|
28353
28370
|
const totalChanges = migrationsCreatedCount + migrationsUpdatedCount;
|
|
28354
28371
|
if (totalChanges === 0) {
|
|
28355
28372
|
info2("-- Nothing to migrate");
|
|
@@ -28363,6 +28380,39 @@ function generateSql(plan, opts = {}) {
|
|
|
28363
28380
|
}
|
|
28364
28381
|
return statements;
|
|
28365
28382
|
}
|
|
28383
|
+
function foreignKeyKey(table, column) {
|
|
28384
|
+
return `${table}\x00${column}`;
|
|
28385
|
+
}
|
|
28386
|
+
function orderTablesForCreate(input, dialect) {
|
|
28387
|
+
if (dialect === "sqlite" || input.length < 2)
|
|
28388
|
+
return { tables: input, deferredForeignKeys: new Set };
|
|
28389
|
+
const byName = new Map(input.map((table) => [table.table, table]));
|
|
28390
|
+
const dependencies = new Map;
|
|
28391
|
+
for (const table of input) {
|
|
28392
|
+
dependencies.set(table.table, new Set(table.columns.map((column) => column.references?.table).filter((target) => Boolean(target && target !== table.table && byName.has(target)))));
|
|
28393
|
+
}
|
|
28394
|
+
const ordered = [];
|
|
28395
|
+
const emitted = new Set;
|
|
28396
|
+
while (ordered.length < input.length) {
|
|
28397
|
+
const ready = input.find((table) => !emitted.has(table.table) && [...dependencies.get(table.table) ?? []].every((target) => emitted.has(target)));
|
|
28398
|
+
if (!ready)
|
|
28399
|
+
break;
|
|
28400
|
+
ordered.push(ready);
|
|
28401
|
+
emitted.add(ready.table);
|
|
28402
|
+
}
|
|
28403
|
+
const cycleTables = new Set(input.filter((table) => !emitted.has(table.table)).map((table) => table.table));
|
|
28404
|
+
const deferredForeignKeys = new Set;
|
|
28405
|
+
for (const table of input) {
|
|
28406
|
+
if (!cycleTables.has(table.table))
|
|
28407
|
+
continue;
|
|
28408
|
+
for (const column of table.columns) {
|
|
28409
|
+
if (column.references && cycleTables.has(column.references.table) && column.references.table !== table.table)
|
|
28410
|
+
deferredForeignKeys.add(foreignKeyKey(table.table, column.name));
|
|
28411
|
+
}
|
|
28412
|
+
}
|
|
28413
|
+
ordered.push(...input.filter((table) => cycleTables.has(table.table)));
|
|
28414
|
+
return { tables: ordered, deferredForeignKeys };
|
|
28415
|
+
}
|
|
28366
28416
|
function generateSqlString(plan) {
|
|
28367
28417
|
return generateSql(plan).join(`
|
|
28368
28418
|
`);
|
|
@@ -28841,9 +28891,10 @@ class Seeder {
|
|
|
28841
28891
|
function defineSeeder(seederClass) {
|
|
28842
28892
|
return seederClass;
|
|
28843
28893
|
}
|
|
28844
|
-
|
|
28845
28894
|
// src/index.ts
|
|
28846
28895
|
var init_src2 = __esm(async () => {
|
|
28896
|
+
init_model2();
|
|
28897
|
+
init_model2();
|
|
28847
28898
|
init_actions();
|
|
28848
28899
|
init_browser();
|
|
28849
28900
|
init_client();
|
|
@@ -28889,7 +28940,7 @@ export {
|
|
|
28889
28940
|
resetDatabase,
|
|
28890
28941
|
resetConnection,
|
|
28891
28942
|
relationDiagram,
|
|
28892
|
-
|
|
28943
|
+
registerModel,
|
|
28893
28944
|
registerBrowserModels,
|
|
28894
28945
|
raw,
|
|
28895
28946
|
queryExplainAll,
|
|
@@ -28917,16 +28968,16 @@ export {
|
|
|
28917
28968
|
importData,
|
|
28918
28969
|
hashTableDefinition,
|
|
28919
28970
|
hashMigrationPlan,
|
|
28920
|
-
|
|
28971
|
+
hasModel,
|
|
28921
28972
|
getPlaceholders,
|
|
28922
28973
|
getPlaceholder,
|
|
28923
|
-
|
|
28924
|
-
|
|
28974
|
+
getModelRegistry,
|
|
28975
|
+
getModel,
|
|
28925
28976
|
getDialectDriver,
|
|
28926
28977
|
getDatabase,
|
|
28927
28978
|
getConfig,
|
|
28928
28979
|
getBrowserConfig,
|
|
28929
|
-
|
|
28980
|
+
getAllModels,
|
|
28930
28981
|
generateSqlString,
|
|
28931
28982
|
generateSql,
|
|
28932
28983
|
generateModelSource,
|
|
@@ -28950,7 +29001,7 @@ export {
|
|
|
28950
29001
|
dumpDatabase,
|
|
28951
29002
|
defineSeeder,
|
|
28952
29003
|
defineModels,
|
|
28953
|
-
|
|
29004
|
+
defineModel,
|
|
28954
29005
|
defaultConfig4 as defaultConfig,
|
|
28955
29006
|
dbWipe,
|
|
28956
29007
|
dbStats,
|
|
@@ -28977,7 +29028,7 @@ export {
|
|
|
28977
29028
|
configureBrowser,
|
|
28978
29029
|
config5 as config,
|
|
28979
29030
|
clearQueryCache,
|
|
28980
|
-
|
|
29031
|
+
clearModelRegistry,
|
|
28981
29032
|
checkSchema,
|
|
28982
29033
|
canonicalizeDefault,
|
|
28983
29034
|
canonicalStorageType,
|
package/package.json
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bun-query-builder",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"
|
|
5
|
-
"version": "0.1.49",
|
|
4
|
+
"version": "0.1.51",
|
|
6
5
|
"description": "A simple yet performant query builder for TypeScript. Built with Bun.",
|
|
7
6
|
"author": "Chris Breuer <chris@stacksjs.org>",
|
|
8
7
|
"license": "MIT",
|