rake-db 2.36.18 → 2.37.0

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
@@ -634,10 +634,15 @@ interface TableChangeMethods extends TableMethods, TableDataMethods<string> {
634
634
  unique(options?: TableData.Index.UniqueColumnArg): ColumnIndexChangeInput;
635
635
  exclude(columns: TableData.Exclude.ColumnOrExpressionOptions[], options?: TableData.Exclude.Options): NonUniqDataItem;
636
636
  exclude(with_: string, options?: TableData.Exclude.ColumnArg): ColumnExcludeChangeInput;
637
- foreignKey<Shape>(columns: [string, ...string[]], fnOrTable: () => new () => {
638
- columns: {
637
+ foreignKey<Shape>(columns: [string, ...string[]], fnOrTable: () => Column$1.ForeignKey.TableParam & {
638
+ columns?: {
639
639
  shape: Shape;
640
640
  };
641
+ instance?: () => {
642
+ columns: {
643
+ shape: Shape;
644
+ };
645
+ };
641
646
  }, foreignColumns: [keyof Shape, ...(keyof Shape)[]], options?: TableData.References.Options): NonUniqDataItem;
642
647
  foreignKey(columns: [string, ...string[]], fnOrTable: string, foreignColumns: [string, ...string[]], options?: TableData.References.Options): NonUniqDataItem;
643
648
  foreignKey(fnOrTable: string, foreignColumn: string, options?: TableData.References.Options): ColumnForeignKeyChangeInput;
@@ -1786,6 +1791,7 @@ interface MigrateConfigBase extends QueryLogOptions {
1786
1791
  snakeCase?: boolean;
1787
1792
  language?: string;
1788
1793
  noPrimaryKey?: NoPrimaryKeyOption;
1794
+ defineTable?: RakeDbDefineTable<unknown>;
1789
1795
  baseTable?: RakeDbBaseTable<unknown>;
1790
1796
  }
1791
1797
  interface MigrateConfigFileBased extends MigrateConfigBase {
@@ -1881,6 +1887,7 @@ interface MigrationsSet {
1881
1887
  type SearchPath = (() => string) | string;
1882
1888
  interface RakeDbCliConfigInputBase<SchemaConfig extends ColumnSchemaConfig, CT = DefaultColumnTypes<DefaultSchemaConfig>> extends QueryLogOptions {
1883
1889
  columnTypes?: CT | ((t: DefaultColumnTypes<DefaultSchemaConfig>) => CT);
1890
+ defineTable?: RakeDbDefineTable<CT>;
1884
1891
  baseTable?: RakeDbBaseTable<CT>;
1885
1892
  schemaConfig?: SchemaConfig;
1886
1893
  basePath?: string;
@@ -2007,6 +2014,7 @@ interface RakeDbConfig<ColumnTypes = unknown> extends QueryLogOptions {
2007
2014
  renameMigrations?: RakeDbRenameMigrationsInput;
2008
2015
  language?: string;
2009
2016
  noPrimaryKey?: NoPrimaryKeyOption;
2017
+ defineTable?: RakeDbDefineTable<unknown>;
2010
2018
  baseTable?: RakeDbBaseTable<unknown>;
2011
2019
  commands: RakeDbCommands;
2012
2020
  }
@@ -2066,6 +2074,58 @@ interface RakeDbBaseTable<CT> {
2066
2074
  language?: string;
2067
2075
  };
2068
2076
  }
2077
+ interface RakeDbDefineTable<CT> {
2078
+ /**
2079
+ * Export name configured for this table factory.
2080
+ */
2081
+ exportAs: string;
2082
+ /**
2083
+ * Returns the source file path for imports generated by rake-db.
2084
+ */
2085
+ getFilePath(): string;
2086
+ /**
2087
+ * Column types configured for this table factory.
2088
+ */
2089
+ types: CT;
2090
+ /**
2091
+ * Custom timestamp SQL configured for this table factory.
2092
+ */
2093
+ nowSQL?: string;
2094
+ /**
2095
+ * Whether this table factory maps columns and table names to snake case.
2096
+ */
2097
+ snakeCase?: boolean;
2098
+ /**
2099
+ * Default full text search language configured for this table factory.
2100
+ */
2101
+ language?: string;
2102
+ }
2103
+ interface RakeDbTableFactoryConfig<CT> {
2104
+ /**
2105
+ * Export name configured for this table factory.
2106
+ */
2107
+ exportAs: string;
2108
+ /**
2109
+ * Returns the source file path for imports generated by rake-db.
2110
+ */
2111
+ getFilePath(): string;
2112
+ /**
2113
+ * Column types configured for this table factory.
2114
+ */
2115
+ columnTypes: CT;
2116
+ /**
2117
+ * Custom timestamp SQL configured for this table factory.
2118
+ */
2119
+ nowSQL?: string;
2120
+ /**
2121
+ * Whether this table factory maps columns and table names to snake case.
2122
+ */
2123
+ snakeCase?: boolean;
2124
+ /**
2125
+ * Default full text search language configured for this table factory.
2126
+ */
2127
+ language?: string;
2128
+ }
2069
2129
  interface ModuleExportsRecord {
2070
2130
  [fileName: string]: () => Promise<unknown>;
2071
2131
  }
@@ -2257,5 +2317,19 @@ declare const createTable: (db: DbParam, sql: string) => Promise<"done" | "alrea
2257
2317
  * ```
2258
2318
  */
2259
2319
  declare const dropTable: (db: DbParam, sql: string) => Promise<"done" | "already">;
2320
+ interface RakeDbTableFactoryConfigInput<CT> {
2321
+ /**
2322
+ * Function-style table factory metadata.
2323
+ */
2324
+ defineTable?: RakeDbDefineTable<CT>;
2325
+ /**
2326
+ * Class-style base table metadata.
2327
+ */
2328
+ baseTable?: RakeDbBaseTable<CT>;
2329
+ }
2330
+ declare const getTableFactoryConfig: <CT>({
2331
+ baseTable,
2332
+ defineTable
2333
+ }: RakeDbTableFactoryConfigInput<CT>) => RakeDbTableFactoryConfig<CT> | undefined;
2260
2334
  declare class RakeDbError extends Error {}
2261
- export { type ChangeCallback, type DbMigration, type DbStructure, type IntrospectedStructure, type MigrateConfig, type MigrateFn, type MigrationChangeFn, type RakeDbAst, type RakeDbChangeFnConfig, type RakeDbCliConfigInput, type RakeDbCliResult, type RakeDbConfig, RakeDbError, type RakeDbFn, type SilentQueries, type StructureToAstCtx, type StructureToAstTableData, astToMigration, concatSchemaAndName, createDatabase, createMigrationChangeFn, createMigrationInterface, createMigrationsSchemaAndTable, createSchema, createTable, dbColumnToAst, dropDatabase, dropSchema, dropTable, encodeColumnDefault, getConstraintName, getDbStructureTableData, getDbTableColumnsChecks, getDbVersion, getExcludeName, getIndexName, getMigrationsSchemaAndTable, getSchemaAndTableFromName, incrementIntermediateCaller, instantiateDbColumn, introspectDbSchema, makeDomainsMap, makeFileVersion, makeStructureToAstCtx, migrate, migrateAndClose, promptSelect, rakeDbCliWithAdapter, rakeDbCommands, rakeDbConfigDefaults, redo, rollback, runMigration, saveMigratedVersion, setRakeDbCliRunFn, structureToAst, tableToAst, writeMigrationFile };
2335
+ export { type ChangeCallback, type DbMigration, type DbStructure, type IntrospectedStructure, type MigrateConfig, type MigrateFn, type MigrationChangeFn, type RakeDbAst, type RakeDbBaseTable, type RakeDbChangeFnConfig, type RakeDbCliConfigInput, type RakeDbCliResult, type RakeDbConfig, type RakeDbDefineTable, RakeDbError, type RakeDbFn, type SilentQueries, type StructureToAstCtx, type StructureToAstTableData, astToMigration, concatSchemaAndName, createDatabase, createMigrationChangeFn, createMigrationInterface, createMigrationsSchemaAndTable, createSchema, createTable, dbColumnToAst, dropDatabase, dropSchema, dropTable, encodeColumnDefault, getConstraintName, getDbStructureTableData, getDbTableColumnsChecks, getDbVersion, getExcludeName, getIndexName, getMigrationsSchemaAndTable, getSchemaAndTableFromName, getTableFactoryConfig, incrementIntermediateCaller, instantiateDbColumn, introspectDbSchema, makeDomainsMap, makeFileVersion, makeStructureToAstCtx, migrate, migrateAndClose, promptSelect, rakeDbCliWithAdapter, rakeDbCommands, rakeDbConfigDefaults, redo, rollback, runMigration, saveMigratedVersion, setRakeDbCliRunFn, structureToAst, tableToAst, writeMigrationFile };
package/dist/index.js CHANGED
@@ -205,7 +205,7 @@ const addColumnComment = (comments, name, item) => {
205
205
  };
206
206
  const getForeignKeyTable = (schema, fnOrTable) => {
207
207
  if (typeof fnOrTable === "string") return getSchemaAndTableFromName(schema, fnOrTable);
208
- const item = new (fnOrTable())();
208
+ const item = (0, pqb_internal.getForeignKeyTableInstance)(fnOrTable());
209
209
  return [item.schema, item.table];
210
210
  };
211
211
  const getConstraintName = (table, constraint, snakeCase) => {
@@ -432,6 +432,33 @@ const migrationsSchemaTableSql = (adapter, config) => {
432
432
  };
433
433
  var RakeDbError = class extends Error {};
434
434
  var NoPrimaryKey = class extends RakeDbError {};
435
+ const getTableFactoryConfig = ({ baseTable, defineTable }) => {
436
+ if (baseTable && defineTable) throw new Error("Configure either baseTable or defineTable, not both");
437
+ if (defineTable) {
438
+ if (!defineTable.types) throw new Error("defineTable is missing types");
439
+ if (!defineTable.exportAs) throw new Error("defineTable is missing exportAs");
440
+ if (typeof defineTable.getFilePath !== "function") throw new Error("defineTable is missing getFilePath");
441
+ return {
442
+ columnTypes: defineTable.types,
443
+ exportAs: defineTable.exportAs,
444
+ getFilePath: defineTable.getFilePath,
445
+ nowSQL: defineTable.nowSQL,
446
+ snakeCase: defineTable.snakeCase,
447
+ language: defineTable.language
448
+ };
449
+ }
450
+ if (baseTable) {
451
+ const { types, snakeCase, language } = baseTable.prototype || {};
452
+ return {
453
+ columnTypes: types,
454
+ exportAs: baseTable.exportAs,
455
+ getFilePath: baseTable.getFilePath,
456
+ nowSQL: baseTable.nowSQL,
457
+ snakeCase,
458
+ language
459
+ };
460
+ }
461
+ };
435
462
  const createTable$1 = async (migration, tableMethods, up, tableName, first, second, third) => {
436
463
  let options;
437
464
  let fn;
@@ -451,7 +478,8 @@ const createTable$1 = async (migration, tableMethods, up, tableName, first, seco
451
478
  let shape;
452
479
  let tableData;
453
480
  if (fn) {
454
- shape = (0, pqb_internal.getColumnTypes)(types, fn, migration.options.baseTable?.nowSQL, language);
481
+ const tableFactory = getTableFactoryConfig(migration.options);
482
+ shape = (0, pqb_internal.getColumnTypes)(types, fn, tableFactory?.nowSQL, language);
455
483
  tableData = (0, pqb_internal.parseTableData)(dataFn);
456
484
  tableData.constraints?.forEach((x, i) => {
457
485
  if (x.name || !x.check) return;
@@ -3290,10 +3318,10 @@ const processMigrateConfig = (config, db) => {
3290
3318
  migrationsPath,
3291
3319
  ...handleConfigLogger(config, db)
3292
3320
  };
3293
- if ("baseTable" in config && config.baseTable) {
3294
- const { snakeCase, language } = config.baseTable.prototype;
3295
- if (snakeCase) result.snakeCase = true;
3296
- if (language) result.language = language;
3321
+ const tableFactory = getTableFactoryConfig(config);
3322
+ if (tableFactory) {
3323
+ if (tableFactory.snakeCase) result.snakeCase = true;
3324
+ if (tableFactory.language) result.language = tableFactory.language;
3297
3325
  }
3298
3326
  return result;
3299
3327
  };
@@ -6148,6 +6176,7 @@ const ensureBasePathAndDbScript = (config, intermediateCallers = 0) => {
6148
6176
  const makeRakeDbConfig = (config, args) => {
6149
6177
  const ic = intermediateCallers;
6150
6178
  intermediateCallers = 0;
6179
+ const tableFactory = getTableFactoryConfig(config);
6151
6180
  const result = {
6152
6181
  ...rakeDbConfigDefaults,
6153
6182
  ...config,
@@ -6157,10 +6186,8 @@ const makeRakeDbConfig = (config, args) => {
6157
6186
  Object.assign(result, processMigrateConfig(result));
6158
6187
  if (!result.recurrentPath && result.migrationsPath) result.recurrentPath = node_path.default.join(result.migrationsPath, "recurrent");
6159
6188
  if (result.recurrentPath && !node_path.default.isAbsolute(result.recurrentPath)) result.recurrentPath = node_path.default.resolve(result.basePath, result.recurrentPath);
6160
- if ("baseTable" in config && config.baseTable) {
6161
- const { types } = config.baseTable.prototype;
6162
- result.columnTypes = types || (0, pqb_internal.makeColumnTypes)((0, pqb_internal.defaultSchemaConfig)());
6163
- } else {
6189
+ if (tableFactory) result.columnTypes = tableFactory.columnTypes || (0, pqb_internal.makeColumnTypes)((0, pqb_internal.defaultSchemaConfig)());
6190
+ else {
6164
6191
  const ct = "columnTypes" in config && config.columnTypes;
6165
6192
  result.columnTypes = (typeof ct === "function" ? ct((0, pqb_internal.makeColumnTypes)((0, pqb_internal.defaultSchemaConfig)())) : ct) || (0, pqb_internal.makeColumnTypes)((0, pqb_internal.defaultSchemaConfig)());
6166
6193
  }
@@ -6290,6 +6317,7 @@ exports.getExcludeName = getExcludeName;
6290
6317
  exports.getIndexName = getIndexName;
6291
6318
  exports.getMigrationsSchemaAndTable = getMigrationsSchemaAndTable;
6292
6319
  exports.getSchemaAndTableFromName = getSchemaAndTableFromName;
6320
+ exports.getTableFactoryConfig = getTableFactoryConfig;
6293
6321
  exports.incrementIntermediateCaller = incrementIntermediateCaller;
6294
6322
  exports.instantiateDbColumn = instantiateDbColumn;
6295
6323
  exports.introspectDbSchema = introspectDbSchema;