import * as mongodb from 'mongodb'; export default DataMigration; /** * Template T is used as the Object document * and TSchema as Schema (according to the mongodb doc) */ type RollBackUpdateObject = { $set?: any; $unset?: any; }; declare class DataMigration { constructor( migrationId: string, collectionName: string, query: mongodb.FilterQuery, projection: mongodb.FindOneOptions, update: (arg: TSchema) => Promise>, options?: DataMigration.Options, ); // Returns the collection name for restoring data after the migration execution getMigrationCollectionName(): string; // Creates the migration collection and returns its definition getMigrationCollection( db: mongodb.Db, ): Promise>; // Returns the collection on which the migration must be applied getCollection(db: mongodb.Db): mongodb.Collection; // Merge MongoDb JSON bulk operation results into a single one static mergeBulkOperationResults( resultA: DataMigration.BulkOperation, resultB: DataMigration.BulkOperation, ): DataMigration.BulkOperation; // Returns a bulk and initialized result object to batch operations static getBulk( collection: mongodb.Collection, ): Promise; // Effectively execute bulk operations and returns the merged results after having log a progress message doBulk( bulk: DataMigration.Bulk, result: DataMigration.Bulk, message: string, doIt: boolean, ): DataMigration.BulkOperation; // Performs the migration // Steps: // 1. Make the backup of the data // 2. After the backup is done, execute the bulk operation update(db: mongodb.Db, doIt: boolean): Promise; // Build the rollback update based on existing filtered document and the corresponding update query getRollbackUpdate(obj: Promise): RollBackUpdateObject; // Performs the migration rollback rollback(db: mongodb.Db, doIt: boolean): Promise; // Remove migration flags from the documents clean(db: mongodb.Db, doIt: boolean): Promise; } declare namespace DataMigration { export interface Options { maxBulkSize?: number; delayBetweenBatches?: number; continueOnFailure?: boolean; filter?: (obj: object) => object; rollback?: (arg: TSchema) => Promise>; } export interface Bulk { bulk: mongodb.UnorderedBulkOperation; result: BulkOperation; } export interface BulkOperation { ok: number; writeErrors: string[]; writeConcernErrors: string[]; insertedIds: string[]; nInserted: number; nUpserted: number; nMatched: number; nModified: number; nRemoved: number; upserted: string[]; } }