@resolveio/server-lib 20.7.1 → 20.7.2

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 n=function(e,o){return(n=Object.setPrototypeOf||({__proto__:[]}instanceof Array?function(e,o){e.__proto__=o}:function(e,o){for(var n in o)Object.prototype.hasOwnProperty.call(o,n)&&(e[n]=o[n])}))(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}n(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,n=1,t=arguments.length;n<t;n++)for(var r in o=arguments[n])Object.prototype.hasOwnProperty.call(o,r)&&(e[r]=o[r]);return e}).apply(this,arguments)},__awaiter=this&&this.__awaiter||function(e,t,r,i){return new(r=r||Promise)(function(n,o){function fulfilled(e){try{step(i.next(e))}catch(e){o(e)}}function rejected(e){try{step(i.throw(e))}catch(e){o(e)}}function step(e){var o;e.done?n(e.value):((o=e.value)instanceof r?o:new r(function(e){e(o)})).then(fulfilled,rejected)}step((i=i.apply(e,t||[])).next())})},__generator=this&&this.__generator||function(t,r){var i,s,a,l={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]},c={next:verb(0),throw:verb(1),return:verb(2)};return"function"==typeof Symbol&&(c[Symbol.iterator]=function(){return this}),c;function verb(n){return function(e){var o=[n,e];if(i)throw new TypeError("Generator is already executing.");for(;l=c&&o[c=0]?0:l;)try{if(i=1,s&&(a=2&o[0]?s.return:o[0]?s.throw||((a=s.return)&&a.call(s),0):s.next)&&!(a=a.call(s,o[1])).done)return a;switch(s=0,(o=a?[2&o[0],a.value]:o)[0]){case 0:case 1:a=o;break;case 4:return l.label++,{value:o[1],done:!1};case 5:l.label++,s=o[1],o=[0];continue;case 7:o=l.ops.pop(),l.trys.pop();continue;default:if(!(a=0<(a=l.trys).length&&a[a.length-1])&&(6===o[0]||2===o[0])){l=0;continue}if(3===o[0]&&(!a||o[1]>a[0]&&o[1]<a[3]))l.label=o[1];else if(6===o[0]&&l.label<a[1])l.label=a[1],a=o;else{if(!(a&&l.label<a[2])){a[2]&&l.ops.pop(),l.trys.pop();continue}l.label=a[2],l.ops.push(o)}}o=r.call(t,l)}catch(e){o=[6,e],s=0}finally{i=a=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}}},__read=this&&this.__read||function(e,o){var n="function"==typeof Symbol&&e[Symbol.iterator];if(!n)return e;var t,r,i=n.call(e),s=[];try{for(;(void 0===o||0<o--)&&!(t=i.next()).done;)s.push(t.value)}catch(e){r={error:e}}finally{try{t&&!t.done&&(n=i.return)&&n.call(i)}finally{if(r)throw r.error}}return s},__spreadArray=this&&this.__spreadArray||function(e,o,n){if(n||2===arguments.length)for(var t,r=0,i=o.length;r<i;r++)!t&&r in o||((t=t||Array.prototype.slice.call(o,0,r))[r]=o[r]);return e.concat(t||Array.prototype.slice.call(o))},__values=this&&this.__values||function(e){var o="function"==typeof Symbol&&Symbol.iterator,n=o&&e[o],t=0;if(n)return n.call(e);if(e&&"number"==typeof e.length)return{next:function(){return{value:(e=e&&t>=e.length?void 0:e)&&e[t++],done:!e}}};throw new TypeError(o?"Object is not iterable.":"Symbol.iterator is not defined.")},simpl_schema_1=(Object.defineProperty(exports,"__esModule",{value:!0}),exports.MongoManagerUserCollection=exports.MongoManagerCollection=exports.MongoManagerModel=exports.MongoManager=void 0,require("simpl-schema")),resolveio_server_app_1=require("../resolveio-server-app"),user_collection_1=require("../collections/user.collection"),common_1=require("../util/common"),NodeCache=require("node-cache"),crypto=require("crypto"),scmp=require("scmp"),monitor_manager_1=require("./monitor.manager"),os_1=require("os"),numCPUs=(0,os_1.cpus)().length,v8=require("v8"),async_1=require("async"),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,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()}return MongoManager.prototype.withSession=function(i){return __awaiter(this,void 0,void 0,function(){var o,n,t,r=this;return __generator(this,function(e){switch(e.label){case 0:(o=resolveio_server_app_1.ResolveIOServer.getMongoConnection().startSession()).startTransaction(),e.label=1;case 1:return e.trys.push([1,4,6,7]),[4,asyncLocalStorage.run(o,function(){return __awaiter(r,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return[4,i()];case 1:return[2,e.sent()]}})})})];case 2:return n=e.sent(),[4,o.commitTransaction()];case 3:return e.sent(),[2,n];case 4:return t=e.sent(),[4,o.abortTransaction()];case 5:throw e.sent(),t;case 6:return o.endSession(),[7];case 7:return[2]}})})},MongoManager.prototype.getSession=function(){return asyncLocalStorage.getStore()},MongoManager.prototype.setCacheLimit=function(){this._isWorkersEnabled?this._heapLimit=this._isWorkerInstance?.8*this._heapSize:.4*this._heapSize:this._heapLimit=.3*this._heapSize},MongoManager.prototype.initServerCollections=function(){return __awaiter(this,void 0,void 0,function(){var o;return __generator(this,function(e){switch(e.label){case 0:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().listCollections().toArray()];case 1:return o=e.sent(),this._serverCollections=o,[2]}})})},MongoManager.prototype.getServerCollections=function(){return this._serverCollections},MongoManager.prototype.registerCollection=function(o){var e=this;o.collectionOptions&&o.collectionOptions.timeseries&&o.collectionOptions.timeseries.timeField&&this._serverCollections.some(function(e){return e.name===o.collectionName&&"collection"===e.type})&&resolveio_server_app_1.ResolveIOServer.getMainDB().dropCollection(o.collectionName).then(function(){e.createCollection(o)}),this._collections.push(o)},MongoManager.prototype.createCollection=function(e){resolveio_server_app_1.ResolveIOServer.getMainDB().createCollection(e.collectionName,e.collectionOptions)},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(r,i,s){return void 0===i&&(i={}),__awaiter(this,void 0,void 0,function(){var o,n,t=this;return __generator(this,function(e){return o=this.generateCacheKey([r],"find",[i,s]),void 0!==(n=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",r),[2,n]):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",r),[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",r),n=this.executeFind(r,i,s,o),this._operationInProgress.set(o,{promise:n,invalidatedDuringExecution:!1,collections:[r]}),n.finally(function(){resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo Find - Done",r),t._operationInProgress.delete(o)}),[2,n])})})},MongoManager.prototype.executeFind=function(t,r,i,s){return __awaiter(this,void 0,void 0,function(){var o,n;return __generator(this,function(e){switch(e.label){case 0:return o=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(t),n=new monitor_manager_1.MonitorMongo("find",t,JSON.stringify([r,i])),[4,o.find(r,i).toArray()];case 1:return o=e.sent(),n.finish(),this._operationInProgress.get(s).invalidatedDuringExecution||this.addToCache([t],s,o),[2,o]}})})},MongoManager.prototype.findOne=function(r,i,s){return void 0===i&&(i={}),__awaiter(this,void 0,void 0,function(){var o,n,t=this;return __generator(this,function(e){return o=this.generateCacheKey([r],"findOne",[i,s]),void 0!==(n=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",r),[2,n]):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",r),[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",r),n=this.executeFindOne(r,i,s,o),this._operationInProgress.set(o,{promise:n,invalidatedDuringExecution:!1,collections:[r]}),n.finally(function(){resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo FindOne - Done",r),t._operationInProgress.delete(o)}),[2,n])})})},MongoManager.prototype.executeFindOne=function(t,r,i,s){return __awaiter(this,void 0,void 0,function(){var o,n;return __generator(this,function(e){switch(e.label){case 0:return o=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(t),n=new monitor_manager_1.MonitorMongo("findOne",t,JSON.stringify([r,i])),[4,o.findOne(r,i)];case 1:return o=e.sent(),n.finish(),this._operationInProgress.get(s).invalidatedDuringExecution||this.addToCache([t],s,o),[2,o]}})})},MongoManager.prototype.aggregate=function(i,s,a){return __awaiter(this,void 0,void 0,function(){var o,n,t,r=this;return __generator(this,function(e){return o=__spreadArray([i],__read(s.flatMap(function(e){var o;return null!=(o=e.$lookup)&&o.from?[e.$lookup.from]:[]})),!1),n=this.generateCacheKey(o,"aggregate",[s,a]),void 0!==(t=this.getFromCache(n))?(resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo Agg - Cache",o),[2,t]):this._operationInProgress.has(n)?(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(n).promise]):(resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo Agg - Running",o),t=this.executeAggregate(i,s,a,n,o),this._operationInProgress.set(n,{promise:t,invalidatedDuringExecution:!1,collections:o}),t.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),r._operationInProgress.delete(n)}),[2,t])})})},MongoManager.prototype.executeAggregate=function(t,r,i,s,a){return __awaiter(this,void 0,void 0,function(){var o,n;return __generator(this,function(e){switch(e.label){case 0:return o=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(t),n=new monitor_manager_1.MonitorMongo("aggregate",t,JSON.stringify([r,i])),[4,o.aggregate(r,i).toArray()];case 1:return o=e.sent(),n.finish(),this._operationInProgress.get(s).invalidatedDuringExecution||this.addToCache(a,s,o),[2,o]}})})},MongoManager.prototype.countDocuments=function(r,i,s){return void 0===i&&(i={}),__awaiter(this,void 0,void 0,function(){var o,n,t=this;return __generator(this,function(e){return o=this.generateCacheKey([r],"countDocuments",[i,s]),void 0!==(n=this.getFromCache(o))?[2,n]:this._operationInProgress.has(o)?[2,this._operationInProgress.get(o).promise]:(n=this.executeCountDocuments(r,i,s,o),this._operationInProgress.set(o,{promise:n,invalidatedDuringExecution:!1,collections:[r]}),n.finally(function(){return t._operationInProgress.delete(o)}),[2,n])})})},MongoManager.prototype.executeCountDocuments=function(t,r,i,s){return __awaiter(this,void 0,void 0,function(){var o,n;return __generator(this,function(e){switch(e.label){case 0:return o=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(t),n=new monitor_manager_1.MonitorMongo("countDocuments",t,JSON.stringify([r,i])),[4,o.countDocuments(r,i)];case 1:return o=e.sent(),n.finish(),this._operationInProgress.get(s).invalidatedDuringExecution||this.addToCache([t],s,o),[2,o]}})})},MongoManager.prototype.generateCacheKey=function(e,o,n){e=JSON.stringify({collections:e,functionName:o,args:n});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,n){var t,r;if((0,common_1.getBinarySize)(JSON.stringify(n))<1e6&&!e.includes("logs")&&!e.find(function(e){return e.endsWith(".versions")})&&!e.find(function(e){return e.startsWith("monitor-")})){var i=this._nodeCache.getStats().vsize;if(i>this._heapLimit){var s=0,a=this._nodeCache.keys();try{for(var l=__values(a),c=l.next();!c.done;c=l.next()){var _=c.value;if(this._nodeCache.del(_),s+=1,(i=this._nodeCache.getStats().vsize)<.75*this._heapLimit)break}}catch(e){t={error:e}}finally{try{c&&!c.done&&(r=l.return)&&r.call(l)}finally{if(t)throw t.error}}console.log("Query Cache: Too Big, - Deleted: "+s+" - "+i)}this._nodeCache.set(o,JSON.stringify(n)),this._cacheMap.push({collections:e,key:o})}},MongoManager.prototype.invalidateQueryCache=function(o){var n,e,t,r,i=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 s=__values(i),a=s.next();!a.done;a=s.next()){var l=a.value;this._nodeCache.del(l.key),this._operationInProgress.has(l.key)&&(this._operationInProgress.get(l.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",l.collections)}}catch(e){n={error:e}}finally{try{a&&!a.done&&(e=s.return)&&e.call(s)}finally{if(n)throw n.error}}try{for(var c=__values(this._operationInProgress.entries()),_=c.next();!_.done;_=c.next()){var g=__read(_.value,2),v=(g[0],g[1]);v.collections.includes(o)&&(v.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)",v.collections)}}catch(e){t={error:e}}finally{try{_&&!_.done&&(r=c.return)&&r.call(c)}finally{if(t)throw t.error}}this._cacheMap=this._cacheMap.filter(function(e){return!e.collections.includes(o)})},MongoManager.prototype.setupChangeStream=function(){var o=this,n=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"});n.on("change",function(n){return __awaiter(o,void 0,void 0,function(){var o;return __generator(this,function(e){return n.ns&&n.ns.coll&&(o=n.ns.coll,this.collection(o))&&this.invalidateQueryCache(o),[2]})})}),n.on("error",function(e){console.log(new Date,"Mongo change stream error. Restart..."),n.close(),setTimeout(function(){return o.setupChangeStream()},5e3)}),n.on("close",function(){console.log(new Date,"Mongo change stream closed. Restarting..."),setTimeout(function(){return o.setupChangeStream()},5e3)})},MongoManager}(),MongoManagerModel=(exports.MongoManager=MongoManager,function(e){var o;this.collection_main=null,this.collection_version=null,"users"===e.collectionName?this.collection_main=new MongoManagerUserCollection(e):this.collection_main=new MongoManagerCollection(e),e.useVersionCollection&&(this.collection_main.useVersions=!0,(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=new MongoManagerUserCollection(o):this.collection_version=new MongoManagerCollection(o),this.collection_version.createIndex({"_id._id":1,"_id.__v":1}),this.collection_main.versionCollection=e.collectionName+".versions")}),MongoManagerCollection=(exports.MongoManagerModel=MongoManagerModel,function(){function MongoManagerCollection(e){var o,n=this,t=(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.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,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(){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&&(resolveio_server_app_1.ResolveIOServer.getMongoManager().registerCollection(n),clearInterval(t))},1))}var e;return 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(r,i,s,a){var l=this;return void 0===s&&(s=!1),void 0===a&&(a=!1),new Promise(function(o,n){var t,e=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession();e&&!a&&(i?i.session=e:i={session:e}),s||l.skipCache?(t=new monitor_manager_1.MonitorMongo("aggregate",l.collectionName,JSON.stringify([r,i])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(l.collectionName,l.collectionOptions).aggregate(r,i).toArray().then(function(e){t.finish(),o(e)},function(e){t.finish(),console.log(new Date,"Error Aggregate",l.collectionName,r,i,e),n(e)})):resolveio_server_app_1.ResolveIOServer.getMongoManager().aggregate(l.collectionName,r,i).then(function(e){o(e)},function(e){n(e)})})},MongoManagerCollection.prototype.aggregateCount=function(i,s,a){return void 0===a&&(a=!1),__awaiter(this,void 0,void 0,function(){var r=this;return __generator(this,function(e){return[2,new Promise(function(o,n){var e=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),t=(e&&!a&&(s?s.session=e:s={session:e}),new monitor_manager_1.MonitorMongo("aggregateCount",r.collectionName,JSON.stringify([i,s])));resolveio_server_app_1.ResolveIOServer.getMainDB().collection(r.collectionName,r.collectionOptions).aggregate(i,s).toArray().then(function(e){t.finish(),o(e.length)},function(e){t.finish(),console.log(new Date,"Error Aggregate",r.collectionName,i,s,e),n(0)})})]})})},MongoManagerCollection.prototype.aggregateCursor=function(e,o,n){void 0===n&&(n=!1);var t=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),r=(t&&!n&&(o?o.session=t:o={session:t}),new monitor_manager_1.MonitorMongo("aggregateCursor",this.collectionName,JSON.stringify([e,o]))),n=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).aggregate(e,o);return n.on("close",function(e){return r.finish(),e}),n},MongoManagerCollection.prototype.aggregateStream=function(e,o,n,t){void 0===t&&(t=!1);var r=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),i=(r&&!t&&(o?o.session=r:o={session:r}),new monitor_manager_1.MonitorMongo("aggregateStream",this.collectionName,JSON.stringify([e,o,n]))),t=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).aggregate(e,o).stream(n);return t.on("end",function(e){return i.finish(),e}).on("error",function(e){return i.finish(),e}).on("close",function(e){return i.finish(),e}),t},MongoManagerCollection.prototype.bulkWrite=function(N,R,C,D,b,B){return void 0===C&&(C=!1),void 0===D&&(D=!1),void 0===b&&(b=!1),void 0===B&&(B=!1),__awaiter(this,void 0,void 0,function(){var o,n,t,r,i,s,a,l,c,_,g,v,d,u,p,m,h,f,O,S,M,y,I,w;return __generator(this,function(e){switch(e.label){case 0:if(!N.length)return[2,null];o=1e3,n=0,t=null,(r=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())&&!B&&(R?R.session=r:R={session:r},t={session:r}),e.label=1;case 1:if(!(n<N.length))return[3,19];i=N.slice(n,n+o),s=[],a=[];try{for(I=void 0,l=__values(i),c=l.next();!c.done;c=l.next()){if(_=c.value,g=Object.keys(_)[0],v=_[g],this.checkSchema&&!C){if(d=this.simplschema.newContext(),u=void 0,"insertOne"===g)u=d.validate(v.document);else if("replaceOne"===g)u=d.validate(v.replacement);else if("updateOne"===g||"updateMany"===g)u=d.validate(v.update,{modifier:!0});else{if("deleteOne"!==g&&"deleteMany"!==g)throw new Error("Unsupported operation type: ".concat(g));u=!0}if(!u)throw new Error("Schema validation failed for ".concat(g,": ").concat(JSON.stringify(d.validationErrors())))}if("insertOne"===g)this.timestamps&&(v.document.createdAt||(v.document.createdAt=new Date),v.document.updatedAt||(v.document.updatedAt=new Date)),!D&&this.createLogs&&s.push({insertOne:{document:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:v.document._id||(0,common_1.objectIdHexString)(),payload:(0,common_1.getBinarySize)(JSON.stringify(v.document))<2e5?JSON.stringify(v.document,null,2):"Too Big",method:"insertOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}}),!b&&this.useVersions&&(p=__assign(__assign({},v.document),{__v:0,_id:{_id:v.document._id||(0,common_1.objectIdHexString)(),__v:0}}),a.push({insertOne:{document:p}})),_[g].document=v.document;else if("updateOne"===g||"updateMany"===g)this.timestamps&&(v.update.$set||(v.update.$set={}),v.update.$set.updatedAt=new Date),!b&&this.useVersions&&(v.update.$inc||(v.update.$inc={}),v.update.$inc.__v=1),!D&&this.createLogs&&s.push({insertOne:{document:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:v.filter._id||null,payload:(0,common_1.getBinarySize)(JSON.stringify(v.update))<2e5?JSON.stringify(v.update,null,2):"Too Big",method:g,id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}}),!b&&this.useVersions&&a.push({filter:v.filter});else if("replaceOne"===g)this.timestamps&&(v.replacement.updatedAt=new Date),!b&&this.useVersions&&(v.replacement.__v=(v.replacement.__v||0)+1),!D&&this.createLogs&&s.push({insertOne:{document:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:v.filter._id||null,payload:(0,common_1.getBinarySize)(JSON.stringify(v.replacement))<2e5?JSON.stringify(v.replacement,null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}}),!b&&this.useVersions&&a.push({filter:v.filter}),_[g].replacement=v.replacement;else{if("deleteOne"!==g&&"deleteMany"!==g)throw new Error("Unsupported operation type: ".concat(g));!D&&this.createLogs&&s.push({insertOne:{document:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:v.filter._id||null,payload:(0,common_1.getBinarySize)(JSON.stringify(v.filter))<2e5?JSON.stringify(v.filter,null,2):"Too Big",method:g,id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}}),!b&&this.useVersions&&a.push({filter:v.filter})}}}catch(e){I={error:e}}finally{try{c&&!c.done&&(w=l.return)&&w.call(l)}finally{if(I)throw I.error}}if(m=[],!(!b&&this.useVersions&&0<a.length))return[3,7];e.label=2;case 2:return e.trys.push([2,6,,7]),h=a.map(function(e){return e.filter}),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName).find({$or:h},t).toArray()];case 3:return(m=e.sent(),0<(h=m.map(function(e){return{insertOne:{document:__assign(__assign({},e),{_id:{_id:e._id,__v:e.__v}})}}})).length)?(O=null,O=t?{session:r,ordered:!1}:{ordered:!1},[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.versionCollection).bulkWrite(h,O)]):[3,5];case 4:e.sent(),e.label=5;case 5:return[3,7];case 6:return S=e.sent(),console.error("Error during version creation:",S),[3,7];case 7:return f=new monitor_manager_1.MonitorMongo("bulkWrite",this.collectionName,JSON.stringify([i,R])),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName).bulkWrite(i,R)];case 8:if(e.sent(),f.finish(),!D&&this.createLogs&&0<s.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,9];resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLogs(s.map(function(e){return{type:"log",data:e}}))}return[3,11];case 9:return O=null,O=t?{session:r,ordered:!1}:{ordered:!1},[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").bulkWrite(s,O)];case 10:e.sent(),e.label=11;case 11:if(b||!this.useVersions)return[3,17];e.label=12;case 12:return e.trys.push([12,16,,17]),S=m.map(function(e){return e._id}),M=[{$match:{"_id._id":{$in:S}}},{$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(M,t).toArray()];case 13:return 0<(M=e.sent()).length?[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.versionCollection).deleteMany({_id:{$in:M.map(function(e){return e._id})}},t)]:[3,15];case 14:e.sent(),e.label=15;case 15:return[3,17];case 16:return y=e.sent(),console.error("Error during version cleanup:",y),[3,17];case 17:return n+=o,[4,new Promise(function(e){return setImmediate(e)})];case 18:return e.sent(),this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[3,1];case 19:return[2,{ok:1}]}})})},MongoManagerCollection.prototype.countDocuments=function(a,l,c,_){var e=this;return void 0===a&&(a={}),void 0===c&&(c=!1),void 0===_&&(_=!1),new Promise(function(i,s){return __awaiter(e,void 0,void 0,function(){var o,n,t,r=this;return __generator(this,function(e){switch(e.label){case 0:return(e.trys.push([0,4,,5]),(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())&&!_&&(l?l.session=o:l={session:o}),0===Object.keys(a).length)?[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).estimatedDocumentCount(l)]:[3,2];case 1:return o=e.sent(),i(o),[3,3];case 2:c||this.skipCache?(n=new monitor_manager_1.MonitorMongo("countDocuments",this.collectionName,JSON.stringify([a,l])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).countDocuments(a,l).then(function(e){n.finish(),i(e)},function(e){n.finish(),console.log(new Date,"Error Count Documents",r.collectionName,a,l,e),s(e)})):resolveio_server_app_1.ResolveIOServer.getMongoManager().countDocuments(this.collectionName,a,l).then(function(e){return i(e)},function(e){return s(e)}),e.label=3;case 3:return[3,5];case 4:return t=e.sent(),console.log(new Date,"Error Count Documents (Stats)",this.collectionName,a,l,t),s(t),[3,5];case 5: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(r,i){var e=this;return new Promise(function(n,t){return __awaiter(e,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(r)}))?[2,n("Index already exists")]:(MongoManagerCollection.indexQueue.push({action:"createIndex",collection:this,specs:[r],options:i},function(e,o){e?t(e):n(o)}),[3,3]);case 2:return o=e.sent(),t(o),[3,3];case 3:return[2]}})})})},MongoManagerCollection.prototype.createIndexes=function(i,s){var e=this;return new Promise(function(t,r){return __awaiter(e,void 0,void 0,function(){var n,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(n=e.sent(),0===(o=i.filter(function(o){return!n.some(function(e){return JSON.stringify(e.key)===JSON.stringify(o.key)})})).length)?[2,t(["All indexes already exist"])]:(MongoManagerCollection.indexQueue.push({action:"createIndexes",collection:this,specs:[o],options:s},function(e,o){e?r(e):t(o)}),[3,3]);case 2:return o=e.sent(),r(o),[3,3];case 3:return[2]}})})})},MongoManagerCollection.prototype.deleteMany=function(v,d,u,p){var e=this;return void 0===v&&(v={}),void 0===u&&(u=!1),void 0===p&&(p=!1),new Promise(function(_,g){return __awaiter(e,void 0,void 0,function(){var o,n,t,r,i,s,a,l,c=this;return __generator(this,function(e){switch(e.label){case 0:return(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),n=null,o&&!p&&(d?d.session=o:d={session:o},n={session:o}),this.createLogs&&!u||this.useVersions)?[4,this.find(v)]:[3,2];case 1:for(t=e.sent(),r=0;r<t.length;r++)i=t[r],this.createLogs&&!u&&("https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?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,v,d]))<2e5?JSON.stringify([i,v,d],null,2):"Too Big",method:"deleteMany",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):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,v,d]))<2e5?JSON.stringify([i,v,d],null,2):"Too Big",method:"deleteMany",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},n)),this.useVersions&&((s=(0,common_1.deepCopy)(i))._id={_id:i._id,__v:i.__v},a=null,a=n?{session:o,upsert:!0}:{upsert:!0},resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:i._id,__v:i.__v}},s,a),resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({$and:[{"_id._id":i._id},{"_id.__v":{$lt:i.__v-1}}]},n));e.label=2;case 2:return l=new monitor_manager_1.MonitorMongo("deleteMany",this.collectionName,JSON.stringify([v,d])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).deleteMany(v,d).then(function(e){l.finish(),e.acknowledged?(c.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(c.collectionName),_(e.deletedCount)):g(e.acknowledged)},function(e){l.finish(),console.log(new Date,"Error Delete Many",c.collectionName,v,d,e),g(e)}),[2]}})})})},MongoManagerCollection.prototype.deleteOne=function(c,_,g,r){var e=this;return void 0===c&&(c={}),void 0===g&&(g=!1),void 0===r&&(r=!1),new Promise(function(l,t){return __awaiter(e,void 0,void 0,function(){var i,s,a,o,n=this;return __generator(this,function(e){return i=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),s=null,i&&!r&&(_?_.session=i:_={session:i},s={session:i}),this.createLogs&&!g?(a=new monitor_manager_1.MonitorMongo("findOneAndDelete",this.collectionName,JSON.stringify([c,_])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndDelete(c,_).then(function(r){return __awaiter(n,void 0,void 0,function(){var o,n,t;return __generator(this,function(e){return a.finish(),(o=r.value)?(this.createLogs&&!g&&("https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:o._id,payload:(0,common_1.getBinarySize)(JSON.stringify([o,c,_]))<2e5?JSON.stringify([o,c,_],null,2):"Too Big",method:"deleteOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:o._id,payload:(0,common_1.getBinarySize)(JSON.stringify([o,c,_]))<2e5?JSON.stringify([o,c,_],null,2):"Too Big",method:"deleteOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},s)),this.useVersions&&((n=(0,common_1.deepCopy)(o))._id={_id:o._id,__v:o.__v},t=null,t=s?{session:i,upsert:!0}:{upsert:!0},resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:o._id,__v:o.__v}},n,t),resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({$and:[{"_id._id":o._id},{"_id.__v":{$lt:o.__v-1}}]},s)),this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),l(1)):l(0),[2]})})},function(e){a.finish(),console.log(new Date,"Error Find One And Delete",n.collectionName,c,_,e),t(e)})):(o=new monitor_manager_1.MonitorMongo("deleteOne",this.collectionName,JSON.stringify([c,_])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).deleteOne(c,_).then(function(e){o.finish(),e.acknowledged?(n.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(n.collectionName),l(e.deletedCount)):t(e.acknowledged)},function(e){o.finish(),console.log(new Date,"Error Delete One",n.collectionName,c,_,e),t(e)})),[2]})})})},MongoManagerCollection.prototype.distinct=function(r,i,s,a){var l=this;return void 0===a&&(a=!1),new Promise(function(o,n){var e=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),t=(e&&!a&&(s?s.session=e:s={session:e}),new monitor_manager_1.MonitorMongo("distinct",l.collectionName,JSON.stringify([r,i,s])));resolveio_server_app_1.ResolveIOServer.getMainDB().collection(l.collectionName,l.collectionOptions).distinct(r,i,s).then(function(e){t.finish(),o(e)},function(e){t.finish(),console.log(new Date,"Error Distinct",l.collectionName,i,s,e),n(e)})})},MongoManagerCollection.prototype.drop=function(r,i){var s=this;return void 0===i&&(i=!1),new Promise(function(o,n){var e=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),t=(e&&!i&&(r?r.session=e:r={session:e}),new monitor_manager_1.MonitorMongo("drop",s.collectionName,JSON.stringify([r])));resolveio_server_app_1.ResolveIOServer.getMainDB().collection(s.collectionName,s.collectionOptions).drop(r).then(function(e){t.finish(),o(e)},function(e){t.finish(),console.log(new Date,"Error Count Documents",s.collectionName,r,e),n(e)})})},MongoManagerCollection.prototype.dropIndex=function(r,i,s){var a=this;return void 0===s&&(s=!1),new Promise(function(o,n){var e=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),t=(e&&!s&&(i?i.session=e:i={session:e}),new monitor_manager_1.MonitorMongo("dropIndex",a.collectionName,JSON.stringify([r,i])));resolveio_server_app_1.ResolveIOServer.getMainDB().collection(a.collectionName,a.collectionOptions).dropIndex(r,i).then(function(e){t.finish(),o(e)},function(e){t.finish(),console.log(new Date,"Error Drop Index",a.collectionName,r,i,e),n(e)})})},MongoManagerCollection.prototype.dropIndexes=function(r,i){var s=this;return void 0===i&&(i=!1),new Promise(function(o,n){var e=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),t=(e&&!i&&(r?r.session=e:r={session:e}),new monitor_manager_1.MonitorMongo("dropIndexes",s.collectionName,JSON.stringify([r])));resolveio_server_app_1.ResolveIOServer.getMainDB().collection(s.collectionName,s.collectionOptions).dropIndexes(r).then(function(e){t.finish(),o(e)},function(e){t.finish(),console.log(new Date,"Error Drop Indexes",s.collectionName,r,e),n(e)})})},MongoManagerCollection.prototype.find=function(s,a,l,c){var e=this;return void 0===s&&(s={}),void 0===l&&(l=!1),void 0===c&&(c=!1),new Promise(function(r,i){return __awaiter(e,void 0,void 0,function(){var o,n,t=this;return __generator(this,function(e){return(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())&&!c&&(a?a.session=o:a={session:o}),l||this.skipCache?(n=new monitor_manager_1.MonitorMongo("find",this.collectionName,JSON.stringify([s,a])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).find(s,a).toArray().then(function(e){n.finish(),r(e)},function(e){n.finish(),console.log(new Date,"Error Find",t.collectionName,s,a,e),i(e)})):resolveio_server_app_1.ResolveIOServer.getMongoManager().find(this.collectionName,s,a).then(function(e){r(e)},function(e){i(e)}),[2]})})})},MongoManagerCollection.prototype.findById=function(r,i,s,a){var l=this;return void 0===s&&(s=!1),void 0===a&&(a=!1),new Promise(function(o,n){var t,e=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession();e&&!a&&(i?i.session=e:i={session:e}),s||l.skipCache?(t=new monitor_manager_1.MonitorMongo("findById",l.collectionName,JSON.stringify([{_id:r},i])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(l.collectionName,l.collectionOptions).findOne({_id:r},i).then(function(e){t.finish(),o(e)},function(e){t.finish(),console.log(new Date,"Error Find By Id",l.collectionName,{_id:r},i,e),n(e)})):resolveio_server_app_1.ResolveIOServer.getMongoManager().findOne(l.collectionName,{_id:r},i).then(function(e){o(e)},function(e){n(e)})})},MongoManagerCollection.prototype.findCount=function(r,i,s){var a=this;return void 0===r&&(r={}),void 0===s&&(s=!1),new Promise(function(o,n){var e=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),t=(e&&!s&&(i?i.session=e:i={session:e}),new monitor_manager_1.MonitorMongo("findCount",a.collectionName,JSON.stringify([r,i])));resolveio_server_app_1.ResolveIOServer.getMainDB().collection(a.collectionName,a.collectionOptions).find(r,i).count().then(function(e){t.finish(),o(e)},function(e){t.finish(),console.log(new Date,"Error Find Count",a.collectionName,r,i,e),n(e)})})},MongoManagerCollection.prototype.findCursor=function(e,o,n){void 0===e&&(e={}),void 0===n&&(n=!1);var t=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),r=(t&&!n&&(o?o.session=t:o={session:t}),new monitor_manager_1.MonitorMongo("findCursor",this.collectionName,JSON.stringify([e,o]))),i=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).find(e,o);return i.on("close",function(e){return r.finish(),i.removeAllListeners(),e}),i},MongoManagerCollection.prototype.findStream=function(e,o,n,t){void 0===e&&(e={}),void 0===t&&(t=!1);var r=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),i=(r&&!t&&(o?o.session=r:o={session:r}),new monitor_manager_1.MonitorMongo("findStream",this.collectionName,JSON.stringify([e,o,n]))),t=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).find(e,o).stream(n);return t.on("end",function(e){return i.finish(),e}).on("error",function(e){return i.finish(),e}).on("close",function(e){return i.finish(),e}),t},MongoManagerCollection.prototype.findOne=function(r,i,s,a){var l=this;return void 0===r&&(r={}),void 0===s&&(s=!1),void 0===a&&(a=!1),new Promise(function(o,n){var t,e=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession();e&&!a&&(i?i.session=e:i={session:e}),s||l.skipCache?(t=new monitor_manager_1.MonitorMongo("findOne",l.collectionName,JSON.stringify([r,i])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(l.collectionName,l.collectionOptions).findOne(r,i).then(function(e){t.finish(),o(e)},function(e){t.finish(),console.log(new Date,"Error Find One",l.collectionName,r,i,e),n(e)})):resolveio_server_app_1.ResolveIOServer.getMongoManager().findOne(l.collectionName,r,i).then(function(e){o(e)},function(e){n(e)})})},MongoManagerCollection.prototype.findOneAndDelete=function(l,c,_,n){var e=this;return void 0===l&&(l={}),void 0===_&&(_=!1),void 0===n&&(n=!1),new Promise(function(a,o){return __awaiter(e,void 0,void 0,function(){var t,r,i,s=this;return __generator(this,function(e){return t=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),r=null,t&&!n&&(c?c.session=t:c={session:t},r={session:t}),i=new monitor_manager_1.MonitorMongo("findOneAndDelete",this.collectionName,JSON.stringify([l,c])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndDelete(l,c).then(function(e){i.finish();var o,n,e=e.value;e?(s.createLogs&&!_&&("https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:s.collectionName,id_document:e._id,payload:(0,common_1.getBinarySize)(JSON.stringify([e,l,c]))<2e5?JSON.stringify([e,l,c],null,2):"Too Big",method:"findOneAndDelete",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:s.collectionName,id_document:e._id,payload:(0,common_1.getBinarySize)(JSON.stringify([e,l,c]))<2e5?JSON.stringify([e,l,c],null,2):"Too Big",method:"findOneAndDelete",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},r)),s.useVersions&&((o=(0,common_1.deepCopy)(e))._id={_id:e._id,__v:e.__v},n=null,n=r?{session:t,upsert:!0}:{upsert:!0},resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(s.versionCollection).replaceOne({_id:{_id:e._id,__v:e.__v}},o,n)),s.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(s.collectionName),a(e)):a(null)},function(e){i.finish(),console.log(new Date,"Error Find One And Delete",s.collectionName,l,c,e),o(e)}),[2]})})})},MongoManagerCollection.prototype.findOneAndReplace=function(c,_,g,v,d,u){var e=this;return void 0===c&&(c={}),void 0===v&&(v=!1),void 0===d&&(d=!1),void 0===u&&(u=!1),new Promise(function(a,l){return __awaiter(e,void 0,void 0,function(){var o,n,t,r,i,s=this;return __generator(this,function(e){return!this.checkSchema||d||(o=this.simplschema.newContext(),o.validate(_))?(n=new Date,this.timestamps&&(_.updatedAt=n),g&&g.upsert&&(_._id||(_._id=(0,common_1.objectIdHexString)()),this.useVersions&&(_.__v=0),this.timestamps)&&!_.createdAt&&(_.createdAt=n),t=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),r=null,t&&!u&&(g?g.session=t:g={session:t},r={session:t}),i=new monitor_manager_1.MonitorMongo("findOneAndReplace",this.collectionName,JSON.stringify([c,_,g])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndReplace(c,_,g).then(function(e){i.finish();var o,n,e=e.value;e?(s.createLogs&&!v&&("https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:s.collectionName,id_document:e._id,payload:(0,common_1.getBinarySize)(JSON.stringify([e,c,_,g]))<2e5?JSON.stringify([e,c,_,g],null,2):"Too Big",method:"findOneAndReplace",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:s.collectionName,id_document:e._id,payload:(0,common_1.getBinarySize)(JSON.stringify([e,c,_,g]))<2e5?JSON.stringify([e,c,_,g],null,2):"Too Big",method:"findOneAndReplace",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},r)),s.useVersions&&((o=(0,common_1.deepCopy)(e))._id={_id:e._id,__v:e.__v},n=null,n=r?{session:t,upsert:!0}:{upsert:!0},resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(s.versionCollection).replaceOne({_id:{_id:e._id,__v:e.__v}},o,n)),s.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(s.collectionName),a(e)):a(null)},function(e){i.finish(),console.log(new Date,"Error Find One And Replace",s.collectionName,c,g,e),l(e)})):(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(),_]),l(o.validationErrors())),[2]})})})},MongoManagerCollection.prototype.findOneAndUpdate=function(c,_,g,v,d,u){var e=this;return void 0===c&&(c={}),void 0===v&&(v=!1),void 0===d&&(d=!1),void 0===u&&(u=!1),new Promise(function(a,l){return __awaiter(e,void 0,void 0,function(){var o,n,t,r,i,s=this;return __generator(this,function(e){return!this.checkSchema||d||(o=this.simplschema.newContext(),o.validate(_,{modifier:!0}))?(this.timestamps&&(n=new Date,_.$set?_.$set.updatedAt=n:_.$set={updatedAt:n}),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)()}),t=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),r=null,t&&!u&&(g?g.session=t:g={session:t},r={session:t}),i=new monitor_manager_1.MonitorMongo("findOneAndUpdate",this.collectionName,JSON.stringify([c,_,g])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndUpdate(c,_,g).then(function(e){i.finish();var o,n,e=e.value;e?(s.createLogs&&!v&&("https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:s.collectionName,id_document:e._id,payload:(0,common_1.getBinarySize)(JSON.stringify([e,c,_,g]))<2e5?JSON.stringify([e,c,_,g],null,2):"Too Big",method:"findOneAndUpdate",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:s.collectionName,id_document:e._id,payload:(0,common_1.getBinarySize)(JSON.stringify([e,c,_,g]))<2e5?JSON.stringify([e,c,_,g],null,2):"Too Big",method:"findOneAndUpdate",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},r)),s.useVersions&&((o=(0,common_1.deepCopy)(e))._id={_id:e._id,__v:e.__v},n=null,n=r?{session:t,upsert:!0}:{upsert:!0},resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(s.versionCollection).replaceOne({_id:{_id:e._id,__v:e.__v}},o,n)),s.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(s.collectionName),a(e)):a(null)},function(e){i.finish(),console.log(new Date,"Error Find One And Update",s.collectionName,c,g,e),l(e)})):(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(),_]),l(o.validationErrors())),[2]})})})},MongoManagerCollection.prototype.indexes=function(r,i){var s=this;return void 0===i&&(i=!1),new Promise(function(o,n){var e=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),t=(e&&!i&&(r?r.session=e:r={session:e}),new monitor_manager_1.MonitorMongo("indexes",s.collectionName,JSON.stringify([r])));resolveio_server_app_1.ResolveIOServer.getMainDB().collection(s.collectionName,s.collectionOptions).indexes(r).then(function(e){t.finish(),o(e)},function(e){t.finish(),console.log(new Date,"Error Indexes",s.collectionName,r,e),n(e)})})},MongoManagerCollection.prototype.indexExists=function(r,i,s){var a=this;return void 0===s&&(s=!1),new Promise(function(o,n){var e=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),t=(e&&!s&&(i?i.session=e:i={session:e}),new monitor_manager_1.MonitorMongo("indexExists",a.collectionName,JSON.stringify([r,i])));resolveio_server_app_1.ResolveIOServer.getMainDB().collection(a.collectionName,a.collectionOptions).indexExists(r,i).then(function(e){t.finish(),o(e)},function(e){t.finish(),console.log(new Date,"Error Index Exists",a.collectionName,r,i,e),n(e)})})},MongoManagerCollection.prototype.insertMany=function(v,d,u,p,m,h){var e=this;return void 0===u&&(u=!1),void 0===p&&(p=!1),void 0===m&&(m=!1),void 0===h&&(h=!1),new Promise(function(_,g){return __awaiter(e,void 0,void 0,function(){var n,o,t,r,i,s,a,l,c=this;return __generator(this,function(e){if(v.length){for(n=[],this.checkSchema&&!p&&v.forEach(function(e){var o=c.simplschema.newContext();o.validate(e)?n.push(!0):(console.log(new Date,c.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 - "+c.collectionName,[o.validationErrors(),e]),n.push(!1))}),o=this.checkSchema?v.filter(function(e,o){return n[o]}):v,t=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),r=null,t&&!h&&(d?d.session=t:d={session:t},r={session:t}),i=0;i<o.length;i++)(s=o[i])._id||(s._id=(0,common_1.objectIdHexString)()),this.timestamps&&(a=new Date,s.createdAt||(s.createdAt=a),s.updatedAt||(s.updatedAt=a)),this.useVersions&&!s.hasOwnProperty("__v")&&(s.__v=0),this.createLogs&&!u&&("https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?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,d]))<2e5?JSON.stringify([s,d],null,2):"Too Big",method:"insertMany",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):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,d]))<2e5?JSON.stringify([s,d],null,2):"Too Big",method:"insertMany",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},r));o.length&&(l=null,m||(l=new monitor_manager_1.MonitorMongo("insertMany",this.collectionName,JSON.stringify([o,d]))),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).insertMany(o,d).then(function(e){l&&l.finish(),e.acknowledged?(c.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(c.collectionName),_(o)):g(e.acknowledged)},function(e){l&&l.finish(),console.log(new Date,"Error Insert Many",c.collectionName,o,d,e),g(e)}))}else _([]);return[2]})})})},MongoManagerCollection.prototype.insertOne=function(l,c,_,g,v){var e=this;return void 0===_&&(_=!1),void 0===g&&(g=!1),void 0===v&&(v=!1),new Promise(function(s,a){return __awaiter(e,void 0,void 0,function(){var o,n,t,r,i=this;return __generator(this,function(e){return l._id||(l._id=(0,common_1.objectIdHexString)()),!this.checkSchema||g||(o=this.simplschema.newContext(),o.validate(l))?(this.timestamps&&(n=new Date,l.createdAt||(l.createdAt=n),l.updatedAt||(l.updatedAt=n)),n=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),t=null,n&&!v&&(c?c.session=n:c={session:n},t={session:n}),this.createLogs&&!_&&("https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:l._id,payload:(0,common_1.getBinarySize)(JSON.stringify([l,c]))<2e5?JSON.stringify([l,c],null,2):"Too Big",method:"insertOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:l._id,payload:(0,common_1.getBinarySize)(JSON.stringify([l,c]))<2e5?JSON.stringify([l,c],null,2):"Too Big",method:"insertOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},t)),this.useVersions&&!l.hasOwnProperty("__v")&&(l.__v=0),r=new monitor_manager_1.MonitorMongo("insertOne",this.collectionName,JSON.stringify([l,c])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).insertOne(l,c).then(function(e){r.finish(),e.acknowledged?(i.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(i.collectionName),s(l)):a(e.acknowledged)},function(e){r.finish(),console.log(new Date,"Error Insert One",i.collectionName,l,c,e),a(e)})):(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(),l]),a(o.validationErrors())),[2]})})})},MongoManagerCollection.prototype.listIndexes=function(e,o){void 0===o&&(o=!1);var n=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),t=(n&&!o&&(e?e.session=n:e={session:n}),new monitor_manager_1.MonitorMongo("listIndexes",this.collectionName,JSON.stringify([e]))),o=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).listIndexes(e);return o.on("close",function(e){return t.finish(),e}),o},MongoManagerCollection.prototype.rename=function(r,i,s){var a=this;return void 0===s&&(s=!1),new Promise(function(o,n){var e=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),t=(e&&!s&&(i?i.session=e:i={session:e}),new monitor_manager_1.MonitorMongo("rename",a.collectionName,JSON.stringify([r,i])));resolveio_server_app_1.ResolveIOServer.getMainDB().collection(a.collectionName,a.collectionOptions).rename(r,i).then(function(e){t.finish(),o(e)},function(e){t.finish(),console.log(new Date,"Error Rename",a.collectionName,r,i,e),n(e)})})},MongoManagerCollection.prototype.replaceOne=function(h,f,O,S,M,y,I){var e=this;return void 0===S&&(S=!1),void 0===M&&(M=!1),void 0===y&&(y=null),void 0===I&&(I=!1),new Promise(function(p,m){return __awaiter(e,void 0,void 0,function(){var o,n,t,r,i,s,a,l,c,_,g,v,d,u=this;return __generator(this,function(e){switch(e.label){case 0:return!this.checkSchema||M||(r=this.simplschema.newContext(),r.validate(f))?(o=new Date,this.timestamps&&(f.updatedAt=o),n=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),t=null,n&&!I&&(O?O.session=n:O={session:n},t={session:n}),this.useVersions?y?[3,2]:[4,this.findOne(h,null,!0)]:[3,8]):(console.log(new Date,this.collectionName,"Schema Errors - replaceOne",r.validationErrors()),resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethod.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on replaceOne - "+this.collectionName,[r.validationErrors(),f]),m(r.validationErrors()),[2]);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,3]:(f.__v+=1,(r=(0,common_1.deepCopy)(y))._id={_id:y._id,__v:y.__v},s=null,s=t?{session:n,upsert:!0}:{upsert:!0},resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:y._id,__v:y.__v}},r,s),4<=y.__v&&resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({$and:[{"_id._id":y._id},{"_id.__v":{$lt:y.__v-4}}]},t),this.createLogs&&!S&&("https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?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,h,f,O]))<2e5?JSON.stringify([y,h,f,O],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):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,h,f,O]))<2e5?JSON.stringify([y,h,f,O],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},t)),i=new monitor_manager_1.MonitorMongo("replaceOne",this.collectionName,JSON.stringify([h,f,O])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).replaceOne(h,f,O).then(function(e){i.finish(),e.acknowledged?(u.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(u.collectionName),p(e.modifiedCount)):m(e.acknowledged)},function(e){i.finish(),console.log(new Date,"Error Replace One",u.collectionName,h,f,O,e),m(e)}),[3,5])):[3,6];case 3: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 4:(s=e.sent())?(a=y._id,l=y.__v,(c=(0,common_1.getMongoMergeUpdatedDoc)(f,y,s))._id=a,c.__v=l+1,this.createLogs&&!S&&("https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:a,payload:(0,common_1.getBinarySize)(JSON.stringify(["invalidVersion - merge",y,h,c,O]))<2e5?JSON.stringify(["invalidVersion - merge",y,h,c,O],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:a,payload:(0,common_1.getBinarySize)(JSON.stringify(["invalidVersion - merge",y,h,c,O]))<2e5?JSON.stringify(["invalidVersion - merge",y,h,c,O],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},t)),_=new monitor_manager_1.MonitorMongo("replaceOne",this.collectionName,JSON.stringify([h,c,O])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).replaceOne(h,c,O).then(function(e){_.finish(),e.acknowledged?(u.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(u.collectionName),p(e.modifiedCount)):m(e.acknowledged)},function(e){_.finish(),console.log(new Date,"Error Replace One",u.collectionName,h,c,O,e),m(e)})):m("Invalid Version And Could Not Find History - DB: "+y.__v+", Trying to update with :"+f.__v),e.label=5;case 5:return[3,7];case 6:O&&O.upsert?(this.timestamps&&(f.createdAt=o),f._id||(f._id=(0,common_1.objectIdHexString)()),f.__v=0,this.createLogs&&!S&&("https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?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",h,f,O]))<2e5?JSON.stringify(["upsert",h,f,O],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):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",h,f,O]))<2e5?JSON.stringify(["upsert",h,f,O],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},t)),g=new monitor_manager_1.MonitorMongo("replaceOne",this.collectionName,JSON.stringify([h,f,O])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).replaceOne(h,f,O).then(function(e){g.finish(),e.acknowledged?(u.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(u.collectionName),p(e.modifiedCount)):m(e.acknowledged)},function(e){g.finish(),console.log(new Date,"Error Replace One",u.collectionName,h,f,O,e),m(e)})):m("No Document"),e.label=7;case 7:return[3,9];case 8:O&&O.upsert&&(this.timestamps&&!f.createdAt&&(f.createdAt=o),f._id||(f._id=(0,common_1.objectIdHexString)()),f.__v=0),this.createLogs&&!S?(O?O.returnDocument="before":O={returnDocument:"before"},v=new monitor_manager_1.MonitorMongo("findOneAndReplace",this.collectionName,JSON.stringify([h,f,O])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndReplace(h,f,O).then(function(e){v.finish();e=e.value;e?"https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:u.collectionName,id_document:e._id,payload:(0,common_1.getBinarySize)(JSON.stringify([e,h,f,O]))<2e5?JSON.stringify([e,h,f,O],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:u.collectionName,id_document:e._id,payload:(0,common_1.getBinarySize)(JSON.stringify([e,h,f,O]))<2e5?JSON.stringify([e,h,f,O],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},t):"https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:u.collectionName,id_document:f._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",h,f,O]))<2e5?JSON.stringify(["upsert",h,f,O],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:u.collectionName,id_document:f._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",h,f,O]))<2e5?JSON.stringify(["upsert",h,f,O],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},t),u.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(u.collectionName),p(1)},function(e){v.finish(),console.log(new Date,"Error Find One And Replace",u.collectionName,h,f,O,e),m(e)})):(d=new monitor_manager_1.MonitorMongo("replaceOne",this.collectionName,JSON.stringify([h,f,O])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).replaceOne(h,f,O).then(function(e){d.finish(),e.acknowledged?(u.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(u.collectionName),p(e.modifiedCount)):m(e.acknowledged)},function(e){d.finish(),console.log(new Date,"Error Replace One",u.collectionName,h,f,O,e),m(e)})),e.label=9;case 9:return[2]}})})})},MongoManagerCollection.prototype.updateMany=function(d,u,p,m,h,f,O){var e=this;return void 0===m&&(m=!1),void 0===h&&(h=!1),void 0===f&&(f=!1),void 0===O&&(O=!1),new Promise(function(g,v){return __awaiter(e,void 0,void 0,function(){var o,n,t,r,i,s,a,l,c,_=this;return __generator(this,function(e){switch(e.label){case 0:return u.$inc&&"{}"!==JSON.stringify(u.$inc)||u.$set&&"{}"!==JSON.stringify(u.$set)||u.$setOnInsert&&"{}"!==JSON.stringify(u.$setOnInsert)||u.$unset&&"{}"!==JSON.stringify(u.$unset)?(this.timestamps&&(o=new Date,u.$set?u.$set.updatedAt=o:u.$set={updatedAt:o}),!this.checkSchema||h||(o=this.simplschema.newContext(),o.validate(u,{modifier:!0}))?(n=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),t=null,n&&!O&&(p?p.session=n:p={session:n},t={session:n}),this.useVersions&&!f||this.createLogs&&!m?[4,this.find(d)]:[3,2]):(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(),u]),v(o.validationErrors()),[2])):(g(1),[2]);case 1:for(r=e.sent(),i=0;i<r.length;i++)s=r[i],this.createLogs&&!m&&("https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?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,d,u,p]))<2e5?JSON.stringify([s,d,u,p],null,2):"Too Big",method:"updateMany",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):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,d,u,p]))<2e5?JSON.stringify([s,d,u,p],null,2):"Too Big",method:"updateMany",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},t)),this.useVersions&&((a=(0,common_1.deepCopy)(s))._id={_id:s._id,__v:s.__v},l=null,l=t?{session:n,upsert:!0}:{upsert:!0},resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:s._id,__v:s.__v}},a,l),4<=s.__v&&resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({$and:[{"_id._id":s._id},{"_id.__v":{$lt:s.__v-4}}]},t),objectContainsPropertyDeep(u,"__v")||(u.$inc?u.$inc.__v||(u.$inc.__v=1):u.$inc={__v:1}));e.label=2;case 2:return c=new monitor_manager_1.MonitorMongo("updateMany",this.collectionName,JSON.stringify([d,u,p])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).updateMany(d,u,p).then(function(e){c.finish(),e.acknowledged?(_.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(_.collectionName),g(e.modifiedCount)):v(e.acknowledged)},function(e){c.finish(),console.log(new Date,"Error Update Many",_.collectionName,d,u,p,e),v(e)}),[2]}})})})},MongoManagerCollection.prototype.updateOne=function(d,u,p,m,h,f){var e=this;return void 0===m&&(m=!1),void 0===h&&(h=!1),void 0===f&&(f=!1),new Promise(function(g,v){return __awaiter(e,void 0,void 0,function(){var o,n,t,r,i,s,a,l,c,_=this;return __generator(this,function(e){switch(e.label){case 0:return!this.checkSchema||h||(t=this.simplschema.newContext(),t.validate(u,{modifier:!0}))?u.$inc&&"{}"!==JSON.stringify(u.$inc)||u.$set&&"{}"!==JSON.stringify(u.$set)||u.$setOnInsert&&"{}"!==JSON.stringify(u.$setOnInsert)||u.$unset&&"{}"!==JSON.stringify(u.$unset)?(o=new Date,this.timestamps&&(u.$set?u.$set.updatedAt=o:u.$set={updatedAt:o}),o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),n=null,o&&!f&&(p?p.session=o:p={session:o},n={session:o}),this.useVersions?[4,this.findOne(d,null,!0)]:[3,2]):(g(1),[2]):(console.log(new Date,this.collectionName,"Schema Errors - updateOne",t.validationErrors()),resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethod.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on updateOne - "+this.collectionName,[t.validationErrors(),u]),v(t.validationErrors()),[2]);case 1:return(t=e.sent())?(this.createLogs&&!m&&("https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:t._id,payload:(0,common_1.getBinarySize)(JSON.stringify([t,d,u,p]))<2e5?JSON.stringify([t,d,u,p],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:t._id,payload:(0,common_1.getBinarySize)(JSON.stringify([t,d,u,p]))<2e5?JSON.stringify([t,d,u,p],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},n)),(r=(0,common_1.deepCopy)(t))._id={_id:t._id,__v:t.__v},i=null,i=n?{session:o,upsert:!0}:{upsert:!0},resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:t._id,__v:t.__v}},r,i),4<=t.__v&&resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({$and:[{"_id._id":t._id},{"_id.__v":{$lt:t.__v-4}}]},n),objectContainsPropertyDeep(u,"__v")||(u.$inc?u.$inc.__v||(u.$inc.__v=1):u.$inc={__v:1}),s=new monitor_manager_1.MonitorMongo("updateOne",this.collectionName,JSON.stringify([d,u,p])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).updateOne(d,u,p).then(function(e){s.finish(),e.acknowledged?(_.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(_.collectionName),g(e.modifiedCount)):v(e.acknowledged)},function(e){s.finish(),console.log(new Date,"Error Update One",_.collectionName,d,u,p,e),v(e)})):p&&p.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)(),__v:0,createdAt:new Date}:u.$setOnInsert={_id:(0,common_1.objectIdHexString)(),__v:0},this.createLogs&&!m&&("https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?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",d,u,p]))<2e5?JSON.stringify(["upsert",d,u,p],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):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",d,u,p]))<2e5?JSON.stringify(["upsert",d,u,p],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},n)),a=new monitor_manager_1.MonitorMongo("updateOne",this.collectionName,JSON.stringify([d,u,p])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).updateOne(d,u,p).then(function(e){a.finish(),e.acknowledged?(_.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(_.collectionName),g(e.modifiedCount)):v(e.acknowledged)},function(e){a.finish(),console.log(new Date,"Error Update One",_.collectionName,d,u,p,e),v(e)})):v("No Document"),[3,3];case 2:p&&p.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&&!m?(p?p.returnDocument="before":p={returnDocument:"before"},l=new monitor_manager_1.MonitorMongo("findOneAndUpdate",this.collectionName,JSON.stringify([d,u,p])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndUpdate(d,u,p).then(function(e){l.finish();var o=e.value;o?("https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:_.collectionName,id_document:o._id,payload:(0,common_1.getBinarySize)(JSON.stringify([o,d,u,p]))<2e5?JSON.stringify([o,d,u,p],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:_.collectionName,id_document:o._id,payload:(0,common_1.getBinarySize)(JSON.stringify([o,d,u,p]))<2e5?JSON.stringify([o,d,u,p],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},n),g(e.ok)):u.$setOnInsert?("https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:_.collectionName,id_document:u.$setOnInsert._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",d,u,p]))<2e5?JSON.stringify(["upsert",d,u,p],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:_.collectionName,id_document:u.$setOnInsert._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",d,u,p]))<2e5?JSON.stringify(["upsert",d,u,p],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}),_.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(_.collectionName),g(e.ok)):g(0)},function(e){l.finish(),console.log(new Date,"Error Find One And Update",_.collectionName,d,u,p,e),v(e)})):(c=new monitor_manager_1.MonitorMongo("updateOne",this.collectionName,JSON.stringify([d,u,p])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).updateOne(d,u,p).then(function(e){c.finish(),e.acknowledged?(_.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(_.collectionName),g(e.modifiedCount)):v(e.acknowledged)},function(e){c.finish(),console.log(new Date,"Error Update One",_.collectionName,d,u,p,e),v(e)})),e.label=3;case 3:return[2]}})})})},MongoManagerCollection.prototype.watchCollection=function(e,o,n){void 0===e&&(e=[]),void 0===n&&(n=!1);var t=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession();return t&&!n&&(o?o.session=t:o={session:t},0),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).watch(e,o)},(e=MongoManagerCollection).indexQueue=async_1.default.queue(function(c,_){return __awaiter(void 0,void 0,void 0,function(){var o,n,t,r,i,s,a,l;return __generator(e,function(e){switch(e.label){case 0:if(o=c.action,n=c.collection,t=c.specs,r=c.options,!(i=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(n.collectionName,n.collectionOptions)))return[2,_(new Error("Collection ".concat(n.collectionName," not found")))];e.label=1;case 1:return e.trys.push([1,3,,10]),[4,i[o].apply(i,__spreadArray(__spreadArray([],__read(t),!1),[r],!1))];case 2:return a=e.sent(),_(null,a),[3,10];case 3:if(85!==(s=e.sent()).code&&!s.message.includes("IndexOptionsConflict"))return[3,9];e.label=4;case 4:return(e.trys.push([4,8,,9]),l=n.extractIndexNameFromError(s.message))?[4,i.dropIndex(l)]:[3,7];case 5:return e.sent(),[4,i[o].apply(i,__spreadArray(__spreadArray([],__read(t),!1),[r],!1))];case 6:return a=e.sent(),_(null,a),[2];case 7:return[3,9];case 8:return l=e.sent(),_(l),[3,9];case 9:return _(s),[3,10];case 10:return[2]}})})},1),MongoManagerCollection}()),MongoManagerUserCollection=function(e){function MongoManagerUserCollection(){return null!==e&&e.apply(this,arguments)||this}return __extends(MongoManagerUserCollection,e),MongoManagerUserCollection.prototype.authenticate=function(s,n){var e=this;return new Promise(function(r,i){return __awaiter(e,void 0,void 0,function(){var o,t=this;return __generator(this,function(e){switch(e.label){case 0:return(o=Math.pow(100,Math.log(s.attempts+1)),o=o<3e5?o:3e5,s.last)?Date.now()-s.last.getTime()<o?(s.last=new Date,[4,user_collection_1.Users.updateOne({_id:s._id},{$set:{last:s.last}})]):[3,2]:[3,3];case 1:return e.sent(),r({data:null,error:"Attempt Too Soon"}),[2];case 2:return[3,4];case 3:s.last=new Date,e.label=4;case 4:return 5<=s.attempts?r({data:null,error:"Too Many Attempts"}):s.salt?pbkdf2(n,s.salt,{iterations:25e3,keylen:512,digestAlgorithm:"sha256"},function(o,n){return __awaiter(t,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return o?[2,i(o)]:scmp(n,Buffer.from(s.hash,"hex"))?(s.last=new Date,s.attempts=0,[4,user_collection_1.Users.updateOne({_id:s._id},{$set:{last:s.last,attempts:s.attempts}})]):[3,2];case 1:return e.sent(),r({data:s,error:""}),[3,4];case 2:return s.last=new Date,s.attempts=s.attempts+1,[4,user_collection_1.Users.updateOne({_id:s._id},{$set:{last:s.last,attempts:s.attempts}})];case 3:e.sent(),5<=s.attempts?r({data:null,error:"Too Many Attempts"}):r({data:null,error:"Invalid Username And Password"}),e.label=4;case 4:return[2]}})})}):r({data:null,error:"No Salt Value Stored"}),[2]}})})})},MongoManagerUserCollection.prototype.serializeUser=function(){return function(e,o){o(null,e.username)}},MongoManagerUserCollection.prototype.deserializeUser=function(){return function(e,o){user_collection_1.Users.findOne({username:e}).then(function(e){o(null,e)},function(e){o(e,null)})}},MongoManagerUserCollection.prototype.setPassword=function(i,s){var e=this;return new Promise(function(t,r){return __awaiter(e,void 0,void 0,function(){var o,n;return __generator(this,function(e){switch(e.label){case 0:return i?[3,1]:(r("No User"),[3,5]);case 1:return s?[3,2]:(r("No Password"),[3,5]);case 2:return[4,randomBytes(32)];case 3:return o=e.sent(),o=o.toString("hex"),[4,pbkdf2Promisified(s,o,{iterations:25e3,keylen:512,digestAlgorithm:"sha256"})];case 4:n=e.sent(),n=Buffer.from(n,"binary").toString("hex"),user_collection_1.Users.updateOne({_id:i._id},{$set:{hash:n,salt:o,services:{},attempts:0}}).then(function(e){return t(e)},function(e){return r(e)}),e.label=5;case 5:return[2]}})})})},MongoManagerUserCollection.prototype.changePassword=function(r,i,s){var e=this;return new Promise(function(n,t){return __awaiter(e,void 0,void 0,function(){var o;return __generator(this,function(e){switch(e.label){case 0:return r?[3,1]:(t("Missing User"),[3,4]);case 1:return i&&s?[3,2]:(t("Missing Password"),[3,4]);case 2:return[4,this.authenticate(r,i)];case 3:(o=e.sent()).data?this.setPassword(r,s).then(function(e){return n(e)},function(e){return t(e)}):t(o.error),e.label=4;case 4:return[2]}})})})},MongoManagerUserCollection.prototype.register=function(t,r){var e=this;return new Promise(function(o,n){return __awaiter(e,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return t.username?[3,1]:(n("Missing Username"),[3,5]);case 1:return[4,user_collection_1.Users.findOne({username:t.username})];case 2:return e.sent()?(n("Username Exists"),[3,5]):[3,3];case 3:return[4,user_collection_1.Users.findOne({email:t.email})];case 4:e.sent()?n("Email Exists"):(t.setPassword(r),o(t)),e.label=5;case 5:return[2]}})})})},MongoManagerUserCollection.prototype.resetAttempts=function(e){return user_collection_1.Users.updateOne({_id:e._id},{$set:{attempts:0}})},MongoManagerUserCollection}(exports.MongoManagerCollection=MongoManagerCollection);function pbkdf2(e,o,n,t){crypto.pbkdf2(e,o,n.iterations,n.keylen,n.digestAlgorithm,t)}function pbkdf2Promisified(e,o,r){return new Promise(function(n,t){return pbkdf2(e,o,r,function(e,o){return e?t(e):n(o)})})}function randomBytes(e){return new Promise(function(n,t){return crypto.randomBytes(e,function(e,o){return e?t(e):n(o)})})}function objectContainsPropertyDeep(e,o){var n=Object.keys(e);if(n.some(function(e){return e===o}))return!0;for(var t=0;t<n.length;t++){var r=n[t];if(e[r]&&"object"==typeof e[r]&&objectContainsPropertyDeep(e[r],o))return!0}return!1}exports.MongoManagerUserCollection=MongoManagerUserCollection;
1
+ "use strict";var __extends=this&&this.__extends||function(){var n=function(e,o){return(n=Object.setPrototypeOf||({__proto__:[]}instanceof Array?function(e,o){e.__proto__=o}:function(e,o){for(var n in o)Object.prototype.hasOwnProperty.call(o,n)&&(e[n]=o[n])}))(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}n(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,n=1,t=arguments.length;n<t;n++)for(var r in o=arguments[n])Object.prototype.hasOwnProperty.call(o,r)&&(e[r]=o[r]);return e}).apply(this,arguments)},__awaiter=this&&this.__awaiter||function(e,t,r,i){return new(r=r||Promise)(function(n,o){function fulfilled(e){try{step(i.next(e))}catch(e){o(e)}}function rejected(e){try{step(i.throw(e))}catch(e){o(e)}}function step(e){var o;e.done?n(e.value):((o=e.value)instanceof r?o:new r(function(e){e(o)})).then(fulfilled,rejected)}step((i=i.apply(e,t||[])).next())})},__generator=this&&this.__generator||function(t,r){var i,s,a,l={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]},c={next:verb(0),throw:verb(1),return:verb(2)};return"function"==typeof Symbol&&(c[Symbol.iterator]=function(){return this}),c;function verb(n){return function(e){var o=[n,e];if(i)throw new TypeError("Generator is already executing.");for(;l=c&&o[c=0]?0:l;)try{if(i=1,s&&(a=2&o[0]?s.return:o[0]?s.throw||((a=s.return)&&a.call(s),0):s.next)&&!(a=a.call(s,o[1])).done)return a;switch(s=0,(o=a?[2&o[0],a.value]:o)[0]){case 0:case 1:a=o;break;case 4:return l.label++,{value:o[1],done:!1};case 5:l.label++,s=o[1],o=[0];continue;case 7:o=l.ops.pop(),l.trys.pop();continue;default:if(!(a=0<(a=l.trys).length&&a[a.length-1])&&(6===o[0]||2===o[0])){l=0;continue}if(3===o[0]&&(!a||o[1]>a[0]&&o[1]<a[3]))l.label=o[1];else if(6===o[0]&&l.label<a[1])l.label=a[1],a=o;else{if(!(a&&l.label<a[2])){a[2]&&l.ops.pop(),l.trys.pop();continue}l.label=a[2],l.ops.push(o)}}o=r.call(t,l)}catch(e){o=[6,e],s=0}finally{i=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 n={};for(r in e)Object.prototype.hasOwnProperty.call(e,r)&&o.indexOf(r)<0&&(n[r]=e[r]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols)for(var t=0,r=Object.getOwnPropertySymbols(e);t<r.length;t++)o.indexOf(r[t])<0&&Object.prototype.propertyIsEnumerable.call(e,r[t])&&(n[r[t]]=e[r[t]]);return n},__read=this&&this.__read||function(e,o){var n="function"==typeof Symbol&&e[Symbol.iterator];if(!n)return e;var t,r,i=n.call(e),s=[];try{for(;(void 0===o||0<o--)&&!(t=i.next()).done;)s.push(t.value)}catch(e){r={error:e}}finally{try{t&&!t.done&&(n=i.return)&&n.call(i)}finally{if(r)throw r.error}}return s},__spreadArray=this&&this.__spreadArray||function(e,o,n){if(n||2===arguments.length)for(var t,r=0,i=o.length;r<i;r++)!t&&r in o||((t=t||Array.prototype.slice.call(o,0,r))[r]=o[r]);return e.concat(t||Array.prototype.slice.call(o))},__values=this&&this.__values||function(e){var o="function"==typeof Symbol&&Symbol.iterator,n=o&&e[o],t=0;if(n)return n.call(e);if(e&&"number"==typeof e.length)return{next:function(){return{value:(e=e&&t>=e.length?void 0:e)&&e[t++],done:!e}}};throw new TypeError(o?"Object is not iterable.":"Symbol.iterator is not defined.")},simpl_schema_1=(Object.defineProperty(exports,"__esModule",{value:!0}),exports.MongoManagerUserCollection=exports.MongoManagerCollection=exports.MongoManagerModel=exports.MongoManager=void 0,require("simpl-schema")),resolveio_server_app_1=require("../resolveio-server-app"),user_collection_1=require("../collections/user.collection"),common_1=require("../util/common"),NodeCache=require("node-cache"),crypto=require("crypto"),scmp=require("scmp"),monitor_manager_1=require("./monitor.manager"),os_1=require("os"),numCPUs=(0,os_1.cpus)().length,v8=require("v8"),async_1=require("async"),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,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()}return MongoManager.prototype.withSession=function(i){return __awaiter(this,void 0,void 0,function(){var o,n,t,r=this;return __generator(this,function(e){switch(e.label){case 0:(o=resolveio_server_app_1.ResolveIOServer.getMongoConnection().startSession()).startTransaction(),e.label=1;case 1:return e.trys.push([1,4,6,7]),[4,asyncLocalStorage.run(o,function(){return __awaiter(r,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return[4,i()];case 1:return[2,e.sent()]}})})})];case 2:return n=e.sent(),[4,o.commitTransaction()];case 3:return e.sent(),[2,n];case 4:return t=e.sent(),[4,o.abortTransaction()];case 5:throw e.sent(),t;case 6:return o.endSession(),[7];case 7:return[2]}})})},MongoManager.prototype.getSession=function(){return asyncLocalStorage.getStore()},MongoManager.prototype.setCacheLimit=function(){this._isWorkersEnabled?this._heapLimit=this._isWorkerInstance?.8*this._heapSize:.4*this._heapSize:this._heapLimit=.3*this._heapSize},MongoManager.prototype.initServerCollections=function(){return __awaiter(this,void 0,void 0,function(){var o;return __generator(this,function(e){switch(e.label){case 0:return[4,resolveio_server_app_1.ResolveIOServer.getMainDB().listCollections().toArray()];case 1:return o=e.sent(),this._serverCollections=o,[2]}})})},MongoManager.prototype.getServerCollections=function(){return this._serverCollections},MongoManager.prototype.registerCollection=function(o){var e=this;o.collectionOptions&&o.collectionOptions.timeseries&&o.collectionOptions.timeseries.timeField&&this._serverCollections.some(function(e){return e.name===o.collectionName&&"collection"===e.type})&&resolveio_server_app_1.ResolveIOServer.getMainDB().dropCollection(o.collectionName).then(function(){e.createCollection(o)}),this._collections.push(o)},MongoManager.prototype.createCollection=function(e){resolveio_server_app_1.ResolveIOServer.getMainDB().createCollection(e.collectionName,e.collectionOptions)},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(r,i,s){return void 0===i&&(i={}),__awaiter(this,void 0,void 0,function(){var o,n,t=this;return __generator(this,function(e){return(n=s||{}).session,n=__rest(n,["session"]),o=this.generateCacheKey([r],"find",[i,n]),void 0!==(n=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",r),[2,n]):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",r),[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",r),n=this.executeFind(r,i,s,o),this._operationInProgress.set(o,{promise:n,invalidatedDuringExecution:!1,collections:[r]}),n.finally(function(){resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo Find - Done",r),t._operationInProgress.delete(o)}),[2,n])})})},MongoManager.prototype.executeFind=function(t,r,i,s){return __awaiter(this,void 0,void 0,function(){var o,n;return __generator(this,function(e){switch(e.label){case 0:return o=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(t),(n=i||{}).session,n=__rest(n,["session"]),n=new monitor_manager_1.MonitorMongo("find",t,JSON.stringify([r,n])),[4,o.find(r,i).toArray()];case 1:return o=e.sent(),n.finish(),this._operationInProgress.get(s).invalidatedDuringExecution||this.addToCache([t],s,o),[2,o]}})})},MongoManager.prototype.findOne=function(r,i,s){return void 0===i&&(i={}),__awaiter(this,void 0,void 0,function(){var o,n,t=this;return __generator(this,function(e){return(n=s||{}).session,n=__rest(n,["session"]),o=this.generateCacheKey([r],"findOne",[i,n]),void 0!==(n=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",r),[2,n]):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",r),[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",r),n=this.executeFindOne(r,i,s,o),this._operationInProgress.set(o,{promise:n,invalidatedDuringExecution:!1,collections:[r]}),n.finally(function(){resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo FindOne - Done",r),t._operationInProgress.delete(o)}),[2,n])})})},MongoManager.prototype.executeFindOne=function(t,r,i,s){return __awaiter(this,void 0,void 0,function(){var o,n;return __generator(this,function(e){switch(e.label){case 0:return o=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(t),(n=i||{}).session,n=__rest(n,["session"]),n=new monitor_manager_1.MonitorMongo("findOne",t,JSON.stringify([r,n])),[4,o.findOne(r,i)];case 1:return o=e.sent(),n.finish(),this._operationInProgress.get(s).invalidatedDuringExecution||this.addToCache([t],s,o),[2,o]}})})},MongoManager.prototype.aggregate=function(i,s,a){return __awaiter(this,void 0,void 0,function(){var o,n,t,r=this;return __generator(this,function(e){return o=__spreadArray([i],__read(s.flatMap(function(e){var o;return null!=(o=e.$lookup)&&o.from?[e.$lookup.from]:[]})),!1),(t=a||{}).session,t=__rest(t,["session"]),n=this.generateCacheKey(o,"aggregate",[s,t]),void 0!==(t=this.getFromCache(n))?(resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo Agg - Cache",o),[2,t]):this._operationInProgress.has(n)?(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(n).promise]):(resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager()&&resolveio_server_app_1.ResolveIOServer.getMainServer().getSubscriptionManager().getEnableDebug()&&console.log(new Date,"Mongo Agg - Running",o),t=this.executeAggregate(i,s,a,n,o),this._operationInProgress.set(n,{promise:t,invalidatedDuringExecution:!1,collections:o}),t.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),r._operationInProgress.delete(n)}),[2,t])})})},MongoManager.prototype.executeAggregate=function(t,r,i,s,a){return __awaiter(this,void 0,void 0,function(){var o,n;return __generator(this,function(e){switch(e.label){case 0:return o=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(t),(n=i||{}).session,n=__rest(n,["session"]),n=new monitor_manager_1.MonitorMongo("aggregate",t,JSON.stringify([r,n])),[4,o.aggregate(r,i).toArray()];case 1:return o=e.sent(),n.finish(),this._operationInProgress.get(s).invalidatedDuringExecution||this.addToCache(a,s,o),[2,o]}})})},MongoManager.prototype.countDocuments=function(r,i,s){return void 0===i&&(i={}),__awaiter(this,void 0,void 0,function(){var o,n,t=this;return __generator(this,function(e){return(n=s||{}).session,n=__rest(n,["session"]),o=this.generateCacheKey([r],"countDocuments",[i,n]),void 0!==(n=this.getFromCache(o))?[2,n]:this._operationInProgress.has(o)?[2,this._operationInProgress.get(o).promise]:(n=this.executeCountDocuments(r,i,s,o),this._operationInProgress.set(o,{promise:n,invalidatedDuringExecution:!1,collections:[r]}),n.finally(function(){return t._operationInProgress.delete(o)}),[2,n])})})},MongoManager.prototype.executeCountDocuments=function(t,r,i,s){return __awaiter(this,void 0,void 0,function(){var o,n;return __generator(this,function(e){switch(e.label){case 0:return o=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(t),(n=i||{}).session,n=__rest(n,["session"]),n=new monitor_manager_1.MonitorMongo("countDocuments",t,JSON.stringify([r,n])),[4,o.countDocuments(r,i)];case 1:return o=e.sent(),n.finish(),this._operationInProgress.get(s).invalidatedDuringExecution||this.addToCache([t],s,o),[2,o]}})})},MongoManager.prototype.generateCacheKey=function(e,o,n){e=JSON.stringify({collections:e,functionName:o,args:n});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,n){var t,r;if((0,common_1.getBinarySize)(JSON.stringify(n))<1e6&&!e.includes("logs")&&!e.find(function(e){return e.endsWith(".versions")})&&!e.find(function(e){return e.startsWith("monitor-")})){var i=this._nodeCache.getStats().vsize;if(i>this._heapLimit){var s=0,a=this._nodeCache.keys();try{for(var l=__values(a),c=l.next();!c.done;c=l.next()){var _=c.value;if(this._nodeCache.del(_),s+=1,(i=this._nodeCache.getStats().vsize)<.75*this._heapLimit)break}}catch(e){t={error:e}}finally{try{c&&!c.done&&(r=l.return)&&r.call(l)}finally{if(t)throw t.error}}console.log("Query Cache: Too Big, - Deleted: "+s+" - "+i)}this._nodeCache.set(o,JSON.stringify(n)),this._cacheMap.push({collections:e,key:o})}},MongoManager.prototype.invalidateQueryCache=function(o){var n,e,t,r,i=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 s=__values(i),a=s.next();!a.done;a=s.next()){var l=a.value;this._nodeCache.del(l.key),this._operationInProgress.has(l.key)&&(this._operationInProgress.get(l.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",l.collections)}}catch(e){n={error:e}}finally{try{a&&!a.done&&(e=s.return)&&e.call(s)}finally{if(n)throw n.error}}try{for(var c=__values(this._operationInProgress.entries()),_=c.next();!_.done;_=c.next()){var g=__read(_.value,2),v=(g[0],g[1]);v.collections.includes(o)&&(v.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)",v.collections)}}catch(e){t={error:e}}finally{try{_&&!_.done&&(r=c.return)&&r.call(c)}finally{if(t)throw t.error}}this._cacheMap=this._cacheMap.filter(function(e){return!e.collections.includes(o)})},MongoManager.prototype.setupChangeStream=function(){var o=this,n=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"});n.on("change",function(n){return __awaiter(o,void 0,void 0,function(){var o;return __generator(this,function(e){return n.ns&&n.ns.coll&&(o=n.ns.coll,this.collection(o))&&this.invalidateQueryCache(o),[2]})})}),n.on("error",function(e){console.log(new Date,"Mongo change stream error. Restart..."),n.close(),setTimeout(function(){return o.setupChangeStream()},5e3)}),n.on("close",function(){console.log(new Date,"Mongo change stream closed. Restarting..."),setTimeout(function(){return o.setupChangeStream()},5e3)})},MongoManager}(),MongoManagerModel=(exports.MongoManager=MongoManager,function(e){var o;this.collection_main=null,this.collection_version=null,"users"===e.collectionName?this.collection_main=new MongoManagerUserCollection(e):this.collection_main=new MongoManagerCollection(e),e.useVersionCollection&&(this.collection_main.useVersions=!0,(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=new MongoManagerUserCollection(o):this.collection_version=new MongoManagerCollection(o),this.collection_version.createIndex({"_id._id":1,"_id.__v":1}),this.collection_main.versionCollection=e.collectionName+".versions")}),MongoManagerCollection=(exports.MongoManagerModel=MongoManagerModel,function(){function MongoManagerCollection(e){var o,n=this,t=(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.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,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(){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&&(resolveio_server_app_1.ResolveIOServer.getMongoManager().registerCollection(n),clearInterval(t))},1))}var e;return 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(i,s,a,l){var c=this;return void 0===a&&(a=!1),void 0===l&&(l=!1),new Promise(function(o,n){var t,r,e=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession();e&&!l&&(s?s.session=e:s={session:e}),a||c.skipCache?((e=s||{}).session,t=__rest(e,["session"]),r=new monitor_manager_1.MonitorMongo("aggregate",c.collectionName,JSON.stringify([i,t])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(c.collectionName,c.collectionOptions).aggregate(i,s).toArray().then(function(e){r.finish(),o(e)},function(e){r.finish(),console.log(new Date,"Error Aggregate",c.collectionName,i,t,e),n(e)})):resolveio_server_app_1.ResolveIOServer.getMongoManager().aggregate(c.collectionName,i,s).then(function(e){o(e)},function(e){n(e)})})},MongoManagerCollection.prototype.aggregateCount=function(s,a,l){return void 0===l&&(l=!1),__awaiter(this,void 0,void 0,function(){var i=this;return __generator(this,function(e){return[2,new Promise(function(o,n){var e=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),e=(e&&!l&&(a?a.session=e:a={session:e}),a||{}),t=(e.session,__rest(e,["session"])),r=new monitor_manager_1.MonitorMongo("aggregateCount",i.collectionName,JSON.stringify([s,t]));resolveio_server_app_1.ResolveIOServer.getMainDB().collection(i.collectionName,i.collectionOptions).aggregate(s,a).toArray().then(function(e){r.finish(),o(e.length)},function(e){r.finish(),console.log(new Date,"Error Aggregate",i.collectionName,s,t,e),n(0)})})]})})},MongoManagerCollection.prototype.aggregateCursor=function(e,o,n){void 0===n&&(n=!1);var t=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),n=(t&&!n&&(o?o.session=t:o={session:t}),o||{}),t=(n.session,__rest(n,["session"])),r=new monitor_manager_1.MonitorMongo("aggregateCursor",this.collectionName,JSON.stringify([e,t])),n=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).aggregate(e,o);return n.on("close",function(e){return r.finish(),e}),n},MongoManagerCollection.prototype.aggregateStream=function(e,o,n,t){void 0===t&&(t=!1);var r=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),t=(r&&!t&&(o?o.session=r:o={session:r}),o||{}),r=(t.session,__rest(t,["session"])),i=new monitor_manager_1.MonitorMongo("aggregateStream",this.collectionName,JSON.stringify([e,r,n])),t=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).aggregate(e,o).stream(n);return t.on("end",function(e){return i.finish(),e}).on("error",function(e){return i.finish(),e}).on("close",function(e){return i.finish(),e}),t},MongoManagerCollection.prototype.bulkWrite=function(N,R,C,D,b,B){return void 0===C&&(C=!1),void 0===D&&(D=!1),void 0===b&&(b=!1),void 0===B&&(B=!1),__awaiter(this,void 0,void 0,function(){var o,n,t,r,i,s,a,l,c,_,g,v,d,u,p,m,h,f,O,S,M,y,I,w;return __generator(this,function(e){switch(e.label){case 0:if(!N.length)return[2,null];o=1e3,n=0,t=null,(r=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())&&!B&&(R?R.session=r:R={session:r},t={session:r}),e.label=1;case 1:if(!(n<N.length))return[3,19];i=N.slice(n,n+o),s=[],a=[];try{for(I=void 0,l=__values(i),c=l.next();!c.done;c=l.next()){if(_=c.value,g=Object.keys(_)[0],v=_[g],this.checkSchema&&!C){if(d=this.simplschema.newContext(),u=void 0,"insertOne"===g)u=d.validate(v.document);else if("replaceOne"===g)u=d.validate(v.replacement);else if("updateOne"===g||"updateMany"===g)u=d.validate(v.update,{modifier:!0});else{if("deleteOne"!==g&&"deleteMany"!==g)throw new Error("Unsupported operation type: ".concat(g));u=!0}if(!u)throw new Error("Schema validation failed for ".concat(g,": ").concat(JSON.stringify(d.validationErrors())))}if("insertOne"===g)this.timestamps&&(v.document.createdAt||(v.document.createdAt=new Date),v.document.updatedAt||(v.document.updatedAt=new Date)),!D&&this.createLogs&&s.push({insertOne:{document:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:v.document._id||(0,common_1.objectIdHexString)(),payload:(0,common_1.getBinarySize)(JSON.stringify(v.document))<2e5?JSON.stringify(v.document,null,2):"Too Big",method:"insertOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}}),!b&&this.useVersions&&(p=__assign(__assign({},v.document),{__v:0,_id:{_id:v.document._id||(0,common_1.objectIdHexString)(),__v:0}}),a.push({insertOne:{document:p}})),_[g].document=v.document;else if("updateOne"===g||"updateMany"===g)this.timestamps&&(v.update.$set||(v.update.$set={}),v.update.$set.updatedAt=new Date),!b&&this.useVersions&&(v.update.$inc||(v.update.$inc={}),v.update.$inc.__v=1),!D&&this.createLogs&&s.push({insertOne:{document:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:v.filter._id||null,payload:(0,common_1.getBinarySize)(JSON.stringify(v.update))<2e5?JSON.stringify(v.update,null,2):"Too Big",method:g,id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}}),!b&&this.useVersions&&a.push({filter:v.filter});else if("replaceOne"===g)this.timestamps&&(v.replacement.updatedAt=new Date),!b&&this.useVersions&&(v.replacement.__v=(v.replacement.__v||0)+1),!D&&this.createLogs&&s.push({insertOne:{document:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:v.filter._id||null,payload:(0,common_1.getBinarySize)(JSON.stringify(v.replacement))<2e5?JSON.stringify(v.replacement,null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}}),!b&&this.useVersions&&a.push({filter:v.filter}),_[g].replacement=v.replacement;else{if("deleteOne"!==g&&"deleteMany"!==g)throw new Error("Unsupported operation type: ".concat(g));!D&&this.createLogs&&s.push({insertOne:{document:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:v.filter._id||null,payload:(0,common_1.getBinarySize)(JSON.stringify(v.filter))<2e5?JSON.stringify(v.filter,null,2):"Too Big",method:g,id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}}),!b&&this.useVersions&&a.push({filter:v.filter})}}}catch(e){I={error:e}}finally{try{c&&!c.done&&(w=l.return)&&w.call(l)}finally{if(I)throw I.error}}if(m=[],!(!b&&this.useVersions&&0<a.length))return[3,7];e.label=2;case 2:return e.trys.push([2,6,,7]),h=a.map(function(e){return e.filter}),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName).find({$or:h},t).toArray()];case 3:return(m=e.sent(),0<(h=m.map(function(e){return{insertOne:{document:__assign(__assign({},e),{_id:{_id:e._id,__v:e.__v}})}}})).length)?(S=null,S=t?{session:r,ordered:!1}:{ordered:!1},[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.versionCollection).bulkWrite(h,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:return(f=R||{}).session,O=__rest(f,["session"]),O=new monitor_manager_1.MonitorMongo("bulkWrite",this.collectionName,JSON.stringify([i,O])),[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName).bulkWrite(i,R)];case 8:if(e.sent(),O.finish(),!D&&this.createLogs&&0<s.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,9];resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLogs(s.map(function(e){return{type:"log",data:e}}))}return[3,11];case 9:return S=null,S=t?{session:r,ordered:!1}:{ordered:!1},[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").bulkWrite(s,S)];case 10:e.sent(),e.label=11;case 11:if(b||!this.useVersions)return[3,17];e.label=12;case 12:return e.trys.push([12,16,,17]),M=m.map(function(e){return e._id}),M=[{$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(M,t).toArray()];case 13:return 0<(M=e.sent()).length?[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.versionCollection).deleteMany({_id:{$in:M.map(function(e){return e._id})}},t)]:[3,15];case 14:e.sent(),e.label=15;case 15:return[3,17];case 16:return y=e.sent(),console.error("Error during version cleanup:",y),[3,17];case 17:return n+=o,[4,new Promise(function(e){return setImmediate(e)})];case 18:return e.sent(),this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),[3,1];case 19:return[2,{ok:1}]}})})},MongoManagerCollection.prototype.countDocuments=function(l,c,_,g){var e=this;return void 0===l&&(l={}),void 0===_&&(_=!1),void 0===g&&(g=!1),new Promise(function(s,a){return __awaiter(e,void 0,void 0,function(){var o,n,t,r,i=this;return __generator(this,function(e){switch(e.label){case 0:(n=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())&&!g&&(c?c.session=n:c={session:n}),(n=c||{}).session,o=__rest(n,["session"]),e.label=1;case 1:return(e.trys.push([1,5,,6]),0===Object.keys(l).length)?[4,resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).estimatedDocumentCount(c)]:[3,3];case 2:return n=e.sent(),s(n),[3,4];case 3:_||this.skipCache?(t=new monitor_manager_1.MonitorMongo("countDocuments",this.collectionName,JSON.stringify([l,o])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).countDocuments(l,c).then(function(e){t.finish(),s(e)},function(e){t.finish(),console.log(new Date,"Error Count Documents",i.collectionName,l,o,e),a(e)})):resolveio_server_app_1.ResolveIOServer.getMongoManager().countDocuments(this.collectionName,l,c).then(function(e){return s(e)},function(e){return a(e)}),e.label=4;case 4:return[3,6];case 5:return r=e.sent(),console.log(new Date,"Error Count Documents (Stats)",this.collectionName,l,o,r),a(r),[3,6];case 6: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(r,i){var e=this;return new Promise(function(n,t){return __awaiter(e,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(r)}))?[2,n("Index already exists")]:(MongoManagerCollection.indexQueue.push({action:"createIndex",collection:this,specs:[r],options:i},function(e,o){e?t(e):n(o)}),[3,3]);case 2:return o=e.sent(),t(o),[3,3];case 3:return[2]}})})})},MongoManagerCollection.prototype.createIndexes=function(i,s){var e=this;return new Promise(function(t,r){return __awaiter(e,void 0,void 0,function(){var n,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(n=e.sent(),0===(o=i.filter(function(o){return!n.some(function(e){return JSON.stringify(e.key)===JSON.stringify(o.key)})})).length)?[2,t(["All indexes already exist"])]:(MongoManagerCollection.indexQueue.push({action:"createIndexes",collection:this,specs:[o],options:s},function(e,o){e?r(e):t(o)}),[3,3]);case 2:return o=e.sent(),r(o),[3,3];case 3:return[2]}})})})},MongoManagerCollection.prototype.deleteMany=function(u,p,m,h){var e=this;return void 0===u&&(u={}),void 0===m&&(m=!1),void 0===h&&(h=!1),new Promise(function(v,d){return __awaiter(e,void 0,void 0,function(){var o,n,t,r,i,s,a,l,c,_,g=this;return __generator(this,function(e){switch(e.label){case 0:return(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),n=null,o&&!h&&(p?p.session=o:p={session:o},n={session:o}),(t=p||{}).session,r=__rest(t,["session"]),this.createLogs&&!m||this.useVersions)?[4,this.find(u)]:[3,2];case 1:for(i=e.sent(),s=0;s<i.length;s++)a=i[s],this.createLogs&&!m&&("https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?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,u,r]))<2e5?JSON.stringify([a,u,r],null,2):"Too Big",method:"deleteMany",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):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,u,r]))<2e5?JSON.stringify([a,u,r],null,2):"Too Big",method:"deleteMany",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},n)),this.useVersions&&((l=(0,common_1.deepCopy)(a))._id={_id:a._id,__v:a.__v},c=null,c=n?{session:o,upsert:!0}:{upsert:!0},resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:a._id,__v:a.__v}},l,c),resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({$and:[{"_id._id":a._id},{"_id.__v":{$lt:a.__v-1}}]},n));e.label=2;case 2:return _=new monitor_manager_1.MonitorMongo("deleteMany",this.collectionName,JSON.stringify([u,r])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).deleteMany(u,p).then(function(e){_.finish(),e.acknowledged?(g.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(g.collectionName),v(e.deletedCount)):d(e.acknowledged)},function(e){_.finish(),console.log(new Date,"Error Delete Many",g.collectionName,u,r,e),d(e)}),[2]}})})})},MongoManagerCollection.prototype.deleteOne=function(_,g,v,d){var e=this;return void 0===_&&(_={}),void 0===v&&(v=!1),void 0===d&&(d=!1),new Promise(function(c,r){return __awaiter(e,void 0,void 0,function(){var i,o,s,a,l,n,t=this;return __generator(this,function(e){return o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),i=null,o&&!d&&(g?g.session=o:g={session:o},i={session:o}),s=(o=g||{}).session,a=__rest(o,["session"]),this.createLogs&&!v?(l=new monitor_manager_1.MonitorMongo("findOneAndDelete",this.collectionName,JSON.stringify([_,a])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndDelete(_,g).then(function(r){return __awaiter(t,void 0,void 0,function(){var o,n,t;return __generator(this,function(e){return l.finish(),(o=r.value)?(this.createLogs&&!v&&("https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:o._id,payload:(0,common_1.getBinarySize)(JSON.stringify([o,_,a]))<2e5?JSON.stringify([o,_,a],null,2):"Too Big",method:"deleteOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:o._id,payload:(0,common_1.getBinarySize)(JSON.stringify([o,_,a]))<2e5?JSON.stringify([o,_,a],null,2):"Too Big",method:"deleteOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},i)),this.useVersions&&((n=(0,common_1.deepCopy)(o))._id={_id:o._id,__v:o.__v},t=null,t=i?{session:s,upsert:!0}:{upsert:!0},resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:o._id,__v:o.__v}},n,t),resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({$and:[{"_id._id":o._id},{"_id.__v":{$lt:o.__v-1}}]},i)),this.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(this.collectionName),c(1)):c(0),[2]})})},function(e){l.finish(),console.log(new Date,"Error Find One And Delete",t.collectionName,_,a,e),r(e)})):(n=new monitor_manager_1.MonitorMongo("deleteOne",this.collectionName,JSON.stringify([_,a])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).deleteOne(_,g).then(function(e){n.finish(),e.acknowledged?(t.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(t.collectionName),c(e.deletedCount)):r(e.acknowledged)},function(e){n.finish(),console.log(new Date,"Error Delete One",t.collectionName,_,a,e),r(e)})),[2]})})})},MongoManagerCollection.prototype.distinct=function(i,s,a,l){var c=this;return void 0===l&&(l=!1),new Promise(function(o,n){var e=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),e=(e&&!l&&(a?a.session=e:a={session:e}),a||{}),t=(e.session,__rest(e,["session"])),r=new monitor_manager_1.MonitorMongo("distinct",c.collectionName,JSON.stringify([i,s,t]));resolveio_server_app_1.ResolveIOServer.getMainDB().collection(c.collectionName,c.collectionOptions).distinct(i,s,a).then(function(e){r.finish(),o(e)},function(e){r.finish(),console.log(new Date,"Error Distinct",c.collectionName,s,t,e),n(e)})})},MongoManagerCollection.prototype.drop=function(i,s){var a=this;return void 0===s&&(s=!1),new Promise(function(o,n){var e=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),e=(e&&!s&&(i?i.session=e:i={session:e}),i||{}),t=(e.session,__rest(e,["session"])),r=new monitor_manager_1.MonitorMongo("drop",a.collectionName,JSON.stringify([t]));resolveio_server_app_1.ResolveIOServer.getMainDB().collection(a.collectionName,a.collectionOptions).drop(i).then(function(e){r.finish(),o(e)},function(e){r.finish(),console.log(new Date,"Error Count Documents",a.collectionName,t,e),n(e)})})},MongoManagerCollection.prototype.dropIndex=function(i,s,a){var l=this;return void 0===a&&(a=!1),new Promise(function(o,n){var e=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),e=(e&&!a&&(s?s.session=e:s={session:e}),s||{}),t=(e.session,__rest(e,["session"])),r=new monitor_manager_1.MonitorMongo("dropIndex",l.collectionName,JSON.stringify([i,t]));resolveio_server_app_1.ResolveIOServer.getMainDB().collection(l.collectionName,l.collectionOptions).dropIndex(i,s).then(function(e){r.finish(),o(e)},function(e){r.finish(),console.log(new Date,"Error Drop Index",l.collectionName,i,t,e),n(e)})})},MongoManagerCollection.prototype.dropIndexes=function(i,s){var a=this;return void 0===s&&(s=!1),new Promise(function(o,n){var e=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),e=(e&&!s&&(i?i.session=e:i={session:e}),i||{}),t=(e.session,__rest(e,["session"])),r=new monitor_manager_1.MonitorMongo("dropIndexes",a.collectionName,JSON.stringify([t]));resolveio_server_app_1.ResolveIOServer.getMainDB().collection(a.collectionName,a.collectionOptions).dropIndexes(i).then(function(e){r.finish(),o(e)},function(e){r.finish(),console.log(new Date,"Error Drop Indexes",a.collectionName,t,e),n(e)})})},MongoManagerCollection.prototype.find=function(a,l,c,_){var e=this;return void 0===a&&(a={}),void 0===c&&(c=!1),void 0===_&&(_=!1),new Promise(function(i,s){return __awaiter(e,void 0,void 0,function(){var o,n,t,r=this;return __generator(this,function(e){return(o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession())&&!_&&(l?l.session=o:l={session:o}),(o=l||{}).session,n=__rest(o,["session"]),c||this.skipCache?(t=new monitor_manager_1.MonitorMongo("find",this.collectionName,JSON.stringify([a,n])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).find(a,l).toArray().then(function(e){t.finish(),i(e)},function(e){t.finish(),console.log(new Date,"Error Find",r.collectionName,a,n,e),s(e)})):resolveio_server_app_1.ResolveIOServer.getMongoManager().find(this.collectionName,a,l).then(function(e){i(e)},function(e){s(e)}),[2]})})})},MongoManagerCollection.prototype.findById=function(i,s,a,l){var c=this;return void 0===a&&(a=!1),void 0===l&&(l=!1),new Promise(function(o,n){var t,e=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),e=(e&&!l&&(s?s.session=e:s={session:e}),s||{}),r=(e.session,__rest(e,["session"]));a||c.skipCache?(t=new monitor_manager_1.MonitorMongo("findById",c.collectionName,JSON.stringify([{_id:i},r])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(c.collectionName,c.collectionOptions).findOne({_id:i},s).then(function(e){t.finish(),o(e)},function(e){t.finish(),console.log(new Date,"Error Find By Id",c.collectionName,{_id:i},r,e),n(e)})):resolveio_server_app_1.ResolveIOServer.getMongoManager().findOne(c.collectionName,{_id:i},s).then(function(e){o(e)},function(e){n(e)})})},MongoManagerCollection.prototype.findCount=function(i,s,a){var l=this;return void 0===i&&(i={}),void 0===a&&(a=!1),new Promise(function(o,n){var e=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),e=(e&&!a&&(s?s.session=e:s={session:e}),s||{}),t=(e.session,__rest(e,["session"])),r=new monitor_manager_1.MonitorMongo("findCount",l.collectionName,JSON.stringify([i,t]));resolveio_server_app_1.ResolveIOServer.getMainDB().collection(l.collectionName,l.collectionOptions).find(i,s).count().then(function(e){r.finish(),o(e)},function(e){r.finish(),console.log(new Date,"Error Find Count",l.collectionName,i,t,e),n(e)})})},MongoManagerCollection.prototype.findCursor=function(e,o,n){void 0===e&&(e={}),void 0===n&&(n=!1);var t=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),n=(t&&!n&&(o?o.session=t:o={session:t}),o||{}),t=(n.session,__rest(n,["session"])),r=new monitor_manager_1.MonitorMongo("findCursor",this.collectionName,JSON.stringify([e,t])),i=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).find(e,o);return i.on("close",function(e){return r.finish(),i.removeAllListeners(),e}),i},MongoManagerCollection.prototype.findStream=function(e,o,n,t){void 0===e&&(e={}),void 0===t&&(t=!1);var r=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),t=(r&&!t&&(o?o.session=r:o={session:r}),o||{}),r=(t.session,__rest(t,["session"])),i=new monitor_manager_1.MonitorMongo("findStream",this.collectionName,JSON.stringify([e,r,n])),t=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).find(e,o).stream(n);return t.on("end",function(e){return i.finish(),e}).on("error",function(e){return i.finish(),e}).on("close",function(e){return i.finish(),e}),t},MongoManagerCollection.prototype.findOne=function(i,s,a,l){var c=this;return void 0===i&&(i={}),void 0===a&&(a=!1),void 0===l&&(l=!1),new Promise(function(o,n){var t,r,e=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession();e&&!l&&(s?s.session=e:s={session:e}),a||c.skipCache?((e=s||{}).session,t=__rest(e,["session"]),r=new monitor_manager_1.MonitorMongo("findOne",c.collectionName,JSON.stringify([i,t])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(c.collectionName,c.collectionOptions).findOne(i,s).then(function(e){r.finish(),o(e)},function(e){r.finish(),console.log(new Date,"Error Find One",c.collectionName,i,t,e),n(e)})):resolveio_server_app_1.ResolveIOServer.getMongoManager().findOne(c.collectionName,i,s).then(function(e){o(e)},function(e){n(e)})})},MongoManagerCollection.prototype.findOneAndDelete=function(c,_,g,v){var e=this;return void 0===c&&(c={}),void 0===g&&(g=!1),void 0===v&&(v=!1),new Promise(function(l,n){return __awaiter(e,void 0,void 0,function(){var t,o,r,i,s,a=this;return __generator(this,function(e){return o=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),t=null,o&&!v&&(_?_.session=o:_={session:o},t={session:o}),r=(o=_||{}).session,i=__rest(o,["session"]),s=new monitor_manager_1.MonitorMongo("findOneAndDelete",this.collectionName,JSON.stringify([c,i])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndDelete(c,_).then(function(e){s.finish();var o,n,e=e.value;e?(a.createLogs&&!g&&("https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:a.collectionName,id_document:e._id,payload:(0,common_1.getBinarySize)(JSON.stringify([e,c,i]))<2e5?JSON.stringify([e,c,i],null,2):"Too Big",method:"findOneAndDelete",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:a.collectionName,id_document:e._id,payload:(0,common_1.getBinarySize)(JSON.stringify([e,c,i]))<2e5?JSON.stringify([e,c,i],null,2):"Too Big",method:"findOneAndDelete",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},t)),a.useVersions&&((o=(0,common_1.deepCopy)(e))._id={_id:e._id,__v:e.__v},n=null,n=t?{session:r,upsert:!0}:{upsert:!0},resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(a.versionCollection).replaceOne({_id:{_id:e._id,__v:e.__v}},o,n)),a.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(a.collectionName),l(e)):l(null)},function(e){s.finish(),console.log(new Date,"Error Find One And Delete",a.collectionName,c,i,e),n(e)}),[2]})})})},MongoManagerCollection.prototype.findOneAndReplace=function(_,g,v,d,u,p){var e=this;return void 0===_&&(_={}),void 0===d&&(d=!1),void 0===u&&(u=!1),void 0===p&&(p=!1),new Promise(function(l,c){return __awaiter(e,void 0,void 0,function(){var o,t,n,r,i,s,a=this;return __generator(this,function(e){return!this.checkSchema||u||(o=this.simplschema.newContext(),o.validate(g))?(n=new Date,this.timestamps&&(g.updatedAt=n),v&&v.upsert&&(g._id||(g._id=(0,common_1.objectIdHexString)()),this.useVersions&&(g.__v=0),this.timestamps)&&!g.createdAt&&(g.createdAt=n),n=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),t=null,n&&!p&&(v?v.session=n:v={session:n},t={session:n}),r=(n=v||{}).session,i=__rest(n,["session"]),s=new monitor_manager_1.MonitorMongo("findOneAndReplace",this.collectionName,JSON.stringify([_,g,i])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndReplace(_,g,v).then(function(e){s.finish();var o,n,e=e.value;e?(a.createLogs&&!d&&("https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:a.collectionName,id_document:e._id,payload:(0,common_1.getBinarySize)(JSON.stringify([e,_,g,i]))<2e5?JSON.stringify([e,_,g,i],null,2):"Too Big",method:"findOneAndReplace",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:a.collectionName,id_document:e._id,payload:(0,common_1.getBinarySize)(JSON.stringify([e,_,g,i]))<2e5?JSON.stringify([e,_,g,i],null,2):"Too Big",method:"findOneAndReplace",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},t)),a.useVersions&&((o=(0,common_1.deepCopy)(e))._id={_id:e._id,__v:e.__v},n=null,n=t?{session:r,upsert:!0}:{upsert:!0},resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(a.versionCollection).replaceOne({_id:{_id:e._id,__v:e.__v}},o,n)),a.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(a.collectionName),l(e)):l(null)},function(e){s.finish(),console.log(new Date,"Error Find One And Replace",a.collectionName,_,i,e),c(e)})):(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(),g]),c(o.validationErrors())),[2]})})})},MongoManagerCollection.prototype.findOneAndUpdate=function(g,v,d,u,p,m){var e=this;return void 0===g&&(g={}),void 0===u&&(u=!1),void 0===p&&(p=!1),void 0===m&&(m=!1),new Promise(function(c,_){return __awaiter(e,void 0,void 0,function(){var o,n,t,r,i,s,a,l=this;return __generator(this,function(e){return!this.checkSchema||p||(o=this.simplschema.newContext(),o.validate(v,{modifier:!0}))?(this.timestamps&&(n=new Date,v.$set?v.$set.updatedAt=n:v.$set={updatedAt:n}),d&&d.upsert&&(v.$setOnInsert?(v.$setOnInsert._id||(v.$setOnInsert._id=(0,common_1.objectIdHexString)()),this.timestamps&&!v.$setOnInsert.createdAt&&(v.$setOnInsert.createdAt=new Date)):this.timestamps?v.$setOnInsert={_id:(0,common_1.objectIdHexString)(),createdAt:new Date}:v.$setOnInsert={_id:(0,common_1.objectIdHexString)()}),n=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),t=null,n&&!m&&(d?d.session=n:d={session:n},t={session:n}),i=(r=d||{}).session,s=__rest(r,["session"]),a=new monitor_manager_1.MonitorMongo("findOneAndUpdate",this.collectionName,JSON.stringify([g,v,s])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndUpdate(g,v,d).then(function(e){a.finish();var o,n,e=e.value;e?(l.createLogs&&!u&&("https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:l.collectionName,id_document:e._id,payload:(0,common_1.getBinarySize)(JSON.stringify([e,g,v,s]))<2e5?JSON.stringify([e,g,v,s],null,2):"Too Big",method:"findOneAndUpdate",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:l.collectionName,id_document:e._id,payload:(0,common_1.getBinarySize)(JSON.stringify([e,g,v,s]))<2e5?JSON.stringify([e,g,v,s],null,2):"Too Big",method:"findOneAndUpdate",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},t)),l.useVersions&&((o=(0,common_1.deepCopy)(e))._id={_id:e._id,__v:e.__v},n=null,n=t?{session:i,upsert:!0}:{upsert:!0},resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(l.versionCollection).replaceOne({_id:{_id:e._id,__v:e.__v}},o,n)),l.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(l.collectionName),c(e)):c(null)},function(e){a.finish(),console.log(new Date,"Error Find One And Update",l.collectionName,g,s,e),_(e)})):(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(),v]),_(o.validationErrors())),[2]})})})},MongoManagerCollection.prototype.indexes=function(i,s){var a=this;return void 0===s&&(s=!1),new Promise(function(o,n){var e=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),e=(e&&!s&&(i?i.session=e:i={session:e}),i||{}),t=(e.session,__rest(e,["session"])),r=new monitor_manager_1.MonitorMongo("indexes",a.collectionName,JSON.stringify([t]));resolveio_server_app_1.ResolveIOServer.getMainDB().collection(a.collectionName,a.collectionOptions).indexes(i).then(function(e){r.finish(),o(e)},function(e){r.finish(),console.log(new Date,"Error Indexes",a.collectionName,t,e),n(e)})})},MongoManagerCollection.prototype.indexExists=function(i,s,a){var l=this;return void 0===a&&(a=!1),new Promise(function(o,n){var e=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),e=(e&&!a&&(s?s.session=e:s={session:e}),s||{}),t=(e.session,__rest(e,["session"])),r=new monitor_manager_1.MonitorMongo("indexExists",l.collectionName,JSON.stringify([i,t]));resolveio_server_app_1.ResolveIOServer.getMainDB().collection(l.collectionName,l.collectionOptions).indexExists(i,s).then(function(e){r.finish(),o(e)},function(e){r.finish(),console.log(new Date,"Error Index Exists",l.collectionName,i,t,e),n(e)})})},MongoManagerCollection.prototype.insertMany=function(d,u,p,m,h,f){var e=this;return void 0===p&&(p=!1),void 0===m&&(m=!1),void 0===h&&(h=!1),void 0===f&&(f=!1),new Promise(function(g,v){return __awaiter(e,void 0,void 0,function(){var n,o,t,r,i,s,a,l,c,_=this;return __generator(this,function(e){if(d.length){for(n=[],this.checkSchema&&!m&&d.forEach(function(e){var o=_.simplschema.newContext();o.validate(e)?n.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]),n.push(!1))}),o=this.checkSchema?d.filter(function(e,o){return n[o]}):d,r=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),t=null,r&&!f&&(u?u.session=r:u={session:r},t={session:r}),(r=u||{}).session,i=__rest(r,["session"]),s=0;s<o.length;s++)(a=o[s])._id||(a._id=(0,common_1.objectIdHexString)()),this.timestamps&&(l=new Date,a.createdAt||(a.createdAt=l),a.updatedAt||(a.updatedAt=l)),this.useVersions&&!a.hasOwnProperty("__v")&&(a.__v=0),this.createLogs&&!p&&("https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?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,i]))<2e5?JSON.stringify([a,i],null,2):"Too Big",method:"insertMany",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):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,i]))<2e5?JSON.stringify([a,i],null,2):"Too Big",method:"insertMany",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},t));o.length&&(c=null,h||(c=new monitor_manager_1.MonitorMongo("insertMany",this.collectionName,JSON.stringify([o,i]))),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).insertMany(o,u).then(function(e){c&&c.finish(),e.acknowledged?(_.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(_.collectionName),g(o)):v(e.acknowledged)},function(e){c&&c.finish(),console.log(new Date,"Error Insert Many",_.collectionName,o,i,e),v(e)}))}else g([]);return[2]})})})},MongoManagerCollection.prototype.insertOne=function(_,g,v,d,u){var e=this;return void 0===v&&(v=!1),void 0===d&&(d=!1),void 0===u&&(u=!1),new Promise(function(l,c){return __awaiter(e,void 0,void 0,function(){var o,n,t,r,i,s,a=this;return __generator(this,function(e){return _._id||(_._id=(0,common_1.objectIdHexString)()),!this.checkSchema||d||(o=this.simplschema.newContext(),o.validate(_))?(this.timestamps&&(n=new Date,_.createdAt||(_.createdAt=n),_.updatedAt||(_.updatedAt=n)),n=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),t=null,n&&!u&&(g?g.session=n:g={session:n},t={session:n}),(r=g||{}).session,i=__rest(r,["session"]),this.createLogs&&!v&&("https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:_._id,payload:(0,common_1.getBinarySize)(JSON.stringify([_,i]))<2e5?JSON.stringify([_,i],null,2):"Too Big",method:"insertOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:_._id,payload:(0,common_1.getBinarySize)(JSON.stringify([_,i]))<2e5?JSON.stringify([_,i],null,2):"Too Big",method:"insertOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},t)),this.useVersions&&!_.hasOwnProperty("__v")&&(_.__v=0),s=new monitor_manager_1.MonitorMongo("insertOne",this.collectionName,JSON.stringify([_,i])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).insertOne(_,g).then(function(e){s.finish(),e.acknowledged?(a.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(a.collectionName),l(_)):c(e.acknowledged)},function(e){s.finish(),console.log(new Date,"Error Insert One",a.collectionName,_,i,e),c(e)})):(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(),_]),c(o.validationErrors())),[2]})})})},MongoManagerCollection.prototype.listIndexes=function(e,o){void 0===o&&(o=!1);var n=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),o=(n&&!o&&(e?e.session=n:e={session:n}),e||{}),n=(o.session,__rest(o,["session"])),t=new monitor_manager_1.MonitorMongo("listIndexes",this.collectionName,JSON.stringify([n])),o=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).listIndexes(e);return o.on("close",function(e){return t.finish(),e}),o},MongoManagerCollection.prototype.rename=function(i,s,a){var l=this;return void 0===a&&(a=!1),new Promise(function(o,n){var e=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),e=(e&&!a&&(s?s.session=e:s={session:e}),s||{}),t=(e.session,__rest(e,["session"])),r=new monitor_manager_1.MonitorMongo("rename",l.collectionName,JSON.stringify([i,t]));resolveio_server_app_1.ResolveIOServer.getMainDB().collection(l.collectionName,l.collectionOptions).rename(i,s).then(function(e){r.finish(),o(e)},function(e){r.finish(),console.log(new Date,"Error Rename",l.collectionName,i,t,e),n(e)})})},MongoManagerCollection.prototype.replaceOne=function(S,M,y,I,w,N,R){var e=this;return void 0===I&&(I=!1),void 0===w&&(w=!1),void 0===N&&(N=null),void 0===R&&(R=!1),new Promise(function(f,O){return __awaiter(e,void 0,void 0,function(){var o,n,t,r,i,s,a,l,c,_,g,v,d,u,p,m,h=this;return __generator(this,function(e){switch(e.label){case 0:return!this.checkSchema||w||(s=this.simplschema.newContext(),s.validate(M))?(o=new Date,this.timestamps&&(M.updatedAt=o),i=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),n=null,i&&!R&&(y?y.session=i:y={session:i},n={session:i}),t=(i=y||{}).session,r=__rest(i,["session"]),this.useVersions?N?[3,2]:[4,this.findOne(S,null,!0)]:[3,8]):(console.log(new Date,this.collectionName,"Schema Errors - replaceOne",s.validationErrors()),resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethod.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on replaceOne - "+this.collectionName,[s.validationErrors(),M]),O(s.validationErrors()),[2]);case 1:N=e.sent(),e.label=2;case 2:return N?(this.timestamps&&!M.createdAt&&N.createdAt&&(M.createdAt=N.createdAt),N.__v!==M.__v?[3,3]:(M.__v+=1,(i=(0,common_1.deepCopy)(N))._id={_id:N._id,__v:N.__v},s=null,s=n?{session:t,upsert:!0}:{upsert:!0},resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:N._id,__v:N.__v}},i,s),4<=N.__v&&resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({$and:[{"_id._id":N._id},{"_id.__v":{$lt:N.__v-4}}]},n),this.createLogs&&!I&&("https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?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,S,M,r]))<2e5?JSON.stringify([N,S,M,r],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):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,S,M,r]))<2e5?JSON.stringify([N,S,M,r],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},n)),a=new monitor_manager_1.MonitorMongo("replaceOne",this.collectionName,JSON.stringify([S,M,r])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).replaceOne(S,M,y).then(function(e){a.finish(),e.acknowledged?(h.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(h.collectionName),f(e.modifiedCount)):O(e.acknowledged)},function(e){a.finish(),console.log(new Date,"Error Replace One",h.collectionName,S,M,r,e),O(e)}),[3,5])):[3,6];case 3:return console.log("invalid version - "+this.collectionName,N.__v,M.__v),[4,resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).findOne({_id:{_id:N._id,__v:M.__v}},null,!0)];case 4:(d=e.sent())?(l=N._id,c=N.__v,(_=(0,common_1.getMongoMergeUpdatedDoc)(M,N,d))._id=l,_.__v=c+1,this.createLogs&&!I&&("https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:l,payload:(0,common_1.getBinarySize)(JSON.stringify(["invalidVersion - merge",N,S,_,r]))<2e5?JSON.stringify(["invalidVersion - merge",N,S,_,r],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:l,payload:(0,common_1.getBinarySize)(JSON.stringify(["invalidVersion - merge",N,S,_,r]))<2e5?JSON.stringify(["invalidVersion - merge",N,S,_,r],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},n)),g=new monitor_manager_1.MonitorMongo("replaceOne",this.collectionName,JSON.stringify([S,_,r])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).replaceOne(S,_,y).then(function(e){g.finish(),e.acknowledged?(h.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(h.collectionName),f(e.modifiedCount)):O(e.acknowledged)},function(e){g.finish(),console.log(new Date,"Error Replace One",h.collectionName,S,_,r,e),O(e)})):O("Invalid Version And Could Not Find History - DB: "+N.__v+", Trying to update with :"+M.__v),e.label=5;case 5:return[3,7];case 6:y&&y.upsert?(this.timestamps&&(M.createdAt=o),M._id||(M._id=(0,common_1.objectIdHexString)()),M.__v=0,this.createLogs&&!I&&("https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:M._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",S,M,r]))<2e5?JSON.stringify(["upsert",S,M,r],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:M._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",S,M,r]))<2e5?JSON.stringify(["upsert",S,M,r],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},n)),v=new monitor_manager_1.MonitorMongo("replaceOne",this.collectionName,JSON.stringify([S,M,r])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).replaceOne(S,M,y).then(function(e){v.finish(),e.acknowledged?(h.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(h.collectionName),f(e.modifiedCount)):O(e.acknowledged)},function(e){v.finish(),console.log(new Date,"Error Replace One",h.collectionName,S,M,r,e),O(e)})):O("No Document"),e.label=7;case 7:return[3,9];case 8:y&&y.upsert&&(this.timestamps&&!M.createdAt&&(M.createdAt=o),M._id||(M._id=(0,common_1.objectIdHexString)()),M.__v=0),this.createLogs&&!I?(y?y.returnDocument="before":y={returnDocument:"before"},(d=y||{}).session,u=__rest(d,["session"]),p=new monitor_manager_1.MonitorMongo("findOneAndReplace",this.collectionName,JSON.stringify([S,M,u])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndReplace(S,M,y).then(function(e){p.finish();e=e.value;e?"https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:h.collectionName,id_document:e._id,payload:(0,common_1.getBinarySize)(JSON.stringify([e,S,M,u]))<2e5?JSON.stringify([e,S,M,u],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:h.collectionName,id_document:e._id,payload:(0,common_1.getBinarySize)(JSON.stringify([e,S,M,u]))<2e5?JSON.stringify([e,S,M,u],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},n):"https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:h.collectionName,id_document:M._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",S,M,u]))<2e5?JSON.stringify(["upsert",S,M,u],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:h.collectionName,id_document:M._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",S,M,u]))<2e5?JSON.stringify(["upsert",S,M,u],null,2):"Too Big",method:"replaceOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},n),h.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(h.collectionName),f(1)},function(e){p.finish(),console.log(new Date,"Error Find One And Replace",h.collectionName,S,M,u,e),O(e)})):(m=new monitor_manager_1.MonitorMongo("replaceOne",this.collectionName,JSON.stringify([S,M,r])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).replaceOne(S,M,y).then(function(e){m.finish(),e.acknowledged?(h.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(h.collectionName),f(e.modifiedCount)):O(e.acknowledged)},function(e){m.finish(),console.log(new Date,"Error Replace One",h.collectionName,S,M,r,e),O(e)})),e.label=9;case 9:return[2]}})})})},MongoManagerCollection.prototype.updateMany=function(p,m,h,f,O,S,M){var e=this;return void 0===f&&(f=!1),void 0===O&&(O=!1),void 0===S&&(S=!1),void 0===M&&(M=!1),new Promise(function(d,u){return __awaiter(e,void 0,void 0,function(){var o,n,t,r,i,s,a,l,c,_,g,v=this;return __generator(this,function(e){switch(e.label){case 0:return m.$inc&&"{}"!==JSON.stringify(m.$inc)||m.$set&&"{}"!==JSON.stringify(m.$set)||m.$setOnInsert&&"{}"!==JSON.stringify(m.$setOnInsert)||m.$unset&&"{}"!==JSON.stringify(m.$unset)?(this.timestamps&&(o=new Date,m.$set?m.$set.updatedAt=o:m.$set={updatedAt:o}),!this.checkSchema||O||(o=this.simplschema.newContext(),o.validate(m,{modifier:!0}))?(t=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),n=null,t&&!M&&(h?h.session=t:h={session:t},n={session:t}),r=(t=h||{}).session,i=__rest(t,["session"]),this.useVersions&&!S||this.createLogs&&!f?[4,this.find(p)]:[3,2]):(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(),m]),u(o.validationErrors()),[2])):(d(1),[2]);case 1:for(s=e.sent(),a=0;a<s.length;a++)l=s[a],this.createLogs&&!f&&("https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:l._id,payload:(0,common_1.getBinarySize)(JSON.stringify([l,p,m,i]))<2e5?JSON.stringify([l,p,m,i],null,2):"Too Big",method:"updateMany",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:l._id,payload:(0,common_1.getBinarySize)(JSON.stringify([l,p,m,i]))<2e5?JSON.stringify([l,p,m,i],null,2):"Too Big",method:"updateMany",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},n)),this.useVersions&&((c=(0,common_1.deepCopy)(l))._id={_id:l._id,__v:l.__v},_=null,_=n?{session:r,upsert:!0}:{upsert:!0},resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:l._id,__v:l.__v}},c,_),4<=l.__v&&resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({$and:[{"_id._id":l._id},{"_id.__v":{$lt:l.__v-4}}]},n),objectContainsPropertyDeep(m,"__v")||(m.$inc?m.$inc.__v||(m.$inc.__v=1):m.$inc={__v:1}));e.label=2;case 2:return g=new monitor_manager_1.MonitorMongo("updateMany",this.collectionName,JSON.stringify([p,m,i])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).updateMany(p,m,h).then(function(e){g.finish(),e.acknowledged?(v.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(v.collectionName),d(e.modifiedCount)):u(e.acknowledged)},function(e){g.finish(),console.log(new Date,"Error Update Many",v.collectionName,p,m,i,e),u(e)}),[2]}})})})},MongoManagerCollection.prototype.updateOne=function(u,p,m,h,f,O){var e=this;return void 0===h&&(h=!1),void 0===f&&(f=!1),void 0===O&&(O=!1),new Promise(function(v,d){return __awaiter(e,void 0,void 0,function(){var n,o,t,r,i,s,a,l,c,_,g=this;return __generator(this,function(e){switch(e.label){case 0:return!this.checkSchema||f||(i=this.simplschema.newContext(),i.validate(p,{modifier:!0}))?p.$inc&&"{}"!==JSON.stringify(p.$inc)||p.$set&&"{}"!==JSON.stringify(p.$set)||p.$setOnInsert&&"{}"!==JSON.stringify(p.$setOnInsert)||p.$unset&&"{}"!==JSON.stringify(p.$unset)?(r=new Date,this.timestamps&&(p.$set?p.$set.updatedAt=r:p.$set={updatedAt:r}),r=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession(),n=null,r&&!O&&(m?m.session=r:m={session:r},n={session:r}),o=(r=m||{}).session,t=__rest(r,["session"]),this.useVersions?[4,this.findOne(u,null,!0)]:[3,2]):(v(1),[2]):(console.log(new Date,this.collectionName,"Schema Errors - updateOne",i.validationErrors()),resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager().callMethod.call(resolveio_server_app_1.ResolveIOServer.getMainServer().getMethodManager(),"insertErrorLog","Schema Failed on updateOne - "+this.collectionName,[i.validationErrors(),p]),d(i.validationErrors()),[2]);case 1:return(r=e.sent())?(this.createLogs&&!h&&("https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:r._id,payload:(0,common_1.getBinarySize)(JSON.stringify([r,u,p,t]))<2e5?JSON.stringify([r,u,p,t],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:r._id,payload:(0,common_1.getBinarySize)(JSON.stringify([r,u,p,t]))<2e5?JSON.stringify([r,u,p,t],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},n)),(i=(0,common_1.deepCopy)(r))._id={_id:r._id,__v:r.__v},s=null,s=n?{session:o,upsert:!0}:{upsert:!0},resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).replaceOne({_id:{_id:r._id,__v:r.__v}},i,s),4<=r.__v&&resolveio_server_app_1.ResolveIOServer.getMongoManager().collection(this.versionCollection).deleteMany({$and:[{"_id._id":r._id},{"_id.__v":{$lt:r.__v-4}}]},n),objectContainsPropertyDeep(p,"__v")||(p.$inc?p.$inc.__v||(p.$inc.__v=1):p.$inc={__v:1}),a=new monitor_manager_1.MonitorMongo("updateOne",this.collectionName,JSON.stringify([u,p,t])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).updateOne(u,p,m).then(function(e){a.finish(),e.acknowledged?(g.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(g.collectionName),v(e.modifiedCount)):d(e.acknowledged)},function(e){a.finish(),console.log(new Date,"Error Update One",g.collectionName,u,p,t,e),d(e)})):m&&m.upsert?(p.$setOnInsert?(p.$setOnInsert._id||(p.$setOnInsert._id=(0,common_1.objectIdHexString)()),this.timestamps&&!p.$setOnInsert.createdAt&&(p.$setOnInsert.createdAt=new Date)):this.timestamps?p.$setOnInsert={_id:(0,common_1.objectIdHexString)(),__v:0,createdAt:new Date}:p.$setOnInsert={_id:(0,common_1.objectIdHexString)(),__v:0},this.createLogs&&!h&&("https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:p.$setOnInsert._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",u,p,t]))<2e5?JSON.stringify(["upsert",u,p,t],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:this.collectionName,id_document:p.$setOnInsert._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",u,p,t]))<2e5?JSON.stringify(["upsert",u,p,t],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},n)),l=new monitor_manager_1.MonitorMongo("updateOne",this.collectionName,JSON.stringify([u,p,t])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).updateOne(u,p,m).then(function(e){l.finish(),e.acknowledged?(g.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(g.collectionName),v(e.modifiedCount)):d(e.acknowledged)},function(e){l.finish(),console.log(new Date,"Error Update One",g.collectionName,u,p,t,e),d(e)})):d("No Document"),[3,3];case 2:m&&m.upsert&&(p.$setOnInsert?(p.$setOnInsert._id||(p.$setOnInsert._id=(0,common_1.objectIdHexString)()),this.timestamps&&!p.$setOnInsert.createdAt&&(p.$setOnInsert.createdAt=new Date)):this.timestamps?p.$setOnInsert={_id:(0,common_1.objectIdHexString)(),createdAt:new Date}:p.$setOnInsert={_id:(0,common_1.objectIdHexString)()}),this.createLogs&&!h?(m?m.returnDocument="before":m={returnDocument:"before"},c=new monitor_manager_1.MonitorMongo("findOneAndUpdate",this.collectionName,JSON.stringify([u,p,t])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).findOneAndUpdate(u,p,m).then(function(e){c.finish();var o=e.value;o?("https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:g.collectionName,id_document:o._id,payload:(0,common_1.getBinarySize)(JSON.stringify([o,u,p,t]))<2e5?JSON.stringify([o,u,p,t],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:g.collectionName,id_document:o._id,payload:(0,common_1.getBinarySize)(JSON.stringify([o,u,p,t]))<2e5?JSON.stringify([o,u,p,t],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date},n),v(e.ok)):p.$setOnInsert?("https://resolveio.com"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL&&"http://localhost:4200"!==resolveio_server_app_1.ResolveIOServer.getServerConfig().ROOT_URL?resolveio_server_app_1.ResolveIOServer.getLocalLogManager().writeLog({type:"log",data:{_id:(0,common_1.objectIdHexString)(),type:"document",collection:g.collectionName,id_document:p.$setOnInsert._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",u,p,t]))<2e5?JSON.stringify(["upsert",u,p,t],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}}):resolveio_server_app_1.ResolveIOServer.getMainDB().collection("logs").insertOne({_id:(0,common_1.objectIdHexString)(),type:"document",collection:g.collectionName,id_document:p.$setOnInsert._id,payload:(0,common_1.getBinarySize)(JSON.stringify(["upsert",u,p,t]))<2e5?JSON.stringify(["upsert",u,p,t],null,2):"Too Big",method:"updateOne",id_user:"",user:"",messageId:0,route:"",createdAt:new Date}),g.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(g.collectionName),v(e.ok)):v(0)},function(e){c.finish(),console.log(new Date,"Error Find One And Update",g.collectionName,u,p,t,e),d(e)})):(_=new monitor_manager_1.MonitorMongo("updateOne",this.collectionName,JSON.stringify([u,p,t])),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).updateOne(u,p,m).then(function(e){_.finish(),e.acknowledged?(g.skipCache||resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(g.collectionName),v(e.modifiedCount)):d(e.acknowledged)},function(e){_.finish(),console.log(new Date,"Error Update One",g.collectionName,u,p,t,e),d(e)})),e.label=3;case 3:return[2]}})})})},MongoManagerCollection.prototype.watchCollection=function(e,o,n){void 0===e&&(e=[]),void 0===n&&(n=!1);var t=resolveio_server_app_1.ResolveIOServer.getMongoManager().getSession();return t&&!n&&(o?o.session=t:o={session:t}),resolveio_server_app_1.ResolveIOServer.getMainDB().collection(this.collectionName,this.collectionOptions).watch(e,o)},(e=MongoManagerCollection).indexQueue=async_1.default.queue(function(c,_){return __awaiter(void 0,void 0,void 0,function(){var o,n,t,r,i,s,a,l;return __generator(e,function(e){switch(e.label){case 0:if(o=c.action,n=c.collection,t=c.specs,r=c.options,!(i=resolveio_server_app_1.ResolveIOServer.getMainDB().collection(n.collectionName,n.collectionOptions)))return[2,_(new Error("Collection ".concat(n.collectionName," not found")))];e.label=1;case 1:return e.trys.push([1,3,,10]),[4,i[o].apply(i,__spreadArray(__spreadArray([],__read(t),!1),[r],!1))];case 2:return a=e.sent(),_(null,a),[3,10];case 3:if(85!==(s=e.sent()).code&&!s.message.includes("IndexOptionsConflict"))return[3,9];e.label=4;case 4:return(e.trys.push([4,8,,9]),l=n.extractIndexNameFromError(s.message))?[4,i.dropIndex(l)]:[3,7];case 5:return e.sent(),[4,i[o].apply(i,__spreadArray(__spreadArray([],__read(t),!1),[r],!1))];case 6:return a=e.sent(),_(null,a),[2];case 7:return[3,9];case 8:return l=e.sent(),_(l),[3,9];case 9:return _(s),[3,10];case 10:return[2]}})})},1),MongoManagerCollection}()),MongoManagerUserCollection=function(e){function MongoManagerUserCollection(){return null!==e&&e.apply(this,arguments)||this}return __extends(MongoManagerUserCollection,e),MongoManagerUserCollection.prototype.authenticate=function(s,n){var e=this;return new Promise(function(r,i){return __awaiter(e,void 0,void 0,function(){var o,t=this;return __generator(this,function(e){switch(e.label){case 0:return(o=Math.pow(100,Math.log(s.attempts+1)),o=o<3e5?o:3e5,s.last)?Date.now()-s.last.getTime()<o?(s.last=new Date,[4,user_collection_1.Users.updateOne({_id:s._id},{$set:{last:s.last}})]):[3,2]:[3,3];case 1:return e.sent(),r({data:null,error:"Attempt Too Soon"}),[2];case 2:return[3,4];case 3:s.last=new Date,e.label=4;case 4:return 5<=s.attempts?r({data:null,error:"Too Many Attempts"}):s.salt?pbkdf2(n,s.salt,{iterations:25e3,keylen:512,digestAlgorithm:"sha256"},function(o,n){return __awaiter(t,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return o?[2,i(o)]:scmp(n,Buffer.from(s.hash,"hex"))?(s.last=new Date,s.attempts=0,[4,user_collection_1.Users.updateOne({_id:s._id},{$set:{last:s.last,attempts:s.attempts}})]):[3,2];case 1:return e.sent(),r({data:s,error:""}),[3,4];case 2:return s.last=new Date,s.attempts=s.attempts+1,[4,user_collection_1.Users.updateOne({_id:s._id},{$set:{last:s.last,attempts:s.attempts}})];case 3:e.sent(),5<=s.attempts?r({data:null,error:"Too Many Attempts"}):r({data:null,error:"Invalid Username And Password"}),e.label=4;case 4:return[2]}})})}):r({data:null,error:"No Salt Value Stored"}),[2]}})})})},MongoManagerUserCollection.prototype.serializeUser=function(){return function(e,o){o(null,e.username)}},MongoManagerUserCollection.prototype.deserializeUser=function(){return function(e,o){user_collection_1.Users.findOne({username:e}).then(function(e){o(null,e)},function(e){o(e,null)})}},MongoManagerUserCollection.prototype.setPassword=function(i,s){var e=this;return new Promise(function(t,r){return __awaiter(e,void 0,void 0,function(){var o,n;return __generator(this,function(e){switch(e.label){case 0:return i?[3,1]:(r("No User"),[3,5]);case 1:return s?[3,2]:(r("No Password"),[3,5]);case 2:return[4,randomBytes(32)];case 3:return o=e.sent(),o=o.toString("hex"),[4,pbkdf2Promisified(s,o,{iterations:25e3,keylen:512,digestAlgorithm:"sha256"})];case 4:n=e.sent(),n=Buffer.from(n,"binary").toString("hex"),user_collection_1.Users.updateOne({_id:i._id},{$set:{hash:n,salt:o,services:{},attempts:0}}).then(function(e){return t(e)},function(e){return r(e)}),e.label=5;case 5:return[2]}})})})},MongoManagerUserCollection.prototype.changePassword=function(r,i,s){var e=this;return new Promise(function(n,t){return __awaiter(e,void 0,void 0,function(){var o;return __generator(this,function(e){switch(e.label){case 0:return r?[3,1]:(t("Missing User"),[3,4]);case 1:return i&&s?[3,2]:(t("Missing Password"),[3,4]);case 2:return[4,this.authenticate(r,i)];case 3:(o=e.sent()).data?this.setPassword(r,s).then(function(e){return n(e)},function(e){return t(e)}):t(o.error),e.label=4;case 4:return[2]}})})})},MongoManagerUserCollection.prototype.register=function(t,r){var e=this;return new Promise(function(o,n){return __awaiter(e,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return t.username?[3,1]:(n("Missing Username"),[3,5]);case 1:return[4,user_collection_1.Users.findOne({username:t.username})];case 2:return e.sent()?(n("Username Exists"),[3,5]):[3,3];case 3:return[4,user_collection_1.Users.findOne({email:t.email})];case 4:e.sent()?n("Email Exists"):(t.setPassword(r),o(t)),e.label=5;case 5:return[2]}})})})},MongoManagerUserCollection.prototype.resetAttempts=function(e){return user_collection_1.Users.updateOne({_id:e._id},{$set:{attempts:0}})},MongoManagerUserCollection}(exports.MongoManagerCollection=MongoManagerCollection);function pbkdf2(e,o,n,t){crypto.pbkdf2(e,o,n.iterations,n.keylen,n.digestAlgorithm,t)}function pbkdf2Promisified(e,o,r){return new Promise(function(n,t){return pbkdf2(e,o,r,function(e,o){return e?t(e):n(o)})})}function randomBytes(e){return new Promise(function(n,t){return crypto.randomBytes(e,function(e,o){return e?t(e):n(o)})})}function objectContainsPropertyDeep(e,o){var n=Object.keys(e);if(n.some(function(e){return e===o}))return!0;for(var t=0;t<n.length;t++){var r=n[t];if(e[r]&&"object"==typeof e[r]&&objectContainsPropertyDeep(e[r],o))return!0}return!1}exports.MongoManagerUserCollection=MongoManagerUserCollection;
2
2
  //# sourceMappingURL=mongo.manager.js.map