@resolveio/server-lib 20.7.12 → 20.7.13

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.
@@ -1,2 +1,2 @@
1
- "use strict";var __extends=this&&this.__extends||function(){var t=function(e,o){return(t=Object.setPrototypeOf||({__proto__:[]}instanceof Array?function(e,o){e.__proto__=o}:function(e,o){for(var t in o)Object.prototype.hasOwnProperty.call(o,t)&&(e[t]=o[t])}))(e,o)};return function(e,o){if("function"!=typeof o&&null!==o)throw new TypeError("Class extends value "+String(o)+" is not a constructor or null");function __(){this.constructor=e}t(e,o),e.prototype=null===o?Object.create(o):(__.prototype=o.prototype,new __)}}(),__assign=this&&this.__assign||function(){return(__assign=Object.assign||function(e){for(var o,t=1,r=arguments.length;t<r;t++)for(var n in o=arguments[t])Object.prototype.hasOwnProperty.call(o,n)&&(e[n]=o[n]);return e}).apply(this,arguments)},__awaiter=this&&this.__awaiter||function(e,r,n,s){return new(n=n||Promise)(function(t,o){function fulfilled(e){try{step(s.next(e))}catch(e){o(e)}}function rejected(e){try{step(s.throw(e))}catch(e){o(e)}}function step(e){var o;e.done?t(e.value):((o=e.value)instanceof n?o:new n(function(e){e(o)})).then(fulfilled,rejected)}step((s=s.apply(e,r||[])).next())})},__generator=this&&this.__generator||function(r,n){var s,i,a,c={label:0,sent:function(){if(1&a[0])throw a[1];return a[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 o=[t,e];if(s)throw new TypeError("Generator is already executing.");for(;c=l&&o[l=0]?0:c;)try{if(s=1,i&&(a=2&o[0]?i.return:o[0]?i.throw||((a=i.return)&&a.call(i),0):i.next)&&!(a=a.call(i,o[1])).done)return a;switch(i=0,(o=a?[2&o[0],a.value]:o)[0]){case 0:case 1:a=o;break;case 4:return c.label++,{value:o[1],done:!1};case 5:c.label++,i=o[1],o=[0];continue;case 7:o=c.ops.pop(),c.trys.pop();continue;default:if(!(a=0<(a=c.trys).length&&a[a.length-1])&&(6===o[0]||2===o[0])){c=0;continue}if(3===o[0]&&(!a||o[1]>a[0]&&o[1]<a[3]))c.label=o[1];else if(6===o[0]&&c.label<a[1])c.label=a[1],a=o;else{if(!(a&&c.label<a[2])){a[2]&&c.ops.pop(),c.trys.pop();continue}c.label=a[2],c.ops.push(o)}}o=n.call(r,c)}catch(e){o=[6,e],i=0}finally{s=a=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}}},__rest=this&&this.__rest||function(e,o){var t={};for(n in e)Object.prototype.hasOwnProperty.call(e,n)&&o.indexOf(n)<0&&(t[n]=e[n]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols)for(var r=0,n=Object.getOwnPropertySymbols(e);r<n.length;r++)o.indexOf(n[r])<0&&Object.prototype.propertyIsEnumerable.call(e,n[r])&&(t[n[r]]=e[n[r]]);return t},__read=this&&this.__read||function(e,o){var t="function"==typeof Symbol&&e[Symbol.iterator];if(!t)return e;var r,n,s=t.call(e),i=[];try{for(;(void 0===o||0<o--)&&!(r=s.next()).done;)i.push(r.value)}catch(e){n={error:e}}finally{try{r&&!r.done&&(t=s.return)&&t.call(s)}finally{if(n)throw n.error}}return i},__spreadArray=this&&this.__spreadArray||function(e,o,t){if(t||2===arguments.length)for(var r,n=0,s=o.length;n<s;n++)!r&&n in o||((r=r||Array.prototype.slice.call(o,0,n))[n]=o[n]);return e.concat(r||Array.prototype.slice.call(o))},__values=this&&this.__values||function(e){var o="function"==typeof Symbol&&Symbol.iterator,t=o&&e[o],r=0;if(t)return t.call(e);if(e&&"number"==typeof e.length)return{next:function(){return{value:(e=e&&r>=e.length?void 0:e)&&e[r++],done:!e}}};throw new TypeError(o?"Object is not iterable.":"Symbol.iterator is not defined.")},async_1=(Object.defineProperty(exports,"__esModule",{value:!0}),exports.MongoManagerUserCollection=exports.MongoManagerCollection=exports.MongoManagerModel=exports.MongoManager=void 0,require("async")),NodeCache=require("node-cache"),os_1=require("os"),simpl_schema_1=require("simpl-schema"),user_collection_1=require("../collections/user.collection"),resolveio_server_app_1=require("../resolveio-server-app"),common_1=require("../util/common"),monitor_manager_1=require("./monitor.manager"),crypto=require("crypto"),scmp=require("scmp"),numCPUs=(0,os_1.cpus)().length,v8=require("v8"),AsyncLocalStorage=require("async_hooks").AsyncLocalStorage,asyncLocalStorage=new AsyncLocalStorage,MongoManager=function(){function MongoManager(){this._collections=[],this._cacheMap=[],this._operationInProgress=new Map,this._heapSize=v8.getHeapStatistics()/numCPUs,this._serverCollections=[],this._isWorkersEnabled=!1,this._isWorkerInstance=!1}return MongoManager.create=function(){return __awaiter(this,void 0,void 0,function(){var o;return __generator(this,function(e){switch(e.label){case 0:return[4,(o=new MongoManager).initialize()];case 1:return e.sent(),[2,o]}})})},MongoManager.prototype.initialize=function(){return __awaiter(this,void 0,void 0,function(){var o;return __generator(this,function(e){switch(e.label){case 0:return 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._isWorkersEnabled&&this._isWorkerInstance&&this.setupChangeStream(),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().listCollections().toArray()];case 1:return o=e.sent(),this._serverCollections=o,[2]}})})},MongoManager.prototype.oneTimeTransaction=function(n){return __awaiter(this,void 0,void 0,function(){var o=this;return __generator(this,function(e){switch(e.label){case 0:return[4,asyncLocalStorage.run({},function(){return __awaiter(o,void 0,void 0,function(){var o,t,r=this;return __generator(this,function(e){switch(e.label){case 0:o=resolveio_server_app_1.ResolveIOServer.getMongoConnection().startSession(),asyncLocalStorage.getStore().session=o,e.label=1;case 1:return e.trys.push([1,3,4,6]),[4,o.withTransaction(function(){return __awaiter(r,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return[4,n()];case 1:return[2,e.sent()]}})})})];case 2:return[2,e.sent()];case 3:throw e.sent();case 4:return[4,o.endSession()];case 5:return e.sent(),(t=asyncLocalStorage.getStore())&&delete t.session,[7];case 6:return[2]}})})})];case 1:return[2,e.sent()]}})})},MongoManager.prototype.getSession=function(){var e=asyncLocalStorage.getStore();return e?e.session:null},MongoManager.prototype.setCacheLimit=function(){this._isWorkersEnabled?this._heapLimit=this._isWorkerInstance?.8*this._heapSize:.4*this._heapSize:this._heapLimit=.3*this._heapSize},MongoManager.prototype.getServerCollections=function(){return this._serverCollections},MongoManager.prototype.registerCollection=function(o){return __awaiter(this,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return o.collectionOptions&&o.collectionOptions.timeseries&&o.collectionOptions.timeseries.timeField&&this._serverCollections.some(function(e){return e.name===o.collectionName&&"collection"===e.type})?[4,resolveio_server_app_1.ResolveIOServer.getMainDB().dropCollection(o.collectionName)]:[3,3];case 1:return e.sent(),[4,this.createCollection(o)];case 2:e.sent(),e.label=3;case 3:return this._collections.push(o),[2]}})})},MongoManager.prototype.createCollection=function(o){return __awaiter(this,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().createCollection(o.collectionName,o.collectionOptions)];case 1:return e.sent(),[2]}})})},MongoManager.prototype.collections=function(){return this._collections},MongoManager.prototype.collection=function(o){return this._collections.find(function(e){return e.collectionName===o})},MongoManager.prototype.delay=function(o){return __awaiter(this,void 0,void 0,function(){return __generator(this,function(e){return[2,new Promise(function(e){return setTimeout(e,o)})]})})},MongoManager.prototype.find=function(s,i,a){return void 0===i&&(i={}),__awaiter(this,void 0,void 0,function(){var o,t,r,n=this;return __generator(this,function(e){switch(e.label){case 0:return((t=a||{}).session,t=__rest(t,["session"]),o=this.generateCacheKey([s],"find",[i,t]),void 0!==(t=this.getFromCache(o)))?(resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo Find - Cache",s),[2,t]):this._operationInProgress.has(o)?(resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo Find - Already running",s),[2,this._operationInProgress.get(o).promise]):(resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo Find - Running",s),r=this.executeFind(s,i,a,o),this._operationInProgress.set(o,{promise:r,invalidatedDuringExecution:!1,collections:[s]}),[4,r.finally(function(){resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo Find - Done",s),n._operationInProgress.delete(o)})]);case 1:return e.sent(),[2,r]}})})},MongoManager.prototype.executeFind=function(s,i,a,c){return __awaiter(this,void 0,void 0,function(){var o,t,r,n;return __generator(this,function(e){switch(e.label){case 0:t=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(s),(r=a||{}).session,r=__rest(r,["session"]),r=monitor_manager_1.MonitorMongo.create("find",s,JSON.stringify([i,r])),e.label=1;case 1:return e.trys.push([1,3,4,6]),[4,t.find(i,a).toArray()];case 2:return o=e.sent(),[3,6];case 3:throw(n=e.sent()).message="Error in Execute Find (".concat(s,"): ").concat(n.message),n;case 4:return[4,r.finish()];case 5:return e.sent(),[7];case 6:return this._operationInProgress.get(c).invalidatedDuringExecution||this.addToCache([s],c,o),[2,o]}})})},MongoManager.prototype.findOne=function(s,i,a){return void 0===i&&(i={}),__awaiter(this,void 0,void 0,function(){var o,t,r,n=this;return __generator(this,function(e){switch(e.label){case 0:return((t=a||{}).session,t=__rest(t,["session"]),o=this.generateCacheKey([s],"findOne",[i,t]),void 0!==(t=this.getFromCache(o)))?(resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo FindOne - Cache",s),[2,t]):this._operationInProgress.has(o)?(resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo FindOne - Already running",s),[2,this._operationInProgress.get(o).promise]):(resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo FindOne - Running",s),r=this.executeFindOne(s,i,a,o),this._operationInProgress.set(o,{promise:r,invalidatedDuringExecution:!1,collections:[s]}),[4,r.finally(function(){resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo FindOne - Done",s),n._operationInProgress.delete(o)})]);case 1:return e.sent(),[2,r]}})})},MongoManager.prototype.executeFindOne=function(s,i,a,c){return __awaiter(this,void 0,void 0,function(){var o,t,r,n;return __generator(this,function(e){switch(e.label){case 0:t=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(s),(r=a||{}).session,r=__rest(r,["session"]),r=monitor_manager_1.MonitorMongo.create("findOne",s,JSON.stringify([i,r])),e.label=1;case 1:return e.trys.push([1,3,4,6]),[4,t.findOne(i,a)];case 2:return o=e.sent(),[3,6];case 3:throw(n=e.sent()).message="Error in Execute Find One (".concat(s,"): ").concat(n.message),n;case 4:return[4,r.finish()];case 5:return e.sent(),[7];case 6:return this._operationInProgress.get(c).invalidatedDuringExecution||this.addToCache([s],c,o),[2,o]}})})},MongoManager.prototype.aggregate=function(i,a,c){return __awaiter(this,void 0,void 0,function(){var o,t,r,n,s=this;return __generator(this,function(e){switch(e.label){case 0:return(o=__spreadArray([i],__read(a.flatMap(function(e){var o;return null!=(o=e.$lookup)&&o.from?[e.$lookup.from]:[]})),!1),(r=c||{}).session,r=__rest(r,["session"]),t=this.generateCacheKey(o,"aggregate",[a,r]),void 0!==(r=this.getFromCache(t)))?(resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo Agg - Cache",o),[2,r]):this._operationInProgress.has(t)?(resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo Agg - Already running",o),[2,this._operationInProgress.get(t).promise]):(resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo Agg - Running",o),n=this.executeAggregate(i,a,c,t,o),this._operationInProgress.set(t,{promise:n,invalidatedDuringExecution:!1,collections:o}),[4,n.finally(function(){resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo Agg - Done",o),s._operationInProgress.delete(t)})]);case 1:return e.sent(),[2,n]}})})},MongoManager.prototype.executeAggregate=function(s,i,a,c,l){return __awaiter(this,void 0,void 0,function(){var o,t,r,n;return __generator(this,function(e){switch(e.label){case 0:t=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(s),(r=a||{}).session,r=__rest(r,["session"]),r=monitor_manager_1.MonitorMongo.create("aggregate",s,JSON.stringify([i,r])),e.label=1;case 1:return e.trys.push([1,3,4,6]),[4,t.aggregate(i,a).toArray()];case 2:return o=e.sent(),[3,6];case 3:throw(n=e.sent()).message="Error in Execute Aggregate (".concat(s,"): ").concat(n.message),n;case 4:return[4,r.finish()];case 5:return e.sent(),[7];case 6:return this._operationInProgress.get(c).invalidatedDuringExecution||this.addToCache(l,c,o),[2,o]}})})},MongoManager.prototype.countDocuments=function(s,i,a){return void 0===i&&(i={}),__awaiter(this,void 0,void 0,function(){var o,t,r,n=this;return __generator(this,function(e){switch(e.label){case 0:return((t=a||{}).session,t=__rest(t,["session"]),o=this.generateCacheKey([s],"countDocuments",[i,t]),void 0!==(t=this.getFromCache(o)))?[2,t]:this._operationInProgress.has(o)?[2,this._operationInProgress.get(o).promise]:(r=this.executeCountDocuments(s,i,a,o),this._operationInProgress.set(o,{promise:r,invalidatedDuringExecution:!1,collections:[s]}),[4,r.finally(function(){return n._operationInProgress.delete(o)})]);case 1:return e.sent(),[2,r]}})})},MongoManager.prototype.executeCountDocuments=function(s,i,a,c){return __awaiter(this,void 0,void 0,function(){var o,t,r,n;return __generator(this,function(e){switch(e.label){case 0:t=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(s),(r=a||{}).session,r=__rest(r,["session"]),r=monitor_manager_1.MonitorMongo.create("countDocuments",s,JSON.stringify([i,r])),e.label=1;case 1:return e.trys.push([1,3,4,6]),[4,t.countDocuments(i,a)];case 2:return o=e.sent(),[3,6];case 3:throw(n=e.sent()).message="Error in Execute Count Documents (".concat(s,"): ").concat(n.message),n;case 4:return[4,r.finish()];case 5:return e.sent(),[7];case 6:return this._operationInProgress.get(c).invalidatedDuringExecution||this.addToCache([s],c,o),[2,o]}})})},MongoManager.prototype.generateCacheKey=function(e,o,t){e=JSON.stringify({collections:e,functionName:o,args:t});return crypto.createHash("sha256").update(e).digest("hex")},MongoManager.prototype.getFromCache=function(o){try{var e=this._nodeCache.get(o);if(e)return JSON.parse(e,common_1.dateReviver)}catch(e){this._nodeCache.del(o)}},MongoManager.prototype.addToCache=function(e,o,t){var r,n;if((0,common_1.getBinarySize)(JSON.stringify(t))<1e6&&!e.includes("logs")&&!e.find(function(e){return e.endsWith(".versions")})&&!e.find(function(e){return e.startsWith("monitor-")})){var s=this._nodeCache.getStats().vsize;if(s>this._heapLimit){var i=0,a=this._nodeCache.keys();try{for(var c=__values(a),l=c.next();!l.done;l=c.next()){var _=l.value;if(this._nodeCache.del(_),i+=1,(s=this._nodeCache.getStats().vsize)<.75*this._heapLimit)break}}catch(e){r={error:e}}finally{try{l&&!l.done&&(n=c.return)&&n.call(c)}finally{if(r)throw r.error}}console.log("Query Cache: Too Big, - Deleted: "+i+" - "+s)}this._nodeCache.set(o,JSON.stringify(t)),this._cacheMap.push({collections:e,key:o})}},MongoManager.prototype.invalidateQueryCache=function(o){var t,e,r,n,s=this._cacheMap.filter(function(e){return e.collections.includes(o)});resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo Invalidate Cache",o);try{for(var i=__values(s),a=i.next();!a.done;a=i.next()){var c=a.value;this._nodeCache.del(c.key),this._operationInProgress.has(c.key)&&(this._operationInProgress.get(c.key).invalidatedDuringExecution=!0,resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager())&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo Invalidated During Execution",c.collections)}}catch(e){t={error:e}}finally{try{a&&!a.done&&(e=i.return)&&e.call(i)}finally{if(t)throw t.error}}try{for(var l=__values(this._operationInProgress.entries()),_=l.next();!_.done;_=l.next()){var g=__read(_.value,2),u=(g[0],g[1]);u.collections.includes(o)&&(u.invalidatedDuringExecution=!0,resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager())&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo Invalidated During Execution (No cache)",u.collections)}}catch(e){r={error:e}}finally{try{_&&!_.done&&(n=l.return)&&n.call(l)}finally{if(r)throw r.error}}this._cacheMap=this._cacheMap.filter(function(e){return!e.collections.includes(o)})},MongoManager.prototype.setupChangeStream=function(){var o=this,t=resolveio_server_app_1.ResolveIOServer.getMainDB().watch([{$match:{$and:[{"ns.coll":{$nin:["log-method-latencies","log-subscriptions","logs","counters","cron-job-histories","email-histories","qb-soap-request-histories","qb-soap-request-responses","qb-soap-requests","qb-soap-retries"]}},{"ns.coll":{$not:/.*\.versions$/}},{"ns.coll":{$not:/^monitor-/}}]}}],{fullDocument:"updateLookup"});t.on("change",function(e){e.ns&&e.ns.coll&&(e=e.ns.coll,o.collection(e))&&o.invalidateQueryCache(e)}),t.on("error",function(){return __awaiter(o,void 0,void 0,function(){var o=this;return __generator(this,function(e){switch(e.label){case 0:return console.log(new Date,"Mongo change stream error. Restart..."),[4,t.close()];case 1:return e.sent(),setTimeout(function(){return o.setupChangeStream()},5e3),[2]}})})}),t.on("close",function(){console.log(new Date,"Mongo change stream closed. Restarting..."),setTimeout(function(){return o.setupChangeStream()},5e3)})},MongoManager}(),MongoManagerModel=(exports.MongoManager=MongoManager,function(){function MongoManagerModel(){this.collection_main=null,this.collection_version=null}return MongoManagerModel.create=function(e){var o=new MongoManagerModel;return o.initialize(e),o},MongoManagerModel.prototype.initialize=function(e){var o,t=this;"users"===e.collectionName?this.collection_main=MongoManagerUserCollection.create(e):this.collection_main=MongoManagerCollection.create(e),e.useVersionCollection&&(this.collection_main.useVersions=!0,(o=(0,common_1.deepCopy)(e.schema))._id.type="Object",o._id.blackbox=!0,o={collectionName:e.collectionName+".versions",schema:o,useVersionCollection:!1,useReportBuilder:!1,reportBuilderLookupTables:[],timestamps:!0,createLogs:!1,checkSchema:!1,collectionOptions:null,skipCache:!0},"users"===e.collectionName?this.collection_version=MongoManagerUserCollection.create(o):this.collection_version=MongoManagerCollection.create(o),setImmediate(function(){return __awaiter(t,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return[4,this.collection_version.createIndex({"_id._id":1,"_id.__v":1})];case 1:return e.sent(),[2]}})})}),this.collection_main.versionCollection=e.collectionName+".versions")},MongoManagerModel}()),MongoManagerCollection=(exports.MongoManagerModel=MongoManagerModel,function(){function MongoManagerCollection(){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.skipCache=!1,this.bypassSession=!1}var e;return MongoManagerCollection.create=function(e){var o=new MongoManagerCollection;return o.initialize(e),o},MongoManagerCollection.prototype.initialize=function(e){var o,t=this,r=(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,this.skipCache=e.skipCache||!1,this.bypassSession=e.bypassSession||!1,e.useReportBuilder&&(this.useRB=!0,o=(0,common_1.deepCopy)(e.schema),e=(0,common_1.buildRbLookups)(e.reportBuilderLookupTables,o,[]),this.rbSchema=(0,common_1.buildRbSchema)(e)),setInterval(function(){return __awaiter(t,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return resolveio_server_app_1.ResolveIOServer&&resolveio_server_app_1.ResolveIOServer.getMainServer()&&resolveio_server_app_1.ResolveIOServer.getMongoManager()&&resolveio_server_app_1.ResolveIOServer.getMongoManager().getServerCollections().length?[4,resolveio_server_app_1.ResolveIOServer.getMongoManager().registerCollection(this)]:[3,2];case 1:e.sent(),clearInterval(r),e.label=2;case 2:return[2]}})})},1))},MongoManagerCollection.prototype.extractIndexNameFromError=function(e){var o=e.match(/Index already exists with a different name: (\S+)/);return(o=!o||o.length<=1?e.match(/existing index:.*name: "([^"]+)"/):o)&&1<o.length?o[1]:null},MongoManagerCollection.prototype.aggregate=function(s,i,a,c){return void 0===a&&(a=!1),void 0===c&&(c=!1),__awaiter(this,void 0,void 0,function(){var o,t,r,n;return __generator(this,function(e){switch(e.label){case 0:return!(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())||c||this.bypassSession||(i?i.session=o:i={session:o}),a||this.skipCache?[3,1]:[2,resolveio_server_app_1.ResolveIOServer.getMongoManager().aggregate(this.collectionName,s,i)];case 1:(o=i||{}).session,t=__rest(o,["session"]),r=monitor_manager_1.MonitorMongo.create("aggregate",this.collectionName,JSON.stringify([s,t])),e.label=2;case 2:return e.trys.push([2,4,5,7]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).aggregate(s,i).toArray()];case 3:return[2,e.sent()];case 4:throw n=e.sent(),console.log(new Date,"Error Aggregate",this.collectionName,JSON.stringify([s,t,n],null,2)),n.message="Error in Aggregate: ".concat(this.collectionName," - ").concat(n.message),n;case 5:return[4,r.finish()];case 6:return e.sent(),[7];case 7:return[2]}})})},MongoManagerCollection.prototype.aggregateCount=function(n,s,i){return void 0===i&&(i=!1),__awaiter(this,void 0,void 0,function(){var o,t,r;return __generator(this,function(e){switch(e.label){case 0:!(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())||i||this.bypassSession||(s?s.session=o:s={session:o}),(o=s||{}).session,o=__rest(o,["session"]),t=monitor_manager_1.MonitorMongo.create("aggregateCount",this.collectionName,JSON.stringify([n,o])),e.label=1;case 1:return e.trys.push([1,3,4,6]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).aggregate(n,s).toArray()];case 2:return[2,e.sent().length];case 3:throw r=e.sent(),console.log(new Date,"Error Aggregate Count",this.collectionName,JSON.stringify([n,o,r],null,2)),r.message="Error in Aggregate Count: ".concat(this.collectionName," - ").concat(r.message),r;case 4:return[4,t.finish()];case 5:return e.sent(),[7];case 6:return[2]}})})},MongoManagerCollection.prototype.aggregateCursor=function(e,o,t){var r=this,n=(void 0===t&&(t=!1),resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession()),t=(!n||t||this.bypassSession||(o?o.session=n:o={session:n}),o||{}),n=(t.session,__rest(t,["session"])),s=monitor_manager_1.MonitorMongo.create("aggregateCursor",this.collectionName,JSON.stringify([e,n])),t=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).aggregate(e,o);return t.on("close",function(o){return __awaiter(r,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return[4,s.finish()];case 1:return e.sent(),[2,o]}})})}),t},MongoManagerCollection.prototype.aggregateStream=function(e,o,t,r){var n=this,s=(void 0===r&&(r=!1),resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession()),r=(!s||r||this.bypassSession||(o?o.session=s:o={session:s}),o||{}),s=(r.session,__rest(r,["session"])),i=monitor_manager_1.MonitorMongo.create("aggregateStream",this.collectionName,JSON.stringify([e,s,t]));return resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).aggregate(e,o).stream(t).on("close",function(o){return __awaiter(n,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return[4,i.finish()];case 1:return e.sent(),[2,o]}})})})},MongoManagerCollection.prototype.bulkWrite=function(R,b,C,D,E,x){return void 0===C&&(C=!1),void 0===D&&(D=!1),void 0===E&&(E=!1),void 0===x&&(x=!1),__awaiter(this,void 0,void 0,function(){var o,t,r,n,s,i,a,c,l,_,g,u,v,d,p,h,m,f,O,S,M,y,I,w,N;return __generator(this,function(e){switch(e.label){case 0:if(!R.length)return[2,null];o=1e3,t=0,r=null,!(n=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())||x||this.bypassSession||(b?b.session=n:b={session:n},r={session:n}),e.label=1;case 1:if(!(t<R.length))return[3,24];s=R.slice(t,t+o),i=[],a=[];try{for(w=void 0,c=__values(s),l=c.next();!l.done;l=c.next()){if(_=l.value,g=Object.keys(_)[0],u=_[g],this.checkSchema&&!C){if(v=this.simplschema.newContext(),d=void 0,"insertOne"===g)d=v.validate(u.document);else if("replaceOne"===g)d=v.validate(u.replacement);else if("updateOne"===g||"updateMany"===g)d=v.validate(u.update,{modifier:!0});else{if("deleteOne"!==g&&"deleteMany"!==g)throw new Error("Unsupported operation type: ".concat(g));d=!0}if(!d)throw new Error("Schema validation failed for ".concat(g,": ").concat(JSON.stringify(v.validationErrors())))}if("insertOne"===g)this.timestamps&&(u.document.createdAt||(u.document.createdAt=new Date),u.document.updatedAt||(u.document.updatedAt=new Date)),!D&&this.createLogs&&i.push({insertOne:{document:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:u.document._id||(0,common_1.objectIdHexString)(),payload:(0,common_1.getBinarySize)(JSON.stringify(u.document))<2e5?JSON.stringify(u.document,null,2):"Too Big",method:"insertOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}}),!E&&this.useVersions&&(p=__assign(__assign({},u.document),{__v:0,_id:{_id:u.document._id||(0,common_1.objectIdHexString)(),__v:0}}),a.push({insertOne:{document:p}})),_[g].document=u.document;else if("updateOne"===g||"updateMany"===g)this.timestamps&&(u.update.$set||(u.update.$set={}),u.update.$set.updatedAt=new Date),!E&&this.useVersions&&(u.update.$inc||(u.update.$inc={}),u.update.$inc.__v=1),!D&&this.createLogs&&i.push({insertOne:{document:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:u.filter._id||null,payload:(0,common_1.getBinarySize)(JSON.stringify(u.update))<2e5?JSON.stringify(u.update,null,2):"Too Big",method:g,id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}}),!E&&this.useVersions&&a.push({filter:u.filter});else if("replaceOne"===g)this.timestamps&&(u.replacement.updatedAt=new Date),!E&&this.useVersions&&(u.replacement.__v=(u.replacement.__v||0)+1),!D&&this.createLogs&&i.push({insertOne:{document:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:u.filter._id||null,payload:(0,common_1.getBinarySize)(JSON.stringify(u.replacement))<2e5?JSON.stringify(u.replacement,null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}}),!E&&this.useVersions&&a.push({filter:u.filter}),_[g].replacement=u.replacement;else{if("deleteOne"!==g&&"deleteMany"!==g)throw new Error("Unsupported operation type: ".concat(g));!D&&this.createLogs&&i.push({insertOne:{document:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:u.filter._id||null,payload:(0,common_1.getBinarySize)(JSON.stringify(u.filter))<2e5?JSON.stringify(u.filter,null,2):"Too Big",method:g,id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}}),!E&&this.useVersions&&a.push({filter:u.filter})}}}catch(e){w={error:e}}finally{try{l&&!l.done&&(N=c.return)&&N.call(c)}finally{if(w)throw w.error}}if(h=[],!(!E&&this.useVersions&&0<a.length))return[3,7];e.label=2;case 2:return e.trys.push([2,6,,7]),m=a.map(function(e){return e.filter}),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName).find({$or:m},r).toArray()];case 3:return(h=e.sent(),0<(m=h.map(function(e){return{insertOne:{document:__assign(__assign({},e),{_id:{_id:e._id,__v:e.__v}})}}})).length)?(S=null,S=r?{session:n,ordered:!1}:{ordered:!1},[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.versionCollection).bulkWrite(m,S)]):[3,5];case 4:e.sent(),e.label=5;case 5:return[3,7];case 6:return f=e.sent(),console.error("Error during version creation:",f),[3,7];case 7:(f=b||{}).session,O=__rest(f,["session"]),O=monitor_manager_1.MonitorMongo.create("bulkWrite",this.collectionName,JSON.stringify([s,O])),e.label=8;case 8:return e.trys.push([8,10,11,13]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName).bulkWrite(s,b)];case 9:return e.sent(),[3,13];case 10:throw(M=e.sent()).message="Error in Bulk Write: ".concat(this.collectionName," - ").concat(M.message),M;case 11:return[4,O.finish()];case 12:return e.sent(),[7];case 13:if(!D&&this.createLogs&&0<i.length){if("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL)return[3,14];resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLogs(i.map(function(e){return{type:"log",data:e}}))}return[3,16];case 14:return S=null,S=r?{session:n,ordered:!1}:{ordered:!1},[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").bulkWrite(i,S)];case 15:e.sent(),e.label=16;case 16:if(E||!this.useVersions)return[3,22];e.label=17;case 17:return e.trys.push([17,21,,22]),M=h.map(function(e){return e._id}),y=[{$match:{"_id._id":{$in:M}}},{$sort:{"_id._id":1,"_id.__v":-1}},{$group:{_id:"$_id._id",versions:{$push:"$_id"}}},{$project:{versionsToDelete:{$slice:["$versions",5,{$size:"$versions"}]}}},{$unwind:"$versionsToDelete"},{$replaceRoot:{newRoot:{_id:"$versionsToDelete"}}}],[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.versionCollection).aggregate(y,r).toArray()];case 18:return 0<(y=e.sent()).length?[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.versionCollection).deleteMany({_id:{$in:y.map(function(e){return e._id})}},r)]:[3,20];case 19:e.sent(),e.label=20;case 20:return[3,22];case 21:return I=e.sent(),console.error("Error during version cleanup:",I),[3,22];case 22:return t+=o,[4,new Promise(function(e){return setImmediate(e)})];case 23:return e.sent(),this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[3,1];case 24:return[2,{ok:1}]}})})},MongoManagerCollection.prototype.countDocuments=function(n,s,i,a){return void 0===n&&(n={}),void 0===i&&(i=!1),void 0===a&&(a=!1),__awaiter(this,void 0,void 0,function(){var o,t,r;return __generator(this,function(e){switch(e.label){case 0:return(!(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())||a||this.bypassSession||(s?s.session=o:s={session:o}),(o=s||{}).session,o=__rest(o,["session"]),0===Object.keys(n).length)?[2,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).estimatedDocumentCount(s)]:[3,1];case 1:return i||this.skipCache?[3,2]:[2,resolveio_server_app_1.ResolveIOServer.getMongoManager().countDocuments(this.collectionName,n,s)];case 2:t=monitor_manager_1.MonitorMongo.create("countDocuments",this.collectionName,JSON.stringify([n,o])),e.label=3;case 3:return e.trys.push([3,5,6,8]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).countDocuments(n,s)];case 4:return[2,e.sent()];case 5:throw r=e.sent(),console.log(new Date,"Error Count Documents",this.collectionName,n,o,r),r.message="Error in Count Documents: ".concat(this.collectionName," - ").concat(r.message),r;case 6:return[4,t.finish()];case 7:return e.sent(),[7];case 8:return[2]}})})},MongoManagerCollection.prototype.create=function(e,o){return Array.isArray(e)?1===e.length?this.insertOne(e[0],o):this.insertMany(e,o):this.insertOne(e,o)},MongoManagerCollection.prototype.createIndex=function(t,r){return __awaiter(this,void 0,void 0,function(){var o;return __generator(this,function(e){switch(e.label){case 0:return e.trys.push([0,2,,3]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).indexes()];case 1:return(o=e.sent(),o.some(function(e){return JSON.stringify(e.key)===JSON.stringify(t)}))?[2,"Index already exists"]:(MongoManagerCollection.indexQueue.push({action:"createIndex",collection:this,specs:[t],options:r},function(e,o){if(e)throw e;return o}),[2,"Index creation queued"]);case 2:throw e.sent();case 3:return[2]}})})},MongoManagerCollection.prototype.createIndexes=function(r,n){return __awaiter(this,void 0,void 0,function(){var t,o;return __generator(this,function(e){switch(e.label){case 0:return e.trys.push([0,2,,3]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).indexes()];case 1:return(t=e.sent(),0===(o=r.filter(function(o){return!t.some(function(e){return JSON.stringify(e.key)===JSON.stringify(o.key)})})).length)?[2,["All indexes already exist"]]:(MongoManagerCollection.indexQueue.push({action:"createIndexes",collection:this,specs:[o],options:n},function(e,o){if(e)throw e;return o}),[2,["Index creation queued"]]);case 2:throw e.sent();case 3:return[2]}})})},MongoManagerCollection.prototype.deleteMany=function(_,g,u,v){return void 0===_&&(_={}),void 0===u&&(u=!1),void 0===v&&(v=!1),__awaiter(this,void 0,void 0,function(){var o,t,r,n,s,i,a,c,l;return __generator(this,function(e){switch(e.label){case 0:return(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),t=null,!o||v||this.bypassSession||(g?g.session=o:g={session:o},t={session:o}),(r=g||{}).session,r=__rest(r,["session"]),this.createLogs&&!u||this.useVersions)?[4,this.find(_)]:[3,9];case 1:n=e.sent(),s=0,e.label=2;case 2:if(!(s<n.length))return[3,9];if(i=n[s],this.createLogs&&!u){if("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL)return[3,3];resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:i._id,payload:(0,common_1.getBinarySize)(JSON.stringify([i,_,r]))<2e5?JSON.stringify([i,_,r],null,2):"Too Big",method:"deleteMany",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}})}return[3,5];case 3:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:i._id,payload:(0,common_1.getBinarySize)(JSON.stringify([i,_,r]))<2e5?JSON.stringify([i,_,r],null,2):"Too Big",method:"deleteMany",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},t)];case 4:e.sent(),e.label=5;case 5:return this.useVersions?((c=(0,common_1.deepCopy)(i))._id={_id:i._id,__v:i.__v},l=null,l=t?{session:o,upsert:!0}:{upsert:!0},[4,resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:i._id,__v:i.__v}},c,l)]):[3,8];case 6:return e.sent(),[4,resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({$and:[{"_id._id":i._id},{"_id.__v":{$lt:i.__v-1}}]},t)];case 7:e.sent(),e.label=8;case 8:return s++,[3,2];case 9:a=monitor_manager_1.MonitorMongo.create("deleteMany",this.collectionName,JSON.stringify([_,r])),e.label=10;case 10:return e.trys.push([10,12,13,15]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).deleteMany(_,g)];case 11:if((c=e.sent()).acknowledged)return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,c.deletedCount];throw new Error("Error in DeleteMany: ".concat(this.collectionName));case 12:throw l=e.sent(),console.log(new Date,"Error Delete Many",this.collectionName,_,r,l),l.message="Error in Delete Many: ".concat(this.collectionName," - ").concat(l.message),l;case 13:return[4,a.finish()];case 14:return e.sent(),[7];case 15:return[2]}})})},MongoManagerCollection.prototype.deleteOne=function(l,_,g,u){return void 0===l&&(l={}),void 0===g&&(g=!1),void 0===u&&(u=!1),__awaiter(this,void 0,void 0,function(){var o,t,r,n,s,i,a,c;return __generator(this,function(e){switch(e.label){case 0:if(r=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),o=null,!r||u||this.bypassSession||(_?_.session=r:_={session:r},o={session:r}),t=(r=_||{}).session,r=__rest(r,["session"]),!this.createLogs||g)return[3,15];i=monitor_manager_1.MonitorMongo.create("findOneAndDelete",this.collectionName,JSON.stringify([l,r])),e.label=1;case 1:return e.trys.push([1,11,12,14]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndDelete(l,_)];case 2:if(n=e.sent(),!(n=n.value))return[3,9];if(this.createLogs&&!g){if("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL)return[3,3];resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:n._id,payload:(0,common_1.getBinarySize)(JSON.stringify([n,l,r]))<2e5?JSON.stringify([n,l,r],null,2):"Too Big",method:"deleteOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}})}return[3,5];case 3:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:n._id,payload:(0,common_1.getBinarySize)(JSON.stringify([n,l,r]))<2e5?JSON.stringify([n,l,r],null,2):"Too Big",method:"deleteOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},o)];case 4:e.sent(),e.label=5;case 5:return this.useVersions?((s=(0,common_1.deepCopy)(n))._id={_id:n._id,__v:n.__v},a=null,a=o?{session:t,upsert:!0}:{upsert:!0},[4,resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:n._id,__v:n.__v}},s,a)]):[3,8];case 6:return e.sent(),[4,resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({$and:[{"_id._id":n._id},{"_id.__v":{$lt:n.__v-1}}]},o)];case 7:e.sent(),e.label=8;case 8:return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,1];case 9:return[2,0];case 10:return[3,14];case 11:throw s=e.sent(),console.log(new Date,"Error Delete One (Find One And Delete)",this.collectionName,l,r,s),s.message="Error in Delete One (Find One And Delete): ".concat(this.collectionName," - ").concat(s.message),s;case 12:return[4,i.finish()];case 13:return e.sent(),[7];case 14:return[3,21];case 15:i=monitor_manager_1.MonitorMongo.create("deleteOne",this.collectionName,JSON.stringify([l,r])),e.label=16;case 16:return e.trys.push([16,18,19,21]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).deleteOne(l,_)];case 17:if((a=e.sent()).acknowledged)return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,a.deletedCount];throw new Error("Error in Delete One: ".concat(this.collectionName));case 18:throw c=e.sent(),console.log(new Date,"Error Delete One",this.collectionName,l,r,c),c.message="Error in Delete One: ".concat(this.collectionName," - ").concat(c.message),c;case 19:return[4,i.finish()];case 20:return e.sent(),[7];case 21:return[2]}})})},MongoManagerCollection.prototype.distinct=function(n,s,i,a){return void 0===a&&(a=!1),__awaiter(this,void 0,void 0,function(){var o,t,r;return __generator(this,function(e){switch(e.label){case 0:!(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())||a||this.bypassSession||(i?i.session=o:i={session:o}),(o=i||{}).session,o=__rest(o,["session"]),t=monitor_manager_1.MonitorMongo.create("distinct",this.collectionName,JSON.stringify([n,s,o])),e.label=1;case 1:return e.trys.push([1,3,4,6]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).distinct(n,s,i)];case 2:return[2,e.sent()];case 3:throw r=e.sent(),console.log(new Date,"Error Distinct",this.collectionName,s,o,r),r.message="Error in Distinct: ".concat(this.collectionName," - ").concat(r.message),r;case 4:return[4,t.finish()];case 5:return e.sent(),[7];case 6:return[2]}})})},MongoManagerCollection.prototype.drop=function(n,s){return void 0===s&&(s=!1),__awaiter(this,void 0,void 0,function(){var o,t,r;return __generator(this,function(e){switch(e.label){case 0:!(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())||s||this.bypassSession||(n?n.session=o:n={session:o}),(o=n||{}).session,o=__rest(o,["session"]),t=monitor_manager_1.MonitorMongo.create("drop",this.collectionName,JSON.stringify([o])),e.label=1;case 1:return e.trys.push([1,3,4,6]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).drop(n)];case 2:return[2,e.sent()];case 3:throw r=e.sent(),console.log(new Date,"Error Count Documents",this.collectionName,o,r),r.message="Error in Count Documents: ".concat(this.collectionName," - ").concat(r.message),r;case 4:return[4,t.finish()];case 5:return e.sent(),[7];case 6:return[2]}})})},MongoManagerCollection.prototype.dropIndex=function(n,s,i){return void 0===i&&(i=!1),__awaiter(this,void 0,void 0,function(){var o,t,r;return __generator(this,function(e){switch(e.label){case 0:!(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())||i||this.bypassSession||(s?s.session=o:s={session:o}),(o=s||{}).session,o=__rest(o,["session"]),t=monitor_manager_1.MonitorMongo.create("dropIndex",this.collectionName,JSON.stringify([n,o])),e.label=1;case 1:return e.trys.push([1,3,4,6]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).dropIndex(n,s)];case 2:return[2,e.sent()];case 3:throw r=e.sent(),console.log(new Date,"Error Drop Index",this.collectionName,n,o,r),r.message="Error in Drop Index: ".concat(this.collectionName," - ").concat(n," - ").concat(r.message),r;case 4:return[4,t.finish()];case 5:return e.sent(),[7];case 6:return[2]}})})},MongoManagerCollection.prototype.dropIndexes=function(n,s){return void 0===s&&(s=!1),__awaiter(this,void 0,void 0,function(){var o,t,r;return __generator(this,function(e){switch(e.label){case 0:!(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())||s||this.bypassSession||(n?n.session=o:n={session:o}),(o=n||{}).session,o=__rest(o,["session"]),t=monitor_manager_1.MonitorMongo.create("dropIndexes",this.collectionName,JSON.stringify([o])),e.label=1;case 1:return e.trys.push([1,3,4,6]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).dropIndexes(n)];case 2:return[2,e.sent()];case 3:throw r=e.sent(),console.log(new Date,"Error Drop Indexes",this.collectionName,o,r),r.message="Error in Drop Indexes: ".concat(this.collectionName," - ").concat(r.message),r;case 4:return[4,t.finish()];case 5:return e.sent(),[7];case 6:return[2]}})})},MongoManagerCollection.prototype.find=function(n,s,i,a){return void 0===n&&(n={}),void 0===i&&(i=!1),void 0===a&&(a=!1),__awaiter(this,void 0,void 0,function(){var o,t,r;return __generator(this,function(e){switch(e.label){case 0:return!(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())||a||this.bypassSession||(s?s.session=o:s={session:o}),(o=s||{}).session,o=__rest(o,["session"]),i||this.skipCache?[3,1]:[2,resolveio_server_app_1.ResolveIOServer.getMongoManager().find(this.collectionName,n,s)];case 1:t=monitor_manager_1.MonitorMongo.create("find",this.collectionName,JSON.stringify([n,o])),e.label=2;case 2:return e.trys.push([2,4,5,7]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).find(n,s).toArray()];case 3:return[2,e.sent()];case 4:throw r=e.sent(),console.log(new Date,"Error Find",this.collectionName,n,o,r),r.message="Error in Find: ".concat(this.collectionName," - ").concat(JSON.stringify(n,null,2)," - ").concat(r.message),r;case 5:return[4,t.finish()];case 6:return e.sent(),[7];case 7:return[2]}})})},MongoManagerCollection.prototype.findById=function(n,s,i,a){return void 0===i&&(i=!1),void 0===a&&(a=!1),__awaiter(this,void 0,void 0,function(){var o,t,r;return __generator(this,function(e){switch(e.label){case 0:return!(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())||a||this.bypassSession||(s?s.session=o:s={session:o}),(o=s||{}).session,o=__rest(o,["session"]),i||this.skipCache?[3,1]:[2,resolveio_server_app_1.ResolveIOServer.getMongoManager().findOne(this.collectionName,{_id:n},s)];case 1:t=monitor_manager_1.MonitorMongo.create("findById",this.collectionName,JSON.stringify([{_id:n},o])),e.label=2;case 2:return e.trys.push([2,4,5,7]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOne({_id:n},s)];case 3:return[2,e.sent()];case 4:throw r=e.sent(),console.log(new Date,"Error Find By Id",this.collectionName,{_id:n},o,r),r.message="Error in Find By ID: ".concat(this.collectionName," - ").concat(n," - ").concat(r.message),r;case 5:return[4,t.finish()];case 6:return e.sent(),[7];case 7:return[2]}})})},MongoManagerCollection.prototype.findCount=function(n,s,i){return void 0===n&&(n={}),void 0===i&&(i=!1),__awaiter(this,void 0,void 0,function(){var o,t,r;return __generator(this,function(e){switch(e.label){case 0:!(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())||i||this.bypassSession||(s?s.session=o:s={session:o}),(o=s||{}).session,o=__rest(o,["session"]),t=monitor_manager_1.MonitorMongo.create("findCount",this.collectionName,JSON.stringify([n,o])),e.label=1;case 1:return e.trys.push([1,3,4,6]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).find(n,s).count()];case 2:return[2,e.sent()];case 3:throw r=e.sent(),console.log(new Date,"Error Find Count",this.collectionName,n,o,r),r.message="Error in Find Count: ".concat(this.collectionName," - ").concat(JSON.stringify(n,null,2)," - ").concat(r.message),r;case 4:return[4,t.finish()];case 5:return e.sent(),[7];case 6:return[2]}})})},MongoManagerCollection.prototype.findCursor=function(e,o,t){var r=this,n=(void 0===e&&(e={}),void 0===t&&(t=!1),resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession()),t=(!n||t||this.bypassSession||(o?o.session=n:o={session:n}),o||{}),n=(t.session,__rest(t,["session"])),s=monitor_manager_1.MonitorMongo.create("findCursor",this.collectionName,JSON.stringify([e,n])),i=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).find(e,o);return i.on("close",function(o){return __awaiter(r,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return[4,s.finish()];case 1:return e.sent(),i.removeAllListeners(),[2,o]}})})}),i},MongoManagerCollection.prototype.findStream=function(e,o,t,r){var n=this,s=(void 0===e&&(e={}),void 0===r&&(r=!1),resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession()),r=(!s||r||this.bypassSession||(o?o.session=s:o={session:s}),o||{}),s=(r.session,__rest(r,["session"])),i=monitor_manager_1.MonitorMongo.create("findStream",this.collectionName,JSON.stringify([e,s,t]));return resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).find(e,o).stream(t).on("close",function(o){return __awaiter(n,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return[4,i.finish()];case 1:return e.sent(),[2,o]}})})})},MongoManagerCollection.prototype.findOne=function(s,i,a,c){return void 0===s&&(s={}),void 0===a&&(a=!1),void 0===c&&(c=!1),__awaiter(this,void 0,void 0,function(){var o,t,r,n;return __generator(this,function(e){switch(e.label){case 0:return!(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())||c||this.bypassSession||(i?i.session=o:i={session:o}),a||this.skipCache?[3,1]:[2,resolveio_server_app_1.ResolveIOServer.getMongoManager().findOne(this.collectionName,s,i)];case 1:(o=i||{}).session,t=__rest(o,["session"]),r=monitor_manager_1.MonitorMongo.create("findOne",this.collectionName,JSON.stringify([s,t])),e.label=2;case 2:return e.trys.push([2,4,5,7]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOne(s,i)];case 3:return[2,e.sent()];case 4:throw n=e.sent(),console.log(new Date,"Error Find One",this.collectionName,s,t,n),n.message="Error in Find One: ".concat(this.collectionName," - ").concat(JSON.stringify(s,null,2)," - ").concat(n.message),n;case 5:return[4,r.finish()];case 6:return e.sent(),[7];case 7:return[2]}})})},MongoManagerCollection.prototype.findOneAndDelete=function(c,l,_,g){return void 0===c&&(c={}),void 0===_&&(_=!1),void 0===g&&(g=!1),__awaiter(this,void 0,void 0,function(){var o,t,r,n,s,i,a;return __generator(this,function(e){switch(e.label){case 0:r=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),o=null,!r||g||this.bypassSession||(l?l.session=r:l={session:r},o={session:r}),t=(r=l||{}).session,r=__rest(r,["session"]),n=monitor_manager_1.MonitorMongo.create("findOneAndDelete",this.collectionName,JSON.stringify([c,r])),e.label=1;case 1:return e.trys.push([1,10,11,13]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndDelete(c,l)];case 2:if(s=e.sent(),!(s=s.value))return[3,8];if(this.createLogs&&!_){if("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL)return[3,3];resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:s._id,payload:(0,common_1.getBinarySize)(JSON.stringify([s,c,r]))<2e5?JSON.stringify([s,c,r],null,2):"Too Big",method:"findOneAndDelete",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}})}return[3,5];case 3:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:s._id,payload:(0,common_1.getBinarySize)(JSON.stringify([s,c,r]))<2e5?JSON.stringify([s,c,r],null,2):"Too Big",method:"findOneAndDelete",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},o)];case 4:e.sent(),e.label=5;case 5:return this.useVersions?((a=(0,common_1.deepCopy)(s))._id={_id:s._id,__v:s.__v},i=null,i=o?{session:t,upsert:!0}:{upsert:!0},[4,resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:s._id,__v:s.__v}},a,i)]):[3,7];case 6:e.sent(),e.label=7;case 7:return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,s];case 8:return[2,null];case 9:return[3,13];case 10:throw a=e.sent(),console.log(new Date,"Error Find One And Delete",this.collectionName,c,r,a),a.message="Error in Find One And Delete: ".concat(this.collectionName," - ").concat(JSON.stringify(c,null,2)," - ").concat(a.message),a;case 11:return[4,n.finish()];case 12:return e.sent(),[7];case 13:return[2]}})})},MongoManagerCollection.prototype.findOneAndReplace=function(l,_,g,u,v,d){return void 0===l&&(l={}),void 0===u&&(u=!1),void 0===v&&(v=!1),void 0===d&&(d=!1),__awaiter(this,void 0,void 0,function(){var o,t,r,n,s,i,a,c;return __generator(this,function(e){switch(e.label){case 0:if(this.checkSchema&&!v&&(o=this.simplschema.newContext(),!o.validate(_)))throw console.log(new Date,this.collectionName,"Schema Errors - findOneAndReplace",o.validationErrors()),resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethod.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on findOneAndReplace - "+this.collectionName,[o.validationErrors(),_]),o.validationErrors();o=new Date,this.timestamps&&(_.updatedAt=o),g&&g.upsert&&(_._id||(_._id=(0,common_1.objectIdHexString)()),this.useVersions&&(_.__v=0),this.timestamps)&&!_.createdAt&&(_.createdAt=o),n=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),t=null,!n||d||this.bypassSession||(g?g.session=n:g={session:n},t={session:n}),r=(n=g||{}).session,n=__rest(n,["session"]),s=monitor_manager_1.MonitorMongo.create("findOneAndReplace",this.collectionName,JSON.stringify([l,_,n])),e.label=1;case 1:return e.trys.push([1,10,11,13]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndReplace(l,_,g)];case 2:if(i=e.sent(),!(i=i.value))return[3,8];if(this.createLogs&&!u){if("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL)return[3,3];resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:i._id,payload:(0,common_1.getBinarySize)(JSON.stringify([i,l,_,n]))<2e5?JSON.stringify([i,l,_,n],null,2):"Too Big",method:"findOneAndReplace",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}})}return[3,5];case 3:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:i._id,payload:(0,common_1.getBinarySize)(JSON.stringify([i,l,_,n]))<2e5?JSON.stringify([i,l,_,n],null,2):"Too Big",method:"findOneAndReplace",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},t)];case 4:e.sent(),e.label=5;case 5:return this.useVersions?((c=(0,common_1.deepCopy)(i))._id={_id:i._id,__v:i.__v},a=null,a=t?{session:r,upsert:!0}:{upsert:!0},[4,resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:i._id,__v:i.__v}},c,a)]):[3,7];case 6:e.sent(),e.label=7;case 7:return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,i];case 8:return[2,null];case 9:return[3,13];case 10:throw c=e.sent(),console.log(new Date,"Error Find One And Replace",this.collectionName,l,n,c),c.message="Error in Find One And Replace: ".concat(this.collectionName," - ").concat(JSON.stringify(l,null,2)," - ").concat(c.message),c;case 11:return[4,s.finish()];case 12:return e.sent(),[7];case 13:return[2]}})})},MongoManagerCollection.prototype.findOneAndUpdate=function(l,_,g,u,v,d){return void 0===l&&(l={}),void 0===u&&(u=!1),void 0===v&&(v=!1),void 0===d&&(d=!1),__awaiter(this,void 0,void 0,function(){var o,t,r,n,s,i,a,c;return __generator(this,function(e){switch(e.label){case 0:if(this.checkSchema&&!v&&(o=this.simplschema.newContext(),!o.validate(_,{modifier:!0})))throw console.log(new Date,this.collectionName,"Schema Errors - findOneAndUpdate",o.validationErrors()),resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethod.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on findOneAndUpdate - "+this.collectionName,[o.validationErrors(),_]),o.validationErrors();this.timestamps&&(o=new Date,_.$set?_.$set.updatedAt=o:_.$set={updatedAt:o}),g&&g.upsert&&(_.$setOnInsert?(_.$setOnInsert._id||(_.$setOnInsert._id=(0,common_1.objectIdHexString)()),this.timestamps&&!_.$setOnInsert.createdAt&&(_.$setOnInsert.createdAt=new Date)):this.timestamps?_.$setOnInsert={_id:(0,common_1.objectIdHexString)(),createdAt:new Date}:_.$setOnInsert={_id:(0,common_1.objectIdHexString)()}),n=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),t=null,!n||d||this.bypassSession||(g?g.session=n:g={session:n},t={session:n}),r=(n=g||{}).session,n=__rest(n,["session"]),s=monitor_manager_1.MonitorMongo.create("findOneAndUpdate",this.collectionName,JSON.stringify([l,_,n])),e.label=1;case 1:return e.trys.push([1,10,11,13]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndUpdate(l,_,g)];case 2:if(i=e.sent(),!(i=i.value))return[3,8];if(this.createLogs&&!u){if("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL)return[3,3];resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:i._id,payload:(0,common_1.getBinarySize)(JSON.stringify([i,l,_,n]))<2e5?JSON.stringify([i,l,_,n],null,2):"Too Big",method:"findOneAndUpdate",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}})}return[3,5];case 3:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:i._id,payload:(0,common_1.getBinarySize)(JSON.stringify([i,l,_,n]))<2e5?JSON.stringify([i,l,_,n],null,2):"Too Big",method:"findOneAndUpdate",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},t)];case 4:e.sent(),e.label=5;case 5:return this.useVersions?((c=(0,common_1.deepCopy)(i))._id={_id:i._id,__v:i.__v},a=null,a=t?{session:r,upsert:!0}:{upsert:!0},[4,resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:i._id,__v:i.__v}},c,a)]):[3,7];case 6:e.sent(),e.label=7;case 7:return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,i];case 8:return[2,null];case 9:return[3,13];case 10:throw c=e.sent(),console.log(new Date,"Error Find One And Update",this.collectionName,l,n,c),c.message="Error in Find One And Update: ".concat(this.collectionName," - ").concat(JSON.stringify(l,null,2)," - ").concat(c.message),c;case 11:return[4,s.finish()];case 12:return e.sent(),[7];case 13:return[2]}})})},MongoManagerCollection.prototype.indexes=function(n,s){return void 0===s&&(s=!1),__awaiter(this,void 0,void 0,function(){var o,t,r;return __generator(this,function(e){switch(e.label){case 0:!(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())||s||this.bypassSession||(n?n.session=o:n={session:o}),(o=n||{}).session,o=__rest(o,["session"]),t=monitor_manager_1.MonitorMongo.create("indexes",this.collectionName,JSON.stringify([o])),e.label=1;case 1:return e.trys.push([1,3,4,6]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).indexes(n)];case 2:return[2,e.sent()];case 3:throw r=e.sent(),console.log(new Date,"Error Indexes",this.collectionName,o,r),r.message="Error in Indexes: ".concat(this.collectionName," - ").concat(r.message),r;case 4:return[4,t.finish()];case 5:return e.sent(),[7];case 6:return[2]}})})},MongoManagerCollection.prototype.indexExists=function(n,s,i){return void 0===i&&(i=!1),__awaiter(this,void 0,void 0,function(){var o,t,r;return __generator(this,function(e){switch(e.label){case 0:!(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())||i||this.bypassSession||(s?s.session=o:s={session:o}),(o=s||{}).session,o=__rest(o,["session"]),t=monitor_manager_1.MonitorMongo.create("indexExists",this.collectionName,JSON.stringify([n,o])),e.label=1;case 1:return e.trys.push([1,3,4,6]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).indexExists(n,s)];case 2:return[2,e.sent()];case 3:throw r=e.sent(),console.log(new Date,"Error Index Exists",this.collectionName,n,o,r),r.message="Error in Index Exists: ".concat(this.collectionName," - ").concat(JSON.stringify(n,null,2)," - ").concat(r.message),r;case 4:return[4,t.finish()];case 5:return e.sent(),[7];case 6:return[2]}})})},MongoManagerCollection.prototype.insertMany=function(g,u,v,d,p,h){return void 0===v&&(v=!1),void 0===d&&(d=!1),void 0===p&&(p=!1),void 0===h&&(h=!1),__awaiter(this,void 0,void 0,function(){var t,o,r,n,s,i,a,c,l,_=this;return __generator(this,function(e){switch(e.label){case 0:if(!g.length)return[2,[]];t=[],this.checkSchema&&!d&&g.forEach(function(e){var o=_.simplschema.newContext();o.validate(e)?t.push(!0):(console.log(new Date,_.collectionName,"Schema Errors - insertMany",o.validationErrors()),resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethod.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on insertMany - "+_.collectionName,[o.validationErrors(),e]),t.push(!1))}),o=this.checkSchema?g.filter(function(e,o){return t[o]}):g,n=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),r=null,!n||h||this.bypassSession||(u?u.session=n:u={session:n},r={session:n}),(n=u||{}).session,n=__rest(n,["session"]),s=0,e.label=1;case 1:if(!(s<o.length))return[3,5];if((i=o[s])._id||(i._id=(0,common_1.objectIdHexString)()),this.timestamps&&(c=new Date,i.createdAt||(i.createdAt=c),i.updatedAt||(i.updatedAt=c)),this.useVersions&&!i.hasOwnProperty("__v")&&(i.__v=0),this.createLogs&&!v){if("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL)return[3,2];resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:i._id,payload:(0,common_1.getBinarySize)(JSON.stringify([i,n]))<2e5?JSON.stringify([i,n],null,2):"Too Big",method:"insertMany",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}})}return[3,4];case 2:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:i._id,payload:(0,common_1.getBinarySize)(JSON.stringify([i,n]))<2e5?JSON.stringify([i,n],null,2):"Too Big",method:"insertMany",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},r)];case 3:e.sent(),e.label=4;case 4:return s++,[3,1];case 5:if(!o.length)return[3,10];a=null,p||(a=monitor_manager_1.MonitorMongo.create("insertMany",this.collectionName,JSON.stringify([o,n]))),e.label=6;case 6:return e.trys.push([6,8,9,10]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).insertMany(o,u)];case 7:if((c=e.sent()).acknowledged)return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,o];throw new Error("Error in Insert Many: ".concat(this.collectionName," - ").concat(JSON.stringify(c,null,2)));case 8:throw l=e.sent(),console.log(new Date,"Error Insert Many",this.collectionName,o,n,l),l.message="Error in Insert Many: ".concat(this.collectionName," - ").concat(l.message),l;case 9:return a&&a.finish(),[7];case 10:return[2,[]]}})})},MongoManagerCollection.prototype.insertOne=function(a,c,l,_,g){return void 0===l&&(l=!1),void 0===_&&(_=!1),void 0===g&&(g=!1),__awaiter(this,void 0,void 0,function(){var o,t,r,n,s,i;return __generator(this,function(e){switch(e.label){case 0:if(a._id||(a._id=(0,common_1.objectIdHexString)()),this.checkSchema&&!_&&(o=this.simplschema.newContext(),!o.validate(a)))throw console.log(new Date,this.collectionName,"Schema Errors - insertOne",o.validationErrors()),resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethod.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on insertOne - "+this.collectionName,[o.validationErrors(),a]),o.validationErrors();if(this.timestamps&&(t=new Date,a.createdAt||(a.createdAt=t),a.updatedAt||(a.updatedAt=t)),t=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),r=null,!t||g||this.bypassSession||(c?c.session=t:c={session:t},r={session:t}),(n=c||{}).session,n=__rest(n,["session"]),this.createLogs&&!l){if("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL)return[3,1];resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:a._id,payload:(0,common_1.getBinarySize)(JSON.stringify([a,n]))<2e5?JSON.stringify([a,n],null,2):"Too Big",method:"insertOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}})}return[3,3];case 1:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:a._id,payload:(0,common_1.getBinarySize)(JSON.stringify([a,n]))<2e5?JSON.stringify([a,n],null,2):"Too Big",method:"insertOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},r)];case 2:e.sent(),e.label=3;case 3:this.useVersions&&!a.hasOwnProperty("__v")&&(a.__v=0),s=monitor_manager_1.MonitorMongo.create("insertOne",this.collectionName,JSON.stringify([a,n])),e.label=4;case 4:return e.trys.push([4,6,7,9]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).insertOne(a,c)];case 5:if((i=e.sent()).acknowledged)return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,a];throw new Error("Error in Insert One: ".concat(this.collectionName," - ").concat(JSON.stringify(i,null,2)));case 6:throw i=e.sent(),console.log(new Date,"Error Insert One",this.collectionName,a,n,i),i.message="Error in Insert One: ".concat(this.collectionName," - ").concat(i.message),i;case 7:return[4,s.finish()];case 8:return e.sent(),[7];case 9:return[2]}})})},MongoManagerCollection.prototype.listIndexes=function(e,o){var t=this,r=(void 0===o&&(o=!1),resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession()),o=(!r||o||this.bypassSession||(e?e.session=r:e={session:r}),e||{}),r=(o.session,__rest(o,["session"])),n=monitor_manager_1.MonitorMongo.create("listIndexes",this.collectionName,JSON.stringify([r])),o=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).listIndexes(e);return o.on("close",function(o){return __awaiter(t,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return[4,n.finish()];case 1:return e.sent(),[2,o]}})})}),o},MongoManagerCollection.prototype.rename=function(n,s,i){return void 0===i&&(i=!1),__awaiter(this,void 0,void 0,function(){var o,t,r;return __generator(this,function(e){switch(e.label){case 0:!(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())||i||this.bypassSession||(s?s.session=o:s={session:o}),(o=s||{}).session,o=__rest(o,["session"]),t=monitor_manager_1.MonitorMongo.create("rename",this.collectionName,JSON.stringify([n,o])),e.label=1;case 1:return e.trys.push([1,3,4,6]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).rename(n,s)];case 2:return[2,e.sent()];case 3:throw r=e.sent(),console.log(new Date,"Error Rename",this.collectionName,n,o,r),r.message="Error in Rename: ".concat(this.collectionName," - ").concat(n," - ").concat(r.message),r;case 4:return[4,t.finish()];case 5:return e.sent(),[7];case 6:return[2]}})})},MongoManagerCollection.prototype.replaceOne=function(m,f,O,S,M,y,I){return void 0===S&&(S=!1),void 0===M&&(M=!1),void 0===y&&(y=null),void 0===I&&(I=!1),__awaiter(this,void 0,void 0,function(){var o,t,r,n,s,i,a,c,l,_,g,u,v,d,p,h;return __generator(this,function(e){switch(e.label){case 0:if(!this.checkSchema||M||(o=this.simplschema.newContext(),o.validate(f)))return t=new Date,this.timestamps&&(f.updatedAt=t),s=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),r=null,!s||I||this.bypassSession||(O?O.session=s:O={session:s},r={session:s}),n=(s=O||{}).session,s=__rest(s,["session"]),this.useVersions?y?[3,2]:[4,this.findOne(m,null,!0)]:[3,40];throw console.log(new Date,this.collectionName,"Schema Errors - replaceOne",o.validationErrors()),resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethod.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on replaceOne - "+this.collectionName,[o.validationErrors(),f]),o.validationErrors();case 1:y=e.sent(),e.label=2;case 2:return y?(this.timestamps&&!f.createdAt&&y.createdAt&&(f.createdAt=y.createdAt),y.__v!==f.__v?[3,15]:(f.__v+=1,(i=(0,common_1.deepCopy)(y))._id={_id:y._id,__v:y.__v},a=null,a=r?{session:n,upsert:!0}:{upsert:!0},[4,resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:y._id,__v:y.__v}},i,a)])):[3,28];case 3:return(e.sent(),4<=y.__v)?[4,resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({$and:[{"_id._id":y._id},{"_id.__v":{$lt:y.__v-4}}]},r)]:[3,5];case 4:e.sent(),e.label=5;case 5:if(this.createLogs&&!S){if("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL)return[3,6];resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:y._id,payload:(0,common_1.getBinarySize)(JSON.stringify([y,m,f,s]))<2e5?JSON.stringify([y,m,f,s],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}})}return[3,8];case 6:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:y._id,payload:(0,common_1.getBinarySize)(JSON.stringify([y,m,f,s]))<2e5?JSON.stringify([y,m,f,s],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},r)];case 7:e.sent(),e.label=8;case 8:d=monitor_manager_1.MonitorMongo.create("replaceOne",this.collectionName,JSON.stringify([m,f,s])),e.label=9;case 9:return e.trys.push([9,11,12,14]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).replaceOne(m,f,O)];case 10:if((p=e.sent()).acknowledged)return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,p.modifiedCount];throw new Error("Error in Replace One: ".concat(this.collectionName," - ").concat(JSON.stringify(p,null,2)));case 11:throw i=e.sent(),console.log(new Date,"Error Replace One",this.collectionName,m,f,s,i),i.message="Error in Replace One: ".concat(this.collectionName," - ").concat(i.message),i;case 12:return[4,d.finish()];case 13:return e.sent(),[7];case 14:return[3,27];case 15:return console.log("invalid version - "+this.collectionName,y.__v,f.__v),[4,resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).findOne({_id:{_id:y._id,__v:f.__v}},null,!0)];case 16:if(!(a=e.sent()))return[3,26];if(c=y._id,_=y.__v,(l=(0,common_1.getMongoMergeUpdatedDoc)(f,y,a))._id=c,l.__v=_+1,this.createLogs&&!S){if("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL)return[3,17];resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:c,payload:(0,common_1.getBinarySize)(JSON.stringify(["invalidVersion - merge",y,m,l,s]))<2e5?JSON.stringify(["invalidVersion - merge",y,m,l,s],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}})}return[3,19];case 17:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:c,payload:(0,common_1.getBinarySize)(JSON.stringify(["invalidVersion - merge",y,m,l,s]))<2e5?JSON.stringify(["invalidVersion - merge",y,m,l,s],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},r)];case 18:e.sent(),e.label=19;case 19:d=monitor_manager_1.MonitorMongo.create("replaceOne",this.collectionName,JSON.stringify([m,l,s])),e.label=20;case 20:return e.trys.push([20,22,23,25]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).replaceOne(m,l,O)];case 21:if((p=e.sent()).acknowledged)return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,p.modifiedCount];throw new Error("Error in Replace One: ".concat(this.collectionName," - ").concat(JSON.stringify(p,null,2)));case 22:throw _=e.sent(),console.log(new Date,"Error Replace One",this.collectionName,m,l,s,_),_.message="Error in Replace One: ".concat(this.collectionName," - ").concat(_.message),_;case 23:return[4,d.finish()];case 24:return e.sent(),[7];case 25:return[3,27];case 26:throw new Error("Error in Replcae One: Invalid Version And Could Not Find History - DB: "+y.__v+", Trying to update with :"+f.__v);case 27:return[3,39];case 28:if(!O||!O.upsert)return[3,38];if(this.timestamps&&(f.createdAt=t),f._id||(f._id=(0,common_1.objectIdHexString)()),f.__v=0,this.createLogs&&!S){if("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL)return[3,29];resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:f._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",m,f,s]))<2e5?JSON.stringify(["upsert",m,f,s],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}})}return[3,31];case 29:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:f._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",m,f,s]))<2e5?JSON.stringify(["upsert",m,f,s],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},r)];case 30:e.sent(),e.label=31;case 31:d=monitor_manager_1.MonitorMongo.create("replaceOne",this.collectionName,JSON.stringify([m,f,s])),e.label=32;case 32:return e.trys.push([32,34,35,37]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).replaceOne(m,f,O)];case 33:if((p=e.sent()).acknowledged)return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,p.modifiedCount];throw new Error("Error in Replace One: ".concat(this.collectionName," - ").concat(JSON.stringify(p,null,2)));case 34:throw g=e.sent(),console.log(new Date,"Error Replace One",this.collectionName,m,f,s,g),g.message="Error in Replace One: ".concat(this.collectionName," - ").concat(g.message),g;case 35:return[4,d.finish()];case 36:return e.sent(),[7];case 37:return[3,39];case 38:throw new Error("Error in Replace One: No Document");case 39:return[3,60];case 40:if(O&&O.upsert&&(this.timestamps&&!f.createdAt&&(f.createdAt=t),f._id||(f._id=(0,common_1.objectIdHexString)()),f.__v=0),!this.createLogs||S)return[3,54];O?O.returnDocument="before":O={returnDocument:"before"},(g=O||{}).session,u=__rest(g,["session"]),d=monitor_manager_1.MonitorMongo.create("findOneAndReplace",this.collectionName,JSON.stringify([m,f,u])),e.label=41;case 41:return e.trys.push([41,50,51,53]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndReplace(m,f,O)];case 42:return(p=e.sent(),v=p.value)?"https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?[3,43]:(resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:v._id,payload:(0,common_1.getBinarySize)(JSON.stringify([v,m,f,u]))<2e5?JSON.stringify([v,m,f,u],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}),[3,45]):[3,46];case 43:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:v._id,payload:(0,common_1.getBinarySize)(JSON.stringify([v,m,f,u]))<2e5?JSON.stringify([v,m,f,u],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},r)];case 44:e.sent(),e.label=45;case 45:return[3,49];case 46:return"https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?[3,47]:(resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:f._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",m,f,u]))<2e5?JSON.stringify(["upsert",m,f,u],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}),[3,49]);case 47:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:f._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",m,f,u]))<2e5?JSON.stringify(["upsert",m,f,u],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},r)];case 48:e.sent(),e.label=49;case 49:return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,1];case 50:throw h=e.sent(),console.log(new Date,"Error Replace One (Find One And Replace)",this.collectionName,m,f,u,h),h.message="Error in Replace One (Find One And Replace): ".concat(this.collectionName," - ").concat(h.message),h;case 51:return[4,d.finish()];case 52:return e.sent(),[7];case 53:return[3,60];case 54:d=monitor_manager_1.MonitorMongo.create("replaceOne",this.collectionName,JSON.stringify([m,f,s])),e.label=55;case 55:return e.trys.push([55,57,58,60]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).replaceOne(m,f,O)];case 56:return(p=e.sent()).acknowledged?(this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,p.modifiedCount]):[2,p.acknowledged];case 57:throw h=e.sent(),console.log(new Date,"Error Replace One",this.collectionName,m,f,s,h),h.message="Error in Aggregate: ".concat(this.collectionName," - ").concat(h.message),h;case 58:return[4,d.finish()];case 59:return e.sent(),[7];case 60:return[2]}})})},MongoManagerCollection.prototype.updateMany=function(u,v,d,p,h,m,f){return void 0===p&&(p=!1),void 0===h&&(h=!1),void 0===m&&(m=!1),void 0===f&&(f=!1),__awaiter(this,void 0,void 0,function(){var o,t,r,n,s,i,a,c,l,_,g;return __generator(this,function(e){switch(e.label){case 0:if(!(v.$inc&&"{}"!==JSON.stringify(v.$inc)||v.$set&&"{}"!==JSON.stringify(v.$set)||v.$setOnInsert&&"{}"!==JSON.stringify(v.$setOnInsert)||v.$unset&&"{}"!==JSON.stringify(v.$unset)))return[2,1];if(this.timestamps&&(t=new Date,v.$set?v.$set.updatedAt=t:v.$set={updatedAt:t}),!this.checkSchema||h||(o=this.simplschema.newContext(),o.validate(v,{modifier:!0})))return t=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),r=null,!t||f||this.bypassSession||(d?d.session=t:d={session:t},r={session:t}),n=(s=d||{}).session,s=__rest(s,["session"]),this.useVersions&&!m||this.createLogs&&!p?[4,this.find(u)]:[3,10];throw console.log(new Date,this.collectionName,"Schema Errors - updateMany",o.validationErrors()),resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethod.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on updateMany - "+this.collectionName,[o.validationErrors(),v]),o.validationErrors();case 1:i=e.sent(),a=0,e.label=2;case 2:if(!(a<i.length))return[3,10];if(c=i[a],this.createLogs&&!p){if("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL)return[3,3];resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:c._id,payload:(0,common_1.getBinarySize)(JSON.stringify([c,u,v,s]))<2e5?JSON.stringify([c,u,v,s],null,2):"Too Big",method:"updateMany",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}})}return[3,5];case 3:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:c._id,payload:(0,common_1.getBinarySize)(JSON.stringify([c,u,v,s]))<2e5?JSON.stringify([c,u,v,s],null,2):"Too Big",method:"updateMany",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},r)];case 4:e.sent(),e.label=5;case 5:return this.useVersions?((_=(0,common_1.deepCopy)(c))._id={_id:c._id,__v:c.__v},g=null,g=r?{session:n,upsert:!0}:{upsert:!0},[4,resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:c._id,__v:c.__v}},_,g)]):[3,9];case 6:return(e.sent(),4<=c.__v)?[4,resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({$and:[{"_id._id":c._id},{"_id.__v":{$lt:c.__v-4}}]},r)]:[3,8];case 7:e.sent(),e.label=8;case 8:objectContainsPropertyDeep(v,"__v")||(v.$inc?v.$inc.__v||(v.$inc.__v=1):v.$inc={__v:1}),e.label=9;case 9:return a++,[3,2];case 10:l=monitor_manager_1.MonitorMongo.create("updateMany",this.collectionName,JSON.stringify([u,v,s])),e.label=11;case 11:return e.trys.push([11,13,14,16]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).updateMany(u,v,d)];case 12:if((_=e.sent()).acknowledged)return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,_.modifiedCount];throw new Error("Error in updateMany: ".concat(_));case 13:throw g=e.sent(),console.log(new Date,"Error Update Many",this.collectionName,u,v,s,g),g.message="Error in Update Many: ".concat(this.collectionName," - ").concat(g.message),g;case 14:return[4,l.finish()];case 15:return e.sent(),[7];case 16:return[2]}})})},MongoManagerCollection.prototype.updateOne=function(g,u,v,d,p,h){return void 0===d&&(d=!1),void 0===p&&(p=!1),void 0===h&&(h=!1),__awaiter(this,void 0,void 0,function(){var o,t,r,n,s,i,a,c,l,_;return __generator(this,function(e){switch(e.label){case 0:if(!this.checkSchema||p||(o=this.simplschema.newContext(),o.validate(u,{modifier:!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)?(n=new Date,this.timestamps&&(u.$set?u.$set.updatedAt=n:u.$set={updatedAt:n}),n=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),t=null,!n||h||this.bypassSession||(v?v.session=n:v={session:n},t={session:n}),r=(n=v||{}).session,n=__rest(n,["session"]),this.useVersions?[4,this.findOne(g,null,!0)]:[3,26]):[2,1];throw console.log(new Date,this.collectionName,"Schema Errors - updateOne",o.validationErrors()),resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethod.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on updateOne - "+this.collectionName,[o.validationErrors(),u]),o.validationErrors();case 1:if(!(a=e.sent()))return[3,14];if(this.createLogs&&!d){if("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL)return[3,2];resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:a._id,payload:(0,common_1.getBinarySize)(JSON.stringify([a,g,u,n]))<2e5?JSON.stringify([a,g,u,n],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}})}return[3,4];case 2:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:a._id,payload:(0,common_1.getBinarySize)(JSON.stringify([a,g,u,n]))<2e5?JSON.stringify([a,g,u,n],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},t)];case 3:e.sent(),e.label=4;case 4:return(s=(0,common_1.deepCopy)(a))._id={_id:a._id,__v:a.__v},i=null,i=t?{session:r,upsert:!0}:{upsert:!0},[4,resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:a._id,__v:a.__v}},s,i)];case 5:return(e.sent(),4<=a.__v)?[4,resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({$and:[{"_id._id":a._id},{"_id.__v":{$lt:a.__v-4}}]},t)]:[3,7];case 6:e.sent(),e.label=7;case 7:objectContainsPropertyDeep(u,"__v")||(u.$inc?u.$inc.__v||(u.$inc.__v=1):u.$inc={__v:1}),c=monitor_manager_1.MonitorMongo.create("updateOne",this.collectionName,JSON.stringify([g,u,n])),e.label=8;case 8:return e.trys.push([8,10,11,13]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).updateOne(g,u,v)];case 9:if((l=e.sent()).acknowledged)return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,l.modifiedCount];throw new Error("Error in Update One: ".concat(this.collectionName," - ").concat(JSON.stringify(l,null,2)));case 10:throw s=e.sent(),console.log(new Date,"Error Update One",this.collectionName,g,u,n,s),s.message="Error in Update One: ".concat(this.collectionName," - ").concat(s.message),s;case 11:return[4,c.finish()];case 12:return e.sent(),[7];case 13:return[3,25];case 14:if(!v||!v.upsert)return[3,24];if(u.$setOnInsert?(u.$setOnInsert._id||(u.$setOnInsert._id=(0,common_1.objectIdHexString)()),this.timestamps&&!u.$setOnInsert.createdAt&&(u.$setOnInsert.createdAt=new Date)):this.timestamps?u.$setOnInsert={_id:(0,common_1.objectIdHexString)(),__v:0,createdAt:new Date}:u.$setOnInsert={_id:(0,common_1.objectIdHexString)(),__v:0},this.createLogs&&!d){if("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL)return[3,15];resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:u.$setOnInsert._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",g,u,n]))<2e5?JSON.stringify(["upsert",g,u,n],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}})}return[3,17];case 15:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:u.$setOnInsert._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",g,u,n]))<2e5?JSON.stringify(["upsert",g,u,n],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},t)];case 16:e.sent(),e.label=17;case 17:c=monitor_manager_1.MonitorMongo.create("updateOne",this.collectionName,JSON.stringify([g,u,n])),e.label=18;case 18:return e.trys.push([18,20,21,23]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).updateOne(g,u,v)];case 19:if((l=e.sent()).acknowledged)return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,l.modifiedCount];throw new Error("Error in Update One: ".concat(this.collectionName," - ").concat(JSON.stringify(l,null,2)));case 20:throw i=e.sent(),console.log(new Date,"Error Update One",this.collectionName,g,u,n,i),i.message="Error in Update One: ".concat(this.collectionName," - ").concat(i.message),i;case 21:return[4,c.finish()];case 22:return e.sent(),[7];case 23:return[3,25];case 24:throw new Error("Error in Update One: No Document");case 25:return[3,48];case 26:if(v&&v.upsert&&(u.$setOnInsert?(u.$setOnInsert._id||(u.$setOnInsert._id=(0,common_1.objectIdHexString)()),this.timestamps&&!u.$setOnInsert.createdAt&&(u.$setOnInsert.createdAt=new Date)):this.timestamps?u.$setOnInsert={_id:(0,common_1.objectIdHexString)(),createdAt:new Date}:u.$setOnInsert={_id:(0,common_1.objectIdHexString)()}),!this.createLogs||d)return[3,42];v?v.returnDocument="before":v={returnDocument:"before"},c=monitor_manager_1.MonitorMongo.create("findOneAndUpdate",this.collectionName,JSON.stringify([g,u,n])),e.label=27;case 27:return e.trys.push([27,38,39,41]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndUpdate(g,u,v)];case 28:return(l=e.sent(),a=l.value)?"https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?[3,29]:(resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:a._id,payload:(0,common_1.getBinarySize)(JSON.stringify([a,g,u,n]))<2e5?JSON.stringify([a,g,u,n],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}),[3,31]):[3,32];case 29:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:a._id,payload:(0,common_1.getBinarySize)(JSON.stringify([a,g,u,n]))<2e5?JSON.stringify([a,g,u,n],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},t)];case 30:e.sent(),e.label=31;case 31:return[2,l.ok];case 32:return u.$setOnInsert?"https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?[3,33]:(resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:u.$setOnInsert._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",g,u,n]))<2e5?JSON.stringify(["upsert",g,u,n],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}),[3,35]):[3,36];case 33:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:u.$setOnInsert._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",g,u,n]))<2e5?JSON.stringify(["upsert",g,u,n],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date})];case 34:e.sent(),e.label=35;case 35:return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,l.ok];case 36:return[2,0];case 37:return[3,41];case 38:throw _=e.sent(),console.log(new Date,"Error Update One (Find One And Update)",this.collectionName,g,u,n,_),_.message="Error in Update One (Find One And Update): ".concat(this.collectionName," - ").concat(_.message),_;case 39:return[4,c.finish()];case 40:return e.sent(),[7];case 41:return[3,48];case 42:c=monitor_manager_1.MonitorMongo.create("updateOne",this.collectionName,JSON.stringify([g,u,n])),e.label=43;case 43:return e.trys.push([43,45,46,48]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).updateOne(g,u,v)];case 44:if((l=e.sent()).acknowledged)return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,l.modifiedCount];throw new Error("Error in Aggregate: ".concat(this.collectionName," - ").concat(JSON.stringify(l,null,2)));case 45:throw _=e.sent(),console.log(new Date,"Error Update One",this.collectionName,g,u,n,_),_.message="Error in Aggregate: ".concat(this.collectionName," - ").concat(_.message),_;case 46:return[4,c.finish()];case 47:return e.sent(),[7];case 48:return[2]}})})},MongoManagerCollection.prototype.watchCollection=function(e,o,t){void 0===e&&(e=[]),void 0===t&&(t=!1);var r=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession();return!r||t||this.bypassSession||(o?o.session=r:o={session:r}),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).watch(e,o)},(e=MongoManagerCollection).indexQueue=async_1.default.queue(function(l,_){return __awaiter(void 0,void 0,void 0,function(){var o,t,r,n,s,i,a,c;return __generator(e,function(e){switch(e.label){case 0:if(o=l.action,t=l.collection,r=l.specs,n=l.options,!(s=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(t.collectionName,t.collectionOptions)))return[2,_(new Error("Collection ".concat(t.collectionName," not found")))];e.label=1;case 1:return e.trys.push([1,3,,10]),[4,s[o].apply(s,__spreadArray(__spreadArray([],__read(r),!1),[n],!1))];case 2:return a=e.sent(),_(null,a),[3,10];case 3:if(85!==(i=e.sent()).code&&!i.message.includes("IndexOptionsConflict"))return[3,9];e.label=4;case 4:return(e.trys.push([4,8,,9]),c=t.extractIndexNameFromError(i.message))?[4,s.dropIndex(c)]:[3,7];case 5:return e.sent(),[4,s[o].apply(s,__spreadArray(__spreadArray([],__read(r),!1),[n],!1))];case 6:return a=e.sent(),_(null,a),[2];case 7:return[3,9];case 8:return c=e.sent(),_(c),[3,9];case 9:return _(i),[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.create=function(e){var o=new MongoManagerUserCollection;return o.initialize(e),o},MongoManagerUserCollection.prototype.authenticate=function(r,n){return __awaiter(this,void 0,void 0,function(){var o,t;return __generator(this,function(e){switch(e.label){case 0:return(o=Math.pow(100,Math.log(r.attempts+1)),o=o<3e5?o:3e5,r.last)?Date.now()-r.last.getTime()<o?(r.last=new Date,[4,user_collection_1.Users.updateOne({_id:r._id},{$set:{last:r.last}})]):[3,2]:[3,3];case 1:return e.sent(),[2,{data:null,error:"Attempt Too Soon"}];case 2:return[3,4];case 3:r.last=new Date,e.label=4;case 4:if(5<=r.attempts)return[2,{data:null,error:"Too Many Attempts"}];if(!r.salt)return[2,{data:null,error:"No Salt Value Stored"}];e.label=5;case 5:return e.trys.push([5,11,,12]),[4,pbkdf2Promisified(n,r.salt,{iterations:25e3,keylen:512,digestAlgorithm:"sha256"})];case 6:return(o=e.sent(),scmp(o,Buffer.from(r.hash,"hex")))?(r.last=new Date,r.attempts=0,[4,user_collection_1.Users.updateOne({_id:r._id},{$set:{last:r.last,attempts:r.attempts}})]):[3,8];case 7:return e.sent(),[2,{data:r,error:""}];case 8:return r.last=new Date,r.attempts=r.attempts+1,[4,user_collection_1.Users.updateOne({_id:r._id},{$set:{last:r.last,attempts:r.attempts}})];case 9:return e.sent(),5<=r.attempts?[2,{data:null,error:"Too Many Attempts"}]:[2,{data:null,error:"Invalid Username And Password"}];case 10:return[3,12];case 11:throw(t=e.sent()).message="Error in User Authenticate: ".concat(JSON.stringify(r,null,2)," - ").concat(t.message),t;case 12:return[2]}})})},MongoManagerUserCollection.prototype.serializeUser=function(){return function(e,o){o(null,e.username)}},MongoManagerUserCollection.prototype.deserializeUser=function(){var e=this;return function(o){return __awaiter(e,void 0,void 0,function(){return __generator(this,function(e){return[2,user_collection_1.Users.findOne({username:o})]})})}},MongoManagerUserCollection.prototype.setPassword=function(r,n){return __awaiter(this,void 0,void 0,function(){var o,t;return __generator(this,function(e){switch(e.label){case 0:if(r)return[3,1];throw new Error("Error In User Set Password: No User");case 1:if(n)return[3,2];throw new Error("Error In User Set Password: No Password");case 2:return[4,randomBytes(32)];case 3:o=e.sent(),o=o.toString("hex"),e.label=4;case 4:return e.trys.push([4,6,,7]),[4,pbkdf2Promisified(n,o,{iterations:25e3,keylen:512,digestAlgorithm:"sha256"})];case 5:return t=e.sent(),t=Buffer.from(t,"binary").toString("hex"),[2,user_collection_1.Users.updateOne({_id:r._id},{$set:{hash:t,salt:o,services:{},attempts:0}})];case 6:throw(t=e.sent()).message="Error in User Set Password: ".concat(JSON.stringify(r,null,2)," - ").concat(t.message),t;case 7:return[2]}})})},MongoManagerUserCollection.prototype.changePassword=function(t,r,n){return __awaiter(this,void 0,void 0,function(){var o;return __generator(this,function(e){switch(e.label){case 0:if(t)return[3,1];throw new Error("Error in User Change Password: Missing User");case 1:if(r&&n)return[3,2];throw new Error("Error in User Change Password: Missing Password");case 2:return[4,this.authenticate(t,r)];case 3:if((o=e.sent()).data)return[2,this.setPassword(t,n)];throw o.error;case 4:return[2]}})})},MongoManagerUserCollection.prototype.register=function(o,t){return __awaiter(this,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:if(o.username)return[3,1];throw new Error("Error in User Register: Missing Username");case 1:return[4,user_collection_1.Users.findOne({username:o.username})];case 2:if(e.sent())throw new Error("Error in User Register: Username Exists");return[3,3];case 3:return[4,user_collection_1.Users.findOne({email:o.email})];case 4:if(e.sent())throw new Error("Error in User Register: Email Exists");return o.setPassword(t),[2,o];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 pbkdf2(e,o,t,r){crypto.pbkdf2(e,o,t.iterations,t.keylen,t.digestAlgorithm,r)}function pbkdf2Promisified(e,o,n){return new Promise(function(t,r){return pbkdf2(e,o,n,function(e,o){return e?r(e):t(o)})})}function randomBytes(e){return new Promise(function(t,r){return crypto.randomBytes(e,function(e,o){return e?r(e):t(o)})})}function objectContainsPropertyDeep(e,o){var t=Object.keys(e);if(t.some(function(e){return e===o}))return!0;for(var r=0;r<t.length;r++){var n=t[r];if(e[n]&&"object"==typeof e[n]&&objectContainsPropertyDeep(e[n],o))return!0}return!1}exports.MongoManagerUserCollection=MongoManagerUserCollection;
1
+ "use strict";var __extends=this&&this.__extends||function(){var t=function(e,o){return(t=Object.setPrototypeOf||({__proto__:[]}instanceof Array?function(e,o){e.__proto__=o}:function(e,o){for(var t in o)Object.prototype.hasOwnProperty.call(o,t)&&(e[t]=o[t])}))(e,o)};return function(e,o){if("function"!=typeof o&&null!==o)throw new TypeError("Class extends value "+String(o)+" is not a constructor or null");function __(){this.constructor=e}t(e,o),e.prototype=null===o?Object.create(o):(__.prototype=o.prototype,new __)}}(),__assign=this&&this.__assign||function(){return(__assign=Object.assign||function(e){for(var o,t=1,r=arguments.length;t<r;t++)for(var n in o=arguments[t])Object.prototype.hasOwnProperty.call(o,n)&&(e[n]=o[n]);return e}).apply(this,arguments)},__awaiter=this&&this.__awaiter||function(e,r,n,s){return new(n=n||Promise)(function(t,o){function fulfilled(e){try{step(s.next(e))}catch(e){o(e)}}function rejected(e){try{step(s.throw(e))}catch(e){o(e)}}function step(e){var o;e.done?t(e.value):((o=e.value)instanceof n?o:new n(function(e){e(o)})).then(fulfilled,rejected)}step((s=s.apply(e,r||[])).next())})},__generator=this&&this.__generator||function(r,n){var s,i,a,c={label:0,sent:function(){if(1&a[0])throw a[1];return a[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 o=[t,e];if(s)throw new TypeError("Generator is already executing.");for(;c=l&&o[l=0]?0:c;)try{if(s=1,i&&(a=2&o[0]?i.return:o[0]?i.throw||((a=i.return)&&a.call(i),0):i.next)&&!(a=a.call(i,o[1])).done)return a;switch(i=0,(o=a?[2&o[0],a.value]:o)[0]){case 0:case 1:a=o;break;case 4:return c.label++,{value:o[1],done:!1};case 5:c.label++,i=o[1],o=[0];continue;case 7:o=c.ops.pop(),c.trys.pop();continue;default:if(!(a=0<(a=c.trys).length&&a[a.length-1])&&(6===o[0]||2===o[0])){c=0;continue}if(3===o[0]&&(!a||o[1]>a[0]&&o[1]<a[3]))c.label=o[1];else if(6===o[0]&&c.label<a[1])c.label=a[1],a=o;else{if(!(a&&c.label<a[2])){a[2]&&c.ops.pop(),c.trys.pop();continue}c.label=a[2],c.ops.push(o)}}o=n.call(r,c)}catch(e){o=[6,e],i=0}finally{s=a=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}}},__rest=this&&this.__rest||function(e,o){var t={};for(n in e)Object.prototype.hasOwnProperty.call(e,n)&&o.indexOf(n)<0&&(t[n]=e[n]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols)for(var r=0,n=Object.getOwnPropertySymbols(e);r<n.length;r++)o.indexOf(n[r])<0&&Object.prototype.propertyIsEnumerable.call(e,n[r])&&(t[n[r]]=e[n[r]]);return t},__read=this&&this.__read||function(e,o){var t="function"==typeof Symbol&&e[Symbol.iterator];if(!t)return e;var r,n,s=t.call(e),i=[];try{for(;(void 0===o||0<o--)&&!(r=s.next()).done;)i.push(r.value)}catch(e){n={error:e}}finally{try{r&&!r.done&&(t=s.return)&&t.call(s)}finally{if(n)throw n.error}}return i},__spreadArray=this&&this.__spreadArray||function(e,o,t){if(t||2===arguments.length)for(var r,n=0,s=o.length;n<s;n++)!r&&n in o||((r=r||Array.prototype.slice.call(o,0,n))[n]=o[n]);return e.concat(r||Array.prototype.slice.call(o))},__values=this&&this.__values||function(e){var o="function"==typeof Symbol&&Symbol.iterator,t=o&&e[o],r=0;if(t)return t.call(e);if(e&&"number"==typeof e.length)return{next:function(){return{value:(e=e&&r>=e.length?void 0:e)&&e[r++],done:!e}}};throw new TypeError(o?"Object is not iterable.":"Symbol.iterator is not defined.")},async_1=(Object.defineProperty(exports,"__esModule",{value:!0}),exports.MongoManagerUserCollection=exports.MongoManagerCollection=exports.MongoManagerModel=exports.MongoManager=void 0,require("async")),NodeCache=require("node-cache"),os_1=require("os"),simpl_schema_1=require("simpl-schema"),user_collection_1=require("../collections/user.collection"),resolveio_server_app_1=require("../resolveio-server-app"),common_1=require("../util/common"),monitor_manager_1=require("./monitor.manager"),crypto=require("crypto"),scmp=require("scmp"),numCPUs=(0,os_1.cpus)().length,v8=require("v8"),AsyncLocalStorage=require("async_hooks").AsyncLocalStorage,asyncLocalStorage=new AsyncLocalStorage,MongoManager=function(){function MongoManager(){this._collections=[],this._cacheMap=[],this._operationInProgress=new Map,this._heapSize=v8.getHeapStatistics()/numCPUs,this._serverCollections=[],this._isWorkersEnabled=!1,this._isWorkerInstance=!1}return MongoManager.create=function(){return __awaiter(this,void 0,void 0,function(){var o;return __generator(this,function(e){switch(e.label){case 0:return[4,(o=new MongoManager).initialize()];case 1:return e.sent(),[2,o]}})})},MongoManager.prototype.initialize=function(){return __awaiter(this,void 0,void 0,function(){var o;return __generator(this,function(e){switch(e.label){case 0:return 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._isWorkersEnabled&&this._isWorkerInstance&&this.setupChangeStream(),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().listCollections().toArray()];case 1:return o=e.sent(),this._serverCollections=o,[2]}})})},MongoManager.prototype.oneTimeTransaction=function(r){return __awaiter(this,void 0,void 0,function(){var o=this;return __generator(this,function(e){switch(e.label){case 0:return[4,asyncLocalStorage.run({},function(){return __awaiter(o,void 0,void 0,function(){var o,t;return __generator(this,function(e){switch(e.label){case 0:o=resolveio_server_app_1.ResolveIOServer.getMongoConnection().startSession(),asyncLocalStorage.getStore().session=o,e.label=1;case 1:return e.trys.push([1,,3,5]),[4,o.withTransaction(function(){return r()})];case 2:return[2,e.sent()];case 3:return[4,o.endSession()];case 4:return e.sent(),(t=asyncLocalStorage.getStore())&&delete t.session,[7];case 5:return[2]}})})})];case 1:return[2,e.sent()]}})})},MongoManager.prototype.getSession=function(){var e=asyncLocalStorage.getStore();return e?e.session:null},MongoManager.prototype.setCacheLimit=function(){this._isWorkersEnabled?this._heapLimit=this._isWorkerInstance?.8*this._heapSize:.4*this._heapSize:this._heapLimit=.3*this._heapSize},MongoManager.prototype.getServerCollections=function(){return this._serverCollections},MongoManager.prototype.registerCollection=function(o){return __awaiter(this,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return o.collectionOptions&&o.collectionOptions.timeseries&&o.collectionOptions.timeseries.timeField&&this._serverCollections.some(function(e){return e.name===o.collectionName&&"collection"===e.type})?[4,resolveio_server_app_1.ResolveIOServer.getMainDB().dropCollection(o.collectionName)]:[3,3];case 1:return e.sent(),[4,this.createCollection(o)];case 2:e.sent(),e.label=3;case 3:return this._collections.push(o),[2]}})})},MongoManager.prototype.createCollection=function(o){return __awaiter(this,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().createCollection(o.collectionName,o.collectionOptions)];case 1:return e.sent(),[2]}})})},MongoManager.prototype.collections=function(){return this._collections},MongoManager.prototype.collection=function(o){return this._collections.find(function(e){return e.collectionName===o})},MongoManager.prototype.delay=function(o){return __awaiter(this,void 0,void 0,function(){return __generator(this,function(e){return[2,new Promise(function(e){return setTimeout(e,o)})]})})},MongoManager.prototype.find=function(s,i,a){return void 0===i&&(i={}),__awaiter(this,void 0,void 0,function(){var o,t,r,n=this;return __generator(this,function(e){switch(e.label){case 0:return((t=a||{}).session,t=__rest(t,["session"]),o=this.generateCacheKey([s],"find",[i,t]),void 0!==(t=this.getFromCache(o)))?(resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo Find - Cache",s),[2,t]):this._operationInProgress.has(o)?(resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo Find - Already running",s),[2,this._operationInProgress.get(o).promise]):(resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo Find - Running",s),r=this.executeFind(s,i,a,o),this._operationInProgress.set(o,{promise:r,invalidatedDuringExecution:!1,collections:[s]}),[4,r.finally(function(){resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo Find - Done",s),n._operationInProgress.delete(o)})]);case 1:return e.sent(),[2,r]}})})},MongoManager.prototype.executeFind=function(s,i,a,c){return __awaiter(this,void 0,void 0,function(){var o,t,r,n;return __generator(this,function(e){switch(e.label){case 0:t=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(s),(r=a||{}).session,r=__rest(r,["session"]),r=monitor_manager_1.MonitorMongo.create("find",s,JSON.stringify([i,r])),e.label=1;case 1:return e.trys.push([1,3,4,6]),[4,t.find(i,a).toArray()];case 2:return o=e.sent(),[3,6];case 3:throw(n=e.sent()).message="Error in Execute Find (".concat(s,"): ").concat(n.message),n;case 4:return[4,r.finish()];case 5:return e.sent(),[7];case 6:return this._operationInProgress.get(c).invalidatedDuringExecution||this.addToCache([s],c,o),[2,o]}})})},MongoManager.prototype.findOne=function(s,i,a){return void 0===i&&(i={}),__awaiter(this,void 0,void 0,function(){var o,t,r,n=this;return __generator(this,function(e){switch(e.label){case 0:return((t=a||{}).session,t=__rest(t,["session"]),o=this.generateCacheKey([s],"findOne",[i,t]),void 0!==(t=this.getFromCache(o)))?(resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo FindOne - Cache",s),[2,t]):this._operationInProgress.has(o)?(resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo FindOne - Already running",s),[2,this._operationInProgress.get(o).promise]):(resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo FindOne - Running",s),r=this.executeFindOne(s,i,a,o),this._operationInProgress.set(o,{promise:r,invalidatedDuringExecution:!1,collections:[s]}),[4,r.finally(function(){resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo FindOne - Done",s),n._operationInProgress.delete(o)})]);case 1:return e.sent(),[2,r]}})})},MongoManager.prototype.executeFindOne=function(s,i,a,c){return __awaiter(this,void 0,void 0,function(){var o,t,r,n;return __generator(this,function(e){switch(e.label){case 0:t=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(s),(r=a||{}).session,r=__rest(r,["session"]),r=monitor_manager_1.MonitorMongo.create("findOne",s,JSON.stringify([i,r])),e.label=1;case 1:return e.trys.push([1,3,4,6]),[4,t.findOne(i,a)];case 2:return o=e.sent(),[3,6];case 3:throw(n=e.sent()).message="Error in Execute Find One (".concat(s,"): ").concat(n.message),n;case 4:return[4,r.finish()];case 5:return e.sent(),[7];case 6:return this._operationInProgress.get(c).invalidatedDuringExecution||this.addToCache([s],c,o),[2,o]}})})},MongoManager.prototype.aggregate=function(i,a,c){return __awaiter(this,void 0,void 0,function(){var o,t,r,n,s=this;return __generator(this,function(e){switch(e.label){case 0:return(o=__spreadArray([i],__read(a.flatMap(function(e){var o;return null!=(o=e.$lookup)&&o.from?[e.$lookup.from]:[]})),!1),(r=c||{}).session,r=__rest(r,["session"]),t=this.generateCacheKey(o,"aggregate",[a,r]),void 0!==(r=this.getFromCache(t)))?(resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo Agg - Cache",o),[2,r]):this._operationInProgress.has(t)?(resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo Agg - Already running",o),[2,this._operationInProgress.get(t).promise]):(resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo Agg - Running",o),n=this.executeAggregate(i,a,c,t,o),this._operationInProgress.set(t,{promise:n,invalidatedDuringExecution:!1,collections:o}),[4,n.finally(function(){resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo Agg - Done",o),s._operationInProgress.delete(t)})]);case 1:return e.sent(),[2,n]}})})},MongoManager.prototype.executeAggregate=function(s,i,a,c,l){return __awaiter(this,void 0,void 0,function(){var o,t,r,n;return __generator(this,function(e){switch(e.label){case 0:t=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(s),(r=a||{}).session,r=__rest(r,["session"]),r=monitor_manager_1.MonitorMongo.create("aggregate",s,JSON.stringify([i,r])),e.label=1;case 1:return e.trys.push([1,3,4,6]),[4,t.aggregate(i,a).toArray()];case 2:return o=e.sent(),[3,6];case 3:throw(n=e.sent()).message="Error in Execute Aggregate (".concat(s,"): ").concat(n.message),n;case 4:return[4,r.finish()];case 5:return e.sent(),[7];case 6:return this._operationInProgress.get(c).invalidatedDuringExecution||this.addToCache(l,c,o),[2,o]}})})},MongoManager.prototype.countDocuments=function(s,i,a){return void 0===i&&(i={}),__awaiter(this,void 0,void 0,function(){var o,t,r,n=this;return __generator(this,function(e){switch(e.label){case 0:return((t=a||{}).session,t=__rest(t,["session"]),o=this.generateCacheKey([s],"countDocuments",[i,t]),void 0!==(t=this.getFromCache(o)))?[2,t]:this._operationInProgress.has(o)?[2,this._operationInProgress.get(o).promise]:(r=this.executeCountDocuments(s,i,a,o),this._operationInProgress.set(o,{promise:r,invalidatedDuringExecution:!1,collections:[s]}),[4,r.finally(function(){return n._operationInProgress.delete(o)})]);case 1:return e.sent(),[2,r]}})})},MongoManager.prototype.executeCountDocuments=function(s,i,a,c){return __awaiter(this,void 0,void 0,function(){var o,t,r,n;return __generator(this,function(e){switch(e.label){case 0:t=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(s),(r=a||{}).session,r=__rest(r,["session"]),r=monitor_manager_1.MonitorMongo.create("countDocuments",s,JSON.stringify([i,r])),e.label=1;case 1:return e.trys.push([1,3,4,6]),[4,t.countDocuments(i,a)];case 2:return o=e.sent(),[3,6];case 3:throw(n=e.sent()).message="Error in Execute Count Documents (".concat(s,"): ").concat(n.message),n;case 4:return[4,r.finish()];case 5:return e.sent(),[7];case 6:return this._operationInProgress.get(c).invalidatedDuringExecution||this.addToCache([s],c,o),[2,o]}})})},MongoManager.prototype.generateCacheKey=function(e,o,t){e=JSON.stringify({collections:e,functionName:o,args:t});return crypto.createHash("sha256").update(e).digest("hex")},MongoManager.prototype.getFromCache=function(o){try{var e=this._nodeCache.get(o);if(e)return JSON.parse(e,common_1.dateReviver)}catch(e){this._nodeCache.del(o)}},MongoManager.prototype.addToCache=function(e,o,t){var r,n;if((0,common_1.getBinarySize)(JSON.stringify(t))<1e6&&!e.includes("logs")&&!e.find(function(e){return e.endsWith(".versions")})&&!e.find(function(e){return e.startsWith("monitor-")})){var s=this._nodeCache.getStats().vsize;if(s>this._heapLimit){var i=0,a=this._nodeCache.keys();try{for(var c=__values(a),l=c.next();!l.done;l=c.next()){var _=l.value;if(this._nodeCache.del(_),i+=1,(s=this._nodeCache.getStats().vsize)<.75*this._heapLimit)break}}catch(e){r={error:e}}finally{try{l&&!l.done&&(n=c.return)&&n.call(c)}finally{if(r)throw r.error}}console.log("Query Cache: Too Big, - Deleted: "+i+" - "+s)}this._nodeCache.set(o,JSON.stringify(t)),this._cacheMap.push({collections:e,key:o})}},MongoManager.prototype.invalidateQueryCache=function(o){var t,e,r,n,s=this._cacheMap.filter(function(e){return e.collections.includes(o)});resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo Invalidate Cache",o);try{for(var i=__values(s),a=i.next();!a.done;a=i.next()){var c=a.value;this._nodeCache.del(c.key),this._operationInProgress.has(c.key)&&(this._operationInProgress.get(c.key).invalidatedDuringExecution=!0,resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager())&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo Invalidated During Execution",c.collections)}}catch(e){t={error:e}}finally{try{a&&!a.done&&(e=i.return)&&e.call(i)}finally{if(t)throw t.error}}try{for(var l=__values(this._operationInProgress.entries()),_=l.next();!_.done;_=l.next()){var g=__read(_.value,2),u=(g[0],g[1]);u.collections.includes(o)&&(u.invalidatedDuringExecution=!0,resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager())&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo Invalidated During Execution (No cache)",u.collections)}}catch(e){r={error:e}}finally{try{_&&!_.done&&(n=l.return)&&n.call(l)}finally{if(r)throw r.error}}this._cacheMap=this._cacheMap.filter(function(e){return!e.collections.includes(o)})},MongoManager.prototype.setupChangeStream=function(){var o=this,t=resolveio_server_app_1.ResolveIOServer.getMainDB().watch([{$match:{$and:[{"ns.coll":{$nin:["log-method-latencies","log-subscriptions","logs","counters","cron-job-histories","email-histories","qb-soap-request-histories","qb-soap-request-responses","qb-soap-requests","qb-soap-retries"]}},{"ns.coll":{$not:/.*\.versions$/}},{"ns.coll":{$not:/^monitor-/}}]}}],{fullDocument:"updateLookup"});t.on("change",function(e){e.ns&&e.ns.coll&&(e=e.ns.coll,o.collection(e))&&o.invalidateQueryCache(e)}),t.on("error",function(){return __awaiter(o,void 0,void 0,function(){var o=this;return __generator(this,function(e){switch(e.label){case 0:return console.log(new Date,"Mongo change stream error. Restart..."),[4,t.close()];case 1:return e.sent(),setTimeout(function(){return o.setupChangeStream()},5e3),[2]}})})}),t.on("close",function(){console.log(new Date,"Mongo change stream closed. Restarting..."),setTimeout(function(){return o.setupChangeStream()},5e3)})},MongoManager}(),MongoManagerModel=(exports.MongoManager=MongoManager,function(){function MongoManagerModel(){this.collection_main=null,this.collection_version=null}return MongoManagerModel.create=function(e){var o=new MongoManagerModel;return o.initialize(e),o},MongoManagerModel.prototype.initialize=function(e){var o,t=this;"users"===e.collectionName?this.collection_main=MongoManagerUserCollection.create(e):this.collection_main=MongoManagerCollection.create(e),e.useVersionCollection&&(this.collection_main.useVersions=!0,(o=(0,common_1.deepCopy)(e.schema))._id.type="Object",o._id.blackbox=!0,o={collectionName:e.collectionName+".versions",schema:o,useVersionCollection:!1,useReportBuilder:!1,reportBuilderLookupTables:[],timestamps:!0,createLogs:!1,checkSchema:!1,collectionOptions:null,skipCache:!0},"users"===e.collectionName?this.collection_version=MongoManagerUserCollection.create(o):this.collection_version=MongoManagerCollection.create(o),setImmediate(function(){return __awaiter(t,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return[4,this.collection_version.createIndex({"_id._id":1,"_id.__v":1})];case 1:return e.sent(),[2]}})})}),this.collection_main.versionCollection=e.collectionName+".versions")},MongoManagerModel}()),MongoManagerCollection=(exports.MongoManagerModel=MongoManagerModel,function(){function MongoManagerCollection(){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.skipCache=!1,this.bypassSession=!1}var e;return MongoManagerCollection.create=function(e){var o=new MongoManagerCollection;return o.initialize(e),o},MongoManagerCollection.prototype.initialize=function(e){var o,t=this,r=(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,this.skipCache=e.skipCache||!1,this.bypassSession=e.bypassSession||!1,e.useReportBuilder&&(this.useRB=!0,o=(0,common_1.deepCopy)(e.schema),e=(0,common_1.buildRbLookups)(e.reportBuilderLookupTables,o,[]),this.rbSchema=(0,common_1.buildRbSchema)(e)),setInterval(function(){return __awaiter(t,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return resolveio_server_app_1.ResolveIOServer&&resolveio_server_app_1.ResolveIOServer.getMainServer()&&resolveio_server_app_1.ResolveIOServer.getMongoManager()&&resolveio_server_app_1.ResolveIOServer.getMongoManager().getServerCollections().length?[4,resolveio_server_app_1.ResolveIOServer.getMongoManager().registerCollection(this)]:[3,2];case 1:e.sent(),clearInterval(r),e.label=2;case 2:return[2]}})})},1))},MongoManagerCollection.prototype.extractIndexNameFromError=function(e){var o=e.match(/Index already exists with a different name: (\S+)/);return(o=!o||o.length<=1?e.match(/existing index:.*name: "([^"]+)"/):o)&&1<o.length?o[1]:null},MongoManagerCollection.prototype.aggregate=function(s,i,a,c){return void 0===a&&(a=!1),void 0===c&&(c=!1),__awaiter(this,void 0,void 0,function(){var o,t,r,n;return __generator(this,function(e){switch(e.label){case 0:return!(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())||c||this.bypassSession||(i?i.session=o:i={session:o}),a||this.skipCache?[3,1]:[2,resolveio_server_app_1.ResolveIOServer.getMongoManager().aggregate(this.collectionName,s,i)];case 1:(o=i||{}).session,t=__rest(o,["session"]),r=monitor_manager_1.MonitorMongo.create("aggregate",this.collectionName,JSON.stringify([s,t])),e.label=2;case 2:return e.trys.push([2,4,5,7]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).aggregate(s,i).toArray()];case 3:return[2,e.sent()];case 4:throw n=e.sent(),console.log(new Date,"Error Aggregate",this.collectionName,JSON.stringify([s,t,n],null,2)),n.message="Error in Aggregate: ".concat(this.collectionName," - ").concat(n.message),n;case 5:return[4,r.finish()];case 6:return e.sent(),[7];case 7:return[2]}})})},MongoManagerCollection.prototype.aggregateCount=function(n,s,i){return void 0===i&&(i=!1),__awaiter(this,void 0,void 0,function(){var o,t,r;return __generator(this,function(e){switch(e.label){case 0:!(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())||i||this.bypassSession||(s?s.session=o:s={session:o}),(o=s||{}).session,o=__rest(o,["session"]),t=monitor_manager_1.MonitorMongo.create("aggregateCount",this.collectionName,JSON.stringify([n,o])),e.label=1;case 1:return e.trys.push([1,3,4,6]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).aggregate(n,s).toArray()];case 2:return[2,e.sent().length];case 3:throw r=e.sent(),console.log(new Date,"Error Aggregate Count",this.collectionName,JSON.stringify([n,o,r],null,2)),r.message="Error in Aggregate Count: ".concat(this.collectionName," - ").concat(r.message),r;case 4:return[4,t.finish()];case 5:return e.sent(),[7];case 6:return[2]}})})},MongoManagerCollection.prototype.aggregateCursor=function(e,o,t){var r=this,n=(void 0===t&&(t=!1),resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession()),t=(!n||t||this.bypassSession||(o?o.session=n:o={session:n}),o||{}),n=(t.session,__rest(t,["session"])),s=monitor_manager_1.MonitorMongo.create("aggregateCursor",this.collectionName,JSON.stringify([e,n])),t=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).aggregate(e,o);return t.on("close",function(o){return __awaiter(r,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return[4,s.finish()];case 1:return e.sent(),[2,o]}})})}),t},MongoManagerCollection.prototype.aggregateStream=function(e,o,t,r){var n=this,s=(void 0===r&&(r=!1),resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession()),r=(!s||r||this.bypassSession||(o?o.session=s:o={session:s}),o||{}),s=(r.session,__rest(r,["session"])),i=monitor_manager_1.MonitorMongo.create("aggregateStream",this.collectionName,JSON.stringify([e,s,t]));return resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).aggregate(e,o).stream(t).on("close",function(o){return __awaiter(n,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return[4,i.finish()];case 1:return e.sent(),[2,o]}})})})},MongoManagerCollection.prototype.bulkWrite=function(R,b,C,D,E,x){return void 0===C&&(C=!1),void 0===D&&(D=!1),void 0===E&&(E=!1),void 0===x&&(x=!1),__awaiter(this,void 0,void 0,function(){var o,t,r,n,s,i,a,c,l,_,g,u,v,d,p,h,m,f,O,S,M,y,I,w,N;return __generator(this,function(e){switch(e.label){case 0:if(!R.length)return[2,null];o=1e3,t=0,r=null,!(n=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())||x||this.bypassSession||(b?b.session=n:b={session:n},r={session:n}),e.label=1;case 1:if(!(t<R.length))return[3,24];s=R.slice(t,t+o),i=[],a=[];try{for(w=void 0,c=__values(s),l=c.next();!l.done;l=c.next()){if(_=l.value,g=Object.keys(_)[0],u=_[g],this.checkSchema&&!C){if(v=this.simplschema.newContext(),d=void 0,"insertOne"===g)d=v.validate(u.document);else if("replaceOne"===g)d=v.validate(u.replacement);else if("updateOne"===g||"updateMany"===g)d=v.validate(u.update,{modifier:!0});else{if("deleteOne"!==g&&"deleteMany"!==g)throw new Error("Unsupported operation type: ".concat(g));d=!0}if(!d)throw new Error("Schema validation failed for ".concat(g,": ").concat(JSON.stringify(v.validationErrors())))}if("insertOne"===g)this.timestamps&&(u.document.createdAt||(u.document.createdAt=new Date),u.document.updatedAt||(u.document.updatedAt=new Date)),!D&&this.createLogs&&i.push({insertOne:{document:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:u.document._id||(0,common_1.objectIdHexString)(),payload:(0,common_1.getBinarySize)(JSON.stringify(u.document))<2e5?JSON.stringify(u.document,null,2):"Too Big",method:"insertOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}}),!E&&this.useVersions&&(p=__assign(__assign({},u.document),{__v:0,_id:{_id:u.document._id||(0,common_1.objectIdHexString)(),__v:0}}),a.push({insertOne:{document:p}})),_[g].document=u.document;else if("updateOne"===g||"updateMany"===g)this.timestamps&&(u.update.$set||(u.update.$set={}),u.update.$set.updatedAt=new Date),!E&&this.useVersions&&(u.update.$inc||(u.update.$inc={}),u.update.$inc.__v=1),!D&&this.createLogs&&i.push({insertOne:{document:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:u.filter._id||null,payload:(0,common_1.getBinarySize)(JSON.stringify(u.update))<2e5?JSON.stringify(u.update,null,2):"Too Big",method:g,id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}}),!E&&this.useVersions&&a.push({filter:u.filter});else if("replaceOne"===g)this.timestamps&&(u.replacement.updatedAt=new Date),!E&&this.useVersions&&(u.replacement.__v=(u.replacement.__v||0)+1),!D&&this.createLogs&&i.push({insertOne:{document:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:u.filter._id||null,payload:(0,common_1.getBinarySize)(JSON.stringify(u.replacement))<2e5?JSON.stringify(u.replacement,null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}}),!E&&this.useVersions&&a.push({filter:u.filter}),_[g].replacement=u.replacement;else{if("deleteOne"!==g&&"deleteMany"!==g)throw new Error("Unsupported operation type: ".concat(g));!D&&this.createLogs&&i.push({insertOne:{document:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:u.filter._id||null,payload:(0,common_1.getBinarySize)(JSON.stringify(u.filter))<2e5?JSON.stringify(u.filter,null,2):"Too Big",method:g,id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}}),!E&&this.useVersions&&a.push({filter:u.filter})}}}catch(e){w={error:e}}finally{try{l&&!l.done&&(N=c.return)&&N.call(c)}finally{if(w)throw w.error}}if(h=[],!(!E&&this.useVersions&&0<a.length))return[3,7];e.label=2;case 2:return e.trys.push([2,6,,7]),m=a.map(function(e){return e.filter}),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName).find({$or:m},r).toArray()];case 3:return(h=e.sent(),0<(m=h.map(function(e){return{insertOne:{document:__assign(__assign({},e),{_id:{_id:e._id,__v:e.__v}})}}})).length)?(S=null,S=r?{session:n,ordered:!1}:{ordered:!1},[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.versionCollection).bulkWrite(m,S)]):[3,5];case 4:e.sent(),e.label=5;case 5:return[3,7];case 6:return f=e.sent(),console.error("Error during version creation:",f),[3,7];case 7:(f=b||{}).session,O=__rest(f,["session"]),O=monitor_manager_1.MonitorMongo.create("bulkWrite",this.collectionName,JSON.stringify([s,O])),e.label=8;case 8:return e.trys.push([8,10,11,13]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName).bulkWrite(s,b)];case 9:return e.sent(),[3,13];case 10:throw(M=e.sent()).message="Error in Bulk Write: ".concat(this.collectionName," - ").concat(M.message),M;case 11:return[4,O.finish()];case 12:return e.sent(),[7];case 13:if(!D&&this.createLogs&&0<i.length){if("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL)return[3,14];resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLogs(i.map(function(e){return{type:"log",data:e}}))}return[3,16];case 14:return S=null,S=r?{session:n,ordered:!1}:{ordered:!1},[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").bulkWrite(i,S)];case 15:e.sent(),e.label=16;case 16:if(E||!this.useVersions)return[3,22];e.label=17;case 17:return e.trys.push([17,21,,22]),M=h.map(function(e){return e._id}),y=[{$match:{"_id._id":{$in:M}}},{$sort:{"_id._id":1,"_id.__v":-1}},{$group:{_id:"$_id._id",versions:{$push:"$_id"}}},{$project:{versionsToDelete:{$slice:["$versions",5,{$size:"$versions"}]}}},{$unwind:"$versionsToDelete"},{$replaceRoot:{newRoot:{_id:"$versionsToDelete"}}}],[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.versionCollection).aggregate(y,r).toArray()];case 18:return 0<(y=e.sent()).length?[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.versionCollection).deleteMany({_id:{$in:y.map(function(e){return e._id})}},r)]:[3,20];case 19:e.sent(),e.label=20;case 20:return[3,22];case 21:return I=e.sent(),console.error("Error during version cleanup:",I),[3,22];case 22:return t+=o,[4,new Promise(function(e){return setImmediate(e)})];case 23:return e.sent(),this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[3,1];case 24:return[2,{ok:1}]}})})},MongoManagerCollection.prototype.countDocuments=function(n,s,i,a){return void 0===n&&(n={}),void 0===i&&(i=!1),void 0===a&&(a=!1),__awaiter(this,void 0,void 0,function(){var o,t,r;return __generator(this,function(e){switch(e.label){case 0:return(!(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())||a||this.bypassSession||(s?s.session=o:s={session:o}),(o=s||{}).session,o=__rest(o,["session"]),0===Object.keys(n).length)?[2,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).estimatedDocumentCount(s)]:[3,1];case 1:return i||this.skipCache?[3,2]:[2,resolveio_server_app_1.ResolveIOServer.getMongoManager().countDocuments(this.collectionName,n,s)];case 2:t=monitor_manager_1.MonitorMongo.create("countDocuments",this.collectionName,JSON.stringify([n,o])),e.label=3;case 3:return e.trys.push([3,5,6,8]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).countDocuments(n,s)];case 4:return[2,e.sent()];case 5:throw r=e.sent(),console.log(new Date,"Error Count Documents",this.collectionName,n,o,r),r.message="Error in Count Documents: ".concat(this.collectionName," - ").concat(r.message),r;case 6:return[4,t.finish()];case 7:return e.sent(),[7];case 8:return[2]}})})},MongoManagerCollection.prototype.create=function(e,o){return Array.isArray(e)?1===e.length?this.insertOne(e[0],o):this.insertMany(e,o):this.insertOne(e,o)},MongoManagerCollection.prototype.createIndex=function(t,r){return __awaiter(this,void 0,void 0,function(){var o;return __generator(this,function(e){switch(e.label){case 0:return e.trys.push([0,2,,3]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).indexes()];case 1:return(o=e.sent(),o.some(function(e){return JSON.stringify(e.key)===JSON.stringify(t)}))?[2,"Index already exists"]:(MongoManagerCollection.indexQueue.push({action:"createIndex",collection:this,specs:[t],options:r},function(e,o){if(e)throw e;return o}),[2,"Index creation queued"]);case 2:throw e.sent();case 3:return[2]}})})},MongoManagerCollection.prototype.createIndexes=function(r,n){return __awaiter(this,void 0,void 0,function(){var t,o;return __generator(this,function(e){switch(e.label){case 0:return e.trys.push([0,2,,3]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).indexes()];case 1:return(t=e.sent(),0===(o=r.filter(function(o){return!t.some(function(e){return JSON.stringify(e.key)===JSON.stringify(o.key)})})).length)?[2,["All indexes already exist"]]:(MongoManagerCollection.indexQueue.push({action:"createIndexes",collection:this,specs:[o],options:n},function(e,o){if(e)throw e;return o}),[2,["Index creation queued"]]);case 2:throw e.sent();case 3:return[2]}})})},MongoManagerCollection.prototype.deleteMany=function(_,g,u,v){return void 0===_&&(_={}),void 0===u&&(u=!1),void 0===v&&(v=!1),__awaiter(this,void 0,void 0,function(){var o,t,r,n,s,i,a,c,l;return __generator(this,function(e){switch(e.label){case 0:return(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),t=null,!o||v||this.bypassSession||(g?g.session=o:g={session:o},t={session:o}),(r=g||{}).session,r=__rest(r,["session"]),this.createLogs&&!u||this.useVersions)?[4,this.find(_)]:[3,9];case 1:n=e.sent(),s=0,e.label=2;case 2:if(!(s<n.length))return[3,9];if(i=n[s],this.createLogs&&!u){if("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL)return[3,3];resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:i._id,payload:(0,common_1.getBinarySize)(JSON.stringify([i,_,r]))<2e5?JSON.stringify([i,_,r],null,2):"Too Big",method:"deleteMany",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}})}return[3,5];case 3:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:i._id,payload:(0,common_1.getBinarySize)(JSON.stringify([i,_,r]))<2e5?JSON.stringify([i,_,r],null,2):"Too Big",method:"deleteMany",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},t)];case 4:e.sent(),e.label=5;case 5:return this.useVersions?((c=(0,common_1.deepCopy)(i))._id={_id:i._id,__v:i.__v},l=null,l=t?{session:o,upsert:!0}:{upsert:!0},[4,resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:i._id,__v:i.__v}},c,l)]):[3,8];case 6:return e.sent(),[4,resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({$and:[{"_id._id":i._id},{"_id.__v":{$lt:i.__v-1}}]},t)];case 7:e.sent(),e.label=8;case 8:return s++,[3,2];case 9:a=monitor_manager_1.MonitorMongo.create("deleteMany",this.collectionName,JSON.stringify([_,r])),e.label=10;case 10:return e.trys.push([10,12,13,15]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).deleteMany(_,g)];case 11:if((c=e.sent()).acknowledged)return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,c.deletedCount];throw new Error("Error in DeleteMany: ".concat(this.collectionName));case 12:throw l=e.sent(),console.log(new Date,"Error Delete Many",this.collectionName,_,r,l),l.message="Error in Delete Many: ".concat(this.collectionName," - ").concat(l.message),l;case 13:return[4,a.finish()];case 14:return e.sent(),[7];case 15:return[2]}})})},MongoManagerCollection.prototype.deleteOne=function(l,_,g,u){return void 0===l&&(l={}),void 0===g&&(g=!1),void 0===u&&(u=!1),__awaiter(this,void 0,void 0,function(){var o,t,r,n,s,i,a,c;return __generator(this,function(e){switch(e.label){case 0:if(r=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),o=null,!r||u||this.bypassSession||(_?_.session=r:_={session:r},o={session:r}),t=(r=_||{}).session,r=__rest(r,["session"]),!this.createLogs||g)return[3,15];i=monitor_manager_1.MonitorMongo.create("findOneAndDelete",this.collectionName,JSON.stringify([l,r])),e.label=1;case 1:return e.trys.push([1,11,12,14]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndDelete(l,_)];case 2:if(n=e.sent(),!(n=n.value))return[3,9];if(this.createLogs&&!g){if("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL)return[3,3];resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:n._id,payload:(0,common_1.getBinarySize)(JSON.stringify([n,l,r]))<2e5?JSON.stringify([n,l,r],null,2):"Too Big",method:"deleteOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}})}return[3,5];case 3:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:n._id,payload:(0,common_1.getBinarySize)(JSON.stringify([n,l,r]))<2e5?JSON.stringify([n,l,r],null,2):"Too Big",method:"deleteOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},o)];case 4:e.sent(),e.label=5;case 5:return this.useVersions?((s=(0,common_1.deepCopy)(n))._id={_id:n._id,__v:n.__v},a=null,a=o?{session:t,upsert:!0}:{upsert:!0},[4,resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:n._id,__v:n.__v}},s,a)]):[3,8];case 6:return e.sent(),[4,resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({$and:[{"_id._id":n._id},{"_id.__v":{$lt:n.__v-1}}]},o)];case 7:e.sent(),e.label=8;case 8:return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,1];case 9:return[2,0];case 10:return[3,14];case 11:throw s=e.sent(),console.log(new Date,"Error Delete One (Find One And Delete)",this.collectionName,l,r,s),s.message="Error in Delete One (Find One And Delete): ".concat(this.collectionName," - ").concat(s.message),s;case 12:return[4,i.finish()];case 13:return e.sent(),[7];case 14:return[3,21];case 15:i=monitor_manager_1.MonitorMongo.create("deleteOne",this.collectionName,JSON.stringify([l,r])),e.label=16;case 16:return e.trys.push([16,18,19,21]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).deleteOne(l,_)];case 17:if((a=e.sent()).acknowledged)return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,a.deletedCount];throw new Error("Error in Delete One: ".concat(this.collectionName));case 18:throw c=e.sent(),console.log(new Date,"Error Delete One",this.collectionName,l,r,c),c.message="Error in Delete One: ".concat(this.collectionName," - ").concat(c.message),c;case 19:return[4,i.finish()];case 20:return e.sent(),[7];case 21:return[2]}})})},MongoManagerCollection.prototype.distinct=function(n,s,i,a){return void 0===a&&(a=!1),__awaiter(this,void 0,void 0,function(){var o,t,r;return __generator(this,function(e){switch(e.label){case 0:!(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())||a||this.bypassSession||(i?i.session=o:i={session:o}),(o=i||{}).session,o=__rest(o,["session"]),t=monitor_manager_1.MonitorMongo.create("distinct",this.collectionName,JSON.stringify([n,s,o])),e.label=1;case 1:return e.trys.push([1,3,4,6]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).distinct(n,s,i)];case 2:return[2,e.sent()];case 3:throw r=e.sent(),console.log(new Date,"Error Distinct",this.collectionName,s,o,r),r.message="Error in Distinct: ".concat(this.collectionName," - ").concat(r.message),r;case 4:return[4,t.finish()];case 5:return e.sent(),[7];case 6:return[2]}})})},MongoManagerCollection.prototype.drop=function(n,s){return void 0===s&&(s=!1),__awaiter(this,void 0,void 0,function(){var o,t,r;return __generator(this,function(e){switch(e.label){case 0:!(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())||s||this.bypassSession||(n?n.session=o:n={session:o}),(o=n||{}).session,o=__rest(o,["session"]),t=monitor_manager_1.MonitorMongo.create("drop",this.collectionName,JSON.stringify([o])),e.label=1;case 1:return e.trys.push([1,3,4,6]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).drop(n)];case 2:return[2,e.sent()];case 3:throw r=e.sent(),console.log(new Date,"Error Count Documents",this.collectionName,o,r),r.message="Error in Count Documents: ".concat(this.collectionName," - ").concat(r.message),r;case 4:return[4,t.finish()];case 5:return e.sent(),[7];case 6:return[2]}})})},MongoManagerCollection.prototype.dropIndex=function(n,s,i){return void 0===i&&(i=!1),__awaiter(this,void 0,void 0,function(){var o,t,r;return __generator(this,function(e){switch(e.label){case 0:!(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())||i||this.bypassSession||(s?s.session=o:s={session:o}),(o=s||{}).session,o=__rest(o,["session"]),t=monitor_manager_1.MonitorMongo.create("dropIndex",this.collectionName,JSON.stringify([n,o])),e.label=1;case 1:return e.trys.push([1,3,4,6]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).dropIndex(n,s)];case 2:return[2,e.sent()];case 3:throw r=e.sent(),console.log(new Date,"Error Drop Index",this.collectionName,n,o,r),r.message="Error in Drop Index: ".concat(this.collectionName," - ").concat(n," - ").concat(r.message),r;case 4:return[4,t.finish()];case 5:return e.sent(),[7];case 6:return[2]}})})},MongoManagerCollection.prototype.dropIndexes=function(n,s){return void 0===s&&(s=!1),__awaiter(this,void 0,void 0,function(){var o,t,r;return __generator(this,function(e){switch(e.label){case 0:!(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())||s||this.bypassSession||(n?n.session=o:n={session:o}),(o=n||{}).session,o=__rest(o,["session"]),t=monitor_manager_1.MonitorMongo.create("dropIndexes",this.collectionName,JSON.stringify([o])),e.label=1;case 1:return e.trys.push([1,3,4,6]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).dropIndexes(n)];case 2:return[2,e.sent()];case 3:throw r=e.sent(),console.log(new Date,"Error Drop Indexes",this.collectionName,o,r),r.message="Error in Drop Indexes: ".concat(this.collectionName," - ").concat(r.message),r;case 4:return[4,t.finish()];case 5:return e.sent(),[7];case 6:return[2]}})})},MongoManagerCollection.prototype.find=function(n,s,i,a){return void 0===n&&(n={}),void 0===i&&(i=!1),void 0===a&&(a=!1),__awaiter(this,void 0,void 0,function(){var o,t,r;return __generator(this,function(e){switch(e.label){case 0:return!(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())||a||this.bypassSession||(s?s.session=o:s={session:o}),(o=s||{}).session,o=__rest(o,["session"]),i||this.skipCache?[3,1]:[2,resolveio_server_app_1.ResolveIOServer.getMongoManager().find(this.collectionName,n,s)];case 1:t=monitor_manager_1.MonitorMongo.create("find",this.collectionName,JSON.stringify([n,o])),e.label=2;case 2:return e.trys.push([2,4,5,7]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).find(n,s).toArray()];case 3:return[2,e.sent()];case 4:throw r=e.sent(),console.log(new Date,"Error Find",this.collectionName,n,o,r),r.message="Error in Find: ".concat(this.collectionName," - ").concat(JSON.stringify(n,null,2)," - ").concat(r.message),r;case 5:return[4,t.finish()];case 6:return e.sent(),[7];case 7:return[2]}})})},MongoManagerCollection.prototype.findById=function(n,s,i,a){return void 0===i&&(i=!1),void 0===a&&(a=!1),__awaiter(this,void 0,void 0,function(){var o,t,r;return __generator(this,function(e){switch(e.label){case 0:return!(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())||a||this.bypassSession||(s?s.session=o:s={session:o}),(o=s||{}).session,o=__rest(o,["session"]),i||this.skipCache?[3,1]:[2,resolveio_server_app_1.ResolveIOServer.getMongoManager().findOne(this.collectionName,{_id:n},s)];case 1:t=monitor_manager_1.MonitorMongo.create("findById",this.collectionName,JSON.stringify([{_id:n},o])),e.label=2;case 2:return e.trys.push([2,4,5,7]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOne({_id:n},s)];case 3:return[2,e.sent()];case 4:throw r=e.sent(),console.log(new Date,"Error Find By Id",this.collectionName,{_id:n},o,r),r.message="Error in Find By ID: ".concat(this.collectionName," - ").concat(n," - ").concat(r.message),r;case 5:return[4,t.finish()];case 6:return e.sent(),[7];case 7:return[2]}})})},MongoManagerCollection.prototype.findCount=function(n,s,i){return void 0===n&&(n={}),void 0===i&&(i=!1),__awaiter(this,void 0,void 0,function(){var o,t,r;return __generator(this,function(e){switch(e.label){case 0:!(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())||i||this.bypassSession||(s?s.session=o:s={session:o}),(o=s||{}).session,o=__rest(o,["session"]),t=monitor_manager_1.MonitorMongo.create("findCount",this.collectionName,JSON.stringify([n,o])),e.label=1;case 1:return e.trys.push([1,3,4,6]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).find(n,s).count()];case 2:return[2,e.sent()];case 3:throw r=e.sent(),console.log(new Date,"Error Find Count",this.collectionName,n,o,r),r.message="Error in Find Count: ".concat(this.collectionName," - ").concat(JSON.stringify(n,null,2)," - ").concat(r.message),r;case 4:return[4,t.finish()];case 5:return e.sent(),[7];case 6:return[2]}})})},MongoManagerCollection.prototype.findCursor=function(e,o,t){var r=this,n=(void 0===e&&(e={}),void 0===t&&(t=!1),resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession()),t=(!n||t||this.bypassSession||(o?o.session=n:o={session:n}),o||{}),n=(t.session,__rest(t,["session"])),s=monitor_manager_1.MonitorMongo.create("findCursor",this.collectionName,JSON.stringify([e,n])),i=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).find(e,o);return i.on("close",function(o){return __awaiter(r,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return[4,s.finish()];case 1:return e.sent(),i.removeAllListeners(),[2,o]}})})}),i},MongoManagerCollection.prototype.findStream=function(e,o,t,r){var n=this,s=(void 0===e&&(e={}),void 0===r&&(r=!1),resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession()),r=(!s||r||this.bypassSession||(o?o.session=s:o={session:s}),o||{}),s=(r.session,__rest(r,["session"])),i=monitor_manager_1.MonitorMongo.create("findStream",this.collectionName,JSON.stringify([e,s,t]));return resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).find(e,o).stream(t).on("close",function(o){return __awaiter(n,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return[4,i.finish()];case 1:return e.sent(),[2,o]}})})})},MongoManagerCollection.prototype.findOne=function(s,i,a,c){return void 0===s&&(s={}),void 0===a&&(a=!1),void 0===c&&(c=!1),__awaiter(this,void 0,void 0,function(){var o,t,r,n;return __generator(this,function(e){switch(e.label){case 0:return!(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())||c||this.bypassSession||(i?i.session=o:i={session:o}),a||this.skipCache?[3,1]:[2,resolveio_server_app_1.ResolveIOServer.getMongoManager().findOne(this.collectionName,s,i)];case 1:(o=i||{}).session,t=__rest(o,["session"]),r=monitor_manager_1.MonitorMongo.create("findOne",this.collectionName,JSON.stringify([s,t])),e.label=2;case 2:return e.trys.push([2,4,5,7]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOne(s,i)];case 3:return[2,e.sent()];case 4:throw n=e.sent(),console.log(new Date,"Error Find One",this.collectionName,s,t,n),n.message="Error in Find One: ".concat(this.collectionName," - ").concat(JSON.stringify(s,null,2)," - ").concat(n.message),n;case 5:return[4,r.finish()];case 6:return e.sent(),[7];case 7:return[2]}})})},MongoManagerCollection.prototype.findOneAndDelete=function(c,l,_,g){return void 0===c&&(c={}),void 0===_&&(_=!1),void 0===g&&(g=!1),__awaiter(this,void 0,void 0,function(){var o,t,r,n,s,i,a;return __generator(this,function(e){switch(e.label){case 0:r=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),o=null,!r||g||this.bypassSession||(l?l.session=r:l={session:r},o={session:r}),t=(r=l||{}).session,r=__rest(r,["session"]),n=monitor_manager_1.MonitorMongo.create("findOneAndDelete",this.collectionName,JSON.stringify([c,r])),e.label=1;case 1:return e.trys.push([1,10,11,13]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndDelete(c,l)];case 2:if(s=e.sent(),!(s=s.value))return[3,8];if(this.createLogs&&!_){if("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL)return[3,3];resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:s._id,payload:(0,common_1.getBinarySize)(JSON.stringify([s,c,r]))<2e5?JSON.stringify([s,c,r],null,2):"Too Big",method:"findOneAndDelete",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}})}return[3,5];case 3:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:s._id,payload:(0,common_1.getBinarySize)(JSON.stringify([s,c,r]))<2e5?JSON.stringify([s,c,r],null,2):"Too Big",method:"findOneAndDelete",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},o)];case 4:e.sent(),e.label=5;case 5:return this.useVersions?((a=(0,common_1.deepCopy)(s))._id={_id:s._id,__v:s.__v},i=null,i=o?{session:t,upsert:!0}:{upsert:!0},[4,resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:s._id,__v:s.__v}},a,i)]):[3,7];case 6:e.sent(),e.label=7;case 7:return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,s];case 8:return[2,null];case 9:return[3,13];case 10:throw a=e.sent(),console.log(new Date,"Error Find One And Delete",this.collectionName,c,r,a),a.message="Error in Find One And Delete: ".concat(this.collectionName," - ").concat(JSON.stringify(c,null,2)," - ").concat(a.message),a;case 11:return[4,n.finish()];case 12:return e.sent(),[7];case 13:return[2]}})})},MongoManagerCollection.prototype.findOneAndReplace=function(l,_,g,u,v,d){return void 0===l&&(l={}),void 0===u&&(u=!1),void 0===v&&(v=!1),void 0===d&&(d=!1),__awaiter(this,void 0,void 0,function(){var o,t,r,n,s,i,a,c;return __generator(this,function(e){switch(e.label){case 0:if(this.checkSchema&&!v&&(o=this.simplschema.newContext(),!o.validate(_)))throw console.log(new Date,this.collectionName,"Schema Errors - findOneAndReplace",o.validationErrors()),resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethod.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on findOneAndReplace - "+this.collectionName,[o.validationErrors(),_]),o.validationErrors();o=new Date,this.timestamps&&(_.updatedAt=o),g&&g.upsert&&(_._id||(_._id=(0,common_1.objectIdHexString)()),this.useVersions&&(_.__v=0),this.timestamps)&&!_.createdAt&&(_.createdAt=o),n=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),t=null,!n||d||this.bypassSession||(g?g.session=n:g={session:n},t={session:n}),r=(n=g||{}).session,n=__rest(n,["session"]),s=monitor_manager_1.MonitorMongo.create("findOneAndReplace",this.collectionName,JSON.stringify([l,_,n])),e.label=1;case 1:return e.trys.push([1,10,11,13]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndReplace(l,_,g)];case 2:if(i=e.sent(),!(i=i.value))return[3,8];if(this.createLogs&&!u){if("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL)return[3,3];resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:i._id,payload:(0,common_1.getBinarySize)(JSON.stringify([i,l,_,n]))<2e5?JSON.stringify([i,l,_,n],null,2):"Too Big",method:"findOneAndReplace",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}})}return[3,5];case 3:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:i._id,payload:(0,common_1.getBinarySize)(JSON.stringify([i,l,_,n]))<2e5?JSON.stringify([i,l,_,n],null,2):"Too Big",method:"findOneAndReplace",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},t)];case 4:e.sent(),e.label=5;case 5:return this.useVersions?((c=(0,common_1.deepCopy)(i))._id={_id:i._id,__v:i.__v},a=null,a=t?{session:r,upsert:!0}:{upsert:!0},[4,resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:i._id,__v:i.__v}},c,a)]):[3,7];case 6:e.sent(),e.label=7;case 7:return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,i];case 8:return[2,null];case 9:return[3,13];case 10:throw c=e.sent(),console.log(new Date,"Error Find One And Replace",this.collectionName,l,n,c),c.message="Error in Find One And Replace: ".concat(this.collectionName," - ").concat(JSON.stringify(l,null,2)," - ").concat(c.message),c;case 11:return[4,s.finish()];case 12:return e.sent(),[7];case 13:return[2]}})})},MongoManagerCollection.prototype.findOneAndUpdate=function(l,_,g,u,v,d){return void 0===l&&(l={}),void 0===u&&(u=!1),void 0===v&&(v=!1),void 0===d&&(d=!1),__awaiter(this,void 0,void 0,function(){var o,t,r,n,s,i,a,c;return __generator(this,function(e){switch(e.label){case 0:if(this.checkSchema&&!v&&(o=this.simplschema.newContext(),!o.validate(_,{modifier:!0})))throw console.log(new Date,this.collectionName,"Schema Errors - findOneAndUpdate",o.validationErrors()),resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethod.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on findOneAndUpdate - "+this.collectionName,[o.validationErrors(),_]),o.validationErrors();this.timestamps&&(o=new Date,_.$set?_.$set.updatedAt=o:_.$set={updatedAt:o}),g&&g.upsert&&(_.$setOnInsert?(_.$setOnInsert._id||(_.$setOnInsert._id=(0,common_1.objectIdHexString)()),this.timestamps&&!_.$setOnInsert.createdAt&&(_.$setOnInsert.createdAt=new Date)):this.timestamps?_.$setOnInsert={_id:(0,common_1.objectIdHexString)(),createdAt:new Date}:_.$setOnInsert={_id:(0,common_1.objectIdHexString)()}),n=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),t=null,!n||d||this.bypassSession||(g?g.session=n:g={session:n},t={session:n}),r=(n=g||{}).session,n=__rest(n,["session"]),s=monitor_manager_1.MonitorMongo.create("findOneAndUpdate",this.collectionName,JSON.stringify([l,_,n])),e.label=1;case 1:return e.trys.push([1,10,11,13]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndUpdate(l,_,g)];case 2:if(i=e.sent(),!(i=i.value))return[3,8];if(this.createLogs&&!u){if("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL)return[3,3];resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:i._id,payload:(0,common_1.getBinarySize)(JSON.stringify([i,l,_,n]))<2e5?JSON.stringify([i,l,_,n],null,2):"Too Big",method:"findOneAndUpdate",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}})}return[3,5];case 3:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:i._id,payload:(0,common_1.getBinarySize)(JSON.stringify([i,l,_,n]))<2e5?JSON.stringify([i,l,_,n],null,2):"Too Big",method:"findOneAndUpdate",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},t)];case 4:e.sent(),e.label=5;case 5:return this.useVersions?((c=(0,common_1.deepCopy)(i))._id={_id:i._id,__v:i.__v},a=null,a=t?{session:r,upsert:!0}:{upsert:!0},[4,resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:i._id,__v:i.__v}},c,a)]):[3,7];case 6:e.sent(),e.label=7;case 7:return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,i];case 8:return[2,null];case 9:return[3,13];case 10:throw c=e.sent(),console.log(new Date,"Error Find One And Update",this.collectionName,l,n,c),c.message="Error in Find One And Update: ".concat(this.collectionName," - ").concat(JSON.stringify(l,null,2)," - ").concat(c.message),c;case 11:return[4,s.finish()];case 12:return e.sent(),[7];case 13:return[2]}})})},MongoManagerCollection.prototype.indexes=function(n,s){return void 0===s&&(s=!1),__awaiter(this,void 0,void 0,function(){var o,t,r;return __generator(this,function(e){switch(e.label){case 0:!(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())||s||this.bypassSession||(n?n.session=o:n={session:o}),(o=n||{}).session,o=__rest(o,["session"]),t=monitor_manager_1.MonitorMongo.create("indexes",this.collectionName,JSON.stringify([o])),e.label=1;case 1:return e.trys.push([1,3,4,6]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).indexes(n)];case 2:return[2,e.sent()];case 3:throw r=e.sent(),console.log(new Date,"Error Indexes",this.collectionName,o,r),r.message="Error in Indexes: ".concat(this.collectionName," - ").concat(r.message),r;case 4:return[4,t.finish()];case 5:return e.sent(),[7];case 6:return[2]}})})},MongoManagerCollection.prototype.indexExists=function(n,s,i){return void 0===i&&(i=!1),__awaiter(this,void 0,void 0,function(){var o,t,r;return __generator(this,function(e){switch(e.label){case 0:!(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())||i||this.bypassSession||(s?s.session=o:s={session:o}),(o=s||{}).session,o=__rest(o,["session"]),t=monitor_manager_1.MonitorMongo.create("indexExists",this.collectionName,JSON.stringify([n,o])),e.label=1;case 1:return e.trys.push([1,3,4,6]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).indexExists(n,s)];case 2:return[2,e.sent()];case 3:throw r=e.sent(),console.log(new Date,"Error Index Exists",this.collectionName,n,o,r),r.message="Error in Index Exists: ".concat(this.collectionName," - ").concat(JSON.stringify(n,null,2)," - ").concat(r.message),r;case 4:return[4,t.finish()];case 5:return e.sent(),[7];case 6:return[2]}})})},MongoManagerCollection.prototype.insertMany=function(g,u,v,d,p,h){return void 0===v&&(v=!1),void 0===d&&(d=!1),void 0===p&&(p=!1),void 0===h&&(h=!1),__awaiter(this,void 0,void 0,function(){var t,o,r,n,s,i,a,c,l,_=this;return __generator(this,function(e){switch(e.label){case 0:if(!g.length)return[2,[]];t=[],this.checkSchema&&!d&&g.forEach(function(e){var o=_.simplschema.newContext();o.validate(e)?t.push(!0):(console.log(new Date,_.collectionName,"Schema Errors - insertMany",o.validationErrors()),resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethod.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on insertMany - "+_.collectionName,[o.validationErrors(),e]),t.push(!1))}),o=this.checkSchema?g.filter(function(e,o){return t[o]}):g,n=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),r=null,!n||h||this.bypassSession||(u?u.session=n:u={session:n},r={session:n}),(n=u||{}).session,n=__rest(n,["session"]),s=0,e.label=1;case 1:if(!(s<o.length))return[3,5];if((i=o[s])._id||(i._id=(0,common_1.objectIdHexString)()),this.timestamps&&(c=new Date,i.createdAt||(i.createdAt=c),i.updatedAt||(i.updatedAt=c)),this.useVersions&&!i.hasOwnProperty("__v")&&(i.__v=0),this.createLogs&&!v){if("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL)return[3,2];resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:i._id,payload:(0,common_1.getBinarySize)(JSON.stringify([i,n]))<2e5?JSON.stringify([i,n],null,2):"Too Big",method:"insertMany",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}})}return[3,4];case 2:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:i._id,payload:(0,common_1.getBinarySize)(JSON.stringify([i,n]))<2e5?JSON.stringify([i,n],null,2):"Too Big",method:"insertMany",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},r)];case 3:e.sent(),e.label=4;case 4:return s++,[3,1];case 5:if(!o.length)return[3,10];a=null,p||(a=monitor_manager_1.MonitorMongo.create("insertMany",this.collectionName,JSON.stringify([o,n]))),e.label=6;case 6:return e.trys.push([6,8,9,10]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).insertMany(o,u)];case 7:if((c=e.sent()).acknowledged)return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,o];throw new Error("Error in Insert Many: ".concat(this.collectionName," - ").concat(JSON.stringify(c,null,2)));case 8:throw l=e.sent(),console.log(new Date,"Error Insert Many",this.collectionName,o,n,l),l.message="Error in Insert Many: ".concat(this.collectionName," - ").concat(l.message),l;case 9:return a&&a.finish(),[7];case 10:return[2,[]]}})})},MongoManagerCollection.prototype.insertOne=function(a,c,l,_,g){return void 0===l&&(l=!1),void 0===_&&(_=!1),void 0===g&&(g=!1),__awaiter(this,void 0,void 0,function(){var o,t,r,n,s,i;return __generator(this,function(e){switch(e.label){case 0:if(a._id||(a._id=(0,common_1.objectIdHexString)()),this.checkSchema&&!_&&(o=this.simplschema.newContext(),!o.validate(a)))throw console.log(new Date,this.collectionName,"Schema Errors - insertOne",o.validationErrors()),resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethod.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on insertOne - "+this.collectionName,[o.validationErrors(),a]),o.validationErrors();if(this.timestamps&&(t=new Date,a.createdAt||(a.createdAt=t),a.updatedAt||(a.updatedAt=t)),t=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),r=null,!t||g||this.bypassSession||(c?c.session=t:c={session:t},r={session:t}),(n=c||{}).session,n=__rest(n,["session"]),this.createLogs&&!l){if("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL)return[3,1];resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:a._id,payload:(0,common_1.getBinarySize)(JSON.stringify([a,n]))<2e5?JSON.stringify([a,n],null,2):"Too Big",method:"insertOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}})}return[3,3];case 1:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:a._id,payload:(0,common_1.getBinarySize)(JSON.stringify([a,n]))<2e5?JSON.stringify([a,n],null,2):"Too Big",method:"insertOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},r)];case 2:e.sent(),e.label=3;case 3:this.useVersions&&!a.hasOwnProperty("__v")&&(a.__v=0),s=monitor_manager_1.MonitorMongo.create("insertOne",this.collectionName,JSON.stringify([a,n])),e.label=4;case 4:return e.trys.push([4,6,7,9]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).insertOne(a,c)];case 5:if((i=e.sent()).acknowledged)return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,a];throw new Error("Error in Insert One: ".concat(this.collectionName," - ").concat(JSON.stringify(i,null,2)));case 6:throw i=e.sent(),console.log(new Date,"Error Insert One",this.collectionName,a,n,i),i.message="Error in Insert One: ".concat(this.collectionName," - ").concat(i.message),i;case 7:return[4,s.finish()];case 8:return e.sent(),[7];case 9:return[2]}})})},MongoManagerCollection.prototype.listIndexes=function(e,o){var t=this,r=(void 0===o&&(o=!1),resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession()),o=(!r||o||this.bypassSession||(e?e.session=r:e={session:r}),e||{}),r=(o.session,__rest(o,["session"])),n=monitor_manager_1.MonitorMongo.create("listIndexes",this.collectionName,JSON.stringify([r])),o=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).listIndexes(e);return o.on("close",function(o){return __awaiter(t,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return[4,n.finish()];case 1:return e.sent(),[2,o]}})})}),o},MongoManagerCollection.prototype.rename=function(n,s,i){return void 0===i&&(i=!1),__awaiter(this,void 0,void 0,function(){var o,t,r;return __generator(this,function(e){switch(e.label){case 0:!(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())||i||this.bypassSession||(s?s.session=o:s={session:o}),(o=s||{}).session,o=__rest(o,["session"]),t=monitor_manager_1.MonitorMongo.create("rename",this.collectionName,JSON.stringify([n,o])),e.label=1;case 1:return e.trys.push([1,3,4,6]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).rename(n,s)];case 2:return[2,e.sent()];case 3:throw r=e.sent(),console.log(new Date,"Error Rename",this.collectionName,n,o,r),r.message="Error in Rename: ".concat(this.collectionName," - ").concat(n," - ").concat(r.message),r;case 4:return[4,t.finish()];case 5:return e.sent(),[7];case 6:return[2]}})})},MongoManagerCollection.prototype.replaceOne=function(m,f,O,S,M,y,I){return void 0===S&&(S=!1),void 0===M&&(M=!1),void 0===y&&(y=null),void 0===I&&(I=!1),__awaiter(this,void 0,void 0,function(){var o,t,r,n,s,i,a,c,l,_,g,u,v,d,p,h;return __generator(this,function(e){switch(e.label){case 0:if(!this.checkSchema||M||(o=this.simplschema.newContext(),o.validate(f)))return t=new Date,this.timestamps&&(f.updatedAt=t),s=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),r=null,!s||I||this.bypassSession||(O?O.session=s:O={session:s},r={session:s}),n=(s=O||{}).session,s=__rest(s,["session"]),this.useVersions?y?[3,2]:[4,this.findOne(m,null,!0)]:[3,40];throw console.log(new Date,this.collectionName,"Schema Errors - replaceOne",o.validationErrors()),resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethod.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on replaceOne - "+this.collectionName,[o.validationErrors(),f]),o.validationErrors();case 1:y=e.sent(),e.label=2;case 2:return y?(this.timestamps&&!f.createdAt&&y.createdAt&&(f.createdAt=y.createdAt),y.__v!==f.__v?[3,15]:(f.__v+=1,(i=(0,common_1.deepCopy)(y))._id={_id:y._id,__v:y.__v},a=null,a=r?{session:n,upsert:!0}:{upsert:!0},[4,resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:y._id,__v:y.__v}},i,a)])):[3,28];case 3:return(e.sent(),4<=y.__v)?[4,resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({$and:[{"_id._id":y._id},{"_id.__v":{$lt:y.__v-4}}]},r)]:[3,5];case 4:e.sent(),e.label=5;case 5:if(this.createLogs&&!S){if("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL)return[3,6];resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:y._id,payload:(0,common_1.getBinarySize)(JSON.stringify([y,m,f,s]))<2e5?JSON.stringify([y,m,f,s],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}})}return[3,8];case 6:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:y._id,payload:(0,common_1.getBinarySize)(JSON.stringify([y,m,f,s]))<2e5?JSON.stringify([y,m,f,s],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},r)];case 7:e.sent(),e.label=8;case 8:d=monitor_manager_1.MonitorMongo.create("replaceOne",this.collectionName,JSON.stringify([m,f,s])),e.label=9;case 9:return e.trys.push([9,11,12,14]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).replaceOne(m,f,O)];case 10:if((p=e.sent()).acknowledged)return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,p.modifiedCount];throw new Error("Error in Replace One: ".concat(this.collectionName," - ").concat(JSON.stringify(p,null,2)));case 11:throw i=e.sent(),console.log(new Date,"Error Replace One",this.collectionName,m,f,s,i),i.message="Error in Replace One: ".concat(this.collectionName," - ").concat(i.message),i;case 12:return[4,d.finish()];case 13:return e.sent(),[7];case 14:return[3,27];case 15:return console.log("invalid version - "+this.collectionName,y.__v,f.__v),[4,resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).findOne({_id:{_id:y._id,__v:f.__v}},null,!0)];case 16:if(!(a=e.sent()))return[3,26];if(c=y._id,_=y.__v,(l=(0,common_1.getMongoMergeUpdatedDoc)(f,y,a))._id=c,l.__v=_+1,this.createLogs&&!S){if("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL)return[3,17];resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:c,payload:(0,common_1.getBinarySize)(JSON.stringify(["invalidVersion - merge",y,m,l,s]))<2e5?JSON.stringify(["invalidVersion - merge",y,m,l,s],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}})}return[3,19];case 17:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:c,payload:(0,common_1.getBinarySize)(JSON.stringify(["invalidVersion - merge",y,m,l,s]))<2e5?JSON.stringify(["invalidVersion - merge",y,m,l,s],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},r)];case 18:e.sent(),e.label=19;case 19:d=monitor_manager_1.MonitorMongo.create("replaceOne",this.collectionName,JSON.stringify([m,l,s])),e.label=20;case 20:return e.trys.push([20,22,23,25]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).replaceOne(m,l,O)];case 21:if((p=e.sent()).acknowledged)return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,p.modifiedCount];throw new Error("Error in Replace One: ".concat(this.collectionName," - ").concat(JSON.stringify(p,null,2)));case 22:throw _=e.sent(),console.log(new Date,"Error Replace One",this.collectionName,m,l,s,_),_.message="Error in Replace One: ".concat(this.collectionName," - ").concat(_.message),_;case 23:return[4,d.finish()];case 24:return e.sent(),[7];case 25:return[3,27];case 26:throw new Error("Error in Replcae One: Invalid Version And Could Not Find History - DB: "+y.__v+", Trying to update with :"+f.__v);case 27:return[3,39];case 28:if(!O||!O.upsert)return[3,38];if(this.timestamps&&(f.createdAt=t),f._id||(f._id=(0,common_1.objectIdHexString)()),f.__v=0,this.createLogs&&!S){if("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL)return[3,29];resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:f._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",m,f,s]))<2e5?JSON.stringify(["upsert",m,f,s],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}})}return[3,31];case 29:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:f._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",m,f,s]))<2e5?JSON.stringify(["upsert",m,f,s],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},r)];case 30:e.sent(),e.label=31;case 31:d=monitor_manager_1.MonitorMongo.create("replaceOne",this.collectionName,JSON.stringify([m,f,s])),e.label=32;case 32:return e.trys.push([32,34,35,37]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).replaceOne(m,f,O)];case 33:if((p=e.sent()).acknowledged)return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,p.modifiedCount];throw new Error("Error in Replace One: ".concat(this.collectionName," - ").concat(JSON.stringify(p,null,2)));case 34:throw g=e.sent(),console.log(new Date,"Error Replace One",this.collectionName,m,f,s,g),g.message="Error in Replace One: ".concat(this.collectionName," - ").concat(g.message),g;case 35:return[4,d.finish()];case 36:return e.sent(),[7];case 37:return[3,39];case 38:throw new Error("Error in Replace One: No Document");case 39:return[3,60];case 40:if(O&&O.upsert&&(this.timestamps&&!f.createdAt&&(f.createdAt=t),f._id||(f._id=(0,common_1.objectIdHexString)()),f.__v=0),!this.createLogs||S)return[3,54];O?O.returnDocument="before":O={returnDocument:"before"},(g=O||{}).session,u=__rest(g,["session"]),d=monitor_manager_1.MonitorMongo.create("findOneAndReplace",this.collectionName,JSON.stringify([m,f,u])),e.label=41;case 41:return e.trys.push([41,50,51,53]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndReplace(m,f,O)];case 42:return(p=e.sent(),v=p.value)?"https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?[3,43]:(resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:v._id,payload:(0,common_1.getBinarySize)(JSON.stringify([v,m,f,u]))<2e5?JSON.stringify([v,m,f,u],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}),[3,45]):[3,46];case 43:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:v._id,payload:(0,common_1.getBinarySize)(JSON.stringify([v,m,f,u]))<2e5?JSON.stringify([v,m,f,u],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},r)];case 44:e.sent(),e.label=45;case 45:return[3,49];case 46:return"https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?[3,47]:(resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:f._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",m,f,u]))<2e5?JSON.stringify(["upsert",m,f,u],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}),[3,49]);case 47:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:f._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",m,f,u]))<2e5?JSON.stringify(["upsert",m,f,u],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},r)];case 48:e.sent(),e.label=49;case 49:return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,1];case 50:throw h=e.sent(),console.log(new Date,"Error Replace One (Find One And Replace)",this.collectionName,m,f,u,h),h.message="Error in Replace One (Find One And Replace): ".concat(this.collectionName," - ").concat(h.message),h;case 51:return[4,d.finish()];case 52:return e.sent(),[7];case 53:return[3,60];case 54:d=monitor_manager_1.MonitorMongo.create("replaceOne",this.collectionName,JSON.stringify([m,f,s])),e.label=55;case 55:return e.trys.push([55,57,58,60]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).replaceOne(m,f,O)];case 56:return(p=e.sent()).acknowledged?(this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,p.modifiedCount]):[2,p.acknowledged];case 57:throw h=e.sent(),console.log(new Date,"Error Replace One",this.collectionName,m,f,s,h),h.message="Error in Aggregate: ".concat(this.collectionName," - ").concat(h.message),h;case 58:return[4,d.finish()];case 59:return e.sent(),[7];case 60:return[2]}})})},MongoManagerCollection.prototype.updateMany=function(u,v,d,p,h,m,f){return void 0===p&&(p=!1),void 0===h&&(h=!1),void 0===m&&(m=!1),void 0===f&&(f=!1),__awaiter(this,void 0,void 0,function(){var o,t,r,n,s,i,a,c,l,_,g;return __generator(this,function(e){switch(e.label){case 0:if(!(v.$inc&&"{}"!==JSON.stringify(v.$inc)||v.$set&&"{}"!==JSON.stringify(v.$set)||v.$setOnInsert&&"{}"!==JSON.stringify(v.$setOnInsert)||v.$unset&&"{}"!==JSON.stringify(v.$unset)))return[2,1];if(this.timestamps&&(t=new Date,v.$set?v.$set.updatedAt=t:v.$set={updatedAt:t}),!this.checkSchema||h||(o=this.simplschema.newContext(),o.validate(v,{modifier:!0})))return t=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),r=null,!t||f||this.bypassSession||(d?d.session=t:d={session:t},r={session:t}),n=(s=d||{}).session,s=__rest(s,["session"]),this.useVersions&&!m||this.createLogs&&!p?[4,this.find(u)]:[3,10];throw console.log(new Date,this.collectionName,"Schema Errors - updateMany",o.validationErrors()),resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethod.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on updateMany - "+this.collectionName,[o.validationErrors(),v]),o.validationErrors();case 1:i=e.sent(),a=0,e.label=2;case 2:if(!(a<i.length))return[3,10];if(c=i[a],this.createLogs&&!p){if("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL)return[3,3];resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:c._id,payload:(0,common_1.getBinarySize)(JSON.stringify([c,u,v,s]))<2e5?JSON.stringify([c,u,v,s],null,2):"Too Big",method:"updateMany",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}})}return[3,5];case 3:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:c._id,payload:(0,common_1.getBinarySize)(JSON.stringify([c,u,v,s]))<2e5?JSON.stringify([c,u,v,s],null,2):"Too Big",method:"updateMany",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},r)];case 4:e.sent(),e.label=5;case 5:return this.useVersions?((_=(0,common_1.deepCopy)(c))._id={_id:c._id,__v:c.__v},g=null,g=r?{session:n,upsert:!0}:{upsert:!0},[4,resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:c._id,__v:c.__v}},_,g)]):[3,9];case 6:return(e.sent(),4<=c.__v)?[4,resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({$and:[{"_id._id":c._id},{"_id.__v":{$lt:c.__v-4}}]},r)]:[3,8];case 7:e.sent(),e.label=8;case 8:objectContainsPropertyDeep(v,"__v")||(v.$inc?v.$inc.__v||(v.$inc.__v=1):v.$inc={__v:1}),e.label=9;case 9:return a++,[3,2];case 10:l=monitor_manager_1.MonitorMongo.create("updateMany",this.collectionName,JSON.stringify([u,v,s])),e.label=11;case 11:return e.trys.push([11,13,14,16]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).updateMany(u,v,d)];case 12:if((_=e.sent()).acknowledged)return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,_.modifiedCount];throw new Error("Error in updateMany: ".concat(_));case 13:throw g=e.sent(),console.log(new Date,"Error Update Many",this.collectionName,u,v,s,g),g.message="Error in Update Many: ".concat(this.collectionName," - ").concat(g.message),g;case 14:return[4,l.finish()];case 15:return e.sent(),[7];case 16:return[2]}})})},MongoManagerCollection.prototype.updateOne=function(g,u,v,d,p,h){return void 0===d&&(d=!1),void 0===p&&(p=!1),void 0===h&&(h=!1),__awaiter(this,void 0,void 0,function(){var o,t,r,n,s,i,a,c,l,_;return __generator(this,function(e){switch(e.label){case 0:if(!this.checkSchema||p||(o=this.simplschema.newContext(),o.validate(u,{modifier:!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)?(n=new Date,this.timestamps&&(u.$set?u.$set.updatedAt=n:u.$set={updatedAt:n}),n=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),t=null,!n||h||this.bypassSession||(v?v.session=n:v={session:n},t={session:n}),r=(n=v||{}).session,n=__rest(n,["session"]),this.useVersions?[4,this.findOne(g,null,!0)]:[3,26]):[2,1];throw console.log(new Date,this.collectionName,"Schema Errors - updateOne",o.validationErrors()),resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethod.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on updateOne - "+this.collectionName,[o.validationErrors(),u]),o.validationErrors();case 1:if(!(a=e.sent()))return[3,14];if(this.createLogs&&!d){if("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL)return[3,2];resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:a._id,payload:(0,common_1.getBinarySize)(JSON.stringify([a,g,u,n]))<2e5?JSON.stringify([a,g,u,n],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}})}return[3,4];case 2:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:a._id,payload:(0,common_1.getBinarySize)(JSON.stringify([a,g,u,n]))<2e5?JSON.stringify([a,g,u,n],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},t)];case 3:e.sent(),e.label=4;case 4:return(s=(0,common_1.deepCopy)(a))._id={_id:a._id,__v:a.__v},i=null,i=t?{session:r,upsert:!0}:{upsert:!0},[4,resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:a._id,__v:a.__v}},s,i)];case 5:return(e.sent(),4<=a.__v)?[4,resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({$and:[{"_id._id":a._id},{"_id.__v":{$lt:a.__v-4}}]},t)]:[3,7];case 6:e.sent(),e.label=7;case 7:objectContainsPropertyDeep(u,"__v")||(u.$inc?u.$inc.__v||(u.$inc.__v=1):u.$inc={__v:1}),c=monitor_manager_1.MonitorMongo.create("updateOne",this.collectionName,JSON.stringify([g,u,n])),e.label=8;case 8:return e.trys.push([8,10,11,13]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).updateOne(g,u,v)];case 9:if((l=e.sent()).acknowledged)return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,l.modifiedCount];throw new Error("Error in Update One: ".concat(this.collectionName," - ").concat(JSON.stringify(l,null,2)));case 10:throw s=e.sent(),console.log(new Date,"Error Update One",this.collectionName,g,u,n,s),s.message="Error in Update One: ".concat(this.collectionName," - ").concat(s.message),s;case 11:return[4,c.finish()];case 12:return e.sent(),[7];case 13:return[3,25];case 14:if(!v||!v.upsert)return[3,24];if(u.$setOnInsert?(u.$setOnInsert._id||(u.$setOnInsert._id=(0,common_1.objectIdHexString)()),this.timestamps&&!u.$setOnInsert.createdAt&&(u.$setOnInsert.createdAt=new Date)):this.timestamps?u.$setOnInsert={_id:(0,common_1.objectIdHexString)(),__v:0,createdAt:new Date}:u.$setOnInsert={_id:(0,common_1.objectIdHexString)(),__v:0},this.createLogs&&!d){if("https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL)return[3,15];resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:u.$setOnInsert._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",g,u,n]))<2e5?JSON.stringify(["upsert",g,u,n],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}})}return[3,17];case 15:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:u.$setOnInsert._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",g,u,n]))<2e5?JSON.stringify(["upsert",g,u,n],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},t)];case 16:e.sent(),e.label=17;case 17:c=monitor_manager_1.MonitorMongo.create("updateOne",this.collectionName,JSON.stringify([g,u,n])),e.label=18;case 18:return e.trys.push([18,20,21,23]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).updateOne(g,u,v)];case 19:if((l=e.sent()).acknowledged)return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,l.modifiedCount];throw new Error("Error in Update One: ".concat(this.collectionName," - ").concat(JSON.stringify(l,null,2)));case 20:throw i=e.sent(),console.log(new Date,"Error Update One",this.collectionName,g,u,n,i),i.message="Error in Update One: ".concat(this.collectionName," - ").concat(i.message),i;case 21:return[4,c.finish()];case 22:return e.sent(),[7];case 23:return[3,25];case 24:throw new Error("Error in Update One: No Document");case 25:return[3,48];case 26:if(v&&v.upsert&&(u.$setOnInsert?(u.$setOnInsert._id||(u.$setOnInsert._id=(0,common_1.objectIdHexString)()),this.timestamps&&!u.$setOnInsert.createdAt&&(u.$setOnInsert.createdAt=new Date)):this.timestamps?u.$setOnInsert={_id:(0,common_1.objectIdHexString)(),createdAt:new Date}:u.$setOnInsert={_id:(0,common_1.objectIdHexString)()}),!this.createLogs||d)return[3,42];v?v.returnDocument="before":v={returnDocument:"before"},c=monitor_manager_1.MonitorMongo.create("findOneAndUpdate",this.collectionName,JSON.stringify([g,u,n])),e.label=27;case 27:return e.trys.push([27,38,39,41]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndUpdate(g,u,v)];case 28:return(l=e.sent(),a=l.value)?"https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?[3,29]:(resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:a._id,payload:(0,common_1.getBinarySize)(JSON.stringify([a,g,u,n]))<2e5?JSON.stringify([a,g,u,n],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}),[3,31]):[3,32];case 29:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:a._id,payload:(0,common_1.getBinarySize)(JSON.stringify([a,g,u,n]))<2e5?JSON.stringify([a,g,u,n],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},t)];case 30:e.sent(),e.label=31;case 31:return[2,l.ok];case 32:return u.$setOnInsert?"https://resolveio.com"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL||"http://localhost:4200"===resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?[3,33]:(resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:u.$setOnInsert._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",g,u,n]))<2e5?JSON.stringify(["upsert",g,u,n],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}),[3,35]):[3,36];case 33:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:u.$setOnInsert._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",g,u,n]))<2e5?JSON.stringify(["upsert",g,u,n],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date})];case 34:e.sent(),e.label=35;case 35:return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,l.ok];case 36:return[2,0];case 37:return[3,41];case 38:throw _=e.sent(),console.log(new Date,"Error Update One (Find One And Update)",this.collectionName,g,u,n,_),_.message="Error in Update One (Find One And Update): ".concat(this.collectionName," - ").concat(_.message),_;case 39:return[4,c.finish()];case 40:return e.sent(),[7];case 41:return[3,48];case 42:c=monitor_manager_1.MonitorMongo.create("updateOne",this.collectionName,JSON.stringify([g,u,n])),e.label=43;case 43:return e.trys.push([43,45,46,48]),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).updateOne(g,u,v)];case 44:if((l=e.sent()).acknowledged)return this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[2,l.modifiedCount];throw new Error("Error in Aggregate: ".concat(this.collectionName," - ").concat(JSON.stringify(l,null,2)));case 45:throw _=e.sent(),console.log(new Date,"Error Update One",this.collectionName,g,u,n,_),_.message="Error in Aggregate: ".concat(this.collectionName," - ").concat(_.message),_;case 46:return[4,c.finish()];case 47:return e.sent(),[7];case 48:return[2]}})})},MongoManagerCollection.prototype.watchCollection=function(e,o,t){void 0===e&&(e=[]),void 0===t&&(t=!1);var r=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession();return!r||t||this.bypassSession||(o?o.session=r:o={session:r}),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).watch(e,o)},(e=MongoManagerCollection).indexQueue=async_1.default.queue(function(l,_){return __awaiter(void 0,void 0,void 0,function(){var o,t,r,n,s,i,a,c;return __generator(e,function(e){switch(e.label){case 0:if(o=l.action,t=l.collection,r=l.specs,n=l.options,!(s=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(t.collectionName,t.collectionOptions)))return[2,_(new Error("Collection ".concat(t.collectionName," not found")))];e.label=1;case 1:return e.trys.push([1,3,,10]),[4,s[o].apply(s,__spreadArray(__spreadArray([],__read(r),!1),[n],!1))];case 2:return a=e.sent(),_(null,a),[3,10];case 3:if(85!==(i=e.sent()).code&&!i.message.includes("IndexOptionsConflict"))return[3,9];e.label=4;case 4:return(e.trys.push([4,8,,9]),c=t.extractIndexNameFromError(i.message))?[4,s.dropIndex(c)]:[3,7];case 5:return e.sent(),[4,s[o].apply(s,__spreadArray(__spreadArray([],__read(r),!1),[n],!1))];case 6:return a=e.sent(),_(null,a),[2];case 7:return[3,9];case 8:return c=e.sent(),_(c),[3,9];case 9:return _(i),[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.create=function(e){var o=new MongoManagerUserCollection;return o.initialize(e),o},MongoManagerUserCollection.prototype.authenticate=function(r,n){return __awaiter(this,void 0,void 0,function(){var o,t;return __generator(this,function(e){switch(e.label){case 0:return(o=Math.pow(100,Math.log(r.attempts+1)),o=o<3e5?o:3e5,r.last)?Date.now()-r.last.getTime()<o?(r.last=new Date,[4,user_collection_1.Users.updateOne({_id:r._id},{$set:{last:r.last}})]):[3,2]:[3,3];case 1:return e.sent(),[2,{data:null,error:"Attempt Too Soon"}];case 2:return[3,4];case 3:r.last=new Date,e.label=4;case 4:if(5<=r.attempts)return[2,{data:null,error:"Too Many Attempts"}];if(!r.salt)return[2,{data:null,error:"No Salt Value Stored"}];e.label=5;case 5:return e.trys.push([5,11,,12]),[4,pbkdf2Promisified(n,r.salt,{iterations:25e3,keylen:512,digestAlgorithm:"sha256"})];case 6:return(o=e.sent(),scmp(o,Buffer.from(r.hash,"hex")))?(r.last=new Date,r.attempts=0,[4,user_collection_1.Users.updateOne({_id:r._id},{$set:{last:r.last,attempts:r.attempts}})]):[3,8];case 7:return e.sent(),[2,{data:r,error:""}];case 8:return r.last=new Date,r.attempts=r.attempts+1,[4,user_collection_1.Users.updateOne({_id:r._id},{$set:{last:r.last,attempts:r.attempts}})];case 9:return e.sent(),5<=r.attempts?[2,{data:null,error:"Too Many Attempts"}]:[2,{data:null,error:"Invalid Username And Password"}];case 10:return[3,12];case 11:throw(t=e.sent()).message="Error in User Authenticate: ".concat(JSON.stringify(r,null,2)," - ").concat(t.message),t;case 12:return[2]}})})},MongoManagerUserCollection.prototype.serializeUser=function(){return function(e,o){o(null,e.username)}},MongoManagerUserCollection.prototype.deserializeUser=function(){var e=this;return function(o){return __awaiter(e,void 0,void 0,function(){return __generator(this,function(e){return[2,user_collection_1.Users.findOne({username:o})]})})}},MongoManagerUserCollection.prototype.setPassword=function(r,n){return __awaiter(this,void 0,void 0,function(){var o,t;return __generator(this,function(e){switch(e.label){case 0:if(r)return[3,1];throw new Error("Error In User Set Password: No User");case 1:if(n)return[3,2];throw new Error("Error In User Set Password: No Password");case 2:return[4,randomBytes(32)];case 3:o=e.sent(),o=o.toString("hex"),e.label=4;case 4:return e.trys.push([4,6,,7]),[4,pbkdf2Promisified(n,o,{iterations:25e3,keylen:512,digestAlgorithm:"sha256"})];case 5:return t=e.sent(),t=Buffer.from(t,"binary").toString("hex"),[2,user_collection_1.Users.updateOne({_id:r._id},{$set:{hash:t,salt:o,services:{},attempts:0}})];case 6:throw(t=e.sent()).message="Error in User Set Password: ".concat(JSON.stringify(r,null,2)," - ").concat(t.message),t;case 7:return[2]}})})},MongoManagerUserCollection.prototype.changePassword=function(t,r,n){return __awaiter(this,void 0,void 0,function(){var o;return __generator(this,function(e){switch(e.label){case 0:if(t)return[3,1];throw new Error("Error in User Change Password: Missing User");case 1:if(r&&n)return[3,2];throw new Error("Error in User Change Password: Missing Password");case 2:return[4,this.authenticate(t,r)];case 3:if((o=e.sent()).data)return[2,this.setPassword(t,n)];throw o.error;case 4:return[2]}})})},MongoManagerUserCollection.prototype.register=function(o,t){return __awaiter(this,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:if(o.username)return[3,1];throw new Error("Error in User Register: Missing Username");case 1:return[4,user_collection_1.Users.findOne({username:o.username})];case 2:if(e.sent())throw new Error("Error in User Register: Username Exists");return[3,3];case 3:return[4,user_collection_1.Users.findOne({email:o.email})];case 4:if(e.sent())throw new Error("Error in User Register: Email Exists");return o.setPassword(t),[2,o];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 pbkdf2(e,o,t,r){crypto.pbkdf2(e,o,t.iterations,t.keylen,t.digestAlgorithm,r)}function pbkdf2Promisified(e,o,n){return new Promise(function(t,r){return pbkdf2(e,o,n,function(e,o){return e?r(e):t(o)})})}function randomBytes(e){return new Promise(function(t,r){return crypto.randomBytes(e,function(e,o){return e?r(e):t(o)})})}function objectContainsPropertyDeep(e,o){var t=Object.keys(e);if(t.some(function(e){return e===o}))return!0;for(var r=0;r<t.length;r++){var n=t[r];if(e[n]&&"object"==typeof e[n]&&objectContainsPropertyDeep(e[n],o))return!0}return!1}exports.MongoManagerUserCollection=MongoManagerUserCollection;
2
2
  //# sourceMappingURL=mongo.manager.js.map