import * as mongodb from 'mongodb'; import { EventEmitter } from 'events'; export default MongoDbConnector; type MongoDbList = Map; declare class MongoDbConnector extends EventEmitter { config: MongoDbConnector.Config; DATABASES: MongoDbList; MODELS: MongoDbConnector.Models; MIGRATIONS: Map; constructor( config: MongoDbConnector.Config, models?: MongoDbConnector.Models, migrations?: Map, ); // Remove the info.uuid of a collection object static removeCollectionInfoUuid( collection: mongodb.Collection, ): mongodb.Collection; // Connect the database connect(): Promise; // Disconnect the database disconnect(): Promise; // Returns a database connection db(databaseName: string): mongodb.Db; // Returns the full information about a database state(databaseName: string): Promise; states(): Promise; // Initialize the different collections based on the models definitions. init(models: MongoDbConnector.Models): Promise; // Initializes a database model initModel(model: MongoDbConnector.ModelDefinition): Promise; // MIGRATIONS checkDirection(direction: MongoDbConnector.MigrationDirectionType): void; // Apply a single migration applyMigration( direction: MongoDbConnector.MigrationDirectionType, migration: string, ): Promise; // Apply all migrations sequentially applyAllMigrations( direction: MongoDbConnector.MigrationDirectionType, ): Promise; // Apply collection definitions from models applyModels( direction: MongoDbConnector.MigrationDirectionType, ): Promise; // Apply a migration to the database migrate( direction: MongoDbConnector.MigrationDirectionType, migration: MongoDbConnector.MigrationType | null, ): Promise; // Apply migrations until a specific version included migrateUntil( direction: MongoDbConnector.MigrationDirectionType, migration: MongoDbConnector.MigrationType | null, ): Promise; // Returns the helpers for applying migrations onto a database getMigrationHelper( helperName: MongoDbConnector.HelperType, ): MongoDbConnector.MigrationHelper; // Check equity between models definitions and migrations attached to the connector checkDangerouslyMigrationsAndModelsEquality(): mongodb.Db; // Check that each migration is effectively rollbackable. checkDangerouslyMigrationsRollbackability(): mongodb.Db; } declare namespace MongoDbConnector { export interface Config { databases: DatabaseConfig[]; ensureIndexInBackground: boolean; } export interface DatabaseConfig { name: string; url: string; options?: mongodb.MongoClientOptions; } export interface DatabaseState { name: string; collections: CollectionInfo[]; collectionIndexes: CursorFirstBatch[]; } export interface Models { [key: string]: ModelDefinition; } /** * Example * cursor: { * id: 0, * ns: 'test.$cmd.listIndexes.collectionName', * firstBatch: [ CursorFirstBatch, CursorFirstBatch, CursorFirstBatch ] * }, * ok: 1 */ export interface ListIndexesCursor { cursor: ListIndexCursor; ok: number; } export interface ListIndexCursor { id: number; ns: string; firstBatch: CursorFirstBatch[]; } /** * Example * [ [ { v: 2, key: { _id: 1 }, name: '_id_', ns: 'dbName.collectionName' }, * { v: 2, * key: { date: 1 }, * name: 'date_1', * ns: 'dbName.collectionName', * expireAfterSeconds: 3600, * background: true }, * { v: 2, * key: { processed: 1 }, * name: 'processed_1', * ns: 'dbName.collectionName', * background: true } ] ] } */ export interface CursorFirstBatch { v: number; key: { date?: number; processed?: number; }; name: string; ns: string; expireAfterSeconds?: number; background?: boolean; } export interface CollectionInfo { name: string; type: string; options: object; info: { readonly?: boolean; uuid: string; } | null; idIndex: CursorFirstBatch; } export interface ModelDefinition { DATABASE: string; COLLECTION: string; COLLECTION_OPTIONS: { capped?: boolean; max?: number; size?: number; }; INDEXES: object[][]; VALIDATOR_SCHEMA: any; VALIDATOR_OPTIONS: any; } export interface Migration { update(mongodbConnector?: MongoDbConnector): Promise; rollback(mongodbConnector?: MongoDbConnector): Promise; } export enum MigrationDirectionType { MIGRATION_UPDATE = 'update', MIGRATION_ROLLBACK = 'rollback', } export enum MigrationType { INIT = 'init', FROM_MODELS = 'from-models', ALL = 'all', } export interface MigrateResult { before: DatabaseState[]; after: DatabaseState[]; } export enum HelperType { CREATE_COLLECTION = 'createCollection', CREATE_INDEX = 'createIndex', DROP_COLLECTION = 'dropCollection', DROP_INDEX = 'dropIndex', UPDATE_VALIDATOR = 'updateValidator', } // createCollection, updateValidator export interface MigrationHelper { update( databaseName: string, collectionName: string, validator: object, validatorOptions: object, ): Promise; rollback(databaseName: string, collectionName: string): Promise; } // createIndex, dropIndex export interface MigrationHelper { update( databaseName: string, collectionName: string, indexName: string, indexKeys: object, indexOptions: object, ): Promise; rollback( databaseName: string, collectionName: string, indexName: string, indexKeys: object, indexOptions: object, ): Promise; } // dropCollection, createCollection without validator export interface MigrationHelper { update(databaseName: string, collectionName: string): Promise; rollback(databaseName: string, collectionName: string): Promise; } }