@resolveio/server-lib 20.4.54 → 20.4.55
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.
|
@@ -125,6 +125,8 @@ export declare class MongoManagerCollection<T extends CollectionDocument> {
|
|
|
125
125
|
aggregateCursor(pipeline: object[], options?: AggregateOptions): AggregationCursor;
|
|
126
126
|
aggregateStream(pipeline: object[], options?: AggregateOptions, streamOptions?: CursorStreamOptions): import("stream").Readable & AsyncIterable<import("bson").Document>;
|
|
127
127
|
bulkWrite(operations: any[], options?: BulkWriteOptions, bypassLogs?: boolean, bypassCheckSchema?: boolean): Promise<BulkWriteResult>;
|
|
128
|
+
private matchesFilter;
|
|
129
|
+
extractFieldNames(filter: any, fieldNamesSet: Set<string>, prefix?: string): void;
|
|
128
130
|
countDocuments(filter?: MongoManagerFilter<T> & MongoManagerFilterOperators<T>, options?: CountDocumentsOptions, skipCache?: boolean): Promise<number>;
|
|
129
131
|
create(f_docs: T | T[], options?: InsertOneOptions | BulkWriteOptions): Promise<T | T[]>;
|
|
130
132
|
createIndex(fieldOrSpec: any, options?: CreateIndexesOptions): Promise<string>;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var __extends=this&&this.__extends||function(){var t=function(e,n){return(t=Object.setPrototypeOf||({__proto__:[]}instanceof Array?function(e,n){e.__proto__=n}:function(e,n){for(var t in n)Object.prototype.hasOwnProperty.call(n,t)&&(e[t]=n[t])}))(e,n)};return function(e,n){if("function"!=typeof n&&null!==n)throw new TypeError("Class extends value "+String(n)+" is not a constructor or null");function __(){this.constructor=e}t(e,n),e.prototype=null===n?Object.create(n):(__.prototype=n.prototype,new __)}}(),__awaiter=this&&this.__awaiter||function(e,o,i,r){return new(i=i||Promise)(function(t,n){function fulfilled(e){try{step(r.next(e))}catch(e){n(e)}}function rejected(e){try{step(r.throw(e))}catch(e){n(e)}}function step(e){var n;e.done?t(e.value):((n=e.value)instanceof i?n:new i(function(e){e(n)})).then(fulfilled,rejected)}step((r=r.apply(e,o||[])).next())})},__generator=this&&this.__generator||function(o,i){var r,a,s,c={label:0,sent:function(){if(1&s[0])throw s[1];return s[1]},trys:[],ops:[]},l={next:verb(0),throw:verb(1),return:verb(2)};return"function"==typeof Symbol&&(l[Symbol.iterator]=function(){return this}),l;function verb(t){return function(e){var n=[t,e];if(r)throw new TypeError("Generator is already executing.");for(;c=l&&n[l=0]?0:c;)try{if(r=1,a&&(s=2&n[0]?a.return:n[0]?a.throw||((s=a.return)&&s.call(a),0):a.next)&&!(s=s.call(a,n[1])).done)return s;switch(a=0,(n=s?[2&n[0],s.value]:n)[0]){case 0:case 1:s=n;break;case 4:return c.label++,{value:n[1],done:!1};case 5:c.label++,a=n[1],n=[0];continue;case 7:n=c.ops.pop(),c.trys.pop();continue;default:if(!(s=0<(s=c.trys).length&&s[s.length-1])&&(6===n[0]||2===n[0])){c=0;continue}if(3===n[0]&&(!s||n[1]>s[0]&&n[1]<s[3]))c.label=n[1];else if(6===n[0]&&c.label<s[1])c.label=s[1],s=n;else{if(!(s&&c.label<s[2])){s[2]&&c.ops.pop(),c.trys.pop();continue}c.label=s[2],c.ops.push(n)}}n=i.call(o,c)}catch(e){n=[6,e],a=0}finally{r=s=0}if(5&n[0])throw n[1];return{value:n[0]?n[1]:void 0,done:!0}}}},__read=this&&this.__read||function(e,n){var t="function"==typeof Symbol&&e[Symbol.iterator];if(!t)return e;var o,i,r=t.call(e),a=[];try{for(;(void 0===n||0<n--)&&!(o=r.next()).done;)a.push(o.value)}catch(e){i={error:e}}finally{try{o&&!o.done&&(t=r.return)&&t.call(r)}finally{if(i)throw i.error}}return a},__spreadArray=this&&this.__spreadArray||function(e,n,t){if(t||2===arguments.length)for(var o,i=0,r=n.length;i<r;i++)!o&&i in n||((o=o||Array.prototype.slice.call(n,0,i))[i]=n[i]);return e.concat(o||Array.prototype.slice.call(n))},__values=this&&this.__values||function(e){var n="function"==typeof Symbol&&Symbol.iterator,t=n&&e[n],o=0;if(t)return t.call(e);if(e&&"number"==typeof e.length)return{next:function(){return{value:(e=e&&o>=e.length?void 0:e)&&e[o++],done:!e}}};throw new TypeError(n?"Object is not iterable.":"Symbol.iterator is not defined.")},simpl_schema_1=(Object.defineProperty(exports,"__esModule",{value:!0}),exports.objectIdHexString=exports.MongoManagerUserCollection=exports.MongoManagerCollection=exports.MongoManagerModel=exports.MongoManager=void 0,require("simpl-schema")),mongodb_1=require("mongodb"),index_1=require("../index"),user_collection_1=require("../collections/user.collection"),common_1=require("../util/common"),log_collection_1=require("../collections/log.collection"),NodeCache=require("node-cache"),crypto=require("crypto"),scmp=require("scmp"),monitor_manager_1=require("./monitor.manager"),os_1=require("os"),numCPUs=(0,os_1.cpus)().length,v8=require("v8"),async_1=require("async"),MongoManager=function(){function MongoManager(e){this._collections=[],this._cacheMap=[],this._operationInProgress=new Map,this._heapSize=v8.getHeapStatistics()/numCPUs,this._serverCollections=[],this._serverConfig=null,this._isWorkersEnabled=!1,this._isWorkerInstance=!1,this._serverConfig=e,this._nodeCache=new NodeCache({stdTTL:0,checkperiod:0}),this._isWorkersEnabled="true"===process.env.IS_WORKERS_ENABLED,this._isWorkerInstance="true"===process.env.IS_WORKER_INSTANCE,this.setCacheLimit(),this.initServerCollections(),this._isWorkersEnabled&&this._isWorkerInstance&&this.setupChangeStream()}return MongoManager.prototype.setCacheLimit=function(){this._isWorkersEnabled?this._heapLimit=this._isWorkerInstance?.8*this._heapSize:.4*this._heapSize:this._heapLimit=.3*this._heapSize},MongoManager.prototype.initServerCollections=function(){return __awaiter(this,void 0,void 0,function(){var n;return __generator(this,function(e){switch(e.label){case 0:return[4,index_1.ResolveIOServer.getMainDB().listCollections().toArray()];case 1:return n=e.sent(),this._serverCollections=n,[2]}})})},MongoManager.prototype.getServerCollections=function(){return this._serverCollections},MongoManager.prototype.registerCollection=function(n){var e=this;n.collectionOptions&&n.collectionOptions.timeseries&&n.collectionOptions.timeseries.timeField&&this._serverCollections.some(function(e){return e.name===n.collectionName&&"collection"===e.type})&&index_1.ResolveIOServer.getMainDB().dropCollection(n.collectionName).then(function(){e.createCollection(n)}),this._collections.push(n)},MongoManager.prototype.createCollection=function(e){index_1.ResolveIOServer.getMainDB().createCollection(e.collectionName,e.collectionOptions)},MongoManager.prototype.collections=function(){return this._collections},MongoManager.prototype.collection=function(n){return this._collections.find(function(e){return e.collectionName===n})},MongoManager.prototype.delay=function(n){return __awaiter(this,void 0,void 0,function(){return __generator(this,function(e){return[2,new Promise(function(e){return setTimeout(e,n)})]})})},MongoManager.prototype.find=function(i,r,a){return void 0===r&&(r={}),__awaiter(this,void 0,void 0,function(){var n,t,o=this;return __generator(this,function(e){return n=this.generateCacheKey([i],"find",[r,a]),void 0!==(t=this.getFromCache(n))?[2,t]:this._operationInProgress.has(n)?[2,this._operationInProgress.get(n).promise]:(t=this.executeFind(i,r,a,n),this._operationInProgress.set(n,{promise:t,invalidatedDuringExecution:!1}),t.finally(function(){return o._operationInProgress.delete(n)}),[2,t])})})},MongoManager.prototype.executeFind=function(o,i,r,a){return __awaiter(this,void 0,void 0,function(){var n,t;return __generator(this,function(e){switch(e.label){case 0:return n=index_1.ResolveIOServer.getMainDB().collection(o),t=new monitor_manager_1.MonitorMongo("find",o,JSON.stringify([i,r])),[4,n.find(i,r).toArray()];case 1:return n=e.sent(),t.finish(),this._operationInProgress.get(a).invalidatedDuringExecution||this.addToCache([o],a,n),[2,n]}})})},MongoManager.prototype.findOne=function(i,r,a){return void 0===r&&(r={}),__awaiter(this,void 0,void 0,function(){var n,t,o=this;return __generator(this,function(e){return n=this.generateCacheKey([i],"findOne",[r,a]),void 0!==(t=this.getFromCache(n))?[2,t]:this._operationInProgress.has(n)?[2,this._operationInProgress.get(n).promise]:(t=this.executeFindOne(i,r,a,n),this._operationInProgress.set(n,{promise:t,invalidatedDuringExecution:!1}),t.finally(function(){return o._operationInProgress.delete(n)}),[2,t])})})},MongoManager.prototype.executeFindOne=function(o,i,r,a){return __awaiter(this,void 0,void 0,function(){var n,t;return __generator(this,function(e){switch(e.label){case 0:return n=index_1.ResolveIOServer.getMainDB().collection(o),t=new monitor_manager_1.MonitorMongo("findOne",o,JSON.stringify([i,r])),[4,n.findOne(i,r)];case 1:return n=e.sent(),t.finish(),this._operationInProgress.get(a).invalidatedDuringExecution||this.addToCache([o],a,n),[2,n]}})})},MongoManager.prototype.aggregate=function(r,a,s){return __awaiter(this,void 0,void 0,function(){var n,t,o,i=this;return __generator(this,function(e){return n=__spreadArray([r],__read(a.flatMap(function(e){var n;return null!=(n=e.$lookup)&&n.from?[e.$lookup.from]:[]})),!1),t=this.generateCacheKey(n,"aggregate",[a,s]),void 0!==(o=this.getFromCache(t))?[2,o]:this._operationInProgress.has(t)?[2,this._operationInProgress.get(t).promise]:(o=this.executeAggregate(r,a,s,t,n),this._operationInProgress.set(t,{promise:o,invalidatedDuringExecution:!1}),o.finally(function(){return i._operationInProgress.delete(t)}),[2,o])})})},MongoManager.prototype.executeAggregate=function(o,i,r,a,s){return __awaiter(this,void 0,void 0,function(){var n,t;return __generator(this,function(e){switch(e.label){case 0:return n=index_1.ResolveIOServer.getMainDB().collection(o),t=new monitor_manager_1.MonitorMongo("aggregate",o,JSON.stringify([i,r])),[4,n.aggregate(i,r).toArray()];case 1:return n=e.sent(),t.finish(),this._operationInProgress.get(a).invalidatedDuringExecution||this.addToCache(s,a,n),[2,n]}})})},MongoManager.prototype.countDocuments=function(i,r,a){return void 0===r&&(r={}),__awaiter(this,void 0,void 0,function(){var n,t,o=this;return __generator(this,function(e){return n=this.generateCacheKey([i],"countDocuments",[r,a]),void 0!==(t=this.getFromCache(n))?[2,t]:this._operationInProgress.has(n)?[2,this._operationInProgress.get(n).promise]:(t=this.executeCountDocuments(i,r,a,n),this._operationInProgress.set(n,{promise:t,invalidatedDuringExecution:!1}),t.finally(function(){return o._operationInProgress.delete(n)}),[2,t])})})},MongoManager.prototype.executeCountDocuments=function(o,i,r,a){return __awaiter(this,void 0,void 0,function(){var n,t;return __generator(this,function(e){switch(e.label){case 0:return n=index_1.ResolveIOServer.getMainDB().collection(o),t=new monitor_manager_1.MonitorMongo("countDocuments",o,JSON.stringify([i,r])),[4,n.countDocuments(i,r)];case 1:return n=e.sent(),t.finish(),this._operationInProgress.get(a).invalidatedDuringExecution||this.addToCache([o],a,n),[2,n]}})})},MongoManager.prototype.generateCacheKey=function(e,n,t){e=JSON.stringify({collections:e,functionName:n,args:t});return crypto.createHash("sha256").update(e).digest("hex")},MongoManager.prototype.getFromCache=function(n){try{var e=this._nodeCache.get(n);if(e)return JSON.parse(e,common_1.dateReviver)}catch(e){this._nodeCache.del(n)}},MongoManager.prototype.addToCache=function(e,n,t){var o,i;if((0,common_1.getBinarySize)(JSON.stringify(t))<1e6){var r=this._nodeCache.getStats().vsize;if(r>this._heapLimit){var a=0,s=this._nodeCache.keys();try{for(var c=__values(s),l=c.next();!l.done;l=c.next()){var d=l.value;if(this._nodeCache.del(d),a+=1,(r=this._nodeCache.getStats().vsize)<.75*this._heapLimit)break}}catch(e){o={error:e}}finally{try{l&&!l.done&&(i=c.return)&&i.call(c)}finally{if(o)throw o.error}}console.log("Query Cache: Too Big, - Deleted: "+a+" - "+r)}this._nodeCache.set(n,JSON.stringify(t)),this._cacheMap.push({collections:e,key:n})}},MongoManager.prototype.invalidateQueryCache=function(n){var t,e,o=this._cacheMap.filter(function(e){return e.collections.includes(n)});try{for(var i=__values(o),r=i.next();!r.done;r=i.next()){var a=r.value;this._nodeCache.del(a.key),this._operationInProgress.has(a.key)&&(this._operationInProgress.get(a.key).invalidatedDuringExecution=!0)}}catch(e){t={error:e}}finally{try{r&&!r.done&&(e=i.return)&&e.call(i)}finally{if(t)throw t.error}}this._cacheMap=this._cacheMap.filter(function(e){return!e.collections.includes(n)})},MongoManager.prototype.setupChangeStream=function(){var n=this,t=index_1.ResolveIOServer.getMongoConnection().db(this._serverConfig.DATABASE).watch();t.on("change",function(t){return __awaiter(n,void 0,void 0,function(){var n;return __generator(this,function(e){return t.ns&&t.ns.coll&&(n=t.ns.coll,this.collection(n))&&this.invalidateQueryCache(n),[2]})})}),t.on("error",function(e){console.log(new Date,"Mongo change stream error. Restart..."),t.close(),setTimeout(function(){return n.setupChangeStream()},5e3)}),t.on("close",function(){console.log(new Date,"Mongo change stream closed. Restarting..."),setTimeout(function(){return n.setupChangeStream()},5e3)})},MongoManager}(),MongoManagerModel=(exports.MongoManager=MongoManager,function(e){var n;this.collection_main=null,this.collection_version=null,"users"===e.collectionName?this.collection_main=new MongoManagerUserCollection(e):this.collection_main=new MongoManagerCollection(e),e.useVersionCollection&&(this.collection_main.useVersions=!0,(n=(0,common_1.deepCopy)(e.schema))._id.type="Object",n._id.blackbox=!0,n={collectionName:e.collectionName+".versions",schema:n,useVersionCollection:!1,useReportBuilder:!1,reportBuilderLookupTables:[],timestamps:!0,createLogs:!1,checkSchema:!1,collectionOptions:null},"users"===e.collectionName?this.collection_version=new MongoManagerUserCollection(n):this.collection_version=new MongoManagerCollection(n),this.collection_version.createIndex({"_id._id":1,"_id.__v":1}),this.collection_main.versionCollection=e.collectionName+".versions")}),MongoManagerCollection=(exports.MongoManagerModel=MongoManagerModel,function(){function MongoManagerCollection(e){var n,t=this,o=(this.collectionName="",this.checkSchema=!1,this.simplschema=null,this.rbSchema=null,this.timestamps=!1,this.useVersions=!1,this.versionCollection="",this.createLogs=!0,this.useRB=!1,this.collectionName=e.collectionName,this.simplschema=new simpl_schema_1.default(e.schema),this.timestamps=e.timestamps,this.createLogs=e.createLogs,this.checkSchema=e.checkSchema,this.collectionOptions=e.collectionOptions,e.useReportBuilder&&(this.useRB=!0,n=(0,common_1.deepCopy)(e.schema),e=(0,common_1.buildRbLookups)(e.reportBuilderLookupTables,n,[]),this.rbSchema=(0,common_1.buildRbSchema)(e)),setInterval(function(){index_1.ResolveIOServer&&index_1.ResolveIOServer.getMainServer()&&index_1.ResolveIOServer.getMongoManager()&&index_1.ResolveIOServer.getMongoManager().getServerCollections().length&&(index_1.ResolveIOServer.getMongoManager().registerCollection(t),clearInterval(o))},1))}var e;return MongoManagerCollection.prototype.extractIndexNameFromError=function(e){var n=e.match(/Index already exists with a different name: (\S+)/);return(n=!n||n.length<=1?e.match(/existing index:.*name: "([^"]+)"/):n)&&1<n.length?n[1]:null},MongoManagerCollection.prototype.aggregate=function(i,r,e){var a=this;return void 0===e&&(e=!1),new Promise(function(n,t){var o;e?(o=new monitor_manager_1.MonitorMongo("aggregate",a.collectionName,JSON.stringify([i,r])),index_1.ResolveIOServer.getMainDB().collection(a.collectionName,a.collectionOptions).aggregate(i,r).toArray().then(function(e){o.finish(),n(e)},function(e){o.finish(),console.log(new Date,"Error Aggregate",a.collectionName,i,r,e),t(e)})):index_1.ResolveIOServer.getMongoManager().aggregate(a.collectionName,i,r).then(function(e){n(e)},function(e){t(e)})})},MongoManagerCollection.prototype.aggregateCount=function(r,a){return __awaiter(this,void 0,void 0,function(){var i=this;return __generator(this,function(e){return[2,new Promise(function(n,t){var o=new monitor_manager_1.MonitorMongo("aggregateCount",i.collectionName,JSON.stringify([r,a]));index_1.ResolveIOServer.getMainDB().collection(i.collectionName,i.collectionOptions).aggregate(r,a).toArray().then(function(e){o.finish(),n(e.length)},function(e){o.finish(),console.log(new Date,"Error Aggregate",i.collectionName,r,a,e),t(0)})})]})})},MongoManagerCollection.prototype.aggregateCursor=function(e,n){var t=new monitor_manager_1.MonitorMongo("aggregateCursor",this.collectionName,JSON.stringify([e,n])),e=index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).aggregate(e,n);return e.on("close",function(e){return t.finish(),e}),e},MongoManagerCollection.prototype.aggregateStream=function(e,n,t){var o=new monitor_manager_1.MonitorMongo("aggregateStream",this.collectionName,JSON.stringify([e,n,t])),e=index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).aggregate(e,n).stream(t);return e.on("end",function(e){return o.finish(),e}).on("error",function(e){return o.finish(),e}).on("close",function(e){return o.finish(),e}),e},MongoManagerCollection.prototype.bulkWrite=function(M,D,b,R){var e=this;return void 0===b&&(b=!1),void 0===R&&(R=!1),new Promise(function(f,C){return __awaiter(e,void 0,void 0,function(){var y,O,S,N,t,o,i,r,n,a,I,s,c,l,w,d,u,g,_,h,m,v,p,x=this;return __generator(this,function(e){switch(e.label){case 0:return M.length?(y=[],O=[],S=[],N=[],t=[],o=[],i=[],M.forEach(function(e){var n=Object.keys(e)[0];"updateOne"===n||"deleteOne"===n||"replaceOne"===n?t.push(e[n].filter):"updateMany"===n?o.push(e[n].filter):"deleteMany"===n&&i.push(e[n].filter)}),t.length||o.length||i.length?[4,this.find({$or:__spreadArray(__spreadArray(__spreadArray([],__read(t),!1),__read(o),!1),__read(i),!1)})]:[3,2]):(f(null),[2]);case 1:return n=e.sent(),[3,3];case 2:n=[],e.label=3;case 3:r=n,a=function(e,n){return n.split(".").reduce(function(e,n){return Array.isArray(e)?e.map(function(e){return e[n]}).filter(function(e){return void 0!==e}):e&&e[n]},e)},I=function(n){return r.filter(function(e){return s(n,e)})},s=function(t,o){return"object"==typeof t&&null!=t&&Object.keys(t).every(function(e){var n=t[e];if(e.startsWith("$")){if("$and"===e)return Array.isArray(n)&&n.every(function(e){return s(e,o)});if("$or"===e)return Array.isArray(n)&&n.some(function(e){return s(e,o)});if("$nor"===e)return Array.isArray(n)&&!n.some(function(e){return s(e,o)});if("$not"===e)return!s(n,o);throw new Error("Unsupported operator in filter: ".concat(e))}return e=a(o,e),Array.isArray(e)?e.some(function(e){return c(n,e)}):c(n,e)})},c=function(t,o){return t instanceof Object&&!(t instanceof RegExp)?Object.keys(t).every(function(e){var n=t[e];switch(e){case"$in":return Array.isArray(n)&&n.includes(o);case"$nin":return Array.isArray(n)&&!n.includes(o);case"$ne":return o!==n;case"$gt":return n<o;case"$gte":return n<=o;case"$lt":return o<n;case"$lte":return o<=n;case"$exists":return void 0!==o===n;case"$regex":return new RegExp(n,t.$options||"").test(o);case"$eq":return o===n;default:throw new Error("Unsupported operator in condition: ".concat(e))}}):o===t},l=function(t){var n,e,o,i,r,a,s,c=Object.keys(t)[0],l=t[c];if("insertOne"===c){if(l.document._id||(l.document._id=objectIdHexString()),w.useVersions&&!l.document.hasOwnProperty("__v")&&(l.document.__v=0),w.timestamps&&(s=new Date,l.document.createdAt||(l.document.createdAt=s),l.document.updatedAt||(l.document.updatedAt=s)),w.checkSchema&&!R)if(!(f=w.simplschema.newContext()).validate(l.document))return console.log(new Date,w.collectionName,"Schema Errors - bulkWrite - insertOne",f.validationErrors()),index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(index_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on bulkWrite insertOne - "+w.collectionName,[f.validationErrors(),l.document]),C(f.validationErrors()),"continue";!b&&w.createLogs&&y.push({insertOne:{document:{_id:objectIdHexString(),type:"document",collection:w.collectionName,id_document:l.document._id,payload:(0,common_1.getBinarySize)(JSON.stringify([t,D]))<2e5?JSON.stringify([t,D],null,2):"Too Big",method:c,id_user:"",user:"",messageId:0,route:""}}}),N.push(t)}else if("updateOne"===c){if((m=I(l.filter)).length)try{n=void 0;for(var d=__values(m),u=d.next();!u.done;u=d.next()){var g=u.value;if(w.timestamps&&(l.update.$set?l.update.$set.updatedAt=new Date:l.update.$set={updatedAt:new Date}),w.checkSchema&&!R)if(!(f=w.simplschema.newContext()).validate(l.update,{modifier:!0})){console.log(new Date,w.collectionName,"Schema Errors - bulkWrite - updateOne",f.validationErrors()),index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(index_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on bulkWrite updateOne - "+w.collectionName,[f.validationErrors(),l.update,g]),C(f.validationErrors());continue}w.useVersions&&((M=(0,common_1.deepCopy)(g))._id={_id:M._id,__v:M.__v},O.push({insertOne:{document:M}}),objectContainsPropertyDeep(l.update,"__v")||(l.update.$inc?l.update.$inc.__v=1:l.update.$inc={__v:1}),4<=M.__v)&&S.push({deleteMany:{filter:{"_id._id":M._id._id,"_id.__v":{$lt:M.__v-4}}}}),!b&&w.createLogs&&y.push({insertOne:{document:{_id:objectIdHexString(),type:"document",collection:w.collectionName,id_document:g._id,payload:(0,common_1.getBinarySize)(JSON.stringify([t,D,g]))<2e5?JSON.stringify([t,D,g],null,2):"Too Big",method:c,id_user:"",user:"",messageId:0,route:""}}}),N.push(t)}}catch(e){n={error:e}}finally{try{u&&!u.done&&(e=d.return)&&e.call(d)}finally{if(n)throw n.error}}else if(l.upsert){if(w.checkSchema&&!R)if(!(f=w.simplschema.newContext()).validate(l.update,{modifier:!0}))return console.log(new Date,w.collectionName,"Schema Errors - bulkWrite - updateOne",f.validationErrors()),index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(index_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on bulkWrite updateOne - "+w.collectionName,[f.validationErrors(),l.update]),C(f.validationErrors()),"continue";objectContainsPropertyDeep(l.update,"_id")||(l.update.$setOnInsert._id=objectIdHexString()),!w.useVersions||objectContainsPropertyDeep(l.update,"__v")||(l.update.$setOnInsert.__v=0),w.timestamps&&(objectContainsPropertyDeep(l.update,"createdAt")||(l.update.$setOnInsert.createdAt=new Date),objectContainsPropertyDeep(l.update,"updatedAt")||(l.update.$setOnInsert.updatedAt=new Date)),!b&&w.createLogs&&y.push({insertOne:{document:{_id:objectIdHexString(),type:"document",collection:w.collectionName,id_document:l.update.$setOnInsert._id,payload:(0,common_1.getBinarySize)(JSON.stringify([t,D,l.update]))<2e5?JSON.stringify([t,D,l.update],null,2):"Too Big",method:c,id_user:"",user:"",messageId:0,route:""}}}),N.push(t)}}else if("replaceOne"===c){if(w.timestamps&&(l.replacement.updatedAt=new Date),(m=I(l.filter)).length)try{o=void 0;for(var _=__values(m),h=_.next();!h.done;h=_.next()){g=h.value;if(w.checkSchema&&!R)if(!(f=w.simplschema.newContext()).validate(l.replacement)){console.log(new Date,w.collectionName,"Schema Errors - bulkWrite - insertOne",f.validationErrors()),index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(index_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on bulkWrite replaceOne - "+w.collectionName,[f.validationErrors(),l.replacement]),C(f.validationErrors());continue}w.useVersions&&((M=(0,common_1.deepCopy)(g))._id={_id:M._id,__v:M.__v},O.push({insertOne:{document:M}}),l.replacement.__v=M.__v+1,4<=M.__v)&&S.push({deleteMany:{filter:{"_id._id":M._id._id,"_id.__v":{$lt:M.__v-4}}}}),!b&&w.createLogs&&y.push({insertOne:{document:{_id:objectIdHexString(),type:"document",collection:w.collectionName,id_document:g._id,payload:(0,common_1.getBinarySize)(JSON.stringify([t,D,g]))<2e5?JSON.stringify([t,D,g],null,2):"Too Big",method:c,id_user:"",user:"",messageId:0,route:""}}}),N.push(t)}}catch(e){o={error:e}}finally{try{h&&!h.done&&(i=_.return)&&i.call(_)}finally{if(o)throw o.error}}else if(l.upsert){if(w.checkSchema&&!R)if(!(f=w.simplschema.newContext()).validate(l.replacement))return console.log(new Date,w.collectionName,"Schema Errors - bulkWrite - replaceOne",f.validationErrors()),index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(index_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on bulkWrite replaceOne - "+w.collectionName,[f.validationErrors(),l.replacement]),C(f.validationErrors()),"continue";objectContainsPropertyDeep(l.replacement,"_id")||(l.replacement._id=objectIdHexString()),!w.useVersions||objectContainsPropertyDeep(l.replacement,"__v")||(l.replacement.__v=0),w.timestamps&&(objectContainsPropertyDeep(l.replacement,"createdAt")||(l.replacement.createdAt=new Date),objectContainsPropertyDeep(l.replacement,"updatedAt")||(l.replacement.updatedAt=new Date)),!b&&w.createLogs&&y.push({insertOne:{document:{_id:objectIdHexString(),type:"document",collection:w.collectionName,id_document:l.replacement._id,payload:(0,common_1.getBinarySize)(JSON.stringify([t,D,l.replacement]))<2e5?JSON.stringify([t,D,l.replacement],null,2):"Too Big",method:c,id_user:"",user:"",messageId:0,route:""}}}),N.push(t)}}else if("deleteOne"===c)(m=I(l.filter)).forEach(function(e){var n;x.useVersions&&((n=(0,common_1.deepCopy)(e))._id={_id:n._id,__v:n.__v},O.push({insertOne:{document:n}}),4<=n.__v)&&S.push({deleteMany:{filter:{"_id._id":n._id._id,"_id.__v":{$lt:n.__v-4}}}}),!b&&x.createLogs&&y.push({insertOne:{document:{_id:objectIdHexString(),type:"document",collection:x.collectionName,id_document:e._id,payload:(0,common_1.getBinarySize)(JSON.stringify([t,D,e]))<2e5?JSON.stringify([t,D,e],null,2):"Too Big",method:c,id_user:"",user:"",messageId:0,route:""}}}),N.push(t)});else if("updateMany"===c){w.timestamps&&(l.update.$set?l.update.$set.updatedAt=new Date:l.update.$set={updatedAt:new Date}),!w.useVersions||objectContainsPropertyDeep(l.update,"__v")||(l.update.$inc?l.update.$inc.__v=1:l.update.$inc={__v:1});var m=I(l.filter);try{r=void 0;for(var v=__values(m),p=v.next();!p.done;p=v.next()){var f,M,g=p.value;if(w.checkSchema&&!R)if(!(f=w.simplschema.newContext()).validate(l.update,{modifier:!0})){console.log(new Date,w.collectionName,"Schema Errors - bulkWrite - insertOne",f.validationErrors()),index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(index_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on bulkWrite updateMany - "+w.collectionName,[f.validationErrors(),l.update]),C(f.validationErrors());continue}w.useVersions&&((M=(0,common_1.deepCopy)(g))._id={_id:M._id,__v:M.__v},O.push({insertOne:{document:M}}),4<=M.__v)&&S.push({deleteMany:{filter:{"_id._id":M._id._id,"_id.__v":{$lt:M.__v-4}}}}),!b&&w.createLogs&&y.push({insertOne:{document:{_id:objectIdHexString(),type:"document",collection:w.collectionName,id_document:g._id,payload:(0,common_1.getBinarySize)(JSON.stringify([t,D]))<2e5?JSON.stringify([t,D],null,2):"Too Big",method:c,id_user:"",user:"",messageId:0,route:""}}}),N.push(t)}}catch(e){r={error:e}}finally{try{p&&!p.done&&(a=v.return)&&a.call(v)}finally{if(r)throw r.error}}}else"deleteMany"===c&&(m=I(l.filter)).forEach(function(e){var n;x.useVersions&&((n=(0,common_1.deepCopy)(e))._id={_id:n._id,__v:n.__v},O.push({insertOne:{document:n}}),4<=n.__v)&&S.push({deleteMany:{filter:{"_id._id":n._id._id,"_id.__v":{$lt:n.__v-4}}}}),!b&&x.createLogs&&y.push({insertOne:{document:{_id:objectIdHexString(),type:"document",collection:x.collectionName,id_document:e._id,payload:(0,common_1.getBinarySize)(JSON.stringify([t,D]))<2e5?JSON.stringify([t,D],null,2):"Too Big",method:c,id_user:"",user:"",messageId:0,route:""}}}),N.push(t)})},w=this;try{for(d=__values(M),u=d.next();!u.done;u=d.next())g=u.value,l(g)}catch(e){v={error:e}}finally{try{u&&!u.done&&(p=d.return)&&p.call(d)}finally{if(v)throw v.error}}e.label=4;case 4:return(e.trys.push([4,12,,13]),N.length)?(_=new monitor_manager_1.MonitorMongo("bulkWrite",this.collectionName,JSON.stringify([N.slice(-100),D])),[4,index_1.ResolveIOServer.getMainDB().collection(this.collectionName).bulkWrite(N,D)]):[3,10];case 5:return(h=e.sent(),index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),O.length||S.length)?[4,index_1.ResolveIOServer.getMainDB().collection(this.versionCollection).bulkWrite(__spreadArray(__spreadArray([],__read(O),!1),__read(S),!1))]:[3,7];case 6:e.sent(),e.label=7;case 7:return b||!y.length?[3,9]:[4,log_collection_1.Logs.bulkWrite(y)];case 8:e.sent(),e.label=9;case 9:return _.finish(),f(h),[3,11];case 10:f(null),e.label=11;case 11:return[3,13];case 12:return m=e.sent(),console.log(new Date,"Error Bulk Write",this.collectionName,N.slice(-100),D,m),C(m),[3,13];case 13:return[2]}})})})},MongoManagerCollection.prototype.countDocuments=function(a,s,c){var e=this;return void 0===a&&(a={}),void 0===c&&(c=!1),new Promise(function(i,r){return __awaiter(e,void 0,void 0,function(){var n,t,o=this;return __generator(this,function(e){switch(e.label){case 0:return(e.trys.push([0,4,,5]),0===Object.keys(a).length)?[4,index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).estimatedDocumentCount(s)]:[3,2];case 1:return t=e.sent(),i(t),[3,3];case 2:c?(n=new monitor_manager_1.MonitorMongo("countDocuments",this.collectionName,JSON.stringify([a,s])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).countDocuments(a,s).then(function(e){n.finish(),i(e)},function(e){n.finish(),console.log(new Date,"Error Count Documents",o.collectionName,a,s,e),r(e)})):index_1.ResolveIOServer.getMongoManager().countDocuments(this.collectionName,a,s).then(function(e){return i(e)},function(e){return r(e)}),e.label=3;case 3:return[3,5];case 4:return t=e.sent(),console.log(new Date,"Error Count Documents (Stats)",this.collectionName,a,s,t),r(t),[3,5];case 5:return[2]}})})})},MongoManagerCollection.prototype.create=function(e,n){return Array.isArray(e)?1===e.length?this.insertOne(e[0],n):this.insertMany(e,n):this.insertOne(e,n)},MongoManagerCollection.prototype.createIndex=function(i,r){var e=this;return new Promise(function(t,o){return __awaiter(e,void 0,void 0,function(){var n;return __generator(this,function(e){switch(e.label){case 0:return e.trys.push([0,2,,3]),[4,index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).indexes()];case 1:return(n=e.sent(),n.some(function(e){return JSON.stringify(e.key)===JSON.stringify(i)}))?[2,t("Index already exists")]:(MongoManagerCollection.indexQueue.push({action:"createIndex",collection:this,specs:[i],options:r},function(e,n){e?o(e):t(n)}),[3,3]);case 2:return n=e.sent(),o(n),[3,3];case 3:return[2]}})})})},MongoManagerCollection.prototype.createIndexes=function(r,a){var e=this;return new Promise(function(o,i){return __awaiter(e,void 0,void 0,function(){var t,n;return __generator(this,function(e){switch(e.label){case 0:return e.trys.push([0,2,,3]),[4,index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).indexes()];case 1:return(t=e.sent(),0===(n=r.filter(function(n){return!t.some(function(e){return JSON.stringify(e.key)===JSON.stringify(n.key)})})).length)?[2,o(["All indexes already exist"])]:(MongoManagerCollection.indexQueue.push({action:"createIndexes",collection:this,specs:[n],options:a},function(e,n){e?i(e):o(n)}),[3,3]);case 2:return n=e.sent(),i(n),[3,3];case 3:return[2]}})})})},MongoManagerCollection.prototype.deleteMany=function(l,d,u){var e=this;return void 0===l&&(l={}),void 0===u&&(u=!1),new Promise(function(s,c){return __awaiter(e,void 0,void 0,function(){var n,t,o,i,r,a=this;return __generator(this,function(e){switch(e.label){case 0:return!this.createLogs||u?[3,2]:[4,this.find(l)];case 1:for(n=e.sent(),t=0;t<n.length;t++)o=n[t],log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:this.collectionName,id_document:o._id,payload:(0,common_1.getBinarySize)(JSON.stringify([o,l,d]))<2e5?JSON.stringify([o,l,d],null,2):"Too Big",method:"deleteMany",id_user:"",user:"",messageId:0,route:""}),this.useVersions&&((i=(0,common_1.deepCopy)(o))._id={_id:o._id,__v:o.__v},index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:o._id,__v:o.__v}},i,{upsert:!0}),index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({$and:[{"_id._id":o._id},{"_id.__v":{$lt:o.__v-1}}]}));e.label=2;case 2:return r=new monitor_manager_1.MonitorMongo("deleteMany",this.collectionName,JSON.stringify([l,d])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).deleteMany(l,d).then(function(e){r.finish(),e.acknowledged?(index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(a.collectionName),s(e.deletedCount)):c(e.acknowledged)},function(e){r.finish(),console.log(new Date,"Error Delete Many",a.collectionName,l,d,e),c(e)}),[2]}})})})},MongoManagerCollection.prototype.deleteOne=function(a,s,c){var e=this;return void 0===a&&(a={}),void 0===c&&(c=!1),new Promise(function(r,o){return __awaiter(e,void 0,void 0,function(){var i,n,t=this;return __generator(this,function(e){return this.createLogs&&!c?(i=new monitor_manager_1.MonitorMongo("findOneAndDelete",this.collectionName,JSON.stringify([a,s])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndDelete(a,s).then(function(o){return __awaiter(t,void 0,void 0,function(){var n,t;return __generator(this,function(e){return i.finish(),(n=o)?(this.createLogs&&!c&&log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:this.collectionName,id_document:n._id,payload:(0,common_1.getBinarySize)(JSON.stringify([n,a,s]))<2e5?JSON.stringify([n,a,s],null,2):"Too Big",method:"deleteOne",id_user:"",user:"",messageId:0,route:""}),this.useVersions&&((t=(0,common_1.deepCopy)(n))._id={_id:n._id,__v:n.__v},index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:n._id,__v:n.__v}},t,{upsert:!0}),index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({$and:[{"_id._id":n._id},{"_id.__v":{$lt:n.__v-1}}]})),index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),r(1)):r(0),[2]})})},function(e){i.finish(),console.log(new Date,"Error Find One And Delete",t.collectionName,a,s,e),o(e)})):(n=new monitor_manager_1.MonitorMongo("deleteOne",this.collectionName,JSON.stringify([a,s])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).deleteOne(a,s).then(function(e){n.finish(),e.acknowledged?(index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(t.collectionName),r(e.deletedCount)):o(e.acknowledged)},function(e){n.finish(),console.log(new Date,"Error Delete One",t.collectionName,a,s,e),o(e)})),[2]})})})},MongoManagerCollection.prototype.distinct=function(e,i,r){var a=this;return new Promise(function(n,t){var o=new monitor_manager_1.MonitorMongo("distinct",a.collectionName,JSON.stringify([e,i,r]));index_1.ResolveIOServer.getMainDB().collection(a.collectionName,a.collectionOptions).distinct(e,i,r).then(function(e){o.finish(),n(e)},function(e){o.finish(),console.log(new Date,"Error Distinct",a.collectionName,i,r,e),t(e)})})},MongoManagerCollection.prototype.drop=function(i){var r=this;return new Promise(function(n,t){var o=new monitor_manager_1.MonitorMongo("drop",r.collectionName,JSON.stringify([i]));index_1.ResolveIOServer.getMainDB().collection(r.collectionName,r.collectionOptions).drop(i).then(function(e){o.finish(),n(e)},function(e){o.finish(),console.log(new Date,"Error Count Documents",r.collectionName,i,e),t(e)})})},MongoManagerCollection.prototype.dropIndex=function(i,r){var a=this;return new Promise(function(n,t){var o=new monitor_manager_1.MonitorMongo("dropIndex",a.collectionName,JSON.stringify([i,r]));index_1.ResolveIOServer.getMainDB().collection(a.collectionName,a.collectionOptions).dropIndex(i,r).then(function(e){o.finish(),n(e)},function(e){o.finish(),console.log(new Date,"Error Drop Index",a.collectionName,i,r,e),t(e)})})},MongoManagerCollection.prototype.dropIndexes=function(i){var r=this;return new Promise(function(n,t){var o=new monitor_manager_1.MonitorMongo("dropIndexes",r.collectionName,JSON.stringify([i]));index_1.ResolveIOServer.getMainDB().collection(r.collectionName,r.collectionOptions).dropIndexes(i).then(function(e){o.finish(),n(e)},function(e){o.finish(),console.log(new Date,"Error Drop Indexes",r.collectionName,i,e),t(e)})})},MongoManagerCollection.prototype.find=function(r,a,s){var e=this;return void 0===r&&(r={}),void 0===s&&(s=!1),new Promise(function(o,i){return __awaiter(e,void 0,void 0,function(){var n,t=this;return __generator(this,function(e){return s?(n=new monitor_manager_1.MonitorMongo("find",this.collectionName,JSON.stringify([r,a])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).find(r,a).toArray().then(function(e){n.finish(),o(e)},function(e){n.finish(),console.log(new Date,"Error Find",t.collectionName,r,a,e),i(e)})):index_1.ResolveIOServer.getMongoManager().find(this.collectionName,r,a).then(function(e){o(e)},function(e){i(e)}),[2]})})})},MongoManagerCollection.prototype.findById=function(i,r){var a=this;return new Promise(function(n,t){var o=new monitor_manager_1.MonitorMongo("findById",a.collectionName,JSON.stringify([{_id:i},r]));index_1.ResolveIOServer.getMainDB().collection(a.collectionName,a.collectionOptions).findOne({_id:i},r).then(function(e){o.finish(),n(e)},function(e){o.finish(),console.log(new Date,"Error Find By Id",a.collectionName,{_id:i},r,e),t(e)})})},MongoManagerCollection.prototype.findCount=function(i,r){var a=this;return void 0===i&&(i={}),new Promise(function(n,t){var o=new monitor_manager_1.MonitorMongo("findCount",a.collectionName,JSON.stringify([i,r]));index_1.ResolveIOServer.getMainDB().collection(a.collectionName,a.collectionOptions).find(i,r).count().then(function(e){o.finish(),n(e)},function(e){o.finish(),console.log(new Date,"Error Find Count",a.collectionName,i,r,e),t(e)})})},MongoManagerCollection.prototype.findCursor=function(e,n){void 0===e&&(e={});var t=new monitor_manager_1.MonitorMongo("findCursor",this.collectionName,JSON.stringify([e,n])),o=index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).find(e,n);return o.on("close",function(e){return t.finish(),o.removeAllListeners(),e}),o},MongoManagerCollection.prototype.findStream=function(e,n,t){void 0===e&&(e={});var o=new monitor_manager_1.MonitorMongo("findStream",this.collectionName,JSON.stringify([e,n,t])),e=index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).find(e,n).stream(t);return e.on("end",function(e){return o.finish(),e}).on("error",function(e){return o.finish(),e}).on("close",function(e){return o.finish(),e}),e},MongoManagerCollection.prototype.findOne=function(i,r,e){var a=this;return void 0===i&&(i={}),void 0===e&&(e=!1),new Promise(function(n,t){var o;e?(o=new monitor_manager_1.MonitorMongo("findOne",a.collectionName,JSON.stringify([i,r])),index_1.ResolveIOServer.getMainDB().collection(a.collectionName,a.collectionOptions).findOne(i,r).then(function(e){o.finish(),n(e)},function(e){o.finish(),console.log(new Date,"Error Find One",a.collectionName,i,r,e),t(e)})):index_1.ResolveIOServer.getMongoManager().findOne(a.collectionName,i,r).then(function(e){n(e)},function(e){t(e)})})},MongoManagerCollection.prototype.findOneAndDelete=function(r,a,s){var e=this;return void 0===r&&(r={}),void 0===s&&(s=!1),new Promise(function(i,n){return __awaiter(e,void 0,void 0,function(){var t,o=this;return __generator(this,function(e){return t=new monitor_manager_1.MonitorMongo("findOneAndDelete",this.collectionName,JSON.stringify([r,a])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndDelete(r,a).then(function(e){t.finish();var n;e?(o.createLogs&&!s&&log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:o.collectionName,id_document:e._id,payload:(0,common_1.getBinarySize)(JSON.stringify([e,r,a]))<2e5?JSON.stringify([e,r,a],null,2):"Too Big",method:"findOneAndDelete",id_user:"",user:"",messageId:0,route:""}),o.useVersions&&((n=(0,common_1.deepCopy)(e))._id={_id:e._id,__v:e.__v},index_1.ResolveIOServer.getMongoManager().collection(o.versionCollection).replaceOne({_id:{_id:e._id,__v:e.__v}},n,{upsert:!0})),index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(o.collectionName),i(e)):i(null)},function(e){t.finish(),console.log(new Date,"Error Find One And Delete",o.collectionName,r,a,e),n(e)}),[2]})})})},MongoManagerCollection.prototype.findOneAndReplace=function(s,c,l,d,u){var e=this;return void 0===s&&(s={}),void 0===d&&(d=!1),void 0===u&&(u=!1),new Promise(function(r,a){return __awaiter(e,void 0,void 0,function(){var n,t,o,i=this;return __generator(this,function(e){return!this.checkSchema||u||(n=this.simplschema.newContext(),n.validate(c))?(t=new Date,this.timestamps&&(c.updatedAt=t),l&&l.upsert&&(c._id||(c._id=objectIdHexString()),this.useVersions&&(c.__v=0),this.timestamps)&&!c.createdAt&&(c.createdAt=t),o=new monitor_manager_1.MonitorMongo("findOneAndReplace",this.collectionName,JSON.stringify([s,c,l])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndReplace(s,c,l).then(function(e){o.finish();var n;e?(i.createLogs&&!d&&log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:i.collectionName,id_document:e._id,payload:(0,common_1.getBinarySize)(JSON.stringify([e,s,c,l]))<2e5?JSON.stringify([e,s,c,l],null,2):"Too Big",method:"findOneAndReplace",id_user:"",user:"",messageId:0,route:""}),i.useVersions&&((n=(0,common_1.deepCopy)(e))._id={_id:e._id,__v:e.__v},index_1.ResolveIOServer.getMongoManager().collection(i.versionCollection).replaceOne({_id:{_id:e._id,__v:e.__v}},n,{upsert:!0})),index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(i.collectionName),r(e)):r(null)},function(e){o.finish(),console.log(new Date,"Error Find One And Replace",i.collectionName,s,l,e),a(e)})):(console.log(new Date,this.collectionName,"Schema Errors - findOneAndReplace",n.validationErrors()),index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(index_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on findOneAndReplace - "+this.collectionName,[n.validationErrors(),c]),a(n.validationErrors())),[2]})})})},MongoManagerCollection.prototype.findOneAndUpdate=function(s,c,l,d,u){var e=this;return void 0===s&&(s={}),void 0===d&&(d=!1),void 0===u&&(u=!1),new Promise(function(r,a){return __awaiter(e,void 0,void 0,function(){var n,t,o,i=this;return __generator(this,function(e){return!this.checkSchema||u||(n=this.simplschema.newContext(),n.validate(c,{modifier:!0}))?(this.timestamps&&(t=new Date,c.$set?c.$set.updatedAt=t:c.$set={updatedAt:t}),l&&l.upsert&&(c.$setOnInsert?(c.$setOnInsert._id||(c.$setOnInsert._id=objectIdHexString()),this.timestamps&&!c.$setOnInsert.createdAt&&(c.$setOnInsert.createdAt=new Date)):this.timestamps?c.$setOnInsert={_id:objectIdHexString(),createdAt:new Date}:c.$setOnInsert={_id:objectIdHexString()}),o=new monitor_manager_1.MonitorMongo("findOneAndUpdate",this.collectionName,JSON.stringify([s,c,l])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndUpdate(s,c,l).then(function(e){o.finish();var n;e?(i.createLogs&&!d&&log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:i.collectionName,id_document:e._id,payload:(0,common_1.getBinarySize)(JSON.stringify([e,s,c,l]))<2e5?JSON.stringify([e,s,c,l],null,2):"Too Big",method:"findOneAndUpdate",id_user:"",user:"",messageId:0,route:""}),i.useVersions&&((n=(0,common_1.deepCopy)(e))._id={_id:e._id,__v:e.__v},index_1.ResolveIOServer.getMongoManager().collection(i.versionCollection).replaceOne({_id:{_id:e._id,__v:e.__v}},n,{upsert:!0})),index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(i.collectionName),r(e)):r(null)},function(e){o.finish(),console.log(new Date,"Error Find One And Update",i.collectionName,s,l,e),a(e)})):(console.log(new Date,this.collectionName,"Schema Errors - findOneAndUpdate",n.validationErrors()),index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(index_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on findOneAndUpdate - "+this.collectionName,[n.validationErrors(),c]),a(n.validationErrors())),[2]})})})},MongoManagerCollection.prototype.indexes=function(i){var r=this;return new Promise(function(n,t){var o=new monitor_manager_1.MonitorMongo("indexes",r.collectionName,JSON.stringify([i]));index_1.ResolveIOServer.getMainDB().collection(r.collectionName,r.collectionOptions).indexes(i).then(function(e){o.finish(),n(e)},function(e){o.finish(),console.log(new Date,"Error Indexes",r.collectionName,i,e),t(e)})})},MongoManagerCollection.prototype.indexExists=function(i,r){var a=this;return new Promise(function(n,t){var o=new monitor_manager_1.MonitorMongo("indexExists",a.collectionName,JSON.stringify([i,r]));index_1.ResolveIOServer.getMainDB().collection(a.collectionName,a.collectionOptions).indexExists(i,r).then(function(e){o.finish(),n(e)},function(e){o.finish(),console.log(new Date,"Error Index Exists",a.collectionName,i,r,e),t(e)})})},MongoManagerCollection.prototype.insertMany=function(d,u,g,_,h){var e=this;return void 0===g&&(g=!1),void 0===_&&(_=!1),void 0===h&&(h=!1),new Promise(function(c,l){return __awaiter(e,void 0,void 0,function(){var t,n,o,i,r,a,s=this;return __generator(this,function(e){if(d.length){for(t=[],this.checkSchema&&!_&&d.forEach(function(e){var n=s.simplschema.newContext();n.validate(e)?t.push(!0):(console.log(new Date,s.collectionName,"Schema Errors - insertMany",n.validationErrors()),index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(index_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on insertMany - "+s.collectionName,[n.validationErrors(),e]),t.push(!1))}),n=this.checkSchema?d.filter(function(e,n){return t[n]}):d,o=0;o<n.length;o++)(i=n[o])._id||(i._id=objectIdHexString()),this.timestamps&&(r=new Date,i.createdAt||(i.createdAt=r),i.updatedAt||(i.updatedAt=r)),this.useVersions&&!i.hasOwnProperty("__v")&&(i.__v=0),this.createLogs&&!g&&log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:this.collectionName,id_document:i._id,payload:(0,common_1.getBinarySize)(JSON.stringify([i,u]))<2e5?JSON.stringify([i,u],null,2):"Too Big",method:"insertMany",id_user:"",user:"",messageId:0,route:""});n.length&&(a=null,h||(a=new monitor_manager_1.MonitorMongo("insertMany",this.collectionName,JSON.stringify([n,u]))),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).insertMany(n,u).then(function(e){a&&a.finish(),e.acknowledged?(index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(s.collectionName),c(n)):l(e.acknowledged)},function(e){a&&a.finish(),console.log(new Date,"Error Insert Many",s.collectionName,n,u,e),l(e)}))}else c([]);return[2]})})})},MongoManagerCollection.prototype.insertOne=function(s,c,l,d){var e=this;return void 0===l&&(l=!1),void 0===d&&(d=!1),new Promise(function(r,a){return __awaiter(e,void 0,void 0,function(){var n,t,o,i=this;return __generator(this,function(e){return s._id||(s._id=objectIdHexString()),!this.checkSchema||d||(n=this.simplschema.newContext(),n.validate(s))?(this.timestamps&&(t=new Date,s.createdAt||(s.createdAt=t),s.updatedAt||(s.updatedAt=t)),this.createLogs&&!l&&log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:this.collectionName,id_document:s._id,payload:(0,common_1.getBinarySize)(JSON.stringify([s,c]))<2e5?JSON.stringify([s,c],null,2):"Too Big",method:"insertOne",id_user:"",user:"",messageId:0,route:""}),this.useVersions&&!s.hasOwnProperty("__v")&&(s.__v=0),o=new monitor_manager_1.MonitorMongo("insertOne",this.collectionName,JSON.stringify([s,c])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).insertOne(s,c).then(function(e){o.finish(),e.acknowledged?(index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(i.collectionName),r(s)):a(e.acknowledged)},function(e){o.finish(),console.log(new Date,"Error Insert One",i.collectionName,s,c,e),a(e)})):(console.log(new Date,this.collectionName,"Schema Errors - insertOne",n.validationErrors()),index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(index_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on insertOne - "+this.collectionName,[n.validationErrors(),s]),a(n.validationErrors())),[2]})})})},MongoManagerCollection.prototype.listIndexes=function(e){var n=new monitor_manager_1.MonitorMongo("listIndexes",this.collectionName,JSON.stringify([e])),e=index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).listIndexes(e);return e.on("close",function(e){return n.finish(),e}),e},MongoManagerCollection.prototype.rename=function(i,r){var a=this;return new Promise(function(n,t){var o=new monitor_manager_1.MonitorMongo("rename",a.collectionName,JSON.stringify([i,r]));index_1.ResolveIOServer.getMainDB().collection(a.collectionName,a.collectionOptions).rename(i,r).then(function(e){o.finish(),n(e)},function(e){o.finish(),console.log(new Date,"Error Rename",a.collectionName,i,r,e),t(e)})})},MongoManagerCollection.prototype.replaceOne=function(m,v,p,f,M,y){var e=this;return void 0===f&&(f=!1),void 0===M&&(M=!1),void 0===y&&(y=null),new Promise(function(_,h){return __awaiter(e,void 0,void 0,function(){var n,t,o,i,r,a,s,c,l,d,u,g=this;return __generator(this,function(e){switch(e.label){case 0:return!this.checkSchema||M||(t=this.simplschema.newContext(),t.validate(v))?(n=new Date,this.timestamps&&(v.updatedAt=n),this.useVersions?y?[3,2]:[4,this.findOne(m,null,!0)]:[3,8]):(console.log(new Date,this.collectionName,"Schema Errors - replaceOne",t.validationErrors()),index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(index_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on replaceOne - "+this.collectionName,[t.validationErrors(),v]),h(t.validationErrors()),[2]);case 1:y=e.sent(),e.label=2;case 2:return y?(this.timestamps&&!v.createdAt&&y.createdAt&&(v.createdAt=y.createdAt),y.__v!==v.__v?[3,3]:(v.__v+=1,(t=(0,common_1.deepCopy)(y))._id={_id:y._id,__v:y.__v},index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:y._id,__v:y.__v}},t,{upsert:!0}),4<=y.__v&&index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({$and:[{"_id._id":y._id},{"_id.__v":{$lt:y.__v-4}}]}),this.createLogs&&!f&&log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:this.collectionName,id_document:y._id,payload:(0,common_1.getBinarySize)(JSON.stringify([y,m,v,p]))<2e5?JSON.stringify([y,m,v,p],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:""}),o=new monitor_manager_1.MonitorMongo("replaceOne",this.collectionName,JSON.stringify([m,v,p])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).replaceOne(m,v,p).then(function(e){o.finish(),e.acknowledged?(index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(g.collectionName),_(e.modifiedCount)):h(e.acknowledged)},function(e){o.finish(),console.log(new Date,"Error Replace One",g.collectionName,m,v,p,e),h(e)}),[3,5])):[3,6];case 3:return console.log("invalid version - "+this.collectionName,y.__v,v.__v),[4,index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).findOne({_id:{_id:y._id,__v:v.__v}},null,!0)];case 4:(i=e.sent())?(r=y._id,a=y.__v,(s=(0,common_1.getMongoMergeUpdatedDoc)(v,y,i))._id=r,s.__v=a+1,this.createLogs&&!f&&log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:this.collectionName,id_document:r,payload:(0,common_1.getBinarySize)(JSON.stringify(["invalidVersion - merge",y,m,s,p]))<2e5?JSON.stringify(["invalidVersion - merge",y,m,s,p],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:""}),c=new monitor_manager_1.MonitorMongo("replaceOne",this.collectionName,JSON.stringify([m,s,p])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).replaceOne(m,s,p).then(function(e){c.finish(),e.acknowledged?(index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(g.collectionName),_(e.modifiedCount)):h(e.acknowledged)},function(e){c.finish(),console.log(new Date,"Error Replace One",g.collectionName,m,s,p,e),h(e)})):h("Invalid Version And Could Not Find History - DB: "+y.__v+", Trying to update with :"+v.__v),e.label=5;case 5:return[3,7];case 6:p&&p.upsert?(this.timestamps&&(v.createdAt=n),v._id||(v._id=objectIdHexString()),v.__v=0,this.createLogs&&!f&&log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:this.collectionName,id_document:v._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",m,v,p]))<2e5?JSON.stringify(["upsert",m,v,p],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:""}),l=new monitor_manager_1.MonitorMongo("replaceOne",this.collectionName,JSON.stringify([m,v,p])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).replaceOne(m,v,p).then(function(e){l.finish(),e.acknowledged?(index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(g.collectionName),_(e.modifiedCount)):h(e.acknowledged)},function(e){l.finish(),console.log(new Date,"Error Replace One",g.collectionName,m,v,p,e),h(e)})):h("No Document"),e.label=7;case 7:return[3,9];case 8:p&&p.upsert&&(this.timestamps&&!v.createdAt&&(v.createdAt=n),v._id||(v._id=objectIdHexString()),v.__v=0),this.createLogs&&!f?(p?p.returnDocument="before":p={returnDocument:"before"},d=new monitor_manager_1.MonitorMongo("findOneAndReplace",this.collectionName,JSON.stringify([m,v,p])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndReplace(m,v,p).then(function(e){d.finish();e?log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:g.collectionName,id_document:e._id,payload:(0,common_1.getBinarySize)(JSON.stringify([e,m,v,p]))<2e5?JSON.stringify([e,m,v,p],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:""}):log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:g.collectionName,id_document:v._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",m,v,p]))<2e5?JSON.stringify(["upsert",m,v,p],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:""}),index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(g.collectionName),_(1)},function(e){d.finish(),console.log(new Date,"Error Find One And Replace",g.collectionName,m,v,p,e),h(e)})):(u=new monitor_manager_1.MonitorMongo("replaceOne",this.collectionName,JSON.stringify([m,v,p])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).replaceOne(m,v,p).then(function(e){u.finish(),e.acknowledged?(index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(g.collectionName),_(e.modifiedCount)):h(e.acknowledged)},function(e){u.finish(),console.log(new Date,"Error Replace One",g.collectionName,m,v,p,e),h(e)})),e.label=9;case 9:return[2]}})})})},MongoManagerCollection.prototype.updateMany=function(d,u,g,_,h,m){var e=this;return void 0===_&&(_=!1),void 0===h&&(h=!1),void 0===m&&(m=!1),new Promise(function(c,l){return __awaiter(e,void 0,void 0,function(){var n,t,o,i,r,a,s=this;return __generator(this,function(e){switch(e.label){case 0:return u.$inc&&"{}"!==JSON.stringify(u.$inc)||u.$set&&"{}"!==JSON.stringify(u.$set)||u.$setOnInsert&&"{}"!==JSON.stringify(u.$setOnInsert)||u.$unset&&"{}"!==JSON.stringify(u.$unset)?(this.timestamps&&(n=new Date,u.$set?u.$set.updatedAt=n:u.$set={updatedAt:n}),!this.checkSchema||h||(n=this.simplschema.newContext(),n.validate(u,{modifier:!0}))?this.useVersions&&!m||this.createLogs&&!_?[4,this.find(d)]:[3,2]:(console.log(new Date,this.collectionName,"Schema Errors - updateMany",n.validationErrors()),index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(index_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on updateMany - "+this.collectionName,[n.validationErrors(),u]),l(n.validationErrors()),[2])):(c(1),[2]);case 1:for(t=e.sent(),o=0;o<t.length;o++)i=t[o],this.createLogs&&!_&&log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:this.collectionName,id_document:i._id,payload:(0,common_1.getBinarySize)(JSON.stringify([i,d,u,g]))<2e5?JSON.stringify([i,d,u,g],null,2):"Too Big",method:"updateMany",id_user:"",user:"",messageId:0,route:""}),this.useVersions&&((r=(0,common_1.deepCopy)(i))._id={_id:i._id,__v:i.__v},index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:i._id,__v:i.__v}},r,{upsert:!0}),4<=i.__v&&index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({$and:[{"_id._id":i._id},{"_id.__v":{$lt:i.__v-4}}]}),objectContainsPropertyDeep(u,"__v")||(u.$inc?u.$inc.__v||(u.$inc.__v=1):u.$inc={__v:1}));e.label=2;case 2:return a=new monitor_manager_1.MonitorMongo("updateMany",this.collectionName,JSON.stringify([d,u,g])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).updateMany(d,u,g).then(function(e){a.finish(),e.acknowledged?(index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(s.collectionName),c(e.modifiedCount)):l(e.acknowledged)},function(e){a.finish(),console.log(new Date,"Error Update Many",s.collectionName,d,u,g,e),l(e)}),[2]}})})})},MongoManagerCollection.prototype.updateOne=function(d,u,g,_,h){var e=this;return void 0===_&&(_=!1),void 0===h&&(h=!1),new Promise(function(c,l){return __awaiter(e,void 0,void 0,function(){var n,t,o,i,r,a,s=this;return __generator(this,function(e){switch(e.label){case 0:return!this.checkSchema||h||(t=this.simplschema.newContext(),t.validate(u,{modifier:!0}))?u.$inc&&"{}"!==JSON.stringify(u.$inc)||u.$set&&"{}"!==JSON.stringify(u.$set)||u.$setOnInsert&&"{}"!==JSON.stringify(u.$setOnInsert)||u.$unset&&"{}"!==JSON.stringify(u.$unset)?(n=new Date,this.timestamps&&(u.$set?u.$set.updatedAt=n:u.$set={updatedAt:n}),this.useVersions?[4,this.findOne(d,null,!0)]:[3,2]):(c(1),[2]):(console.log(new Date,this.collectionName,"Schema Errors - updateOne",t.validationErrors()),index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(index_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on updateOne - "+this.collectionName,[t.validationErrors(),u]),l(t.validationErrors()),[2]);case 1:return(n=e.sent())?(this.createLogs&&!_&&log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:this.collectionName,id_document:n._id,payload:(0,common_1.getBinarySize)(JSON.stringify([n,d,u,g]))<2e5?JSON.stringify([n,d,u,g],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:""}),(t=(0,common_1.deepCopy)(n))._id={_id:n._id,__v:n.__v},index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:n._id,__v:n.__v}},t,{upsert:!0}),4<=n.__v&&index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({$and:[{"_id._id":n._id},{"_id.__v":{$lt:n.__v-4}}]}),objectContainsPropertyDeep(u,"__v")||(u.$inc?u.$inc.__v||(u.$inc.__v=1):u.$inc={__v:1}),o=new monitor_manager_1.MonitorMongo("updateOne",this.collectionName,JSON.stringify([d,u,g])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).updateOne(d,u,g).then(function(e){o.finish(),e.acknowledged?(index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(s.collectionName),c(e.modifiedCount)):l(e.acknowledged)},function(e){o.finish(),console.log(new Date,"Error Update One",s.collectionName,d,u,g,e),l(e)})):g&&g.upsert?(u.$setOnInsert?(u.$setOnInsert._id||(u.$setOnInsert._id=objectIdHexString()),this.timestamps&&!u.$setOnInsert.createdAt&&(u.$setOnInsert.createdAt=new Date)):this.timestamps?u.$setOnInsert={_id:objectIdHexString(),__v:0,createdAt:new Date}:u.$setOnInsert={_id:objectIdHexString(),__v:0},this.createLogs&&!_&&log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:this.collectionName,id_document:u.$setOnInsert._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",d,u,g]))<2e5?JSON.stringify(["upsert",d,u,g],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:""}),i=new monitor_manager_1.MonitorMongo("updateOne",this.collectionName,JSON.stringify([d,u,g])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).updateOne(d,u,g).then(function(e){i.finish(),e.acknowledged?(index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(s.collectionName),c(e.modifiedCount)):l(e.acknowledged)},function(e){i.finish(),console.log(new Date,"Error Update One",s.collectionName,d,u,g,e),l(e)})):l("No Document"),[3,3];case 2:g&&g.upsert&&(u.$setOnInsert?(u.$setOnInsert._id||(u.$setOnInsert._id=objectIdHexString()),this.timestamps&&!u.$setOnInsert.createdAt&&(u.$setOnInsert.createdAt=new Date)):this.timestamps?u.$setOnInsert={_id:objectIdHexString(),createdAt:new Date}:u.$setOnInsert={_id:objectIdHexString()}),this.createLogs&&!_?(g?g.returnDocument="before":g={returnDocument:"before"},r=new monitor_manager_1.MonitorMongo("findOneAndUpdate",this.collectionName,JSON.stringify([d,u,g])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndUpdate(d,u,g).then(function(e){r.finish();var n=e.value;n?(log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:s.collectionName,id_document:n._id,payload:(0,common_1.getBinarySize)(JSON.stringify([n,d,u,g]))<2e5?JSON.stringify([n,d,u,g],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:""}),c(e.ok)):u.$setOnInsert?(log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:s.collectionName,id_document:u.$setOnInsert._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",d,u,g]))<2e5?JSON.stringify(["upsert",d,u,g],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:""}),index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(s.collectionName),c(e.ok)):c(0)},function(e){r.finish(),console.log(new Date,"Error Find One And Update",s.collectionName,d,u,g,e),l(e)})):(a=new monitor_manager_1.MonitorMongo("updateOne",this.collectionName,JSON.stringify([d,u,g])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).updateOne(d,u,g).then(function(e){a.finish(),e.acknowledged?(index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(s.collectionName),c(e.modifiedCount)):l(e.acknowledged)},function(e){a.finish(),console.log(new Date,"Error Update One",s.collectionName,d,u,g,e),l(e)})),e.label=3;case 3:return[2]}})})})},MongoManagerCollection.prototype.watchCollection=function(e,n){return void 0===e&&(e=[]),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).watch(e,n)},(e=MongoManagerCollection).indexQueue=async_1.default.queue(function(d,u){return __awaiter(void 0,void 0,void 0,function(){var n,t,o,i,r,a,s,c,l;return __generator(e,function(e){switch(e.label){case 0:if(n=d.action,t=d.collection,o=d.specs,i=d.options,!(r=index_1.ResolveIOServer.getMainDB().collection(t.collectionName,t.collectionOptions)))return[2,u(new Error("Collection ".concat(t.collectionName," not found")))];a=new monitor_manager_1.MonitorMongo(n,t.collectionName,JSON.stringify([o,i])),e.label=1;case 1:return e.trys.push([1,3,,10]),[4,r[n].apply(r,__spreadArray(__spreadArray([],__read(o),!1),[i],!1))];case 2:return c=e.sent(),a.finish(),u(null,c),[3,10];case 3:if(85!==(s=e.sent()).code&&!s.message.includes("IndexOptionsConflict"))return[3,9];e.label=4;case 4:return(e.trys.push([4,8,,9]),l=t.extractIndexNameFromError(s.message))?[4,r.dropIndex(l)]:[3,7];case 5:return e.sent(),[4,r[n].apply(r,__spreadArray(__spreadArray([],__read(o),!1),[i],!1))];case 6:return c=e.sent(),a.finish(),u(null,c),[2];case 7:return[3,9];case 8:return l=e.sent(),u(l),[3,9];case 9:return a.finish(),u(s),[3,10];case 10:return[2]}})})},1),MongoManagerCollection}()),MongoManagerUserCollection=function(e){function MongoManagerUserCollection(){return null!==e&&e.apply(this,arguments)||this}return __extends(MongoManagerUserCollection,e),MongoManagerUserCollection.prototype.authenticate=function(a,t){var e=this;return new Promise(function(i,r){return __awaiter(e,void 0,void 0,function(){var n,o=this;return __generator(this,function(e){switch(e.label){case 0:return(n=Math.pow(100,Math.log(a.attempts+1)),n=n<3e5?n:3e5,a.last)?Date.now()-a.last.getTime()<n?(a.last=new Date,[4,user_collection_1.Users.updateOne({_id:a._id},{$set:{last:a.last}})]):[3,2]:[3,3];case 1:return e.sent(),i({data:null,error:"Attempt Too Soon"}),[2];case 2:return[3,4];case 3:a.last=new Date,e.label=4;case 4:return 5<=a.attempts?i({data:null,error:"Too Many Attempts"}):a.salt?pbkdf2(t,a.salt,{iterations:25e3,keylen:512,digestAlgorithm:"sha256"},function(n,t){return __awaiter(o,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return n?[2,r(n)]:scmp(t,Buffer.from(a.hash,"hex"))?(a.last=new Date,a.attempts=0,[4,user_collection_1.Users.updateOne({_id:a._id},{$set:{last:a.last,attempts:a.attempts}})]):[3,2];case 1:return e.sent(),i({data:a,error:""}),[3,4];case 2:return a.last=new Date,a.attempts=a.attempts+1,[4,user_collection_1.Users.updateOne({_id:a._id},{$set:{last:a.last,attempts:a.attempts}})];case 3:e.sent(),5<=a.attempts?i({data:null,error:"Too Many Attempts"}):i({data:null,error:"Invalid Username And Password"}),e.label=4;case 4:return[2]}})})}):i({data:null,error:"No Salt Value Stored"}),[2]}})})})},MongoManagerUserCollection.prototype.serializeUser=function(){return function(e,n){n(null,e.username)}},MongoManagerUserCollection.prototype.deserializeUser=function(){return function(e,n){user_collection_1.Users.findOne({username:e}).then(function(e){n(null,e)},function(e){n(e,null)})}},MongoManagerUserCollection.prototype.setPassword=function(r,a){var e=this;return new Promise(function(o,i){return __awaiter(e,void 0,void 0,function(){var n,t;return __generator(this,function(e){switch(e.label){case 0:return r?[3,1]:(i("No User"),[3,5]);case 1:return a?[3,2]:(i("No Password"),[3,5]);case 2:return[4,randomBytes(32)];case 3:return n=e.sent(),n=n.toString("hex"),[4,pbkdf2Promisified(a,n,{iterations:25e3,keylen:512,digestAlgorithm:"sha256"})];case 4:t=e.sent(),t=Buffer.from(t,"binary").toString("hex"),user_collection_1.Users.updateOne({_id:r._id},{$set:{hash:t,salt:n,services:{},attempts:0}}).then(function(e){return o(e)},function(e){return i(e)}),e.label=5;case 5:return[2]}})})})},MongoManagerUserCollection.prototype.changePassword=function(i,r,a){var e=this;return new Promise(function(t,o){return __awaiter(e,void 0,void 0,function(){var n;return __generator(this,function(e){switch(e.label){case 0:return i?[3,1]:(o("Missing User"),[3,4]);case 1:return r&&a?[3,2]:(o("Missing Password"),[3,4]);case 2:return[4,this.authenticate(i,r)];case 3:(n=e.sent()).data?this.setPassword(i,a).then(function(e){return t(e)},function(e){return o(e)}):o(n.error),e.label=4;case 4:return[2]}})})})},MongoManagerUserCollection.prototype.register=function(o,i){var e=this;return new Promise(function(n,t){return __awaiter(e,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return o.username?[3,1]:(t("Missing Username"),[3,5]);case 1:return[4,user_collection_1.Users.findOne({username:o.username})];case 2:return e.sent()?(t("Username Exists"),[3,5]):[3,3];case 3:return[4,user_collection_1.Users.findOne({email:o.email})];case 4:e.sent()?t("Email Exists"):(o.setPassword(i),n(o)),e.label=5;case 5:return[2]}})})})},MongoManagerUserCollection.prototype.resetAttempts=function(e){return user_collection_1.Users.updateOne({_id:e._id},{$set:{attempts:0}})},MongoManagerUserCollection}(exports.MongoManagerCollection=MongoManagerCollection);function objectIdHexString(){return(new mongodb_1.ObjectId).toHexString()}function pbkdf2(e,n,t,o){crypto.pbkdf2(e,n,t.iterations,t.keylen,t.digestAlgorithm,o)}function pbkdf2Promisified(e,n,i){return new Promise(function(t,o){return pbkdf2(e,n,i,function(e,n){return e?o(e):t(n)})})}function randomBytes(e){return new Promise(function(t,o){return crypto.randomBytes(e,function(e,n){return e?o(e):t(n)})})}function objectContainsPropertyDeep(e,n){var t=Object.keys(e);if(t.some(function(e){return e===n}))return!0;for(var o=0;o<t.length;o++){var i=t[o];if(e[i]&&"object"==typeof e[i]&&objectContainsPropertyDeep(e[i],n))return!0}return!1}exports.MongoManagerUserCollection=MongoManagerUserCollection,exports.objectIdHexString=objectIdHexString;
|
|
1
|
+
"use strict";var __extends=this&&this.__extends||function(){var t=function(e,n){return(t=Object.setPrototypeOf||({__proto__:[]}instanceof Array?function(e,n){e.__proto__=n}:function(e,n){for(var t in n)Object.prototype.hasOwnProperty.call(n,t)&&(e[t]=n[t])}))(e,n)};return function(e,n){if("function"!=typeof n&&null!==n)throw new TypeError("Class extends value "+String(n)+" is not a constructor or null");function __(){this.constructor=e}t(e,n),e.prototype=null===n?Object.create(n):(__.prototype=n.prototype,new __)}}(),__awaiter=this&&this.__awaiter||function(e,o,i,r){return new(i=i||Promise)(function(t,n){function fulfilled(e){try{step(r.next(e))}catch(e){n(e)}}function rejected(e){try{step(r.throw(e))}catch(e){n(e)}}function step(e){var n;e.done?t(e.value):((n=e.value)instanceof i?n:new i(function(e){e(n)})).then(fulfilled,rejected)}step((r=r.apply(e,o||[])).next())})},__generator=this&&this.__generator||function(o,i){var r,a,s,c={label:0,sent:function(){if(1&s[0])throw s[1];return s[1]},trys:[],ops:[]},l={next:verb(0),throw:verb(1),return:verb(2)};return"function"==typeof Symbol&&(l[Symbol.iterator]=function(){return this}),l;function verb(t){return function(e){var n=[t,e];if(r)throw new TypeError("Generator is already executing.");for(;c=l&&n[l=0]?0:c;)try{if(r=1,a&&(s=2&n[0]?a.return:n[0]?a.throw||((s=a.return)&&s.call(a),0):a.next)&&!(s=s.call(a,n[1])).done)return s;switch(a=0,(n=s?[2&n[0],s.value]:n)[0]){case 0:case 1:s=n;break;case 4:return c.label++,{value:n[1],done:!1};case 5:c.label++,a=n[1],n=[0];continue;case 7:n=c.ops.pop(),c.trys.pop();continue;default:if(!(s=0<(s=c.trys).length&&s[s.length-1])&&(6===n[0]||2===n[0])){c=0;continue}if(3===n[0]&&(!s||n[1]>s[0]&&n[1]<s[3]))c.label=n[1];else if(6===n[0]&&c.label<s[1])c.label=s[1],s=n;else{if(!(s&&c.label<s[2])){s[2]&&c.ops.pop(),c.trys.pop();continue}c.label=s[2],c.ops.push(n)}}n=i.call(o,c)}catch(e){n=[6,e],a=0}finally{r=s=0}if(5&n[0])throw n[1];return{value:n[0]?n[1]:void 0,done:!0}}}},__read=this&&this.__read||function(e,n){var t="function"==typeof Symbol&&e[Symbol.iterator];if(!t)return e;var o,i,r=t.call(e),a=[];try{for(;(void 0===n||0<n--)&&!(o=r.next()).done;)a.push(o.value)}catch(e){i={error:e}}finally{try{o&&!o.done&&(t=r.return)&&t.call(r)}finally{if(i)throw i.error}}return a},__spreadArray=this&&this.__spreadArray||function(e,n,t){if(t||2===arguments.length)for(var o,i=0,r=n.length;i<r;i++)!o&&i in n||((o=o||Array.prototype.slice.call(n,0,i))[i]=n[i]);return e.concat(o||Array.prototype.slice.call(n))},__values=this&&this.__values||function(e){var n="function"==typeof Symbol&&Symbol.iterator,t=n&&e[n],o=0;if(t)return t.call(e);if(e&&"number"==typeof e.length)return{next:function(){return{value:(e=e&&o>=e.length?void 0:e)&&e[o++],done:!e}}};throw new TypeError(n?"Object is not iterable.":"Symbol.iterator is not defined.")},simpl_schema_1=(Object.defineProperty(exports,"__esModule",{value:!0}),exports.objectIdHexString=exports.MongoManagerUserCollection=exports.MongoManagerCollection=exports.MongoManagerModel=exports.MongoManager=void 0,require("simpl-schema")),mongodb_1=require("mongodb"),index_1=require("../index"),user_collection_1=require("../collections/user.collection"),common_1=require("../util/common"),log_collection_1=require("../collections/log.collection"),NodeCache=require("node-cache"),crypto=require("crypto"),scmp=require("scmp"),monitor_manager_1=require("./monitor.manager"),os_1=require("os"),numCPUs=(0,os_1.cpus)().length,v8=require("v8"),async_1=require("async"),sift_1=require("sift"),MongoManager=function(){function MongoManager(e){this._collections=[],this._cacheMap=[],this._operationInProgress=new Map,this._heapSize=v8.getHeapStatistics()/numCPUs,this._serverCollections=[],this._serverConfig=null,this._isWorkersEnabled=!1,this._isWorkerInstance=!1,this._serverConfig=e,this._nodeCache=new NodeCache({stdTTL:0,checkperiod:0}),this._isWorkersEnabled="true"===process.env.IS_WORKERS_ENABLED,this._isWorkerInstance="true"===process.env.IS_WORKER_INSTANCE,this.setCacheLimit(),this.initServerCollections(),this._isWorkersEnabled&&this._isWorkerInstance&&this.setupChangeStream()}return MongoManager.prototype.setCacheLimit=function(){this._isWorkersEnabled?this._heapLimit=this._isWorkerInstance?.8*this._heapSize:.4*this._heapSize:this._heapLimit=.3*this._heapSize},MongoManager.prototype.initServerCollections=function(){return __awaiter(this,void 0,void 0,function(){var n;return __generator(this,function(e){switch(e.label){case 0:return[4,index_1.ResolveIOServer.getMainDB().listCollections().toArray()];case 1:return n=e.sent(),this._serverCollections=n,[2]}})})},MongoManager.prototype.getServerCollections=function(){return this._serverCollections},MongoManager.prototype.registerCollection=function(n){var e=this;n.collectionOptions&&n.collectionOptions.timeseries&&n.collectionOptions.timeseries.timeField&&this._serverCollections.some(function(e){return e.name===n.collectionName&&"collection"===e.type})&&index_1.ResolveIOServer.getMainDB().dropCollection(n.collectionName).then(function(){e.createCollection(n)}),this._collections.push(n)},MongoManager.prototype.createCollection=function(e){index_1.ResolveIOServer.getMainDB().createCollection(e.collectionName,e.collectionOptions)},MongoManager.prototype.collections=function(){return this._collections},MongoManager.prototype.collection=function(n){return this._collections.find(function(e){return e.collectionName===n})},MongoManager.prototype.delay=function(n){return __awaiter(this,void 0,void 0,function(){return __generator(this,function(e){return[2,new Promise(function(e){return setTimeout(e,n)})]})})},MongoManager.prototype.find=function(i,r,a){return void 0===r&&(r={}),__awaiter(this,void 0,void 0,function(){var n,t,o=this;return __generator(this,function(e){return n=this.generateCacheKey([i],"find",[r,a]),void 0!==(t=this.getFromCache(n))?[2,t]:this._operationInProgress.has(n)?[2,this._operationInProgress.get(n).promise]:(t=this.executeFind(i,r,a,n),this._operationInProgress.set(n,{promise:t,invalidatedDuringExecution:!1}),t.finally(function(){return o._operationInProgress.delete(n)}),[2,t])})})},MongoManager.prototype.executeFind=function(o,i,r,a){return __awaiter(this,void 0,void 0,function(){var n,t;return __generator(this,function(e){switch(e.label){case 0:return n=index_1.ResolveIOServer.getMainDB().collection(o),t=new monitor_manager_1.MonitorMongo("find",o,JSON.stringify([i,r])),[4,n.find(i,r).toArray()];case 1:return n=e.sent(),t.finish(),this._operationInProgress.get(a).invalidatedDuringExecution||this.addToCache([o],a,n),[2,n]}})})},MongoManager.prototype.findOne=function(i,r,a){return void 0===r&&(r={}),__awaiter(this,void 0,void 0,function(){var n,t,o=this;return __generator(this,function(e){return n=this.generateCacheKey([i],"findOne",[r,a]),void 0!==(t=this.getFromCache(n))?[2,t]:this._operationInProgress.has(n)?[2,this._operationInProgress.get(n).promise]:(t=this.executeFindOne(i,r,a,n),this._operationInProgress.set(n,{promise:t,invalidatedDuringExecution:!1}),t.finally(function(){return o._operationInProgress.delete(n)}),[2,t])})})},MongoManager.prototype.executeFindOne=function(o,i,r,a){return __awaiter(this,void 0,void 0,function(){var n,t;return __generator(this,function(e){switch(e.label){case 0:return n=index_1.ResolveIOServer.getMainDB().collection(o),t=new monitor_manager_1.MonitorMongo("findOne",o,JSON.stringify([i,r])),[4,n.findOne(i,r)];case 1:return n=e.sent(),t.finish(),this._operationInProgress.get(a).invalidatedDuringExecution||this.addToCache([o],a,n),[2,n]}})})},MongoManager.prototype.aggregate=function(r,a,s){return __awaiter(this,void 0,void 0,function(){var n,t,o,i=this;return __generator(this,function(e){return n=__spreadArray([r],__read(a.flatMap(function(e){var n;return null!=(n=e.$lookup)&&n.from?[e.$lookup.from]:[]})),!1),t=this.generateCacheKey(n,"aggregate",[a,s]),void 0!==(o=this.getFromCache(t))?[2,o]:this._operationInProgress.has(t)?[2,this._operationInProgress.get(t).promise]:(o=this.executeAggregate(r,a,s,t,n),this._operationInProgress.set(t,{promise:o,invalidatedDuringExecution:!1}),o.finally(function(){return i._operationInProgress.delete(t)}),[2,o])})})},MongoManager.prototype.executeAggregate=function(o,i,r,a,s){return __awaiter(this,void 0,void 0,function(){var n,t;return __generator(this,function(e){switch(e.label){case 0:return n=index_1.ResolveIOServer.getMainDB().collection(o),t=new monitor_manager_1.MonitorMongo("aggregate",o,JSON.stringify([i,r])),[4,n.aggregate(i,r).toArray()];case 1:return n=e.sent(),t.finish(),this._operationInProgress.get(a).invalidatedDuringExecution||this.addToCache(s,a,n),[2,n]}})})},MongoManager.prototype.countDocuments=function(i,r,a){return void 0===r&&(r={}),__awaiter(this,void 0,void 0,function(){var n,t,o=this;return __generator(this,function(e){return n=this.generateCacheKey([i],"countDocuments",[r,a]),void 0!==(t=this.getFromCache(n))?[2,t]:this._operationInProgress.has(n)?[2,this._operationInProgress.get(n).promise]:(t=this.executeCountDocuments(i,r,a,n),this._operationInProgress.set(n,{promise:t,invalidatedDuringExecution:!1}),t.finally(function(){return o._operationInProgress.delete(n)}),[2,t])})})},MongoManager.prototype.executeCountDocuments=function(o,i,r,a){return __awaiter(this,void 0,void 0,function(){var n,t;return __generator(this,function(e){switch(e.label){case 0:return n=index_1.ResolveIOServer.getMainDB().collection(o),t=new monitor_manager_1.MonitorMongo("countDocuments",o,JSON.stringify([i,r])),[4,n.countDocuments(i,r)];case 1:return n=e.sent(),t.finish(),this._operationInProgress.get(a).invalidatedDuringExecution||this.addToCache([o],a,n),[2,n]}})})},MongoManager.prototype.generateCacheKey=function(e,n,t){e=JSON.stringify({collections:e,functionName:n,args:t});return crypto.createHash("sha256").update(e).digest("hex")},MongoManager.prototype.getFromCache=function(n){try{var e=this._nodeCache.get(n);if(e)return JSON.parse(e,common_1.dateReviver)}catch(e){this._nodeCache.del(n)}},MongoManager.prototype.addToCache=function(e,n,t){var o,i;if((0,common_1.getBinarySize)(JSON.stringify(t))<1e6){var r=this._nodeCache.getStats().vsize;if(r>this._heapLimit){var a=0,s=this._nodeCache.keys();try{for(var c=__values(s),l=c.next();!l.done;l=c.next()){var d=l.value;if(this._nodeCache.del(d),a+=1,(r=this._nodeCache.getStats().vsize)<.75*this._heapLimit)break}}catch(e){o={error:e}}finally{try{l&&!l.done&&(i=c.return)&&i.call(c)}finally{if(o)throw o.error}}console.log("Query Cache: Too Big, - Deleted: "+a+" - "+r)}this._nodeCache.set(n,JSON.stringify(t)),this._cacheMap.push({collections:e,key:n})}},MongoManager.prototype.invalidateQueryCache=function(n){var t,e,o=this._cacheMap.filter(function(e){return e.collections.includes(n)});try{for(var i=__values(o),r=i.next();!r.done;r=i.next()){var a=r.value;this._nodeCache.del(a.key),this._operationInProgress.has(a.key)&&(this._operationInProgress.get(a.key).invalidatedDuringExecution=!0)}}catch(e){t={error:e}}finally{try{r&&!r.done&&(e=i.return)&&e.call(i)}finally{if(t)throw t.error}}this._cacheMap=this._cacheMap.filter(function(e){return!e.collections.includes(n)})},MongoManager.prototype.setupChangeStream=function(){var n=this,t=index_1.ResolveIOServer.getMongoConnection().db(this._serverConfig.DATABASE).watch();t.on("change",function(t){return __awaiter(n,void 0,void 0,function(){var n;return __generator(this,function(e){return t.ns&&t.ns.coll&&(n=t.ns.coll,this.collection(n))&&this.invalidateQueryCache(n),[2]})})}),t.on("error",function(e){console.log(new Date,"Mongo change stream error. Restart..."),t.close(),setTimeout(function(){return n.setupChangeStream()},5e3)}),t.on("close",function(){console.log(new Date,"Mongo change stream closed. Restarting..."),setTimeout(function(){return n.setupChangeStream()},5e3)})},MongoManager}(),MongoManagerModel=(exports.MongoManager=MongoManager,function(e){var n;this.collection_main=null,this.collection_version=null,"users"===e.collectionName?this.collection_main=new MongoManagerUserCollection(e):this.collection_main=new MongoManagerCollection(e),e.useVersionCollection&&(this.collection_main.useVersions=!0,(n=(0,common_1.deepCopy)(e.schema))._id.type="Object",n._id.blackbox=!0,n={collectionName:e.collectionName+".versions",schema:n,useVersionCollection:!1,useReportBuilder:!1,reportBuilderLookupTables:[],timestamps:!0,createLogs:!1,checkSchema:!1,collectionOptions:null},"users"===e.collectionName?this.collection_version=new MongoManagerUserCollection(n):this.collection_version=new MongoManagerCollection(n),this.collection_version.createIndex({"_id._id":1,"_id.__v":1}),this.collection_main.versionCollection=e.collectionName+".versions")}),MongoManagerCollection=(exports.MongoManagerModel=MongoManagerModel,function(){function MongoManagerCollection(e){var n,t=this,o=(this.collectionName="",this.checkSchema=!1,this.simplschema=null,this.rbSchema=null,this.timestamps=!1,this.useVersions=!1,this.versionCollection="",this.createLogs=!0,this.useRB=!1,this.collectionName=e.collectionName,this.simplschema=new simpl_schema_1.default(e.schema),this.timestamps=e.timestamps,this.createLogs=e.createLogs,this.checkSchema=e.checkSchema,this.collectionOptions=e.collectionOptions,e.useReportBuilder&&(this.useRB=!0,n=(0,common_1.deepCopy)(e.schema),e=(0,common_1.buildRbLookups)(e.reportBuilderLookupTables,n,[]),this.rbSchema=(0,common_1.buildRbSchema)(e)),setInterval(function(){index_1.ResolveIOServer&&index_1.ResolveIOServer.getMainServer()&&index_1.ResolveIOServer.getMongoManager()&&index_1.ResolveIOServer.getMongoManager().getServerCollections().length&&(index_1.ResolveIOServer.getMongoManager().registerCollection(t),clearInterval(o))},1))}var e;return MongoManagerCollection.prototype.extractIndexNameFromError=function(e){var n=e.match(/Index already exists with a different name: (\S+)/);return(n=!n||n.length<=1?e.match(/existing index:.*name: "([^"]+)"/):n)&&1<n.length?n[1]:null},MongoManagerCollection.prototype.aggregate=function(i,r,e){var a=this;return void 0===e&&(e=!1),new Promise(function(n,t){var o;e?(o=new monitor_manager_1.MonitorMongo("aggregate",a.collectionName,JSON.stringify([i,r])),index_1.ResolveIOServer.getMainDB().collection(a.collectionName,a.collectionOptions).aggregate(i,r).toArray().then(function(e){o.finish(),n(e)},function(e){o.finish(),console.log(new Date,"Error Aggregate",a.collectionName,i,r,e),t(e)})):index_1.ResolveIOServer.getMongoManager().aggregate(a.collectionName,i,r).then(function(e){n(e)},function(e){t(e)})})},MongoManagerCollection.prototype.aggregateCount=function(r,a){return __awaiter(this,void 0,void 0,function(){var i=this;return __generator(this,function(e){return[2,new Promise(function(n,t){var o=new monitor_manager_1.MonitorMongo("aggregateCount",i.collectionName,JSON.stringify([r,a]));index_1.ResolveIOServer.getMainDB().collection(i.collectionName,i.collectionOptions).aggregate(r,a).toArray().then(function(e){o.finish(),n(e.length)},function(e){o.finish(),console.log(new Date,"Error Aggregate",i.collectionName,r,a,e),t(0)})})]})})},MongoManagerCollection.prototype.aggregateCursor=function(e,n){var t=new monitor_manager_1.MonitorMongo("aggregateCursor",this.collectionName,JSON.stringify([e,n])),e=index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).aggregate(e,n);return e.on("close",function(e){return t.finish(),e}),e},MongoManagerCollection.prototype.aggregateStream=function(e,n,t){var o=new monitor_manager_1.MonitorMongo("aggregateStream",this.collectionName,JSON.stringify([e,n,t])),e=index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).aggregate(e,n).stream(t);return e.on("end",function(e){return o.finish(),e}).on("error",function(e){return o.finish(),e}).on("close",function(e){return o.finish(),e}),e},MongoManagerCollection.prototype.bulkWrite=function(b,R,A,B){return void 0===A&&(A=!1),void 0===B&&(B=!1),__awaiter(this,void 0,void 0,function(){var n,t,o,i,r,a,s,c,l,d,u,g,_,h,m,f,v,p,M,O,y,S,N,w,I,x,C,D;return __generator(this,function(e){switch(e.label){case 0:if(!b.length)return[2,null];n=[],t=[],o=[],i=[],r=new Set;try{for(a=__values(b),s=a.next();!s.done;s=a.next())if(c=s.value,l=Object.keys(c)[0],v=c[l],"insertOne"===l){if(v.document._id||(v.document._id=objectIdHexString()),this.useVersions&&void 0===v.document.__v&&(v.document.__v=0),this.timestamps&&(d=new Date,v.document.createdAt=v.document.createdAt||d,v.document.updatedAt=v.document.updatedAt||d),this.checkSchema&&!B&&!(u=this.simplschema.newContext()).validate(v.document))throw new Error("Schema validation failed for insertOne: ".concat(JSON.stringify(u.validationErrors())));n.push(c),!A&&this.createLogs&&o.push({insertOne:{document:{_id:objectIdHexString(),type:"document",collection:this.collectionName,id_document:v.document._id,payload:(0,common_1.getBinarySize)(JSON.stringify([c,R]))<2e5?JSON.stringify([c,R],null,2):"Too Big",method:l,id_user:"",user:"",messageId:0,route:""}}})}else if("updateOne"===l||"updateMany"===l){if(v.update.$set||(v.update.$set={}),this.timestamps&&(v.update.$set.updatedAt=new Date),this.useVersions&&(v.update.$inc||(v.update.$inc={}),v.update.$inc.__v=1),v.upsert&&(v.update.$setOnInsert||(v.update.$setOnInsert={}),v.update.$setOnInsert._id||(v.update.$setOnInsert._id=objectIdHexString()),this.timestamps&&(v.update.$setOnInsert.createdAt=new Date),this.useVersions)&&(v.update.$setOnInsert.__v=0),this.checkSchema&&!B&&!(u=this.simplschema.newContext()).validate(v.update,{modifier:!0}))throw new Error("Schema validation failed for update: ".concat(JSON.stringify(u.validationErrors())));n.push(c),t.push(v.filter),!A&&this.createLogs&&(g="",v.filter._id?g=v.filter._id:(i.push({filter:v.filter,opIndex:o.length}),this.extractFieldNames(v.filter,r)),o.push({insertOne:{document:{_id:objectIdHexString(),type:"document",collection:this.collectionName,id_document:g,payload:(0,common_1.getBinarySize)(JSON.stringify([c,R]))<2e5?JSON.stringify([c,R],null,2):"Too Big",method:l,id_user:"",user:"",messageId:0,route:""}}}))}else if("replaceOne"===l){if(this.timestamps&&(v.replacement.updatedAt=new Date,v.upsert)&&!v.replacement.createdAt&&(v.replacement.createdAt=new Date),this.useVersions&&(v.replacement.__v=void 0!==v.replacement.__v?v.replacement.__v+1:0),this.checkSchema&&!B&&!(u=this.simplschema.newContext()).validate(v.replacement))throw new Error("Schema validation failed for replaceOne: ".concat(JSON.stringify(u.validationErrors())));n.push(c),t.push(v.filter),!A&&this.createLogs&&(g="",v.filter._id?g=v.filter._id:v.replacement._id?g=v.replacement._id:(i.push({filter:v.filter,opIndex:o.length}),this.extractFieldNames(v.filter,r)),o.push({insertOne:{document:{_id:objectIdHexString(),type:"document",collection:this.collectionName,id_document:g,payload:(0,common_1.getBinarySize)(JSON.stringify([c,R]))<2e5?JSON.stringify([c,R],null,2):"Too Big",method:l,id_user:"",user:"",messageId:0,route:""}}}))}else{if("deleteOne"!==l&&"deleteMany"!==l)throw new Error("Unsupported operation type: ".concat(l));n.push(c),t.push(v.filter),!A&&this.createLogs&&(g="",v.filter._id?g=v.filter._id:(i.push({filter:v.filter,opIndex:o.length}),this.extractFieldNames(v.filter,r)),o.push({insertOne:{document:{_id:objectIdHexString(),type:"document",collection:this.collectionName,id_document:g,payload:(0,common_1.getBinarySize)(JSON.stringify([c,R]))<2e5?JSON.stringify([c,R],null,2):"Too Big",method:l,id_user:"",user:"",messageId:0,route:""}}}))}}catch(e){N={error:e}}finally{try{s&&!s.done&&(w=a.return)&&w.call(a)}finally{if(N)throw N.error}}e.label=1;case 1:return(e.trys.push([1,9,,10]),i.length)?(h={$or:i.map(function(e){return e.filter})},_={_id:1},r.forEach(function(e){_[e]=1}),[4,index_1.ResolveIOServer.getMainDB().collection(this.collectionName).find(h,{projection:_}).toArray()]):[3,3];case 2:h=e.sent();try{for(m=__values(h),f=m.next();!f.done;f=m.next()){v=f.value;try{for(C=void 0,p=__values(i),M=p.next();!M.done;M=p.next())O=M.value,this.matchesFilter(v,O.filter)&&(o[O.opIndex].insertOne.document.id_document=v._id)}catch(e){C={error:e}}finally{try{M&&!M.done&&(D=p.return)&&D.call(p)}finally{if(C)throw C.error}}}}catch(e){I={error:e}}finally{try{f&&!f.done&&(x=m.return)&&x.call(m)}finally{if(I)throw I.error}}e.label=3;case 3:return this.useVersions&&t.length?(S=[{$match:{$or:t}},{$addFields:{_id:{_id:"$_id",__v:"$__v"}}},{$merge:{into:this.versionCollection,on:"_id",whenMatched:"fail",whenNotMatched:"insert"}}],[4,index_1.ResolveIOServer.getMainDB().collection(this.collectionName).aggregate(S,{allowDiskUse:!0}).toArray()]):[3,5];case 4:e.sent(),e.label=5;case 5:return[4,index_1.ResolveIOServer.getMainDB().collection(this.collectionName).bulkWrite(n,R)];case 6:return y=e.sent(),A||!o.length?[3,8]:[4,log_collection_1.Logs.bulkWrite(o)];case 7:e.sent(),e.label=8;case 8:return index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,y];case 9:throw S=e.sent(),console.error("Error in bulkWrite:",S),S;case 10:return[2]}})})},MongoManagerCollection.prototype.matchesFilter=function(e,n){return(0,sift_1.default)(n)(e)},MongoManagerCollection.prototype.extractFieldNames=function(e,n,t){var o=this;if(void 0===t&&(t=""),"object"==typeof e&&null!=e)for(var i in e){var r=e[i];i.startsWith("$")?Array.isArray(r)?r.forEach(function(e){return o.extractFieldNames(e,n,t)}):"object"==typeof r&&this.extractFieldNames(r,n,t):(i=t?"".concat(t,".").concat(i):i,"object"!=typeof r||null===r||Array.isArray(r)?n.add(i):(n.add(i),this.extractFieldNames(r,n,i)))}},MongoManagerCollection.prototype.countDocuments=function(a,s,c){var e=this;return void 0===a&&(a={}),void 0===c&&(c=!1),new Promise(function(i,r){return __awaiter(e,void 0,void 0,function(){var n,t,o=this;return __generator(this,function(e){switch(e.label){case 0:return(e.trys.push([0,4,,5]),0===Object.keys(a).length)?[4,index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).estimatedDocumentCount(s)]:[3,2];case 1:return t=e.sent(),i(t),[3,3];case 2:c?(n=new monitor_manager_1.MonitorMongo("countDocuments",this.collectionName,JSON.stringify([a,s])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).countDocuments(a,s).then(function(e){n.finish(),i(e)},function(e){n.finish(),console.log(new Date,"Error Count Documents",o.collectionName,a,s,e),r(e)})):index_1.ResolveIOServer.getMongoManager().countDocuments(this.collectionName,a,s).then(function(e){return i(e)},function(e){return r(e)}),e.label=3;case 3:return[3,5];case 4:return t=e.sent(),console.log(new Date,"Error Count Documents (Stats)",this.collectionName,a,s,t),r(t),[3,5];case 5:return[2]}})})})},MongoManagerCollection.prototype.create=function(e,n){return Array.isArray(e)?1===e.length?this.insertOne(e[0],n):this.insertMany(e,n):this.insertOne(e,n)},MongoManagerCollection.prototype.createIndex=function(i,r){var e=this;return new Promise(function(t,o){return __awaiter(e,void 0,void 0,function(){var n;return __generator(this,function(e){switch(e.label){case 0:return e.trys.push([0,2,,3]),[4,index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).indexes()];case 1:return(n=e.sent(),n.some(function(e){return JSON.stringify(e.key)===JSON.stringify(i)}))?[2,t("Index already exists")]:(MongoManagerCollection.indexQueue.push({action:"createIndex",collection:this,specs:[i],options:r},function(e,n){e?o(e):t(n)}),[3,3]);case 2:return n=e.sent(),o(n),[3,3];case 3:return[2]}})})})},MongoManagerCollection.prototype.createIndexes=function(r,a){var e=this;return new Promise(function(o,i){return __awaiter(e,void 0,void 0,function(){var t,n;return __generator(this,function(e){switch(e.label){case 0:return e.trys.push([0,2,,3]),[4,index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).indexes()];case 1:return(t=e.sent(),0===(n=r.filter(function(n){return!t.some(function(e){return JSON.stringify(e.key)===JSON.stringify(n.key)})})).length)?[2,o(["All indexes already exist"])]:(MongoManagerCollection.indexQueue.push({action:"createIndexes",collection:this,specs:[n],options:a},function(e,n){e?i(e):o(n)}),[3,3]);case 2:return n=e.sent(),i(n),[3,3];case 3:return[2]}})})})},MongoManagerCollection.prototype.deleteMany=function(l,d,u){var e=this;return void 0===l&&(l={}),void 0===u&&(u=!1),new Promise(function(s,c){return __awaiter(e,void 0,void 0,function(){var n,t,o,i,r,a=this;return __generator(this,function(e){switch(e.label){case 0:return!this.createLogs||u?[3,2]:[4,this.find(l)];case 1:for(n=e.sent(),t=0;t<n.length;t++)o=n[t],log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:this.collectionName,id_document:o._id,payload:(0,common_1.getBinarySize)(JSON.stringify([o,l,d]))<2e5?JSON.stringify([o,l,d],null,2):"Too Big",method:"deleteMany",id_user:"",user:"",messageId:0,route:""}),this.useVersions&&((i=(0,common_1.deepCopy)(o))._id={_id:o._id,__v:o.__v},index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:o._id,__v:o.__v}},i,{upsert:!0}),index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({$and:[{"_id._id":o._id},{"_id.__v":{$lt:o.__v-1}}]}));e.label=2;case 2:return r=new monitor_manager_1.MonitorMongo("deleteMany",this.collectionName,JSON.stringify([l,d])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).deleteMany(l,d).then(function(e){r.finish(),e.acknowledged?(index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(a.collectionName),s(e.deletedCount)):c(e.acknowledged)},function(e){r.finish(),console.log(new Date,"Error Delete Many",a.collectionName,l,d,e),c(e)}),[2]}})})})},MongoManagerCollection.prototype.deleteOne=function(a,s,c){var e=this;return void 0===a&&(a={}),void 0===c&&(c=!1),new Promise(function(r,o){return __awaiter(e,void 0,void 0,function(){var i,n,t=this;return __generator(this,function(e){return this.createLogs&&!c?(i=new monitor_manager_1.MonitorMongo("findOneAndDelete",this.collectionName,JSON.stringify([a,s])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndDelete(a,s).then(function(o){return __awaiter(t,void 0,void 0,function(){var n,t;return __generator(this,function(e){return i.finish(),(n=o)?(this.createLogs&&!c&&log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:this.collectionName,id_document:n._id,payload:(0,common_1.getBinarySize)(JSON.stringify([n,a,s]))<2e5?JSON.stringify([n,a,s],null,2):"Too Big",method:"deleteOne",id_user:"",user:"",messageId:0,route:""}),this.useVersions&&((t=(0,common_1.deepCopy)(n))._id={_id:n._id,__v:n.__v},index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:n._id,__v:n.__v}},t,{upsert:!0}),index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({$and:[{"_id._id":n._id},{"_id.__v":{$lt:n.__v-1}}]})),index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),r(1)):r(0),[2]})})},function(e){i.finish(),console.log(new Date,"Error Find One And Delete",t.collectionName,a,s,e),o(e)})):(n=new monitor_manager_1.MonitorMongo("deleteOne",this.collectionName,JSON.stringify([a,s])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).deleteOne(a,s).then(function(e){n.finish(),e.acknowledged?(index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(t.collectionName),r(e.deletedCount)):o(e.acknowledged)},function(e){n.finish(),console.log(new Date,"Error Delete One",t.collectionName,a,s,e),o(e)})),[2]})})})},MongoManagerCollection.prototype.distinct=function(e,i,r){var a=this;return new Promise(function(n,t){var o=new monitor_manager_1.MonitorMongo("distinct",a.collectionName,JSON.stringify([e,i,r]));index_1.ResolveIOServer.getMainDB().collection(a.collectionName,a.collectionOptions).distinct(e,i,r).then(function(e){o.finish(),n(e)},function(e){o.finish(),console.log(new Date,"Error Distinct",a.collectionName,i,r,e),t(e)})})},MongoManagerCollection.prototype.drop=function(i){var r=this;return new Promise(function(n,t){var o=new monitor_manager_1.MonitorMongo("drop",r.collectionName,JSON.stringify([i]));index_1.ResolveIOServer.getMainDB().collection(r.collectionName,r.collectionOptions).drop(i).then(function(e){o.finish(),n(e)},function(e){o.finish(),console.log(new Date,"Error Count Documents",r.collectionName,i,e),t(e)})})},MongoManagerCollection.prototype.dropIndex=function(i,r){var a=this;return new Promise(function(n,t){var o=new monitor_manager_1.MonitorMongo("dropIndex",a.collectionName,JSON.stringify([i,r]));index_1.ResolveIOServer.getMainDB().collection(a.collectionName,a.collectionOptions).dropIndex(i,r).then(function(e){o.finish(),n(e)},function(e){o.finish(),console.log(new Date,"Error Drop Index",a.collectionName,i,r,e),t(e)})})},MongoManagerCollection.prototype.dropIndexes=function(i){var r=this;return new Promise(function(n,t){var o=new monitor_manager_1.MonitorMongo("dropIndexes",r.collectionName,JSON.stringify([i]));index_1.ResolveIOServer.getMainDB().collection(r.collectionName,r.collectionOptions).dropIndexes(i).then(function(e){o.finish(),n(e)},function(e){o.finish(),console.log(new Date,"Error Drop Indexes",r.collectionName,i,e),t(e)})})},MongoManagerCollection.prototype.find=function(r,a,s){var e=this;return void 0===r&&(r={}),void 0===s&&(s=!1),new Promise(function(o,i){return __awaiter(e,void 0,void 0,function(){var n,t=this;return __generator(this,function(e){return s?(n=new monitor_manager_1.MonitorMongo("find",this.collectionName,JSON.stringify([r,a])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).find(r,a).toArray().then(function(e){n.finish(),o(e)},function(e){n.finish(),console.log(new Date,"Error Find",t.collectionName,r,a,e),i(e)})):index_1.ResolveIOServer.getMongoManager().find(this.collectionName,r,a).then(function(e){o(e)},function(e){i(e)}),[2]})})})},MongoManagerCollection.prototype.findById=function(i,r){var a=this;return new Promise(function(n,t){var o=new monitor_manager_1.MonitorMongo("findById",a.collectionName,JSON.stringify([{_id:i},r]));index_1.ResolveIOServer.getMainDB().collection(a.collectionName,a.collectionOptions).findOne({_id:i},r).then(function(e){o.finish(),n(e)},function(e){o.finish(),console.log(new Date,"Error Find By Id",a.collectionName,{_id:i},r,e),t(e)})})},MongoManagerCollection.prototype.findCount=function(i,r){var a=this;return void 0===i&&(i={}),new Promise(function(n,t){var o=new monitor_manager_1.MonitorMongo("findCount",a.collectionName,JSON.stringify([i,r]));index_1.ResolveIOServer.getMainDB().collection(a.collectionName,a.collectionOptions).find(i,r).count().then(function(e){o.finish(),n(e)},function(e){o.finish(),console.log(new Date,"Error Find Count",a.collectionName,i,r,e),t(e)})})},MongoManagerCollection.prototype.findCursor=function(e,n){void 0===e&&(e={});var t=new monitor_manager_1.MonitorMongo("findCursor",this.collectionName,JSON.stringify([e,n])),o=index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).find(e,n);return o.on("close",function(e){return t.finish(),o.removeAllListeners(),e}),o},MongoManagerCollection.prototype.findStream=function(e,n,t){void 0===e&&(e={});var o=new monitor_manager_1.MonitorMongo("findStream",this.collectionName,JSON.stringify([e,n,t])),e=index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).find(e,n).stream(t);return e.on("end",function(e){return o.finish(),e}).on("error",function(e){return o.finish(),e}).on("close",function(e){return o.finish(),e}),e},MongoManagerCollection.prototype.findOne=function(i,r,e){var a=this;return void 0===i&&(i={}),void 0===e&&(e=!1),new Promise(function(n,t){var o;e?(o=new monitor_manager_1.MonitorMongo("findOne",a.collectionName,JSON.stringify([i,r])),index_1.ResolveIOServer.getMainDB().collection(a.collectionName,a.collectionOptions).findOne(i,r).then(function(e){o.finish(),n(e)},function(e){o.finish(),console.log(new Date,"Error Find One",a.collectionName,i,r,e),t(e)})):index_1.ResolveIOServer.getMongoManager().findOne(a.collectionName,i,r).then(function(e){n(e)},function(e){t(e)})})},MongoManagerCollection.prototype.findOneAndDelete=function(r,a,s){var e=this;return void 0===r&&(r={}),void 0===s&&(s=!1),new Promise(function(i,n){return __awaiter(e,void 0,void 0,function(){var t,o=this;return __generator(this,function(e){return t=new monitor_manager_1.MonitorMongo("findOneAndDelete",this.collectionName,JSON.stringify([r,a])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndDelete(r,a).then(function(e){t.finish();var n;e?(o.createLogs&&!s&&log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:o.collectionName,id_document:e._id,payload:(0,common_1.getBinarySize)(JSON.stringify([e,r,a]))<2e5?JSON.stringify([e,r,a],null,2):"Too Big",method:"findOneAndDelete",id_user:"",user:"",messageId:0,route:""}),o.useVersions&&((n=(0,common_1.deepCopy)(e))._id={_id:e._id,__v:e.__v},index_1.ResolveIOServer.getMongoManager().collection(o.versionCollection).replaceOne({_id:{_id:e._id,__v:e.__v}},n,{upsert:!0})),index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(o.collectionName),i(e)):i(null)},function(e){t.finish(),console.log(new Date,"Error Find One And Delete",o.collectionName,r,a,e),n(e)}),[2]})})})},MongoManagerCollection.prototype.findOneAndReplace=function(s,c,l,d,u){var e=this;return void 0===s&&(s={}),void 0===d&&(d=!1),void 0===u&&(u=!1),new Promise(function(r,a){return __awaiter(e,void 0,void 0,function(){var n,t,o,i=this;return __generator(this,function(e){return!this.checkSchema||u||(n=this.simplschema.newContext(),n.validate(c))?(t=new Date,this.timestamps&&(c.updatedAt=t),l&&l.upsert&&(c._id||(c._id=objectIdHexString()),this.useVersions&&(c.__v=0),this.timestamps)&&!c.createdAt&&(c.createdAt=t),o=new monitor_manager_1.MonitorMongo("findOneAndReplace",this.collectionName,JSON.stringify([s,c,l])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndReplace(s,c,l).then(function(e){o.finish();var n;e?(i.createLogs&&!d&&log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:i.collectionName,id_document:e._id,payload:(0,common_1.getBinarySize)(JSON.stringify([e,s,c,l]))<2e5?JSON.stringify([e,s,c,l],null,2):"Too Big",method:"findOneAndReplace",id_user:"",user:"",messageId:0,route:""}),i.useVersions&&((n=(0,common_1.deepCopy)(e))._id={_id:e._id,__v:e.__v},index_1.ResolveIOServer.getMongoManager().collection(i.versionCollection).replaceOne({_id:{_id:e._id,__v:e.__v}},n,{upsert:!0})),index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(i.collectionName),r(e)):r(null)},function(e){o.finish(),console.log(new Date,"Error Find One And Replace",i.collectionName,s,l,e),a(e)})):(console.log(new Date,this.collectionName,"Schema Errors - findOneAndReplace",n.validationErrors()),index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(index_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on findOneAndReplace - "+this.collectionName,[n.validationErrors(),c]),a(n.validationErrors())),[2]})})})},MongoManagerCollection.prototype.findOneAndUpdate=function(s,c,l,d,u){var e=this;return void 0===s&&(s={}),void 0===d&&(d=!1),void 0===u&&(u=!1),new Promise(function(r,a){return __awaiter(e,void 0,void 0,function(){var n,t,o,i=this;return __generator(this,function(e){return!this.checkSchema||u||(n=this.simplschema.newContext(),n.validate(c,{modifier:!0}))?(this.timestamps&&(t=new Date,c.$set?c.$set.updatedAt=t:c.$set={updatedAt:t}),l&&l.upsert&&(c.$setOnInsert?(c.$setOnInsert._id||(c.$setOnInsert._id=objectIdHexString()),this.timestamps&&!c.$setOnInsert.createdAt&&(c.$setOnInsert.createdAt=new Date)):this.timestamps?c.$setOnInsert={_id:objectIdHexString(),createdAt:new Date}:c.$setOnInsert={_id:objectIdHexString()}),o=new monitor_manager_1.MonitorMongo("findOneAndUpdate",this.collectionName,JSON.stringify([s,c,l])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndUpdate(s,c,l).then(function(e){o.finish();var n;e?(i.createLogs&&!d&&log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:i.collectionName,id_document:e._id,payload:(0,common_1.getBinarySize)(JSON.stringify([e,s,c,l]))<2e5?JSON.stringify([e,s,c,l],null,2):"Too Big",method:"findOneAndUpdate",id_user:"",user:"",messageId:0,route:""}),i.useVersions&&((n=(0,common_1.deepCopy)(e))._id={_id:e._id,__v:e.__v},index_1.ResolveIOServer.getMongoManager().collection(i.versionCollection).replaceOne({_id:{_id:e._id,__v:e.__v}},n,{upsert:!0})),index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(i.collectionName),r(e)):r(null)},function(e){o.finish(),console.log(new Date,"Error Find One And Update",i.collectionName,s,l,e),a(e)})):(console.log(new Date,this.collectionName,"Schema Errors - findOneAndUpdate",n.validationErrors()),index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(index_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on findOneAndUpdate - "+this.collectionName,[n.validationErrors(),c]),a(n.validationErrors())),[2]})})})},MongoManagerCollection.prototype.indexes=function(i){var r=this;return new Promise(function(n,t){var o=new monitor_manager_1.MonitorMongo("indexes",r.collectionName,JSON.stringify([i]));index_1.ResolveIOServer.getMainDB().collection(r.collectionName,r.collectionOptions).indexes(i).then(function(e){o.finish(),n(e)},function(e){o.finish(),console.log(new Date,"Error Indexes",r.collectionName,i,e),t(e)})})},MongoManagerCollection.prototype.indexExists=function(i,r){var a=this;return new Promise(function(n,t){var o=new monitor_manager_1.MonitorMongo("indexExists",a.collectionName,JSON.stringify([i,r]));index_1.ResolveIOServer.getMainDB().collection(a.collectionName,a.collectionOptions).indexExists(i,r).then(function(e){o.finish(),n(e)},function(e){o.finish(),console.log(new Date,"Error Index Exists",a.collectionName,i,r,e),t(e)})})},MongoManagerCollection.prototype.insertMany=function(d,u,g,_,h){var e=this;return void 0===g&&(g=!1),void 0===_&&(_=!1),void 0===h&&(h=!1),new Promise(function(c,l){return __awaiter(e,void 0,void 0,function(){var t,n,o,i,r,a,s=this;return __generator(this,function(e){if(d.length){for(t=[],this.checkSchema&&!_&&d.forEach(function(e){var n=s.simplschema.newContext();n.validate(e)?t.push(!0):(console.log(new Date,s.collectionName,"Schema Errors - insertMany",n.validationErrors()),index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(index_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on insertMany - "+s.collectionName,[n.validationErrors(),e]),t.push(!1))}),n=this.checkSchema?d.filter(function(e,n){return t[n]}):d,o=0;o<n.length;o++)(i=n[o])._id||(i._id=objectIdHexString()),this.timestamps&&(r=new Date,i.createdAt||(i.createdAt=r),i.updatedAt||(i.updatedAt=r)),this.useVersions&&!i.hasOwnProperty("__v")&&(i.__v=0),this.createLogs&&!g&&log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:this.collectionName,id_document:i._id,payload:(0,common_1.getBinarySize)(JSON.stringify([i,u]))<2e5?JSON.stringify([i,u],null,2):"Too Big",method:"insertMany",id_user:"",user:"",messageId:0,route:""});n.length&&(a=null,h||(a=new monitor_manager_1.MonitorMongo("insertMany",this.collectionName,JSON.stringify([n,u]))),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).insertMany(n,u).then(function(e){a&&a.finish(),e.acknowledged?(index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(s.collectionName),c(n)):l(e.acknowledged)},function(e){a&&a.finish(),console.log(new Date,"Error Insert Many",s.collectionName,n,u,e),l(e)}))}else c([]);return[2]})})})},MongoManagerCollection.prototype.insertOne=function(s,c,l,d){var e=this;return void 0===l&&(l=!1),void 0===d&&(d=!1),new Promise(function(r,a){return __awaiter(e,void 0,void 0,function(){var n,t,o,i=this;return __generator(this,function(e){return s._id||(s._id=objectIdHexString()),!this.checkSchema||d||(n=this.simplschema.newContext(),n.validate(s))?(this.timestamps&&(t=new Date,s.createdAt||(s.createdAt=t),s.updatedAt||(s.updatedAt=t)),this.createLogs&&!l&&log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:this.collectionName,id_document:s._id,payload:(0,common_1.getBinarySize)(JSON.stringify([s,c]))<2e5?JSON.stringify([s,c],null,2):"Too Big",method:"insertOne",id_user:"",user:"",messageId:0,route:""}),this.useVersions&&!s.hasOwnProperty("__v")&&(s.__v=0),o=new monitor_manager_1.MonitorMongo("insertOne",this.collectionName,JSON.stringify([s,c])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).insertOne(s,c).then(function(e){o.finish(),e.acknowledged?(index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(i.collectionName),r(s)):a(e.acknowledged)},function(e){o.finish(),console.log(new Date,"Error Insert One",i.collectionName,s,c,e),a(e)})):(console.log(new Date,this.collectionName,"Schema Errors - insertOne",n.validationErrors()),index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(index_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on insertOne - "+this.collectionName,[n.validationErrors(),s]),a(n.validationErrors())),[2]})})})},MongoManagerCollection.prototype.listIndexes=function(e){var n=new monitor_manager_1.MonitorMongo("listIndexes",this.collectionName,JSON.stringify([e])),e=index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).listIndexes(e);return e.on("close",function(e){return n.finish(),e}),e},MongoManagerCollection.prototype.rename=function(i,r){var a=this;return new Promise(function(n,t){var o=new monitor_manager_1.MonitorMongo("rename",a.collectionName,JSON.stringify([i,r]));index_1.ResolveIOServer.getMainDB().collection(a.collectionName,a.collectionOptions).rename(i,r).then(function(e){o.finish(),n(e)},function(e){o.finish(),console.log(new Date,"Error Rename",a.collectionName,i,r,e),t(e)})})},MongoManagerCollection.prototype.replaceOne=function(m,f,v,p,M,O){var e=this;return void 0===p&&(p=!1),void 0===M&&(M=!1),void 0===O&&(O=null),new Promise(function(_,h){return __awaiter(e,void 0,void 0,function(){var n,t,o,i,r,a,s,c,l,d,u,g=this;return __generator(this,function(e){switch(e.label){case 0:return!this.checkSchema||M||(t=this.simplschema.newContext(),t.validate(f))?(n=new Date,this.timestamps&&(f.updatedAt=n),this.useVersions?O?[3,2]:[4,this.findOne(m,null,!0)]:[3,8]):(console.log(new Date,this.collectionName,"Schema Errors - replaceOne",t.validationErrors()),index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(index_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on replaceOne - "+this.collectionName,[t.validationErrors(),f]),h(t.validationErrors()),[2]);case 1:O=e.sent(),e.label=2;case 2:return O?(this.timestamps&&!f.createdAt&&O.createdAt&&(f.createdAt=O.createdAt),O.__v!==f.__v?[3,3]:(f.__v+=1,(t=(0,common_1.deepCopy)(O))._id={_id:O._id,__v:O.__v},index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:O._id,__v:O.__v}},t,{upsert:!0}),4<=O.__v&&index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({$and:[{"_id._id":O._id},{"_id.__v":{$lt:O.__v-4}}]}),this.createLogs&&!p&&log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:this.collectionName,id_document:O._id,payload:(0,common_1.getBinarySize)(JSON.stringify([O,m,f,v]))<2e5?JSON.stringify([O,m,f,v],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:""}),o=new monitor_manager_1.MonitorMongo("replaceOne",this.collectionName,JSON.stringify([m,f,v])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).replaceOne(m,f,v).then(function(e){o.finish(),e.acknowledged?(index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(g.collectionName),_(e.modifiedCount)):h(e.acknowledged)},function(e){o.finish(),console.log(new Date,"Error Replace One",g.collectionName,m,f,v,e),h(e)}),[3,5])):[3,6];case 3:return console.log("invalid version - "+this.collectionName,O.__v,f.__v),[4,index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).findOne({_id:{_id:O._id,__v:f.__v}},null,!0)];case 4:(i=e.sent())?(r=O._id,a=O.__v,(s=(0,common_1.getMongoMergeUpdatedDoc)(f,O,i))._id=r,s.__v=a+1,this.createLogs&&!p&&log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:this.collectionName,id_document:r,payload:(0,common_1.getBinarySize)(JSON.stringify(["invalidVersion - merge",O,m,s,v]))<2e5?JSON.stringify(["invalidVersion - merge",O,m,s,v],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:""}),c=new monitor_manager_1.MonitorMongo("replaceOne",this.collectionName,JSON.stringify([m,s,v])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).replaceOne(m,s,v).then(function(e){c.finish(),e.acknowledged?(index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(g.collectionName),_(e.modifiedCount)):h(e.acknowledged)},function(e){c.finish(),console.log(new Date,"Error Replace One",g.collectionName,m,s,v,e),h(e)})):h("Invalid Version And Could Not Find History - DB: "+O.__v+", Trying to update with :"+f.__v),e.label=5;case 5:return[3,7];case 6:v&&v.upsert?(this.timestamps&&(f.createdAt=n),f._id||(f._id=objectIdHexString()),f.__v=0,this.createLogs&&!p&&log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:this.collectionName,id_document:f._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",m,f,v]))<2e5?JSON.stringify(["upsert",m,f,v],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:""}),l=new monitor_manager_1.MonitorMongo("replaceOne",this.collectionName,JSON.stringify([m,f,v])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).replaceOne(m,f,v).then(function(e){l.finish(),e.acknowledged?(index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(g.collectionName),_(e.modifiedCount)):h(e.acknowledged)},function(e){l.finish(),console.log(new Date,"Error Replace One",g.collectionName,m,f,v,e),h(e)})):h("No Document"),e.label=7;case 7:return[3,9];case 8:v&&v.upsert&&(this.timestamps&&!f.createdAt&&(f.createdAt=n),f._id||(f._id=objectIdHexString()),f.__v=0),this.createLogs&&!p?(v?v.returnDocument="before":v={returnDocument:"before"},d=new monitor_manager_1.MonitorMongo("findOneAndReplace",this.collectionName,JSON.stringify([m,f,v])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndReplace(m,f,v).then(function(e){d.finish();e?log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:g.collectionName,id_document:e._id,payload:(0,common_1.getBinarySize)(JSON.stringify([e,m,f,v]))<2e5?JSON.stringify([e,m,f,v],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:""}):log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:g.collectionName,id_document:f._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",m,f,v]))<2e5?JSON.stringify(["upsert",m,f,v],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:""}),index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(g.collectionName),_(1)},function(e){d.finish(),console.log(new Date,"Error Find One And Replace",g.collectionName,m,f,v,e),h(e)})):(u=new monitor_manager_1.MonitorMongo("replaceOne",this.collectionName,JSON.stringify([m,f,v])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).replaceOne(m,f,v).then(function(e){u.finish(),e.acknowledged?(index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(g.collectionName),_(e.modifiedCount)):h(e.acknowledged)},function(e){u.finish(),console.log(new Date,"Error Replace One",g.collectionName,m,f,v,e),h(e)})),e.label=9;case 9:return[2]}})})})},MongoManagerCollection.prototype.updateMany=function(d,u,g,_,h,m){var e=this;return void 0===_&&(_=!1),void 0===h&&(h=!1),void 0===m&&(m=!1),new Promise(function(c,l){return __awaiter(e,void 0,void 0,function(){var n,t,o,i,r,a,s=this;return __generator(this,function(e){switch(e.label){case 0:return u.$inc&&"{}"!==JSON.stringify(u.$inc)||u.$set&&"{}"!==JSON.stringify(u.$set)||u.$setOnInsert&&"{}"!==JSON.stringify(u.$setOnInsert)||u.$unset&&"{}"!==JSON.stringify(u.$unset)?(this.timestamps&&(n=new Date,u.$set?u.$set.updatedAt=n:u.$set={updatedAt:n}),!this.checkSchema||h||(n=this.simplschema.newContext(),n.validate(u,{modifier:!0}))?this.useVersions&&!m||this.createLogs&&!_?[4,this.find(d)]:[3,2]:(console.log(new Date,this.collectionName,"Schema Errors - updateMany",n.validationErrors()),index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(index_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on updateMany - "+this.collectionName,[n.validationErrors(),u]),l(n.validationErrors()),[2])):(c(1),[2]);case 1:for(t=e.sent(),o=0;o<t.length;o++)i=t[o],this.createLogs&&!_&&log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:this.collectionName,id_document:i._id,payload:(0,common_1.getBinarySize)(JSON.stringify([i,d,u,g]))<2e5?JSON.stringify([i,d,u,g],null,2):"Too Big",method:"updateMany",id_user:"",user:"",messageId:0,route:""}),this.useVersions&&((r=(0,common_1.deepCopy)(i))._id={_id:i._id,__v:i.__v},index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:i._id,__v:i.__v}},r,{upsert:!0}),4<=i.__v&&index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({$and:[{"_id._id":i._id},{"_id.__v":{$lt:i.__v-4}}]}),objectContainsPropertyDeep(u,"__v")||(u.$inc?u.$inc.__v||(u.$inc.__v=1):u.$inc={__v:1}));e.label=2;case 2:return a=new monitor_manager_1.MonitorMongo("updateMany",this.collectionName,JSON.stringify([d,u,g])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).updateMany(d,u,g).then(function(e){a.finish(),e.acknowledged?(index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(s.collectionName),c(e.modifiedCount)):l(e.acknowledged)},function(e){a.finish(),console.log(new Date,"Error Update Many",s.collectionName,d,u,g,e),l(e)}),[2]}})})})},MongoManagerCollection.prototype.updateOne=function(d,u,g,_,h){var e=this;return void 0===_&&(_=!1),void 0===h&&(h=!1),new Promise(function(c,l){return __awaiter(e,void 0,void 0,function(){var n,t,o,i,r,a,s=this;return __generator(this,function(e){switch(e.label){case 0:return!this.checkSchema||h||(t=this.simplschema.newContext(),t.validate(u,{modifier:!0}))?u.$inc&&"{}"!==JSON.stringify(u.$inc)||u.$set&&"{}"!==JSON.stringify(u.$set)||u.$setOnInsert&&"{}"!==JSON.stringify(u.$setOnInsert)||u.$unset&&"{}"!==JSON.stringify(u.$unset)?(n=new Date,this.timestamps&&(u.$set?u.$set.updatedAt=n:u.$set={updatedAt:n}),this.useVersions?[4,this.findOne(d,null,!0)]:[3,2]):(c(1),[2]):(console.log(new Date,this.collectionName,"Schema Errors - updateOne",t.validationErrors()),index_1.ResolveIOServer.getMainServer().getMethodManager().callMethodInternal.call(index_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on updateOne - "+this.collectionName,[t.validationErrors(),u]),l(t.validationErrors()),[2]);case 1:return(n=e.sent())?(this.createLogs&&!_&&log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:this.collectionName,id_document:n._id,payload:(0,common_1.getBinarySize)(JSON.stringify([n,d,u,g]))<2e5?JSON.stringify([n,d,u,g],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:""}),(t=(0,common_1.deepCopy)(n))._id={_id:n._id,__v:n.__v},index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:n._id,__v:n.__v}},t,{upsert:!0}),4<=n.__v&&index_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({$and:[{"_id._id":n._id},{"_id.__v":{$lt:n.__v-4}}]}),objectContainsPropertyDeep(u,"__v")||(u.$inc?u.$inc.__v||(u.$inc.__v=1):u.$inc={__v:1}),o=new monitor_manager_1.MonitorMongo("updateOne",this.collectionName,JSON.stringify([d,u,g])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).updateOne(d,u,g).then(function(e){o.finish(),e.acknowledged?(index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(s.collectionName),c(e.modifiedCount)):l(e.acknowledged)},function(e){o.finish(),console.log(new Date,"Error Update One",s.collectionName,d,u,g,e),l(e)})):g&&g.upsert?(u.$setOnInsert?(u.$setOnInsert._id||(u.$setOnInsert._id=objectIdHexString()),this.timestamps&&!u.$setOnInsert.createdAt&&(u.$setOnInsert.createdAt=new Date)):this.timestamps?u.$setOnInsert={_id:objectIdHexString(),__v:0,createdAt:new Date}:u.$setOnInsert={_id:objectIdHexString(),__v:0},this.createLogs&&!_&&log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:this.collectionName,id_document:u.$setOnInsert._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",d,u,g]))<2e5?JSON.stringify(["upsert",d,u,g],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:""}),i=new monitor_manager_1.MonitorMongo("updateOne",this.collectionName,JSON.stringify([d,u,g])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).updateOne(d,u,g).then(function(e){i.finish(),e.acknowledged?(index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(s.collectionName),c(e.modifiedCount)):l(e.acknowledged)},function(e){i.finish(),console.log(new Date,"Error Update One",s.collectionName,d,u,g,e),l(e)})):l("No Document"),[3,3];case 2:g&&g.upsert&&(u.$setOnInsert?(u.$setOnInsert._id||(u.$setOnInsert._id=objectIdHexString()),this.timestamps&&!u.$setOnInsert.createdAt&&(u.$setOnInsert.createdAt=new Date)):this.timestamps?u.$setOnInsert={_id:objectIdHexString(),createdAt:new Date}:u.$setOnInsert={_id:objectIdHexString()}),this.createLogs&&!_?(g?g.returnDocument="before":g={returnDocument:"before"},r=new monitor_manager_1.MonitorMongo("findOneAndUpdate",this.collectionName,JSON.stringify([d,u,g])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndUpdate(d,u,g).then(function(e){r.finish();var n=e.value;n?(log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:s.collectionName,id_document:n._id,payload:(0,common_1.getBinarySize)(JSON.stringify([n,d,u,g]))<2e5?JSON.stringify([n,d,u,g],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:""}),c(e.ok)):u.$setOnInsert?(log_collection_1.Logs.insertOne({_id:objectIdHexString(),type:"document",collection:s.collectionName,id_document:u.$setOnInsert._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",d,u,g]))<2e5?JSON.stringify(["upsert",d,u,g],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:""}),index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(s.collectionName),c(e.ok)):c(0)},function(e){r.finish(),console.log(new Date,"Error Find One And Update",s.collectionName,d,u,g,e),l(e)})):(a=new monitor_manager_1.MonitorMongo("updateOne",this.collectionName,JSON.stringify([d,u,g])),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).updateOne(d,u,g).then(function(e){a.finish(),e.acknowledged?(index_1.ResolveIOServer.getMongoManager().invalidateQueryCache(s.collectionName),c(e.modifiedCount)):l(e.acknowledged)},function(e){a.finish(),console.log(new Date,"Error Update One",s.collectionName,d,u,g,e),l(e)})),e.label=3;case 3:return[2]}})})})},MongoManagerCollection.prototype.watchCollection=function(e,n){return void 0===e&&(e=[]),index_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).watch(e,n)},(e=MongoManagerCollection).indexQueue=async_1.default.queue(function(d,u){return __awaiter(void 0,void 0,void 0,function(){var n,t,o,i,r,a,s,c,l;return __generator(e,function(e){switch(e.label){case 0:if(n=d.action,t=d.collection,o=d.specs,i=d.options,!(r=index_1.ResolveIOServer.getMainDB().collection(t.collectionName,t.collectionOptions)))return[2,u(new Error("Collection ".concat(t.collectionName," not found")))];a=new monitor_manager_1.MonitorMongo(n,t.collectionName,JSON.stringify([o,i])),e.label=1;case 1:return e.trys.push([1,3,,10]),[4,r[n].apply(r,__spreadArray(__spreadArray([],__read(o),!1),[i],!1))];case 2:return c=e.sent(),a.finish(),u(null,c),[3,10];case 3:if(85!==(s=e.sent()).code&&!s.message.includes("IndexOptionsConflict"))return[3,9];e.label=4;case 4:return(e.trys.push([4,8,,9]),l=t.extractIndexNameFromError(s.message))?[4,r.dropIndex(l)]:[3,7];case 5:return e.sent(),[4,r[n].apply(r,__spreadArray(__spreadArray([],__read(o),!1),[i],!1))];case 6:return c=e.sent(),a.finish(),u(null,c),[2];case 7:return[3,9];case 8:return l=e.sent(),u(l),[3,9];case 9:return a.finish(),u(s),[3,10];case 10:return[2]}})})},1),MongoManagerCollection}()),MongoManagerUserCollection=function(e){function MongoManagerUserCollection(){return null!==e&&e.apply(this,arguments)||this}return __extends(MongoManagerUserCollection,e),MongoManagerUserCollection.prototype.authenticate=function(a,t){var e=this;return new Promise(function(i,r){return __awaiter(e,void 0,void 0,function(){var n,o=this;return __generator(this,function(e){switch(e.label){case 0:return(n=Math.pow(100,Math.log(a.attempts+1)),n=n<3e5?n:3e5,a.last)?Date.now()-a.last.getTime()<n?(a.last=new Date,[4,user_collection_1.Users.updateOne({_id:a._id},{$set:{last:a.last}})]):[3,2]:[3,3];case 1:return e.sent(),i({data:null,error:"Attempt Too Soon"}),[2];case 2:return[3,4];case 3:a.last=new Date,e.label=4;case 4:return 5<=a.attempts?i({data:null,error:"Too Many Attempts"}):a.salt?pbkdf2(t,a.salt,{iterations:25e3,keylen:512,digestAlgorithm:"sha256"},function(n,t){return __awaiter(o,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return n?[2,r(n)]:scmp(t,Buffer.from(a.hash,"hex"))?(a.last=new Date,a.attempts=0,[4,user_collection_1.Users.updateOne({_id:a._id},{$set:{last:a.last,attempts:a.attempts}})]):[3,2];case 1:return e.sent(),i({data:a,error:""}),[3,4];case 2:return a.last=new Date,a.attempts=a.attempts+1,[4,user_collection_1.Users.updateOne({_id:a._id},{$set:{last:a.last,attempts:a.attempts}})];case 3:e.sent(),5<=a.attempts?i({data:null,error:"Too Many Attempts"}):i({data:null,error:"Invalid Username And Password"}),e.label=4;case 4:return[2]}})})}):i({data:null,error:"No Salt Value Stored"}),[2]}})})})},MongoManagerUserCollection.prototype.serializeUser=function(){return function(e,n){n(null,e.username)}},MongoManagerUserCollection.prototype.deserializeUser=function(){return function(e,n){user_collection_1.Users.findOne({username:e}).then(function(e){n(null,e)},function(e){n(e,null)})}},MongoManagerUserCollection.prototype.setPassword=function(r,a){var e=this;return new Promise(function(o,i){return __awaiter(e,void 0,void 0,function(){var n,t;return __generator(this,function(e){switch(e.label){case 0:return r?[3,1]:(i("No User"),[3,5]);case 1:return a?[3,2]:(i("No Password"),[3,5]);case 2:return[4,randomBytes(32)];case 3:return n=e.sent(),n=n.toString("hex"),[4,pbkdf2Promisified(a,n,{iterations:25e3,keylen:512,digestAlgorithm:"sha256"})];case 4:t=e.sent(),t=Buffer.from(t,"binary").toString("hex"),user_collection_1.Users.updateOne({_id:r._id},{$set:{hash:t,salt:n,services:{},attempts:0}}).then(function(e){return o(e)},function(e){return i(e)}),e.label=5;case 5:return[2]}})})})},MongoManagerUserCollection.prototype.changePassword=function(i,r,a){var e=this;return new Promise(function(t,o){return __awaiter(e,void 0,void 0,function(){var n;return __generator(this,function(e){switch(e.label){case 0:return i?[3,1]:(o("Missing User"),[3,4]);case 1:return r&&a?[3,2]:(o("Missing Password"),[3,4]);case 2:return[4,this.authenticate(i,r)];case 3:(n=e.sent()).data?this.setPassword(i,a).then(function(e){return t(e)},function(e){return o(e)}):o(n.error),e.label=4;case 4:return[2]}})})})},MongoManagerUserCollection.prototype.register=function(o,i){var e=this;return new Promise(function(n,t){return __awaiter(e,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return o.username?[3,1]:(t("Missing Username"),[3,5]);case 1:return[4,user_collection_1.Users.findOne({username:o.username})];case 2:return e.sent()?(t("Username Exists"),[3,5]):[3,3];case 3:return[4,user_collection_1.Users.findOne({email:o.email})];case 4:e.sent()?t("Email Exists"):(o.setPassword(i),n(o)),e.label=5;case 5:return[2]}})})})},MongoManagerUserCollection.prototype.resetAttempts=function(e){return user_collection_1.Users.updateOne({_id:e._id},{$set:{attempts:0}})},MongoManagerUserCollection}(exports.MongoManagerCollection=MongoManagerCollection);function objectIdHexString(){return(new mongodb_1.ObjectId).toHexString()}function pbkdf2(e,n,t,o){crypto.pbkdf2(e,n,t.iterations,t.keylen,t.digestAlgorithm,o)}function pbkdf2Promisified(e,n,i){return new Promise(function(t,o){return pbkdf2(e,n,i,function(e,n){return e?o(e):t(n)})})}function randomBytes(e){return new Promise(function(t,o){return crypto.randomBytes(e,function(e,n){return e?o(e):t(n)})})}function objectContainsPropertyDeep(e,n){var t=Object.keys(e);if(t.some(function(e){return e===n}))return!0;for(var o=0;o<t.length;o++){var i=t[o];if(e[i]&&"object"==typeof e[i]&&objectContainsPropertyDeep(e[i],n))return!0}return!1}exports.MongoManagerUserCollection=MongoManagerUserCollection,exports.objectIdHexString=objectIdHexString;
|
|
2
2
|
//# sourceMappingURL=mongo.manager.js.map
|