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.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { ArrayColumn, Column, CustomTypeColumn, DomainColumn, EnumColumn, PostgisGeographyPointColumn, RawSql, TimestampColumn, TimestampTZColumn, UnknownColumn, addCode, assignDbDataToColumn, backtickQuote, codeToString, colors, constraintInnerToCode, consumeColumnName, deepCompare, defaultSchemaConfig, emptyArray, emptyObject, escapeForMigration, escapeString, excludeInnerToCode, exhaustive, getColumnTypes, getDriverErrorCode, getImportPath, getStackTrace, indexInnerToCode, isRawSQL, logParamToLogObject, makeColumnTypes, makeColumnsByType, parseTableData, parseTableDataInput, pathToLog, primaryKeyInnerToCode, pushTableDataCode, quoteIdentifier, quoteObjectKey, raw, rawSqlToCode, referencesArgsToCode, setCurrentColumnName, setDefaultLanguage, singleQuote, tableDataMethods, toArray, toCamelCase, toSnakeCase } from "pqb/internal";
1
+ import { ArrayColumn, Column, CustomTypeColumn, DomainColumn, EnumColumn, PostgisGeographyPointColumn, RawSql, TimestampColumn, TimestampTZColumn, UnknownColumn, addCode, assignDbDataToColumn, backtickQuote, codeToString, colors, constraintInnerToCode, consumeColumnName, deepCompare, defaultSchemaConfig, emptyArray, emptyObject, escapeForMigration, escapeString, excludeInnerToCode, exhaustive, getColumnTypes, getDriverErrorCode, getForeignKeyTableInstance, getImportPath, getStackTrace, indexInnerToCode, isRawSQL, logParamToLogObject, makeColumnTypes, makeColumnsByType, parseTableData, parseTableDataInput, pathToLog, primaryKeyInnerToCode, pushTableDataCode, quoteIdentifier, quoteObjectKey, raw, rawSqlToCode, referencesArgsToCode, setCurrentColumnName, setDefaultLanguage, singleQuote, tableDataMethods, toArray, toCamelCase, toSnakeCase } from "pqb/internal";
2
2
  import path, { join } from "node:path";
3
3
  import { fileURLToPath, pathToFileURL } from "node:url";
4
4
  import { createDbWithAdapter } from "pqb";
@@ -182,7 +182,7 @@ const addColumnComment = (comments, name, item) => {
182
182
  };
183
183
  const getForeignKeyTable = (schema, fnOrTable) => {
184
184
  if (typeof fnOrTable === "string") return getSchemaAndTableFromName(schema, fnOrTable);
185
- const item = new (fnOrTable())();
185
+ const item = getForeignKeyTableInstance(fnOrTable());
186
186
  return [item.schema, item.table];
187
187
  };
188
188
  const getConstraintName = (table, constraint, snakeCase) => {
@@ -409,6 +409,33 @@ const migrationsSchemaTableSql = (adapter, config) => {
409
409
  };
410
410
  var RakeDbError = class extends Error {};
411
411
  var NoPrimaryKey = class extends RakeDbError {};
412
+ const getTableFactoryConfig = ({ baseTable, defineTable }) => {
413
+ if (baseTable && defineTable) throw new Error("Configure either baseTable or defineTable, not both");
414
+ if (defineTable) {
415
+ if (!defineTable.types) throw new Error("defineTable is missing types");
416
+ if (!defineTable.exportAs) throw new Error("defineTable is missing exportAs");
417
+ if (typeof defineTable.getFilePath !== "function") throw new Error("defineTable is missing getFilePath");
418
+ return {
419
+ columnTypes: defineTable.types,
420
+ exportAs: defineTable.exportAs,
421
+ getFilePath: defineTable.getFilePath,
422
+ nowSQL: defineTable.nowSQL,
423
+ snakeCase: defineTable.snakeCase,
424
+ language: defineTable.language
425
+ };
426
+ }
427
+ if (baseTable) {
428
+ const { types, snakeCase, language } = baseTable.prototype || {};
429
+ return {
430
+ columnTypes: types,
431
+ exportAs: baseTable.exportAs,
432
+ getFilePath: baseTable.getFilePath,
433
+ nowSQL: baseTable.nowSQL,
434
+ snakeCase,
435
+ language
436
+ };
437
+ }
438
+ };
412
439
  const createTable$1 = async (migration, tableMethods, up, tableName, first, second, third) => {
413
440
  let options;
414
441
  let fn;
@@ -428,7 +455,8 @@ const createTable$1 = async (migration, tableMethods, up, tableName, first, seco
428
455
  let shape;
429
456
  let tableData;
430
457
  if (fn) {
431
- shape = getColumnTypes(types, fn, migration.options.baseTable?.nowSQL, language);
458
+ const tableFactory = getTableFactoryConfig(migration.options);
459
+ shape = getColumnTypes(types, fn, tableFactory?.nowSQL, language);
432
460
  tableData = parseTableData(dataFn);
433
461
  tableData.constraints?.forEach((x, i) => {
434
462
  if (x.name || !x.check) return;
@@ -3267,10 +3295,10 @@ const processMigrateConfig = (config, db) => {
3267
3295
  migrationsPath,
3268
3296
  ...handleConfigLogger(config, db)
3269
3297
  };
3270
- if ("baseTable" in config && config.baseTable) {
3271
- const { snakeCase, language } = config.baseTable.prototype;
3272
- if (snakeCase) result.snakeCase = true;
3273
- if (language) result.language = language;
3298
+ const tableFactory = getTableFactoryConfig(config);
3299
+ if (tableFactory) {
3300
+ if (tableFactory.snakeCase) result.snakeCase = true;
3301
+ if (tableFactory.language) result.language = tableFactory.language;
3274
3302
  }
3275
3303
  return result;
3276
3304
  };
@@ -6125,6 +6153,7 @@ const ensureBasePathAndDbScript = (config, intermediateCallers = 0) => {
6125
6153
  const makeRakeDbConfig = (config, args) => {
6126
6154
  const ic = intermediateCallers;
6127
6155
  intermediateCallers = 0;
6156
+ const tableFactory = getTableFactoryConfig(config);
6128
6157
  const result = {
6129
6158
  ...rakeDbConfigDefaults,
6130
6159
  ...config,
@@ -6134,10 +6163,8 @@ const makeRakeDbConfig = (config, args) => {
6134
6163
  Object.assign(result, processMigrateConfig(result));
6135
6164
  if (!result.recurrentPath && result.migrationsPath) result.recurrentPath = path.join(result.migrationsPath, "recurrent");
6136
6165
  if (result.recurrentPath && !path.isAbsolute(result.recurrentPath)) result.recurrentPath = path.resolve(result.basePath, result.recurrentPath);
6137
- if ("baseTable" in config && config.baseTable) {
6138
- const { types } = config.baseTable.prototype;
6139
- result.columnTypes = types || makeColumnTypes(defaultSchemaConfig());
6140
- } else {
6166
+ if (tableFactory) result.columnTypes = tableFactory.columnTypes || makeColumnTypes(defaultSchemaConfig());
6167
+ else {
6141
6168
  const ct = "columnTypes" in config && config.columnTypes;
6142
6169
  result.columnTypes = (typeof ct === "function" ? ct(makeColumnTypes(defaultSchemaConfig())) : ct) || makeColumnTypes(defaultSchemaConfig());
6143
6170
  }
@@ -6245,6 +6272,6 @@ ${commandsHelp.map(([key, help, helpArguments]) => {
6245
6272
  `);
6246
6273
  }
6247
6274
  };
6248
- export { RakeDbError, 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 };
6275
+ export { RakeDbError, 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 };
6249
6276
 
6250
6277
  //# sourceMappingURL=index.mjs.map