@oliasoft-open-source/node-json-migrator 5.6.5 → 5.7.0-beta-2

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/README.md CHANGED
@@ -181,6 +181,15 @@ and [merge requests](https://gitlab.com/oliasoft-open-source/node-json-migrator/
181
181
  - to build: `npm run buld` (this transpile a production build to the `dist` directory)
182
182
  - the build `dist` directory should be committed
183
183
 
184
+ ## Migrating selected records
185
+
186
+ Pass `recordIds` to `migrateAll()` to restrict the usual migration eligibility checks to a list of IDs.
187
+ An empty list selects zero records. Omitting `recordIds` checks the entire entity table.
188
+
189
+ For multiple batches, call `const migrateBatch = await prepareMigrationRun(options)` once with the configuration
190
+ and callbacks. Call `await migrateBatch(recordIds)` for each batch. The migrator prepares one plan for the run.
191
+ Each batch returns the number of eligible records.
192
+
184
193
  ## Concurrent database migrations
185
194
 
186
195
  `migrateAll()` accepts `config.concurrency`, a positive integer that defaults to `1` (sequential processing).
package/dist/index.d.mts CHANGED
@@ -42,6 +42,7 @@ type TRecordDetails = {
42
42
  version: string;
43
43
  currentPlan?: TPlan;
44
44
  };
45
+ type TRecordIds = readonly TRecordDetails['id'][];
45
46
  type TRecordDetailsList = TRecordDetails[];
46
47
  type TEntityColumnNames = {
47
48
  id: string;
@@ -136,20 +137,19 @@ declare const migrate: ({
136
137
  }>;
137
138
  //#endregion
138
139
  //#region src/migrate-all/migrate-all.d.ts
139
- /**
140
- * Migrate all records (only use via command line interface)
141
- */
142
- declare const migrateAll: ({
143
- config,
144
- beforeMigrateRecord,
145
- afterMigrateRecord,
146
- onMigrationErrors
147
- }: {
140
+ type MigrationRunOptions = {
148
141
  config: TConfig;
149
142
  beforeMigrateRecord: TBeforeMigrateRecord;
150
143
  afterMigrateRecord: TAfterMigrateRecord;
151
144
  onMigrationErrors: TOnMigrationErrors;
152
- }) => Promise<void>;
145
+ };
146
+ declare const prepareMigrationRun: (options: MigrationRunOptions) => Promise<(recordIds: TRecordIds) => Promise<number>>;
147
+ declare const migrateAll: ({
148
+ recordIds,
149
+ ...options
150
+ }: MigrationRunOptions & {
151
+ recordIds?: TRecordIds;
152
+ }) => Promise<number>;
153
153
  //#endregion
154
154
  //#region src/pipe/pipe.d.ts
155
155
  /**
@@ -174,4 +174,4 @@ declare const printVersionHistory: ({
174
174
  config: TConfig;
175
175
  }) => Promise<void>;
176
176
  //#endregion
177
- export { type TAfterMigrateRecord, type TAfterMigrateRecordPayload, type TBeforeMigrateRecord, type TBeforeMigrateRecordPayload, type TConfig, type TEntityColumnNames, type TMetaData, type TMigration, type TMigrationErrorPayload, type TOnMigrationErrors, type TOnMigrationErrorsPayload, type TPayload, type TPlan, type TPlanEntry, type TRecord, type TRecordDetails, type TRecordDetailsList, checkMigrationPlan, createMigration, getPlannedMigrations, getPlannedVersion, getVersions, migrate, migrateAll, pipe, printVersionHistory };
177
+ export { type TAfterMigrateRecord, type TAfterMigrateRecordPayload, type TBeforeMigrateRecord, type TBeforeMigrateRecordPayload, type TConfig, type TEntityColumnNames, type TMetaData, type TMigration, type TMigrationErrorPayload, type TOnMigrationErrors, type TOnMigrationErrorsPayload, type TPayload, type TPlan, type TPlanEntry, type TRecord, type TRecordDetails, type TRecordDetailsList, checkMigrationPlan, createMigration, getPlannedMigrations, getPlannedVersion, getVersions, migrate, migrateAll, pipe, prepareMigrationRun, printVersionHistory };
package/dist/index.mjs CHANGED
@@ -4299,7 +4299,8 @@ const filterRecordsWithNoPendingMigrations = ({ records, latestPlan }) => {
4299
4299
  return latestPlan.some((p) => !currentHashes.has(p.fileHash));
4300
4300
  });
4301
4301
  };
4302
- const getRecordList = async (db, entity, entityTableName, entityColumnNames, latestVersion, latestPlan) => {
4302
+ const getRecordList = async (db, entity, entityTableName, entityColumnNames, latestVersion, latestPlan, recordIds) => {
4303
+ if (recordIds?.length === 0) return [];
4303
4304
  const query = `
4304
4305
  SELECT
4305
4306
  e.$(entityIdColumn:name) AS id,
@@ -4310,10 +4311,12 @@ const getRecordList = async (db, entity, entityTableName, entityColumnNames, lat
4310
4311
  FROM $(entityTable:name) e
4311
4312
  LEFT JOIN $(entityVersionsTable:name) v_current
4312
4313
  ON v_current.version = e.$(entityVersionColumn:name)
4313
- WHERE
4314
+ WHERE (
4314
4315
  e.$(entityVersionColumn:name) IS NULL
4315
4316
  OR e.$(entityVersionColumn:name) = ''
4316
4317
  OR e.$(entityVersionColumn:name) <> $(latestVersion)
4318
+ )
4319
+ ${recordIds === void 0 ? "" : "AND e.$(entityIdColumn:name) IN ($(recordIds:csv))"}
4317
4320
  `;
4318
4321
  const params = {
4319
4322
  entityTable: entityTableName,
@@ -4322,7 +4325,8 @@ const getRecordList = async (db, entity, entityTableName, entityColumnNames, lat
4322
4325
  entityNameColumn: entityColumnNames.name,
4323
4326
  entityCreatedColumn: entityColumnNames.created,
4324
4327
  entityVersionColumn: entityColumnNames.version,
4325
- latestVersion
4328
+ latestVersion,
4329
+ recordIds
4326
4330
  };
4327
4331
  return filterRecordsWithNoPendingMigrations({
4328
4332
  records: (await db.any(query, params)).map((r) => ({
@@ -4989,21 +4993,34 @@ const migrateRecords = async ({ recordList, config, preparedPlan, beforeMigrateR
4989
4993
  console.log(chalk.yellow(`Completed ${config?.entity ?? "entity"} migrations (some failed)${config?.dry ? " [dry-run, no output written]" : ""} ✓`));
4990
4994
  } else console.log(chalk.green(`Completed ${config?.entity ?? "entity"} migrations (success)${config?.dry ? " [dry-run, no output written]" : ""} ✓`));
4991
4995
  };
4992
- /**
4993
- * Migrate all records (only use via command line interface)
4994
- */
4995
- const migrateAll = async ({ config, beforeMigrateRecord, afterMigrateRecord, onMigrationErrors }) => {
4996
+ const prepareRun = async ({ config: runConfig, beforeMigrateRecord, afterMigrateRecord, onMigrationErrors }) => {
4997
+ const config = { ...runConfig };
4996
4998
  validateConcurrency(config.concurrency ?? 1);
4997
- const preparedPlan = await PlanModule.getPlannedMigrations({ config });
4998
- const { validatedPlan: latestPlan, nextVersion: latestVersion } = preparedPlan;
4999
- await migrateRecords({
5000
- config,
5001
- preparedPlan,
5002
- recordList: await getRecordList(config?.database, config?.entity, config?.entityTableName, config?.entityColumnNames, latestVersion, latestPlan),
5003
- beforeMigrateRecord,
5004
- afterMigrateRecord,
5005
- onMigrationErrors
5006
- });
4999
+ const plan = await PlanModule.getPlannedMigrations({ config });
5000
+ const { validatedPlan: latestPlan, nextVersion: latestVersion } = plan;
5001
+ return async (recordIds) => {
5002
+ const recordList = await getRecordList(config?.database, config?.entity, config?.entityTableName, config?.entityColumnNames, latestVersion, latestPlan, recordIds);
5003
+ if (recordIds !== void 0 && recordList.length === 0) return 0;
5004
+ await migrateRecords({
5005
+ config,
5006
+ preparedPlan: plan,
5007
+ recordList,
5008
+ beforeMigrateRecord,
5009
+ afterMigrateRecord,
5010
+ onMigrationErrors
5011
+ });
5012
+ return recordList.length;
5013
+ };
5014
+ };
5015
+ const prepareMigrationRun = async (options) => {
5016
+ const migrateRecordsForRun = await prepareRun(options);
5017
+ return (recordIds) => {
5018
+ if (!Array.isArray(recordIds)) throw new TypeError("A migration batch requires an array of record IDs");
5019
+ return migrateRecordsForRun(recordIds);
5020
+ };
5021
+ };
5022
+ const migrateAll = async ({ recordIds, ...options }) => {
5023
+ return (await prepareRun(options))(recordIds);
5007
5024
  };
5008
5025
  //#endregion
5009
5026
  //#region src/history/history.ts
@@ -5059,4 +5076,4 @@ const printVersionHistory = async ({ config }) => {
5059
5076
  }
5060
5077
  };
5061
5078
  //#endregion
5062
- export { checkMigrationPlan, createMigration, getPlannedMigrations, getPlannedVersion, getVersions, migrate, migrateAll, pipe, printVersionHistory };
5079
+ export { checkMigrationPlan, createMigration, getPlannedMigrations, getPlannedVersion, getVersions, migrate, migrateAll, pipe, prepareMigrationRun, printVersionHistory };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oliasoft-open-source/node-json-migrator",
3
- "version": "5.6.5",
3
+ "version": "5.7.0-beta-2",
4
4
  "description": "A library for JSON migrations",
5
5
  "homepage": "https://oliasoft-open-source.gitlab.io/node-json-migrator",
6
6
  "bugs": {