bun-query-builder 0.2.2 → 0.2.4

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
@@ -18389,12 +18389,14 @@ class PostgresDriver {
18389
18389
  const table = this.quoteIdentifier(tableName);
18390
18390
  const name = this.quoteIdentifier(column.name);
18391
18391
  const statements = [
18392
+ `ALTER TABLE ${table} ALTER COLUMN ${name} DROP DEFAULT;`,
18392
18393
  `ALTER TABLE ${table} ALTER COLUMN ${name} TYPE ${typeSql} USING ${name}::${typeSql};`
18393
18394
  ];
18394
18395
  if (!column.isPrimaryKey)
18395
18396
  statements.push(`ALTER TABLE ${table} ALTER COLUMN ${name} ${column.isNullable ? "DROP NOT NULL" : "SET NOT NULL"};`);
18396
18397
  const defaultValue = this.getDefaultValue(column);
18397
- statements.push(defaultValue ? `ALTER TABLE ${table} ALTER COLUMN ${name} SET ${defaultValue};` : `ALTER TABLE ${table} ALTER COLUMN ${name} DROP DEFAULT;`);
18398
+ if (defaultValue)
18399
+ statements.push(`ALTER TABLE ${table} ALTER COLUMN ${name} SET ${defaultValue};`);
18398
18400
  return statements.join(`
18399
18401
  `);
18400
18402
  }
@@ -28759,10 +28761,20 @@ function buildMigrationPlan2(models, options) {
28759
28761
  const pivotTable = cfg.table || [parentSingular, relatedSingular].sort().join("_");
28760
28762
  const fkParent = cfg.foreignKey || `${parentSingular}_id`;
28761
28763
  const fkRelated = cfg.relatedKey || `${relatedSingular}_id`;
28764
+ const pivotColumnNames = new Set([fkParent, fkRelated, ...Object.keys(pivotCfg.columns ?? {})]);
28765
+ const pivotPolymorphic = new Set;
28766
+ for (const column of pivotColumnNames) {
28767
+ if (!column.endsWith("_type"))
28768
+ continue;
28769
+ const idColumn = `${column.slice(0, -"_type".length)}_id`;
28770
+ if (pivotColumnNames.has(idColumn))
28771
+ pivotPolymorphic.add(idColumn);
28772
+ }
28773
+ const referenceTo = (column, table) => pivotPolymorphic.has(column) ? undefined : { table, column: "id" };
28762
28774
  const cols = [
28763
28775
  { name: "id", type: "bigint", isPrimaryKey: true, isUnique: false, isNullable: false, hasDefault: false },
28764
- { name: fkParent, type: "bigint", isPrimaryKey: false, isUnique: false, isNullable: false, hasDefault: false, references: { table: parentTable, column: "id" } },
28765
- { name: fkRelated, type: "bigint", isPrimaryKey: false, isUnique: false, isNullable: false, hasDefault: false, references: { table: relatedTable, column: "id" } }
28776
+ { name: fkParent, type: "bigint", isPrimaryKey: false, isUnique: false, isNullable: false, hasDefault: false, references: referenceTo(fkParent, parentTable) },
28777
+ { name: fkRelated, type: "bigint", isPrimaryKey: false, isUnique: false, isNullable: false, hasDefault: false, references: referenceTo(fkRelated, relatedTable) }
28766
28778
  ];
28767
28779
  if (pivotCfg.columns) {
28768
28780
  for (const [name, attr] of Object.entries(pivotCfg.columns)) {
@@ -31452,7 +31464,7 @@ function getPrefix() {
31452
31464
  }
31453
31465
  var prefix = getPrefix();
31454
31466
  // package.json
31455
- var version2 = "0.2.2";
31467
+ var version2 = "0.2.4";
31456
31468
 
31457
31469
  // bin/cli.ts
31458
31470
  init_actions();
package/dist/src/index.js CHANGED
@@ -18388,12 +18388,14 @@ class PostgresDriver {
18388
18388
  const table = this.quoteIdentifier(tableName);
18389
18389
  const name = this.quoteIdentifier(column.name);
18390
18390
  const statements = [
18391
+ `ALTER TABLE ${table} ALTER COLUMN ${name} DROP DEFAULT;`,
18391
18392
  `ALTER TABLE ${table} ALTER COLUMN ${name} TYPE ${typeSql} USING ${name}::${typeSql};`
18392
18393
  ];
18393
18394
  if (!column.isPrimaryKey)
18394
18395
  statements.push(`ALTER TABLE ${table} ALTER COLUMN ${name} ${column.isNullable ? "DROP NOT NULL" : "SET NOT NULL"};`);
18395
18396
  const defaultValue = this.getDefaultValue(column);
18396
- statements.push(defaultValue ? `ALTER TABLE ${table} ALTER COLUMN ${name} SET ${defaultValue};` : `ALTER TABLE ${table} ALTER COLUMN ${name} DROP DEFAULT;`);
18397
+ if (defaultValue)
18398
+ statements.push(`ALTER TABLE ${table} ALTER COLUMN ${name} SET ${defaultValue};`);
18397
18399
  return statements.join(`
18398
18400
  `);
18399
18401
  }
@@ -28758,10 +28760,20 @@ function buildMigrationPlan2(models, options) {
28758
28760
  const pivotTable = cfg.table || [parentSingular, relatedSingular].sort().join("_");
28759
28761
  const fkParent = cfg.foreignKey || `${parentSingular}_id`;
28760
28762
  const fkRelated = cfg.relatedKey || `${relatedSingular}_id`;
28763
+ const pivotColumnNames = new Set([fkParent, fkRelated, ...Object.keys(pivotCfg.columns ?? {})]);
28764
+ const pivotPolymorphic = new Set;
28765
+ for (const column of pivotColumnNames) {
28766
+ if (!column.endsWith("_type"))
28767
+ continue;
28768
+ const idColumn = `${column.slice(0, -"_type".length)}_id`;
28769
+ if (pivotColumnNames.has(idColumn))
28770
+ pivotPolymorphic.add(idColumn);
28771
+ }
28772
+ const referenceTo = (column, table) => pivotPolymorphic.has(column) ? undefined : { table, column: "id" };
28761
28773
  const cols = [
28762
28774
  { name: "id", type: "bigint", isPrimaryKey: true, isUnique: false, isNullable: false, hasDefault: false },
28763
- { name: fkParent, type: "bigint", isPrimaryKey: false, isUnique: false, isNullable: false, hasDefault: false, references: { table: parentTable, column: "id" } },
28764
- { name: fkRelated, type: "bigint", isPrimaryKey: false, isUnique: false, isNullable: false, hasDefault: false, references: { table: relatedTable, column: "id" } }
28775
+ { name: fkParent, type: "bigint", isPrimaryKey: false, isUnique: false, isNullable: false, hasDefault: false, references: referenceTo(fkParent, parentTable) },
28776
+ { name: fkRelated, type: "bigint", isPrimaryKey: false, isUnique: false, isNullable: false, hasDefault: false, references: referenceTo(fkRelated, relatedTable) }
28765
28777
  ];
28766
28778
  if (pivotCfg.columns) {
28767
28779
  for (const [name, attr] of Object.entries(pivotCfg.columns)) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bun-query-builder",
3
3
  "type": "module",
4
- "version": "0.2.2",
4
+ "version": "0.2.4",
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",