import { cloneDeep, cloneDeepWith, escapeRegExp, isEqual, isObject } from 'lodash'; import { WithoutId, ChangeStreamOptions, Collection, AggregateOptions, BulkWriteOptions, Filter, FindOptions, UpdateOneModel, CountDocumentsOptions, CreateIndexesOptions, MongoClient, DeleteOptions, IndexSpecification, OptionalUnlessRequiredId, UpdateFilter, UpdateOptions, InsertOneOptions, UpdateResult, Document, IndexDescription, DropIndexesOptions, ReplaceOptions, DistinctOptions, IndexInformationOptions, } from 'mongodb'; import { IDocument, FindResult, IChangePublisher, IDatabase, ServiceOptions, ReadConfig, CreateConfig, UpdateConfig, DeleteConfig, } from './types'; import logger from './utils/logger'; import { addUpdatedOnField, generateId } from './utils/helpers'; import { inMemoryPublisher } from './events/in-memory'; const defaultOptions: ServiceOptions = { skipDeletedOnDocs: true, publishEvents: true, outbox: false, addCreatedOnField: true, addUpdatedOnField: true, escapeRegExp: false, }; const isDev = process.env.NODE_ENV === 'development'; class Service { private client?: MongoClient; private collection: Collection | null; private _collectionName: string; private options: ServiceOptions; private db; private waitForConnection: () => Promise; private changePublisher: IChangePublisher; constructor( collectionName: string, db: IDatabase, options: ServiceOptions = {}, ) { this._collectionName = collectionName; this.db = db; this.options = { ...defaultOptions, ...options, }; this.waitForConnection = db.waitForConnection; if (this.options.outbox) { this.changePublisher = db.getOutboxService(); } else { this.changePublisher = inMemoryPublisher; } this.collection = null; this.db.getClient() .then((client) => { this.client = client; }); } public get collectionName(): string { return this._collectionName; } public validateSchema = async (entity: U | Partial): Promise> => { if (this.options.schemaValidator) { const { schemaValidator } = this.options; try { const result = await schemaValidator(entity); return result; } catch (err: any) { logger.error(`Schema is not valid for ${this._collectionName} collection: ${err.stack || err}`, entity); throw err; } } return entity; }; protected escapeFilterRegExp = ( query: Filter, ): Filter => { return cloneDeepWith(query, (value, key) => { if (!isObject(value)) { return key === '$regex' ? escapeRegExp(value) : value; } }); }; protected handleReadOperations = ( query: Filter, readConfig: ReadConfig, ): Filter => { const shouldSkipDeletedDocs = typeof readConfig.skipDeletedOnDocs === 'boolean' ? readConfig.skipDeletedOnDocs : this.options.skipDeletedOnDocs; if (!query.deletedOn && shouldSkipDeletedDocs) { query = { ...query, deletedOn: { $exists: false } }; } if (this.options.escapeRegExp) { query = this.escapeFilterRegExp(query); } return query; }; protected validateCreateOperation = async ( object: Partial, createConfig: CreateConfig, ): Promise => { let entity = object; if (!entity._id) { entity._id = generateId(); } const timestamp = new Date(); if (!entity.createdOn && this.options.addCreatedOnField) { entity.createdOn = timestamp; } if (!entity.updatedOn && this.options.addUpdatedOnField) { entity.updatedOn = timestamp; } const shouldValidateSchema = typeof createConfig.validateSchema === 'boolean' ? createConfig.validateSchema : Boolean(this.options.schemaValidator); if (shouldValidateSchema) { entity = await this.validateSchema(entity); } return entity as U; }; protected getCollection = async (): Promise> => { await this.waitForConnection(); if (!this.collection) { this.collection = await this.db.getOrCreateCollection(this.collectionName, { collectionCreateOptions: this.options.collectionCreateOptions || {}, collectionOptions: this.options.collectionOptions || {}, }); if (!this.collection) { throw new Error(`Mongo collection ${this._collectionName} is not initialized.`); } } return this.collection as unknown as Collection; }; findOne = async ( filter: Filter, readConfig: ReadConfig = {}, findOptions: FindOptions = {}, ): Promise => { const collection = await this.getCollection(); filter = this.handleReadOperations(filter, readConfig); return collection.findOne(filter, findOptions); }; find = async ( filter: Filter, readConfig: ReadConfig & { page?: number; perPage?: number } = {}, findOptions: FindOptions = {}, ): Promise> => { const collection = await this.getCollection(); const { page, perPage } = readConfig; const hasPaging = !!page && !!perPage; filter = this.handleReadOperations(filter, readConfig); if (!hasPaging) { const results = await collection.find(filter, findOptions).toArray(); return { pagesCount: 1, results, count: results.length, }; } findOptions.skip = (page - 1) * perPage; findOptions.limit = perPage; const [results, count] = await Promise.all([ collection.find(filter, findOptions).toArray(), collection.countDocuments(filter), ]); const pagesCount = Math.ceil(count / perPage) || 1; return { pagesCount, results, count, }; }; exists = async ( filter: Filter, readConfig: ReadConfig = {}, findOptions: FindOptions = {}, ): Promise => { const doc = await this.findOne(filter, readConfig, findOptions); return Boolean(doc); }; countDocuments = async ( filter: Filter, readConfig: ReadConfig = {}, countDocumentOptions: CountDocumentsOptions = {}, ): Promise => { const collection = await this.getCollection(); filter = this.handleReadOperations(filter, readConfig); return collection.countDocuments(filter, countDocumentOptions); }; distinct = async ( key: string, filter: Filter, readConfig: ReadConfig = {}, distinctOptions: DistinctOptions = {}, ): Promise => { const collection = await this.getCollection(); filter = this.handleReadOperations(filter, readConfig); return collection.distinct(key, filter, distinctOptions); }; insertOne = async ( object: Partial, createConfig: CreateConfig = {}, insertOneOptions: InsertOneOptions = {}, ): Promise => { const collection = await this.getCollection(); const validEntity = await this.validateCreateOperation(object, createConfig); const shouldPublishEvents = typeof createConfig.publishEvents === 'boolean' ? createConfig.publishEvents : this.options.publishEvents; if (shouldPublishEvents) { const insertOneWithEvent = async (opts: InsertOneOptions): Promise => { await collection.insertOne(validEntity as OptionalUnlessRequiredId, opts); return this.changePublisher.publishDbChange( this._collectionName, 'create', { doc: validEntity }, { session: opts.session }, ); }; if (this.options.outbox && !insertOneOptions.session) { await this.db.withTransaction(((session) => insertOneWithEvent({ session }))); } else { await insertOneWithEvent(insertOneOptions); } } else { await collection.insertOne(validEntity as OptionalUnlessRequiredId, insertOneOptions); } return validEntity; }; insertMany = async ( objects: Partial[], createConfig: CreateConfig = {}, bulkWriteOptions: BulkWriteOptions = {}, ): Promise => { const collection = await this.getCollection(); const validEntities = await Promise.all(objects.map( (o) => this.validateCreateOperation(o, createConfig), )); const shouldPublishEvents = typeof createConfig.publishEvents === 'boolean' ? createConfig.publishEvents : this.options.publishEvents; if (shouldPublishEvents) { const insertManyWithEvents = async (opts: BulkWriteOptions): Promise => { await collection.insertMany(validEntities as OptionalUnlessRequiredId[], opts); return this.changePublisher.publishDbChanges( this._collectionName, 'create', validEntities.map((e) => ({ doc: e })), { session: opts.session }, ); }; if (this.options.outbox && !bulkWriteOptions.session) { await this.db.withTransaction(((session) => insertManyWithEvents({ session }))); } else { await insertManyWithEvents(bulkWriteOptions); } } else { await collection.insertMany(validEntities as OptionalUnlessRequiredId[], bulkWriteOptions); } return validEntities; }; replaceOne = async ( filter: Filter, replacement: Partial, readConfig: ReadConfig = {}, replaceOptions: ReplaceOptions = {}, ): Promise => { const collection = await this.getCollection(); filter = this.handleReadOperations(filter, readConfig); if (this.options.addUpdatedOnField) { replacement.updatedOn = new Date(); } return collection.replaceOne(filter, replacement as WithoutId, replaceOptions); }; updateOne = async ( filter: Filter, updateFn: (doc: U) => Partial, updateConfig: UpdateConfig = {}, updateOptions: UpdateOptions = {}, ): Promise => { const collection = await this.getCollection(); filter = this.handleReadOperations(filter, updateConfig); const doc = await this.findOne(filter, updateConfig); if (!doc) { if (isDev) { logger.warn(`Document not found when updating ${this._collectionName} collection. Request query — ${JSON.stringify(filter)}`); } return null; } const prevDoc = cloneDeep(doc); const updatedFields = await updateFn(doc); const newDoc = { ...doc, ...updatedFields }; const isUpdated = !isEqual(prevDoc, newDoc); if (!isUpdated) { if (isDev) { logger.warn(`Document hasn't changed when updating ${this._collectionName} collection. Request query — ${JSON.stringify(filter)}`); } return newDoc; } if (this.options.addUpdatedOnField) { const updatedOnDate = new Date(); updatedFields.updatedOn = updatedOnDate; newDoc.updatedOn = updatedOnDate; } const shouldValidateSchema = typeof updateConfig.validateSchema === 'boolean' ? updateConfig.validateSchema : Boolean(this.options.schemaValidator); if (shouldValidateSchema) { await this.validateSchema(newDoc); } const shouldPublishEvents = typeof updateConfig.publishEvents === 'boolean' ? updateConfig.publishEvents : this.options.publishEvents; if (shouldPublishEvents) { const updateOneWithEvent = async (opts: UpdateOptions): Promise => { await collection.updateOne( { _id: doc._id } as Filter, { $set: updatedFields } as UpdateFilter, opts, ); return this.changePublisher.publishDbChange( this._collectionName, 'update', { doc: newDoc, prevDoc }, { session: opts.session }, ); }; if (this.options.outbox && !updateOptions.session) { await this.db.withTransaction(((session) => updateOneWithEvent({ session }))); } else { await updateOneWithEvent(updateOptions); } } else { await collection.updateOne( { _id: doc._id } as Filter, { $set: updatedFields } as UpdateFilter, updateOptions, ); } return newDoc; }; updateMany = async ( filter: Filter, updateFn: (doc: U) => Partial, updateConfig: UpdateConfig = {}, updateOptions: UpdateOptions = {}, ): Promise => { const collection = await this.getCollection(); filter = this.handleReadOperations(filter, updateConfig); const documents = await collection.find(filter).toArray(); if (documents.length === 0) { if (isDev) { logger.warn(`Documents not found when updating ${this._collectionName} collection. Request query — ${JSON.stringify(filter)}`); } return []; } const updated = await Promise.all( documents.map(async (doc) => { const prevDoc = cloneDeep(doc); const updatedFields = await updateFn(doc); const newDoc = { ...doc, ...updatedFields }; return { doc: newDoc, prevDoc, updatedFields, isUpdated: !isEqual(prevDoc, newDoc), }; }), ); const isUpdated = updated.find((u) => u.isUpdated) !== undefined; if (!isUpdated) { if (isDev) { logger.warn(`Documents hasn't changed when updating ${this._collectionName} collection. Request query — ${JSON.stringify(filter)}`); } return updated.map((u) => u.doc); } if (this.options.addUpdatedOnField) { const updatedOnDate = new Date(); updated.forEach((u) => { if (u.isUpdated) { u.doc.updatedOn = updatedOnDate; u.updatedFields.updatedOn = updatedOnDate; } }); } const shouldValidateSchema = typeof updateConfig.validateSchema === 'boolean' ? updateConfig.validateSchema : Boolean(this.options.schemaValidator); if (shouldValidateSchema) { await Promise.all((updated.map((u) => this.validateSchema(u.doc)))); } const updatedDocuments = updated.filter((u) => u.isUpdated); const bulkWriteQuery = updatedDocuments.map( (u): { updateOne: UpdateOneModel } => { const filterQuery = { _id: u.doc._id } as Filter; return { updateOne: { filter: filterQuery, update: { $set: u.updatedFields } as UpdateFilter, }, }; }, ); const shouldPublishEvents = typeof updateConfig.publishEvents === 'boolean' ? updateConfig.publishEvents : this.options.publishEvents; if (shouldPublishEvents) { const updateManyWithEvents = async (opts: UpdateOptions): Promise => { await collection.bulkWrite(bulkWriteQuery, updateOptions); return this.changePublisher.publishDbChanges( this._collectionName, 'update', updatedDocuments.map((u) => ({ doc: u.doc, prevDoc: u.prevDoc })), { session: opts.session }, ); }; if (this.options.outbox && !updateOptions.session) { await this.db.withTransaction(((session) => updateManyWithEvents({ session }))); } else { await updateManyWithEvents(updateOptions); } } else { await collection.bulkWrite(bulkWriteQuery, updateOptions); } return updated.map((u) => u.doc); }; deleteOne = async ( filter: Filter, deleteConfig: DeleteConfig = {}, deleteOptions: DeleteOptions = {}, ): Promise => { const collection = await this.getCollection(); const doc = await this.findOne(filter, deleteConfig); if (!doc) { if (isDev) { logger.warn(`Document not found when deleting ${this._collectionName} collection. Request query — ${JSON.stringify(filter)}`); } return null; } const shouldPublishEvents = typeof deleteConfig.publishEvents === 'boolean' ? deleteConfig.publishEvents : this.options.publishEvents; if (shouldPublishEvents) { const deleteOneWithEvent = async (opts: DeleteOptions): Promise => { await collection.deleteOne(filter, opts); return this.changePublisher.publishDbChange( this._collectionName, 'delete', { doc }, { session: opts.session }, ); }; if (this.options.outbox && !deleteOptions.session) { await this.db.withTransaction(((session) => deleteOneWithEvent({ session }))); } else { await deleteOneWithEvent(deleteOptions); } } else { await collection.deleteOne(filter, deleteOptions); } return doc; }; deleteMany = async ( filter: Filter, deleteConfig: DeleteConfig = {}, deleteOptions: DeleteOptions = {}, ): Promise => { const collection = await this.getCollection(); filter = this.handleReadOperations(filter, deleteConfig); const documents = await collection.find(filter).toArray(); if (documents.length === 0) { if (isDev) { logger.warn(`Documents not found when deleting ${this._collectionName} collection. Request query — ${JSON.stringify(filter)}`); } return []; } const shouldPublishEvents = typeof deleteConfig.publishEvents === 'boolean' ? deleteConfig.publishEvents : this.options.publishEvents; if (shouldPublishEvents) { const deleteManyWithEvents = async (opts: DeleteOptions): Promise => { await collection.deleteMany(filter, opts); return this.changePublisher.publishDbChanges( this._collectionName, 'delete', documents.map((doc) => ({ doc })), { session: opts.session }, ); }; if (this.options.outbox && !deleteOptions.session) { await this.db.withTransaction(((session) => deleteManyWithEvents({ session }))); } else { await deleteManyWithEvents(deleteOptions); } } else { await collection.deleteMany(filter, deleteOptions); } return documents; }; deleteSoft = async ( filter: Filter, deleteConfig: DeleteConfig = {}, deleteOptions: DeleteOptions = {}, ): Promise => { const collection = await this.getCollection(); filter = this.handleReadOperations(filter, deleteConfig); const documents = await collection.find(filter).toArray(); if (documents.length === 0) { if (isDev) { logger.warn(`Documents not found when deleting ${this._collectionName} collection. Request query — ${JSON.stringify(filter)}`); } return []; } const deletedOnDate = new Date(); const deletedDocuments = documents.map((doc) => ({ ...doc, deletedOn: deletedOnDate })); const shouldPublishEvents = typeof deleteConfig.publishEvents === 'boolean' ? deleteConfig.publishEvents : this.options.publishEvents; if (shouldPublishEvents) { const deleteSoftWithEvent = async (opts: UpdateOptions): Promise => { await collection.updateMany( filter, { $set: { deletedOn: deletedOnDate } } as UpdateFilter, opts, ); return this.changePublisher.publishDbChanges( this._collectionName, 'delete', deletedDocuments.map((doc) => ({ doc })), { session: opts.session }, ); }; if (this.options.outbox && !deleteOptions.session) { await this.db.withTransaction(((session) => deleteSoftWithEvent({ session }))); } else { await deleteSoftWithEvent(deleteOptions); } } else { await collection.updateMany( filter, { $set: { deletedOn: deletedOnDate } } as UpdateFilter, deleteOptions, ); } return deletedDocuments; }; atomic = { updateOne: async ( filter: Filter, updateFilter: UpdateFilter, readConfig: ReadConfig = {}, updateOptions: UpdateOptions = {}, ):Promise => { const collection = await this.getCollection(); filter = this.handleReadOperations(filter, readConfig); if (this.options.addUpdatedOnField) { updateFilter = addUpdatedOnField(updateFilter); } return collection.updateOne(filter, updateFilter, updateOptions); }, updateMany: async ( filter: Filter, updateFilter: UpdateFilter, readConfig: ReadConfig = {}, updateOptions: UpdateOptions = {}, ): Promise => { const collection = await this.getCollection(); filter = this.handleReadOperations(filter, readConfig); if (this.options.addUpdatedOnField) { updateFilter = addUpdatedOnField(updateFilter); } return collection.updateMany(filter, updateFilter, updateOptions); }, }; aggregate = async ( pipeline: any[], options: AggregateOptions = {}, ): Promise => { const collection = await this.getCollection(); return collection.aggregate(pipeline, options).toArray(); }; indexExists = async ( indexes: string | string[], indexInformationOptions: IndexInformationOptions = {}, ): Promise => { const collection = await this.getCollection(); return collection.indexExists(indexes, indexInformationOptions); }; createIndex = async ( indexSpec: IndexSpecification, options: CreateIndexesOptions = {}, ): Promise => { const collection = await this.getCollection(); return collection.createIndex(indexSpec, options) .catch((err: any) => { logger.info(err, { collection: this.collectionName }); }); }; createIndexes = async ( indexSpecs: IndexDescription[], options: CreateIndexesOptions = {}, ): Promise => { const collection = await this.getCollection(); return collection.createIndexes(indexSpecs, options) .catch((err: any) => { logger.info(err, { collection: this.collectionName }); }); }; dropIndex = async ( indexName: string, options: DropIndexesOptions = {}, ): Promise => { const collection = await this.getCollection(); return collection.dropIndex(indexName, options) .catch((err: any) => { logger.info(err, { collection: this.collectionName }); }); }; dropIndexes = async ( options: DropIndexesOptions = {}, ): Promise => { const collection = await this.getCollection(); return collection.dropIndexes(options) .catch((err: any) => { logger.info(err, { collection: this.collectionName }); }); }; watch = async ( pipeline: Document[] | undefined, options: ChangeStreamOptions = {}, ): Promise => { const collection = await this.getCollection(); return collection.watch(pipeline, options); }; drop = async (recreate = false): Promise => { const collection = await this.getCollection(); await collection.drop(); if (recreate) { this.collection = await this.db.getOrCreateCollection(this.collectionName, { collectionCreateOptions: this.options.collectionCreateOptions || {}, collectionOptions: this.options.collectionOptions || {}, }); } else { this.collection = null; } }; } export default Service;