rake-db 2.33.10 → 2.33.11

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/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Adapter, Column as Column$1, ColumnSchemaConfig, ColumnsByType, ColumnsShape, DbDomainArg, DbResult, DbStructureDomainsMap, DefaultColumnTypes, DefaultPrivileges, DefaultSchemaConfig, EmptyObject, EnumColumn, MaybeArray, MaybePromise, NoPrimaryKeyOption, QueryLogObject, QueryLogOptions, QueryLogger, QuerySchema, RawSqlBase, RecordOptionalString, RecordString, SearchWeight, TableData, TableDataFn, TableDataItem, TableDataMethods, raw } from "pqb/internal";
1
+ import { Adapter, Column as Column$1, ColumnSchemaConfig, ColumnsByType, ColumnsShape, DbDomainArg, DbResult, DbStructureDomainsMap, DefaultColumnTypes, DefaultPrivileges, DefaultSchemaConfig, EmptyObject, EnumColumn, MaybeArray, MaybePromise, NoPrimaryKeyOption, NonUniqDataItem, QueryLogObject, QueryLogOptions, QueryLogger, QuerySchema, RawSqlBase, RecordOptionalString, RecordString, SearchWeight, TableData, TableDataFn, TableDataItem, TableDataMethods, raw } from "pqb/internal";
2
2
  import { Db } from "pqb";
3
3
  declare namespace DbStructure {
4
4
  interface TableNameAndSchemaName {
@@ -463,11 +463,26 @@ interface OneWayChange {
463
463
  to: RakeDbAst.ColumnChange;
464
464
  using?: RakeDbAst.ChangeTableItem.ChangeUsing;
465
465
  }
466
+ interface ColumnForeignKeyChangeInput {
467
+ columnForeignKey: TableData.ColumnReferences;
468
+ }
469
+ interface NoForeignKeyChangeInput {
470
+ noForeignKey: true;
471
+ }
472
+ type ChangeInput = Column$1 | OneWayChange | NonUniqDataItem | ColumnForeignKeyChangeInput | NoForeignKeyChangeInput;
466
473
  interface TableChangeMethods extends TableMethods, TableDataMethods<string> {
467
474
  name(name: string): TableChangeMethods;
468
475
  add: Add;
469
476
  drop: Add;
470
- change(from: Column$1 | OneWayChange, to: Column$1 | OneWayChange, using?: ChangeOptions): Change;
477
+ foreignKey<Shape>(columns: [string, ...string[]], fnOrTable: () => new () => {
478
+ columns: {
479
+ shape: Shape;
480
+ };
481
+ }, foreignColumns: [keyof Shape, ...(keyof Shape)[]], options?: TableData.References.Options): NonUniqDataItem;
482
+ foreignKey(columns: [string, ...string[]], fnOrTable: string, foreignColumns: [string, ...string[]], options?: TableData.References.Options): NonUniqDataItem;
483
+ foreignKey(fnOrTable: string, foreignColumn: string, options?: TableData.References.Options): ColumnForeignKeyChangeInput;
484
+ noForeignKey(): NoForeignKeyChangeInput;
485
+ change(from: ChangeInput, to: ChangeInput, using?: ChangeOptions): Change;
471
486
  default(value: unknown | RawSqlBase): OneWayChange;
472
487
  nullable(): OneWayChange;
473
488
  nonNullable(): OneWayChange;
package/dist/index.js CHANGED
@@ -544,6 +544,7 @@ const addOrDropChanges = [];
544
544
  function add(item, options) {
545
545
  (0, pqb_internal.consumeColumnName)();
546
546
  setName(this, item);
547
+ if (isNoForeignKeyChangeInput(item)) throw new Error("t.noForeignKey() is only supported in t.change(...)");
547
548
  if (item instanceof pqb_internal.Column) {
548
549
  const result = addOrDrop("add", item, options);
549
550
  if (result.type === "change") return result;
@@ -567,6 +568,7 @@ function add(item, options) {
567
568
  const drop = function(item, options) {
568
569
  (0, pqb_internal.consumeColumnName)();
569
570
  setName(this, item);
571
+ if (isNoForeignKeyChangeInput(item)) throw new Error("t.noForeignKey() is only supported in t.change(...)");
570
572
  if (item instanceof pqb_internal.Column) {
571
573
  const result = addOrDrop("drop", item, options);
572
574
  if (result.type === "change") return result;
@@ -610,6 +612,28 @@ const addOrDrop = (type, item, options) => {
610
612
  dropMode: options?.dropMode
611
613
  };
612
614
  };
615
+ const isColumnForeignKeyChangeInput = (item) => {
616
+ return "columnForeignKey" in item;
617
+ };
618
+ const isNoForeignKeyChangeInput = (item) => {
619
+ return "noForeignKey" in item;
620
+ };
621
+ const isCheckConstraintItem = (item) => {
622
+ return !!item.constraint?.check;
623
+ };
624
+ const changeInputToColumnChange = (item, opposite) => {
625
+ if (item instanceof pqb_internal.Column || "type" in item) return columnTypeToColumnChange(item);
626
+ if (isColumnForeignKeyChangeInput(item)) return { foreignKeys: [item.columnForeignKey] };
627
+ if (isNoForeignKeyChangeInput(item)) {
628
+ if (!isColumnForeignKeyChangeInput(opposite)) throw new Error("t.noForeignKey() in t.change(...) must be paired with t.foreignKey(...)");
629
+ return { foreignKeys: [] };
630
+ }
631
+ if (isCheckConstraintItem(item)) return { checks: [{
632
+ sql: item.constraint.check,
633
+ name: item.constraint.name
634
+ }] };
635
+ throw new Error("t.change(...) supports t.check(...) only for check constraints in this form");
636
+ };
613
637
  const columnTypeToColumnChange = (item, name) => {
614
638
  if (item instanceof pqb_internal.Column) {
615
639
  let column = item;
@@ -641,6 +665,18 @@ const setName = (self, item) => {
641
665
  else if (item instanceof pqb_internal.Column) item.data.name ??= name;
642
666
  else item.name ??= name;
643
667
  };
668
+ function foreignKey(...args) {
669
+ if (Array.isArray(args[0])) {
670
+ const [columns, fnOrTable, foreignColumns, options] = args;
671
+ return pqb_internal.tableDataMethods.foreignKey(columns, fnOrTable, foreignColumns, options);
672
+ }
673
+ const [fnOrTable, foreignColumn, options] = args;
674
+ return { columnForeignKey: {
675
+ fnOrTable,
676
+ foreignColumns: [foreignColumn],
677
+ options
678
+ } };
679
+ }
644
680
  const tableChangeMethods = {
645
681
  ...tableMethods,
646
682
  ...pqb_internal.tableDataMethods,
@@ -652,10 +688,14 @@ const tableChangeMethods = {
652
688
  },
653
689
  add,
654
690
  drop,
691
+ foreignKey,
692
+ noForeignKey() {
693
+ return { noForeignKey: true };
694
+ },
655
695
  change(from, to, using) {
656
696
  (0, pqb_internal.consumeColumnName)();
657
- const f = columnTypeToColumnChange(from);
658
- const t = columnTypeToColumnChange(to);
697
+ const f = changeInputToColumnChange(from, to);
698
+ const t = changeInputToColumnChange(to, from);
659
699
  setName(this, f);
660
700
  setName(this, t);
661
701
  return {