blixify-server 0.1.30 → 0.1.31

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 +1 @@
1
- {"version":3,"file":"mongoWrapper.d.ts","sourceRoot":"","sources":["../../src/apis/mongoWrapper.ts"],"names":[],"mappings":"AAQA,OAAO,cAAc,MAAM,yBAAyB,CAAC;AAErD,MAAM,WAAW,UAAU;IACzB,OAAO,CAAC,EAAE,GAAG,CAAC;CACf;AAED;;;;GAIG;AACH,qBAAa,YAAY;IACvB,OAAO,EAAE,GAAG,CAAM;IAClB,UAAU,SAAM;IAChB,MAAM,UAAS;IACf,MAAM,EAAE,cAAc,CAQpB;IACF,GAAG,EAAE,UAAU,CAAC;IAChB,OAAO,SAAM;IACb,KAAK,EAAE,GAAG,CAAC;IAEX,YAAY,QAAS,GAAG,aAEtB;gBAGA,OAAO,EAAE,GAAG,EACZ,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,OAAO,EACf,MAAM,EAAE,cAAc,EACtB,YAAY,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,OAAO,KAAK,OAAO,EACrD,GAAG,EAAE,UAAU,EACf,OAAO,CAAC,EAAE,MAAM,EAChB,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,KAAK,IAAI;IAa3D,UAAU,SAAU,GAAG,SAIrB;IAEF,eAAe,QACR,GAAG,OACH,GAAG,yBACc,GAAG,EAAE,KAAK,QAAQ,GAAG,CAAC,gCAiD5C;IAEF,UAAU,QACH,GAAG,OACH,GAAG,qBACU,GAAG,KAAK,QAAQ,GAAG,CAAC,gCA+BtC;IAEF,OAAO,QAAe,GAAG,OAAO,GAAG,mBA8BjC;IAEF,eAAe,QAAe,GAAG,OAAO,GAAG,mBAkIzC;IAEF,UAAU,QAAe,GAAG,OAAO,GAAG,mBAgEpC;IAEF,eAAe,QACR,GAAG,OACH,GAAG,yBACc,GAAG,EAAE,KAAK,QAAQ,GAAG,CAAC,gCAuH5C;IAEF,UAAU,QACH,GAAG,OACH,GAAG,qBACU,GAAG,KAAK,QAAQ,GAAG,CAAC,gCAmCtC;IAEF,QAAQ,QAAe,GAAG,OAAO,GAAG,mBAyOlC;IAEF,IAAI,YA4EF;CACH"}
1
+ {"version":3,"file":"mongoWrapper.d.ts","sourceRoot":"","sources":["../../src/apis/mongoWrapper.ts"],"names":[],"mappings":"AAQA,OAAO,cAAc,MAAM,yBAAyB,CAAC;AAErD,MAAM,WAAW,UAAU;IACzB,OAAO,CAAC,EAAE,GAAG,CAAC;CACf;AAED;;;;GAIG;AACH,qBAAa,YAAY;IACvB,OAAO,EAAE,GAAG,CAAM;IAClB,UAAU,SAAM;IAChB,MAAM,UAAS;IACf,MAAM,EAAE,cAAc,CAQpB;IACF,GAAG,EAAE,UAAU,CAAC;IAChB,OAAO,SAAM;IACb,KAAK,EAAE,GAAG,CAAC;IAEX,YAAY,QAAS,GAAG,aAEtB;gBAGA,OAAO,EAAE,GAAG,EACZ,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,OAAO,EACf,MAAM,EAAE,cAAc,EACtB,YAAY,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,OAAO,KAAK,OAAO,EACrD,GAAG,EAAE,UAAU,EACf,OAAO,CAAC,EAAE,MAAM,EAChB,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,KAAK,IAAI;IAa3D,UAAU,SAAU,GAAG,SAIrB;IAEF,eAAe,QACR,GAAG,OACH,GAAG,yBACc,GAAG,EAAE,KAAK,QAAQ,GAAG,CAAC,gCAkD5C;IAEF,UAAU,QACH,GAAG,OACH,GAAG,qBACU,GAAG,KAAK,QAAQ,GAAG,CAAC,gCAiCtC;IAEF,OAAO,QAAe,GAAG,OAAO,GAAG,mBA+BjC;IAEF,eAAe,QAAe,GAAG,OAAO,GAAG,mBAmIzC;IAEF,UAAU,QAAe,GAAG,OAAO,GAAG,mBAkEpC;IAEF,eAAe,QACR,GAAG,OACH,GAAG,yBACc,GAAG,EAAE,KAAK,QAAQ,GAAG,CAAC,gCAyH5C;IAEF,UAAU,QACH,GAAG,OACH,GAAG,qBACU,GAAG,KAAK,QAAQ,GAAG,CAAC,gCAoCtC;IAEF,QAAQ,QAAe,GAAG,OAAO,GAAG,mBA2OlC;IAEF,IAAI,YA4EF;CACH"}
@@ -1 +1 @@
1
- "use strict";var __awaiter=this&&this.__awaiter||function(e,n,r,d){return new(r=r||Promise)(function(i,t){function s(e){try{o(d.next(e))}catch(e){t(e)}}function a(e){try{o(d.throw(e))}catch(e){t(e)}}function o(e){var t;e.done?i(e.value):((t=e.value)instanceof r?t:new r(function(e){e(t)})).then(s,a)}o((d=d.apply(e,n||[])).next())})},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.MongoWrapper=void 0;const moment_1=__importDefault(require("moment")),QueryModel_1=require("../model/QueryModel");class MongoWrapper{constructor(e,t,i,s,a,o,n,r){this.mongoDB="",this.collection="",this.isProd=!1,this.config={baseConfig:[],opsConfig:{read:[],create:[],update:[],delete:[]}},this.tableId="",this.modelChecker=e=>!1,this.parseModel=e=>(delete e._id,e.baseUpdatedAt=(0,moment_1.default)().toDate(),e),this.initBatchCreate=(a,o,n)=>__awaiter(this,void 0,void 0,function*(){try{if(Array.isArray(a.body.data)){let t=!0;a.body.data.map(e=>{this.modelChecker(e)||(t=!1)}),t||o.status(400).json({err:"Invalid Model Structure"});var e,s=(0,QueryModel_1.checkBaseConfig)(this.config,a);let i=!0;yield Promise.all(a.body.data.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"update",a,e)||(i=!1)})),s&&i?(e=a.body.data.map(e=>Object.assign(Object.assign({},e),{baseUpdatedAt:(0,moment_1.default)().toDate()})),n&&(yield n(e)),yield this.mongoDB.db(this.tableId).collection(this.collection).insertMany(e),o.send({success:!0})):o.status(400).json({err:"Invalid Security Configuration"})}else o.status(400).json({err:"Invalid Fields"})}catch(e){o.status(400).json({err:e})}}),this.initCreate=(i,s,a)=>__awaiter(this,void 0,void 0,function*(){try{var e,t;this.modelChecker(i.body.data)?(e=(0,QueryModel_1.checkBaseConfig)(this.config,i),t=(0,QueryModel_1.checkOpsConfig)(this.config,"create",i,i.body.data),e&&t?(i.body.data.baseUpdatedAt=(0,moment_1.default)().toDate(),a&&(yield a(i.body.data)),yield this.mongoDB.db(this.tableId).collection(this.collection).insertOne(i.body.data),s.send({success:!0})):s.status(400).json({err:"Invalid Security Configuration"})):s.status(400).json({err:"Invalid Model Structure"})}catch(e){s.status(400).json({err:e})}}),this.initGet=(a,o)=>__awaiter(this,void 0,void 0,function*(){try{var e,t,i,s=a.body.id;s?(e=yield this.mongoDB.db(this.tableId).collection(this.collection).findOne({_id:s}),t=(0,QueryModel_1.checkBaseConfig)(this.config,a),i=(0,QueryModel_1.checkOpsConfig)(this.config,"read",a,e),t&&i?o.send({data:e}):o.status(400).json({err:"Invalid Security Configuration"})):o.status(400).json({err:"Invalid Fields"})}catch(e){o.status(400).json({err:e})}}),this.initBatchUpdate=(y,g)=>__awaiter(this,void 0,void 0,function*(){var e,i;try{if(this.modelChecker(y.body.data)&&y.body.query){var s=y.body.unsetData,a=null!=(e=y.body.query)?e:[];const c=y.body.isOr?"$or":"$and",h={};h[c]=[],h.$or=[];var o=this.parseModel(null!=(i=y.body.data)?i:[]),n=(a&&0<a.length&&a.map(e=>{var t,i=null!=(t=e.queryId)?t:"";const s=e.value;switch(e.type){case"search":e.searchIds&&0<e.searchIds.length&&e.searchIds.map(e=>{h.$or.push({[e]:{$regex:s,$options:"i"}})});break;case"=":h[c].push({[i]:s});break;case"!=":h[c].push({[i]:{$ne:s}});break;case">":h[c].push({[i]:{$gt:s}});break;case"<":h[c].push({[i]:{$lt:s}});break;case"><":Array.isArray(s)&&2<=s.length&&(h[c].push({[i]:{$lt:s}}),h[i]={[i]:{$gte:s[0],$lte:s[1]}});break;case"in":h[c].push({[i]:{$in:s}})}}),(0,QueryModel_1.checkBaseConfig)(this.config,y));let t=!0;var r=this.mongoDB.db(this.tableId).collection(this.collection),d=yield(yield r.find(Object.assign({},h))).toArray();if(yield Promise.all(d.map(e=>{y.body.sensitive&&!(0,moment_1.default)(y.body.data.baseUpdatedAt).isAfter((0,moment_1.default)(e.baseUpdatedAt))&&g.status(400).json({err:"Refresh Sensitive Model"}),(0,QueryModel_1.checkOpsConfig)(this.config,"update",y,e)||(t=!1)})),n&&t){const u={};var l={};s&&(s.forEach(e=>{u[e]=""}),l.$unset=Object.assign({},u)),o&&(l.$set=Object.assign({},o)),yield r.updateMany(h,l),g.send({success:!0})}else g.status(400).json({err:"Invalid Security Configuration"})}}catch(e){g.status(400).json({err:e})}}),this.initUpdate=(r,d)=>__awaiter(this,void 0,void 0,function*(){try{var e,t,i,s,a,o,n=this.modelChecker(r.body.data)&&r.body.id;n?(t=yield(e=this.mongoDB.db(this.tableId).collection(this.collection)).findOne({_id:n}),this.debug&&this.debug(r.body,t),i=(0,QueryModel_1.checkBaseConfig)(this.config,r),s=(0,QueryModel_1.checkOpsConfig)(this.config,"update",r,t),i&&s?r.body.sensitive?(0,moment_1.default)(r.body.data.baseUpdatedAt).isAfter((0,moment_1.default)(t.baseUpdatedAt))?(a=this.parseModel(r.body.data),yield e.updateOne({_id:n},{$set:Object.assign({},a)}),d.send({success:!0})):d.status(400).json({err:"Refresh Sensitive Model"}):(o=this.parseModel(r.body.data),yield e.updateOne({_id:n},{$set:Object.assign({},o)}),d.send({success:!0})):d.status(400).json({err:"Invalid Security Configuration"})):d.status(400).json({err:"Invalid Model Structure"})}catch(e){this.debug&&this.debug(null,null,e),d.status(400).json({err:e})}}),this.initBatchDelete=(c,h,u)=>__awaiter(this,void 0,void 0,function*(){try{if(Array.isArray(c.body.id)){var e=this.mongoDB.db(this.tableId).collection(this.collection),i=yield e.find({_id:{$in:c.body.id}}),s=(0,QueryModel_1.checkBaseConfig)(this.config,c),a=yield i.toArray();let t=!0;0<a.length&&(yield Promise.all(a.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"delete",c,e)||(t=!1)}))),s&&t?(u&&(yield u(a)),yield e.deleteMany({_id:{$in:c.body.id}}),h.send({success:!0})):h.status(400).json({err:"Invalid Security Configuration"})}else{var o=c.body.query;const l={};o&&0<o.length&&o.map(e=>{var t,i=null!=(t=e.queryId)?t:"";const s=e.value;switch(e.type){case"search":e.searchIds&&0<e.searchIds.length&&(l.$or=[],e.searchIds.map(e=>{l.$or.push({[e]:{$regex:s,$options:"i"}})}));break;case"=":l[i]=s;break;case"!=":l[i]={$ne:s};break;case">":l[i]={$gt:s};break;case"<":l[i]={$lt:s};break;case"><":Array.isArray(s)&&2<=s.length&&(l[i]={$gte:s[0],$lte:s[1]});break;case"in":l[i]={$in:s}}});var n=this.mongoDB.db(this.tableId).collection(this.collection),r=yield n.find(l).toArray(),d=(0,QueryModel_1.checkBaseConfig)(this.config,c);let t=!0;0<r.length&&r.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"delete",c,e)||(t=!1)}),u&&(yield u(r)),d&&t?(yield n.deleteMany(l),h.send({success:!0})):h.status(400).json({err:"Invalid Security Configuration"})}}catch(e){h.status(400).json({err:e})}}),this.initDelete=(o,n,r)=>__awaiter(this,void 0,void 0,function*(){try{var e,t,i,s,a=o.body.id;a?(t=yield(e=this.mongoDB.db(this.tableId).collection(this.collection)).findOne({_id:a}),i=(0,QueryModel_1.checkBaseConfig)(this.config,o),s=(0,QueryModel_1.checkOpsConfig)(this.config,"delete",o,t),i&&s?(r&&(yield r(t)),yield e.findOneAndDelete({_id:a}),n.send({success:!0})):n.status(400).json({err:"Invalid Security Configuration"})):n.status(400).json({err:"Invalid Fields"})}catch(e){n.status(400).json({err:e})}}),this.initList=(A,B)=>__awaiter(this,void 0,void 0,function*(){var e,t,i,s,a,o;try{var n=(0,QueryModel_1.checkBaseConfig)(this.config,A);if(n){var r=A.body.aggregate,d=null!=(e=A.body.query)?e:[];const Y={};if(d&&0<d.length&&d.map(e=>{var t,i=null!=(t=e.queryId)?t:"";const s=e.value;switch(e.type){case"search":e.searchIds&&0<e.searchIds.length&&(Y.$or=[],e.searchIds.map(e=>{Y.$or.push({[e]:{$regex:s,$options:"i"}})}));break;case"=":Y[i]=s;break;case"!=":Y[i]={$ne:s};break;case">":Y[i]={$gt:s};break;case"<":Y[i]={$lt:s};break;case"><":Array.isArray(s)&&2<=s.length&&(Y[i]={$gte:s[0],$lte:s[1]});break;case"in":Y[i]={$in:s}}}),r){var l=[],c=null!=(t=r.queryId)?t:"",h=null!=(i=r.dateId)?i:"baseUpdatedAt";for(const O of null!=(s=r.range)?s:[]){Object.assign({},Y);var u=O.split("-"),[y,g=(0,moment_1.default)().format("DD/MM/YYYY HH:mm:ss")]=u,f=2===u.length,b=(0,moment_1.default)(y,"DD/MM/YYYY").isValid(),p=(0,moment_1.default)(g,"DD/MM/YYYY").isValid(),m=(0,moment_1.default)(y,"DD/MM/YYYY HH:mm:ss").format("YYYY-MM-DD HH:mm:ss"),v=(0,moment_1.default)(g,"DD/MM/YYYY HH:mm:ss").format("YYYY-MM-DD HH:mm:ss");if(!b||!p)return void B.status(400).json({err:"Invalid Aggregate Range Configuration"});var _=f?{$match:Object.assign(Object.assign({},Y),{[h]:{$gte:new Date(m),$lte:new Date(v)}})}:{$match:Object.assign(Object.assign({},Y),{[h]:{$lte:new Date(m)}})},M=[];let e=!1;var $=f?O:"Begining until "+(0,moment_1.default)(y,"DD/MM/YYYY HH:mm:ss").format("DD/MM/YYYY");switch(r.type){case"avg":M.push(_,{$group:{_id:$,value:{$avg:"$"+c}}});break;case"sum":M.push(_,{$group:{_id:$,value:{$sum:"$"+c}}});break;case"count":e=!0}if(0===M.length&&!e)return void B.status(400).json({err:"Invalid Aggregate Configuration"});var D=this.mongoDB.db(this.tableId).collection(this.collection);let t=0;t=e?yield D.find(_.$match).count({}):null!=(o=null==(a=(yield D.aggregate(M).toArray())[0])?void 0:a.value)?o:0,l.push({_id:$,value:t})}B.send({data:l})}else{let e=this.mongoDB.db(this.tableId).collection(this.collection).find(Y);var k,I,C=yield e.count({}),j=(A.body.cursor&&(e=e.skip(A.body.cursor)),A.body.limit&&(e=e.limit(A.body.limit)),A.body.sort&&(I={[(k=A.body.sort).sortId]:"asc"===k.type?1:-1},e=e.sort(I)),yield e.toArray());let t=!0;0<j.length&&j.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"read",A,e)||(t=!1)}),n&&t?B.send({data:j,count:C}):B.status(400).json({err:"Invalid Security Configuration"})}}else B.status(400).json({err:"Invalid Security Configuration"})}catch(e){B.status(400).json({err:e})}}),this.init=()=>{var e=this.lib.express.Router();return e.post("/create",(e,t)=>{this.initCreate(e,t)}),e.post("/batchCreate",(e,t)=>{this.initBatchCreate(e,t)}),e.post("/get",(e,t)=>{this.initGet(e,t)}),e.post("/update",(e,t)=>{this.initUpdate(e,t)}),e.post("/batchUpdate",(e,t)=>{this.initBatchUpdate(e,t)}),e.post("/delete",(e,t)=>{this.initDelete(e,t)}),e.post("/batchDelete",(e,t)=>{this.initBatchDelete(e,t)}),e.post("/list",(e,t)=>{this.initList(e,t)}),e},this.mongoDB=e,this.collection=t,this.isProd=i,this.config=s,this.modelChecker=a,this.lib=o;e=this.isProd?"prod":"dev";this.tableId=n?n+"-"+e:e,this.debug=r}}exports.MongoWrapper=MongoWrapper;
1
+ "use strict";var __awaiter=this&&this.__awaiter||function(e,n,r,d){return new(r=r||Promise)(function(s,t){function i(e){try{o(d.next(e))}catch(e){t(e)}}function a(e){try{o(d.throw(e))}catch(e){t(e)}}function o(e){var t;e.done?s(e.value):((t=e.value)instanceof r?t:new r(function(e){e(t)})).then(i,a)}o((d=d.apply(e,n||[])).next())})},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.MongoWrapper=void 0;const moment_1=__importDefault(require("moment")),QueryModel_1=require("../model/QueryModel");class MongoWrapper{constructor(e,t,s,i,a,o,n,r){this.mongoDB="",this.collection="",this.isProd=!1,this.config={baseConfig:[],opsConfig:{read:[],create:[],update:[],delete:[]}},this.tableId="",this.modelChecker=e=>!1,this.parseModel=e=>(delete e._id,e.baseUpdatedAt=(0,moment_1.default)().toDate(),e),this.initBatchCreate=(o,n,r)=>__awaiter(this,void 0,void 0,function*(){var e;try{if(Array.isArray(o.body.data)){let t=!0;o.body.data.map(e=>{this.modelChecker(e)||(t=!1)}),t||n.status(400).json({err:"Invalid Model Structure"});var i,a=(0,QueryModel_1.checkBaseConfig)(this.config,o);let s=!0;yield Promise.all(o.body.data.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"update",o,e)||(s=!1)})),a&&s?(i=o.body.data.map(e=>Object.assign(Object.assign({},e),{baseUpdatedAt:(0,moment_1.default)().toDate()})),r&&(yield r(i)),yield this.mongoDB.db(this.tableId).collection(this.collection).insertMany(i),null!=(e=o.body)&&e.stopRes||n.send({success:!0})):n.status(400).json({err:"Invalid Security Configuration"})}else n.status(400).json({err:"Invalid Fields"})}catch(e){n.status(400).json({err:e})}}),this.initCreate=(i,a,o)=>__awaiter(this,void 0,void 0,function*(){var e,t,s;try{this.modelChecker(i.body.data)?(t=(0,QueryModel_1.checkBaseConfig)(this.config,i),s=(0,QueryModel_1.checkOpsConfig)(this.config,"create",i,i.body.data),t&&s?(i.body.data.baseUpdatedAt=(0,moment_1.default)().toDate(),o&&(yield o(i.body.data)),yield this.mongoDB.db(this.tableId).collection(this.collection).insertOne(i.body.data),null!=(e=i.body)&&e.stopRes||a.send({success:!0})):a.status(400).json({err:"Invalid Security Configuration"})):a.status(400).json({err:"Invalid Model Structure"})}catch(e){a.status(400).json({err:e})}}),this.initGet=(o,n)=>__awaiter(this,void 0,void 0,function*(){var e;try{var t,s,i,a=o.body.id;a?(t=yield this.mongoDB.db(this.tableId).collection(this.collection).findOne({_id:a}),s=(0,QueryModel_1.checkBaseConfig)(this.config,o),i=(0,QueryModel_1.checkOpsConfig)(this.config,"read",o,t),s&&i?null!=(e=o.body)&&e.stopRes||n.send({data:t}):n.status(400).json({err:"Invalid Security Configuration"})):n.status(400).json({err:"Invalid Fields"})}catch(e){n.status(400).json({err:e})}}),this.initBatchUpdate=(g,b)=>__awaiter(this,void 0,void 0,function*(){var e,s,i;try{if(this.modelChecker(g.body.data)&&g.body.query){var a=g.body.unsetData,o=null!=(e=g.body.query)?e:[];const u=g.body.isOr?"$or":"$and",h={};h[u]=[],h.$or=[];var n=this.parseModel(null!=(s=g.body.data)?s:[]),r=(o&&0<o.length&&o.map(e=>{var t,s=null!=(t=e.queryId)?t:"";const i=e.value;switch(e.type){case"search":e.searchIds&&0<e.searchIds.length&&e.searchIds.map(e=>{h.$or.push({[e]:{$regex:i,$options:"i"}})});break;case"=":h[u].push({[s]:i});break;case"!=":h[u].push({[s]:{$ne:i}});break;case">":h[u].push({[s]:{$gt:i}});break;case"<":h[u].push({[s]:{$lt:i}});break;case"><":Array.isArray(i)&&2<=i.length&&(h[u].push({[s]:{$lt:i}}),h[s]={[s]:{$gte:i[0],$lte:i[1]}});break;case"in":h[u].push({[s]:{$in:i}})}}),(0,QueryModel_1.checkBaseConfig)(this.config,g));let t=!0;var d=this.mongoDB.db(this.tableId).collection(this.collection),l=yield(yield d.find(Object.assign({},h))).toArray();if(yield Promise.all(l.map(e=>{g.body.sensitive&&!(0,moment_1.default)(g.body.data.baseUpdatedAt).isAfter((0,moment_1.default)(e.baseUpdatedAt))&&b.status(400).json({err:"Refresh Sensitive Model"}),(0,QueryModel_1.checkOpsConfig)(this.config,"update",g,e)||(t=!1)})),r&&t){const y={};var c={};a&&(a.forEach(e=>{y[e]=""}),c.$unset=Object.assign({},y)),n&&(c.$set=Object.assign({},n)),yield d.updateMany(h,c),null!=(i=g.body)&&i.stopRes||b.send({success:!0})}else b.status(400).json({err:"Invalid Security Configuration"})}}catch(e){b.status(400).json({err:e})}}),this.initUpdate=(l,c)=>__awaiter(this,void 0,void 0,function*(){var e,t;try{var s,i,a,o,n,r,d=this.modelChecker(l.body.data)&&l.body.id;d?(i=yield(s=this.mongoDB.db(this.tableId).collection(this.collection)).findOne({_id:d}),this.debug&&this.debug(l.body,i),a=(0,QueryModel_1.checkBaseConfig)(this.config,l),o=(0,QueryModel_1.checkOpsConfig)(this.config,"update",l,i),a&&o?l.body.sensitive?(0,moment_1.default)(l.body.data.baseUpdatedAt).isAfter((0,moment_1.default)(i.baseUpdatedAt))?(n=this.parseModel(l.body.data),yield s.updateOne({_id:d},{$set:Object.assign({},n)}),null!=(e=l.body)&&e.stopRes||c.send({success:!0})):c.status(400).json({err:"Refresh Sensitive Model"}):(r=this.parseModel(l.body.data),yield s.updateOne({_id:d},{$set:Object.assign({},r)}),null!=(t=l.body)&&t.stopRes||c.send({success:!0})):c.status(400).json({err:"Invalid Security Configuration"})):c.status(400).json({err:"Invalid Model Structure"})}catch(e){this.debug&&this.debug(null,null,e),c.status(400).json({err:e})}}),this.initBatchDelete=(h,y,g)=>__awaiter(this,void 0,void 0,function*(){var e,s;try{if(Array.isArray(h.body.id)){var i=this.mongoDB.db(this.tableId).collection(this.collection),a=yield i.find({_id:{$in:h.body.id}}),o=(0,QueryModel_1.checkBaseConfig)(this.config,h),n=yield a.toArray();let t=!0;0<n.length&&(yield Promise.all(n.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"delete",h,e)||(t=!1)}))),o&&t?(g&&(yield g(n)),yield i.deleteMany({_id:{$in:h.body.id}}),null!=(e=h.body)&&e.stopRes||y.send({success:!0})):y.status(400).json({err:"Invalid Security Configuration"})}else{var r=h.body.query;const u={};r&&0<r.length&&r.map(e=>{var t,s=null!=(t=e.queryId)?t:"";const i=e.value;switch(e.type){case"search":e.searchIds&&0<e.searchIds.length&&(u.$or=[],e.searchIds.map(e=>{u.$or.push({[e]:{$regex:i,$options:"i"}})}));break;case"=":u[s]=i;break;case"!=":u[s]={$ne:i};break;case">":u[s]={$gt:i};break;case"<":u[s]={$lt:i};break;case"><":Array.isArray(i)&&2<=i.length&&(u[s]={$gte:i[0],$lte:i[1]});break;case"in":u[s]={$in:i}}});var d=this.mongoDB.db(this.tableId).collection(this.collection),l=yield d.find(u).toArray(),c=(0,QueryModel_1.checkBaseConfig)(this.config,h);let t=!0;0<l.length&&l.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"delete",h,e)||(t=!1)}),g&&(yield g(l)),c&&t?(yield d.deleteMany(u),null!=(s=h.body)&&s.stopRes||y.send({success:!0})):y.status(400).json({err:"Invalid Security Configuration"})}}catch(e){y.status(400).json({err:e})}}),this.initDelete=(n,r,d)=>__awaiter(this,void 0,void 0,function*(){var e;try{var t,s,i,a,o=n.body.id;o?(s=yield(t=this.mongoDB.db(this.tableId).collection(this.collection)).findOne({_id:o}),i=(0,QueryModel_1.checkBaseConfig)(this.config,n),a=(0,QueryModel_1.checkOpsConfig)(this.config,"delete",n,s),i&&a?(d&&(yield d(s)),yield t.findOneAndDelete({_id:o}),null!=(e=n.body)&&e.stopRes||r.send({success:!0})):r.status(400).json({err:"Invalid Security Configuration"})):r.status(400).json({err:"Invalid Fields"})}catch(e){r.status(400).json({err:e})}}),this.initList=(w,Q)=>__awaiter(this,void 0,void 0,function*(){var e,t,s,i,a,o,n,r;try{var d=(0,QueryModel_1.checkBaseConfig)(this.config,w);if(d){var l=w.body.aggregate,c=null!=(e=w.body.query)?e:[];const A={};if(c&&0<c.length&&c.map(e=>{var t,s=null!=(t=e.queryId)?t:"";const i=e.value;switch(e.type){case"search":e.searchIds&&0<e.searchIds.length&&(A.$or=[],e.searchIds.map(e=>{A.$or.push({[e]:{$regex:i,$options:"i"}})}));break;case"=":A[s]=i;break;case"!=":A[s]={$ne:i};break;case">":A[s]={$gt:i};break;case"<":A[s]={$lt:i};break;case"><":Array.isArray(i)&&2<=i.length&&(A[s]={$gte:i[0],$lte:i[1]});break;case"in":A[s]={$in:i}}}),l){var u=[],h=null!=(t=l.queryId)?t:"",y=null!=(s=l.dateId)?s:"baseUpdatedAt";for(const B of null!=(i=l.range)?i:[]){Object.assign({},A);var g=B.split("-"),[b,f=(0,moment_1.default)().format("DD/MM/YYYY HH:mm:ss")]=g,p=2===g.length,m=(0,moment_1.default)(b,"DD/MM/YYYY").isValid(),v=(0,moment_1.default)(f,"DD/MM/YYYY").isValid(),_=(0,moment_1.default)(b,"DD/MM/YYYY HH:mm:ss").format("YYYY-MM-DD HH:mm:ss"),M=(0,moment_1.default)(f,"DD/MM/YYYY HH:mm:ss").format("YYYY-MM-DD HH:mm:ss");if(!m||!v)return void Q.status(400).json({err:"Invalid Aggregate Range Configuration"});var $=p?{$match:Object.assign(Object.assign({},A),{[y]:{$gte:new Date(_),$lte:new Date(M)}})}:{$match:Object.assign(Object.assign({},A),{[y]:{$lte:new Date(_)}})},D=[];let e=!1;var k=p?B:"Begining until "+(0,moment_1.default)(b,"DD/MM/YYYY HH:mm:ss").format("DD/MM/YYYY");switch(l.type){case"avg":D.push($,{$group:{_id:k,value:{$avg:"$"+h}}});break;case"sum":D.push($,{$group:{_id:k,value:{$sum:"$"+h}}});break;case"count":e=!0}if(0===D.length&&!e)return void Q.status(400).json({err:"Invalid Aggregate Configuration"});var I=this.mongoDB.db(this.tableId).collection(this.collection);let t=0;t=e?yield I.find($.$match).count({}):null!=(o=null==(a=(yield I.aggregate(D).toArray())[0])?void 0:a.value)?o:0,u.push({_id:k,value:t})}null!=(n=w.body)&&n.stopRes||Q.send({data:u})}else{let e=this.mongoDB.db(this.tableId).collection(this.collection).find(A);var C,j,Y=yield e.count({}),O=(w.body.cursor&&(e=e.skip(w.body.cursor)),w.body.limit&&(e=e.limit(w.body.limit)),w.body.sort&&(j={[(C=w.body.sort).sortId]:"asc"===C.type?1:-1},e=e.sort(j)),yield e.toArray());let t=!0;0<O.length&&O.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"read",w,e)||(t=!1)}),d&&t?null!=(r=w.body)&&r.stopRes||Q.send({data:O,count:Y}):Q.status(400).json({err:"Invalid Security Configuration"})}}else Q.status(400).json({err:"Invalid Security Configuration"})}catch(e){Q.status(400).json({err:e})}}),this.init=()=>{var e=this.lib.express.Router();return e.post("/create",(e,t)=>{this.initCreate(e,t)}),e.post("/batchCreate",(e,t)=>{this.initBatchCreate(e,t)}),e.post("/get",(e,t)=>{this.initGet(e,t)}),e.post("/update",(e,t)=>{this.initUpdate(e,t)}),e.post("/batchUpdate",(e,t)=>{this.initBatchUpdate(e,t)}),e.post("/delete",(e,t)=>{this.initDelete(e,t)}),e.post("/batchDelete",(e,t)=>{this.initBatchDelete(e,t)}),e.post("/list",(e,t)=>{this.initList(e,t)}),e},this.mongoDB=e,this.collection=t,this.isProd=s,this.config=i,this.modelChecker=a,this.lib=o;e=this.isProd?"prod":"dev";this.tableId=n?n+"-"+e:e,this.debug=r}}exports.MongoWrapper=MongoWrapper;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blixify-server",
3
- "version": "0.1.30",
3
+ "version": "0.1.31",
4
4
  "license": "MIT",
5
5
  "main": "dist/apis/index.js",
6
6
  "private": false,