bun-query-builder 0.1.50 → 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 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 isNullable = true;
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
- for (const t2 of plan.tables) {
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 plan.tables) {
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
  `);
@@ -30902,7 +30952,7 @@ function getPrefix() {
30902
30952
  }
30903
30953
  var prefix = getPrefix();
30904
30954
  // package.json
30905
- var version2 = "0.1.50";
30955
+ var version2 = "0.1.51";
30906
30956
 
30907
30957
  // bin/cli.ts
30908
30958
  init_actions();
package/dist/schema.d.ts CHANGED
@@ -52,6 +52,8 @@ export declare interface ForeignKeyConfig {
52
52
  * ```
53
53
  */
54
54
  export declare interface Attribute {
55
+ required?: boolean
56
+ nullable?: boolean
55
57
  default?: string | number | boolean | Date
56
58
  unique?: boolean
57
59
  order?: number
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 isNullable = true;
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
- for (const t2 of plan.tables) {
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 plan.tables) {
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
  `);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bun-query-builder",
3
3
  "type": "module",
4
- "version": "0.1.50",
4
+ "version": "0.1.51",
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",