blixify-server 0.1.51 → 0.1.53

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
- "use strict";var __awaiter=this&&this.__awaiter||function(e,n,r,d){return new(r=r||Promise)(function(i,t){function s(e){try{a(d.next(e))}catch(e){t(e)}}function o(e){try{a(d.throw(e))}catch(e){t(e)}}function a(e){var t;e.done?i(e.value):((t=e.value)instanceof r?t:new r(function(e){e(t)})).then(s,o)}a((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_timezone_1=__importDefault(require("moment-timezone")),QueryModel_1=require("../model/QueryModel"),utils_1=require("./utils"),handleParseQueryFilter=(e,o)=>{const a={};return e&&0<e.length&&e.map(e=>{var t;let i=null!=(t=e.queryId)?t:"";o&&(i=o+"."+i);const s=e.value;switch(e.type){case"search":e.searchIds&&0<e.searchIds.length&&(a.$or=[],e.searchIds.map(e=>{a.$or.push({[e]:{$regex:s,$options:"i"}})}));break;case"=":a[i]=s;break;case"!=":a[i]={$ne:s};break;case">":a[i]={$gt:s};break;case"<":a[i]={$lt:s};break;case"><":Array.isArray(s)&&2<=s.length&&(a[i]={$gte:s[0],$lte:s[1]});break;case"in":a[i]={$in:s}}}),a};class MongoWrapper{constructor(e,t,i,s,o,a,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_timezone_1.default)().toDate(),e),this.initBatchCreate=(u,c,h)=>__awaiter(this,void 0,void 0,function*(){var e;try{if(Array.isArray(u.body.data)){let t=!0;u.body.data.map(e=>{this.modelChecker(e)||(t=!1)}),t||c.status(400).json({err:"Invalid Model Structure"});var s=(0,QueryModel_1.checkBaseConfig)(this.config,u);let i=!0;const d=[];if(yield Promise.all(u.body.data.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"update",u,e)||(i=!1),u.body.unique&&null!=e&&e[u.body.unique]&&d.push(e[u.body.unique])})),s&&i){var o=this.mongoDB.db(this.tableId).collection(this.collection);if(0<d.length){var a=yield(yield o.find({[u.body.unique]:{$in:d}})).toArray();if(0<a.length)return void c.status(400).json({err:`Unique:${a.map(e=>e[u.body.unique])} exists`})}const l=[];var n=u.body.data.map(e=>(l.push(e._id),Object.assign(Object.assign({},e),{baseUpdatedAt:(0,moment_timezone_1.default)().toDate()}))),r=(h&&(yield h(n)),yield o.insertMany(n),{success:l});if(null!=(e=u.body)&&e.stopRes)return r;c.send(r)}else c.status(400).json({err:"Invalid Security Configuration"})}else c.status(400).json({err:"Invalid Fields"})}catch(e){c.status(400).json({err:e})}}),this.initCreate=(n,r,d)=>__awaiter(this,void 0,void 0,function*(){var e,t;try{if(this.modelChecker(n.body.data)){var i=(0,QueryModel_1.checkBaseConfig)(this.config,n),s=(0,QueryModel_1.checkOpsConfig)(this.config,"create",n,n.body.data);if(i&&s){var o=this.mongoDB.db(this.tableId).collection(this.collection);if(n.body.data.baseUpdatedAt=(0,moment_timezone_1.default)().toDate(),n.body.unique&&null!=(e=n.body.data)&&e[n.body.unique])if(yield o.findOne({[n.body.unique]:n.body.data[n.body.unique]}))return void r.status(400).json({err:`Unique:${n.body.data[n.body.unique]} exists`});d&&(yield d(n.body.data)),yield o.insertOne(n.body.data);var a={success:!0};if(null!=(t=n.body)&&t.stopRes)return a;r.send(a)}else r.status(400).json({err:"Invalid Security Configuration"})}else r.status(400).json({err:"Invalid Model Structure"})}catch(e){r.status(400).json({err:e})}}),this.initGet=(n,r)=>__awaiter(this,void 0,void 0,function*(){var e;try{var t=n.body.id;if(t){var i,s=yield this.mongoDB.db(this.tableId).collection(this.collection).findOne({_id:t}),o=(0,QueryModel_1.checkBaseConfig)(this.config,n),a=(0,QueryModel_1.checkOpsConfig)(this.config,"read",n,s);if(o&&a)return i={data:s},null!=(e=n.body)&&e.stopRes?i:void r.send(i);r.status(400).json({err:"Invalid Security Configuration"})}else r.status(400).json({err:"Invalid Fields"})}catch(e){r.status(400).json({err:e})}}),this.initBatchUpdate=(p,m,v)=>__awaiter(this,void 0,void 0,function*(){var e,i,s;try{if(this.modelChecker(p.body.data)&&p.body.query||p.body.unsetData){var o=p.body.unsetData,a=null!=(e=p.body.query)?e:[];const y=p.body.isOr?"$or":"$and",f={};var n=this.parseModel(null!=(i=p.body.data)?i:[]),r=(a&&0<a.length&&(f[y]=[],a.some(e=>"search"===e.type)&&(f.$or=[]),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=>{f.$or.push({[e]:{$regex:s,$options:"i"}})});break;case"=":f[y].push({[i]:s});break;case"!=":f[y].push({[i]:{$ne:s}});break;case">":f[y].push({[i]:{$gt:s}});break;case"<":f[y].push({[i]:{$lt:s}});break;case"><":Array.isArray(s)&&2<=s.length&&(f[y].push({[i]:{$lt:s}}),f[y]={[i]:{$gte:s[0],$lte:s[1]}});break;case"in":f[y].push({[i]:{$in:s}})}})),(0,QueryModel_1.checkBaseConfig)(this.config,p));let t=!0;var d=this.mongoDB.db(this.tableId).collection(this.collection),l=yield(yield d.find(Object.assign({},f))).toArray();const b=[];if(yield Promise.all(l.map(e=>{p.body.sensitive&&!(0,moment_timezone_1.default)(p.body.data.baseUpdatedAt).isAfter((0,moment_timezone_1.default)(e.baseUpdatedAt))&&m.status(400).json({err:"Refresh Sensitive Model"}),(0,QueryModel_1.checkOpsConfig)(this.config,"update",p,e)||(t=!1);e=(0,utils_1.compareUpdatedFields)(e,p.body.data).workflowUpdateFields;b.concat(e)})),r&&t){const g={};var u={},c=(o&&(o.forEach(e=>{g[e]=""}),u.$unset=Object.assign({},g)),n&&(u.$set=Object.assign({},n)),v&&(yield v(b)),yield d.updateMany(f,u),b.map(e=>e._id)),h={success:c};if(null!=(s=p.body)&&s.stopRes)return h;m.send(h)}else m.status(400).json({err:"Invalid Security Configuration"})}}catch(e){m.status(400).json({err:e})}}),this.initUpdate=(y,f,b)=>__awaiter(this,void 0,void 0,function*(){var e,t,i;try{var s=this.modelChecker(y.body.data)&&y.body.id;if(s){var o=this.mongoDB.db(this.tableId).collection(this.collection),a=yield o.findOne({_id:s}),n=(this.debug&&this.debug(y.body,a),(0,QueryModel_1.checkBaseConfig)(this.config,y)),r=(0,QueryModel_1.checkOpsConfig)(this.config,"update",y,a);if(n&&r){if(y.body.unique&&null!=(e=y.body.data)&&e[y.body.unique]){var d=yield o.findOne({[y.body.unique]:y.body.data[y.body.unique]});if(d&&y.body.id!==d._id)return void f.status(400).json({err:y.body.data[y.body.unique]+" exists"})}var{workflowUpdateFields:l,updatedFields:u}=(0,utils_1.compareUpdatedFields)(a,y.body.data),c={success:!0},h=(b&&(yield b(l)),this.parseModel(u));if(y.body.sensitive)if((0,moment_timezone_1.default)(y.body.data.baseUpdatedAt).isAfter((0,moment_timezone_1.default)(a.baseUpdatedAt))){if(yield o.updateOne({_id:s},{$set:Object.assign({},h)}),null!=(t=y.body)&&t.stopRes)return c;f.send(c)}else f.status(400).json({err:"Refresh Sensitive Model"});else{if(yield o.updateOne({_id:s},{$set:Object.assign({},h)}),null!=(i=y.body)&&i.stopRes)return c;f.send(c)}}else f.status(400).json({err:"Invalid Security Configuration"})}else f.status(400).json({err:"Invalid Model Structure"})}catch(e){this.debug&&this.debug(null,null,e),f.status(400).json({err:e})}}),this.initBatchDelete=(b,g,p)=>__awaiter(this,void 0,void 0,function*(){var e,i;try{if(Array.isArray(b.body.id)){var s=this.mongoDB.db(this.tableId).collection(this.collection),o=yield s.find({_id:{$in:b.body.id}}),a=(0,QueryModel_1.checkBaseConfig)(this.config,b),n=yield o.toArray();let t=!0;if(0<n.length&&(yield Promise.all(n.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"delete",b,e)||(t=!1)}))),a&&t){p&&(yield p(n)),yield s.deleteMany({_id:{$in:b.body.id}});var r={success:b.body.id};if(null!=(e=b.body)&&e.stopRes)return r;g.send(r)}else g.status(400).json({err:"Invalid Security Configuration"})}else{var d=b.body.query;const y={};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}}});var l=this.mongoDB.db(this.tableId).collection(this.collection),u=yield l.find(y).toArray(),c=(0,QueryModel_1.checkBaseConfig)(this.config,b);let t=!0;const f=[];if(0<u.length&&u.map(e=>{f.push(e._id),(0,QueryModel_1.checkOpsConfig)(this.config,"delete",b,e)||(t=!1)}),p&&(yield p(u)),c&&t){yield l.deleteMany(y);var h={success:f};if(null!=(i=b.body)&&i.stopRes)return h;g.send(h)}else g.status(400).json({err:"Invalid Security Configuration"})}}catch(e){g.status(400).json({err:e})}}),this.initDelete=(r,d,l)=>__awaiter(this,void 0,void 0,function*(){var e;try{var t=r.body.id;if(t){var i=this.mongoDB.db(this.tableId).collection(this.collection),s=yield i.findOne({_id:t}),o=(0,QueryModel_1.checkBaseConfig)(this.config,r),a=(0,QueryModel_1.checkOpsConfig)(this.config,"delete",r,s);if(o&&a){l&&(yield l(s)),yield i.findOneAndDelete({_id:t});var n={success:!0};if(null!=(e=r.body)&&e.stopRes)return n;d.send(n)}else d.status(400).json({err:"Invalid Security Configuration"})}else d.status(400).json({err:"Invalid Fields"})}catch(e){d.status(400).json({err:e})}}),this.initList=(x,P)=>__awaiter(this,void 0,void 0,function*(){var e,t,i,s,o,a,n,r,d,l,u,c;try{var h=(0,QueryModel_1.checkBaseConfig)(this.config,x);if(h){var y=x.body.aggregate,f=null!=(e=x.body.query)?e:[],b=handleParseQueryFilter(f);if(y){var g=[],p=null!=(t=y.queryId)?t:"",m=null!=(i=y.dateId)?i:"baseUpdatedAt";for(const U of null!=(s=y.range)?s:[(0,moment_timezone_1.default)().format("DD/MM/YYYY HH:mm:ss")]){Object.assign({},b);var v=U.split("-"),[_,$=(0,moment_timezone_1.default)().format("DD/MM/YYYY HH:mm:ss")]=v,M=2===v.length,k=(0,moment_timezone_1.default)(_,"DD/MM/YYYY").isValid(),z=(0,moment_timezone_1.default)($,"DD/MM/YYYY").isValid(),I=(0,moment_timezone_1.default)(_,"DD/MM/YYYY HH:mm:ss").tz("Asia/Kuala_lumpur").toDate(),R=(0,moment_timezone_1.default)($,"DD/MM/YYYY HH:mm:ss").tz("Asia/Kuala_lumpur").toDate();if(!k||!z)return void P.status(400).json({err:"Invalid Aggregate Range Configuration"});var j=M?{$match:Object.assign(Object.assign({},b),{[m]:{$gte:I,$lte:R}})}:{$match:Object.assign(Object.assign({},b),{[m]:{$lte:I}})},D=[];let e=!1;var C=M?U:"Begining until "+(0,moment_timezone_1.default)(_,"DD/MM/YYYY HH:mm:ss").format("DD/MM/YYYY");switch(y.type){case"avg":D.push(j,{$group:{_id:C,value:{$avg:"$"+p}}});break;case"sum":D.push(j,{$group:{_id:C,value:{$sum:"$"+p}}});break;case"count":e=!0;break;case"unwind":var O="model",S=handleParseQueryFilter(null!=(a=null==(o=y.unwind)?void 0:o.query)?a:[],O);D.push(j,{$lookup:{from:null==(n=y.unwind)?void 0:n.collectionId,localField:y.queryId,foreignField:"_id",as:O}},{$unwind:"$"+O},{$match:S},{$count:"value"})}if(0===D.length&&!e)return void P.status(400).json({err:"Invalid Aggregate Configuration"});var Y=this.mongoDB.db(this.tableId).collection(this.collection);let t=0;t=e?yield Y.find(j.$match).count({}):null!=(d=null==(r=(yield Y.aggregate(D).toArray())[0])?void 0:r.value)?d:0,g.push({_id:C,value:t})}var A={data:g};if(null!=(l=x.body)&&l.stopRes)return A;P.send(A)}else{let e=this.mongoDB.db(this.tableId).collection(this.collection).find(b);var q,B,w,F=yield e.count({}),Q=(x.body.cursor&&(e=e.skip(x.body.cursor)),x.body.stopLimit||(e=e.limit(null!=(u=x.body.limit)?u:10)),x.body.sort&&(B={[(q=x.body.sort).sortId]:"asc"===q.type?1:-1},e=e.sort(B)),yield e.toArray());let t=!0;if(0<Q.length&&Q.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"read",x,e)||(t=!1)}),h&&t)return w={data:Q,count:F},null!=(c=x.body)&&c.stopRes?w:void P.send(w);P.status(400).json({err:"Invalid Security Configuration"})}}else P.status(400).json({err:"Invalid Security Configuration"})}catch(e){P.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=o,this.lib=a;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(i,t){function s(e){try{a(d.next(e))}catch(e){t(e)}}function o(e){try{a(d.throw(e))}catch(e){t(e)}}function a(e){var t;e.done?i(e.value):((t=e.value)instanceof r?t:new r(function(e){e(t)})).then(s,o)}a((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_timezone_1=__importDefault(require("moment-timezone")),QueryModel_1=require("../model/QueryModel"),utils_1=require("./utils"),handleParseQueryFilter=(e,o)=>{const a={};return e&&0<e.length&&e.map(e=>{var t;let i=null!=(t=e.queryId)?t:"";o&&(i=o+"."+i);const s=e.value;switch(e.type){case"search":e.searchIds&&0<e.searchIds.length&&(a.$or=[],e.searchIds.map(e=>{a.$or.push({[e]:{$regex:s,$options:"i"}})}));break;case"=":a[i]=s;break;case"!=":a[i]={$ne:s};break;case">":a[i]={$gt:s};break;case"<":a[i]={$lt:s};break;case"><":Array.isArray(s)&&2<=s.length&&(a[i]={$gte:s[0],$lte:s[1]});break;case"in":a[i]={$in:s}}}),a};class MongoWrapper{constructor(e,t,i,s,o,a,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_timezone_1.default)().toDate(),e),this.initBatchCreate=(u,c,h)=>__awaiter(this,void 0,void 0,function*(){var e;try{if(Array.isArray(u.body.data)){let t=!0;u.body.data.map(e=>{this.modelChecker(e)||(t=!1)}),t||c.status(400).json({err:"Invalid Model Structure"});var s=(0,QueryModel_1.checkBaseConfig)(this.config,u);let i=!0;const d=[];if(yield Promise.all(u.body.data.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"update",u,e)||(i=!1),u.body.unique&&null!=e&&e[u.body.unique]&&d.push(e[u.body.unique])})),s&&i){var o=this.mongoDB.db(this.tableId).collection(this.collection);if(0<d.length){var a=yield(yield o.find({[u.body.unique]:{$in:d}})).toArray();if(0<a.length)return void c.status(400).json({err:`Unique:${a.map(e=>e[u.body.unique])} exists`})}const l=[];var n=u.body.data.map(e=>(l.push(e._id),Object.assign(Object.assign({},e),{baseUpdatedAt:(0,moment_timezone_1.default)().toDate()}))),r=(h&&(yield h(n)),yield o.insertMany(n),{success:l});if(null!=(e=u.body)&&e.stopRes)return r;c.send(r)}else c.status(400).json({err:"Invalid Security Configuration"})}else c.status(400).json({err:"Invalid Fields"})}catch(e){c.status(400).json({err:e})}}),this.initCreate=(n,r,d)=>__awaiter(this,void 0,void 0,function*(){var e,t;try{if(this.modelChecker(n.body.data)){var i=(0,QueryModel_1.checkBaseConfig)(this.config,n),s=(0,QueryModel_1.checkOpsConfig)(this.config,"create",n,n.body.data);if(i&&s){var o=this.mongoDB.db(this.tableId).collection(this.collection);if(n.body.data.baseUpdatedAt=(0,moment_timezone_1.default)().toDate(),n.body.unique&&null!=(e=n.body.data)&&e[n.body.unique])if(yield o.findOne({[n.body.unique]:n.body.data[n.body.unique]}))return void r.status(400).json({err:`Unique:${n.body.data[n.body.unique]} exists`});d&&(yield d(n.body.data)),yield o.insertOne(n.body.data);var a={success:!0};if(null!=(t=n.body)&&t.stopRes)return a;r.send(a)}else r.status(400).json({err:"Invalid Security Configuration"})}else r.status(400).json({err:"Invalid Model Structure"})}catch(e){r.status(400).json({err:e})}}),this.initGet=(n,r)=>__awaiter(this,void 0,void 0,function*(){var e;try{var t=n.body.id;if(t){var i,s=yield this.mongoDB.db(this.tableId).collection(this.collection).findOne({_id:t}),o=(0,QueryModel_1.checkBaseConfig)(this.config,n),a=(0,QueryModel_1.checkOpsConfig)(this.config,"read",n,s);if(o&&a)return i={data:s},null!=(e=n.body)&&e.stopRes?i:void r.send(i);r.status(400).json({err:"Invalid Security Configuration"})}else r.status(400).json({err:"Invalid Fields"})}catch(e){r.status(400).json({err:e})}}),this.initBatchUpdate=(p,m,v)=>__awaiter(this,void 0,void 0,function*(){var e,i,s;try{if(this.modelChecker(p.body.data)&&p.body.query||p.body.unsetData){var o=p.body.unsetData,a=null!=(e=p.body.query)?e:[];const y=p.body.isOr?"$or":"$and",f={};var n=this.parseModel(null!=(i=p.body.data)?i:[]),r=(a&&0<a.length&&(f[y]=[],a.some(e=>"search"===e.type)&&(f.$or=[]),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=>{f.$or.push({[e]:{$regex:s,$options:"i"}})});break;case"=":f[y].push({[i]:s});break;case"!=":f[y].push({[i]:{$ne:s}});break;case">":f[y].push({[i]:{$gt:s}});break;case"<":f[y].push({[i]:{$lt:s}});break;case"><":Array.isArray(s)&&2<=s.length&&(f[y].push({[i]:{$lt:s}}),f[y]={[i]:{$gte:s[0],$lte:s[1]}});break;case"in":f[y].push({[i]:{$in:s}})}})),(0,QueryModel_1.checkBaseConfig)(this.config,p));let t=!0;var d=this.mongoDB.db(this.tableId).collection(this.collection),l=yield(yield d.find(Object.assign({},f))).toArray();const b=[];if(yield Promise.all(l.map(e=>{p.body.sensitive&&!(0,moment_timezone_1.default)(p.body.data.baseUpdatedAt).isAfter((0,moment_timezone_1.default)(e.baseUpdatedAt))&&m.status(400).json({err:"Refresh Sensitive Model"}),(0,QueryModel_1.checkOpsConfig)(this.config,"update",p,e)||(t=!1);e=(0,utils_1.compareUpdatedFields)(e,p.body.data).workflowUpdateFields;b.concat(e)})),r&&t){const g={};var u={},c=(o&&(o.forEach(e=>{g[e]=""}),u.$unset=Object.assign({},g)),n&&(u.$set=Object.assign({},n)),v&&(yield v(b)),yield d.updateMany(f,u),b.map(e=>e._id)),h={success:c};if(null!=(s=p.body)&&s.stopRes)return h;m.send(h)}else m.status(400).json({err:"Invalid Security Configuration"})}}catch(e){m.status(400).json({err:e})}}),this.initUpdate=(y,f,b)=>__awaiter(this,void 0,void 0,function*(){var e,t,i;try{var s=this.modelChecker(y.body.data)&&y.body.id;if(s){var o=this.mongoDB.db(this.tableId).collection(this.collection),a=yield o.findOne({_id:s}),n=(this.debug&&this.debug(y.body,a),(0,QueryModel_1.checkBaseConfig)(this.config,y)),r=(0,QueryModel_1.checkOpsConfig)(this.config,"update",y,a);if(n&&r){if(y.body.unique&&null!=(e=y.body.data)&&e[y.body.unique]){var d=yield o.findOne({[y.body.unique]:y.body.data[y.body.unique]});if(d&&y.body.id!==d._id)return void f.status(400).json({err:y.body.data[y.body.unique]+" exists"})}var{workflowUpdateFields:l,updatedFields:u}=(0,utils_1.compareUpdatedFields)(a,y.body.data),c={success:!0},h=(b&&(yield b(l)),this.parseModel(u));if(y.body.sensitive)if((0,moment_timezone_1.default)(y.body.data.baseUpdatedAt).isAfter((0,moment_timezone_1.default)(a.baseUpdatedAt))){if(yield o.updateOne({_id:s},{$set:Object.assign({},h)}),null!=(t=y.body)&&t.stopRes)return c;f.send(c)}else f.status(400).json({err:"Refresh Sensitive Model"});else{if(yield o.updateOne({_id:s},{$set:Object.assign({},h)}),null!=(i=y.body)&&i.stopRes)return c;f.send(c)}}else f.status(400).json({err:"Invalid Security Configuration"})}else f.status(400).json({err:"Invalid Model Structure"})}catch(e){this.debug&&this.debug(null,null,e),f.status(400).json({err:e})}}),this.initBatchDelete=(b,g,p)=>__awaiter(this,void 0,void 0,function*(){var e,i;try{if(Array.isArray(b.body.id)){var s=this.mongoDB.db(this.tableId).collection(this.collection),o=yield s.find({_id:{$in:b.body.id}}),a=(0,QueryModel_1.checkBaseConfig)(this.config,b),n=yield o.toArray();let t=!0;if(0<n.length&&(yield Promise.all(n.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"delete",b,e)||(t=!1)}))),a&&t){p&&(yield p(n)),yield s.deleteMany({_id:{$in:b.body.id}});var r={success:b.body.id};if(null!=(e=b.body)&&e.stopRes)return r;g.send(r)}else g.status(400).json({err:"Invalid Security Configuration"})}else{var d=b.body.query;const y={};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}}});var l=this.mongoDB.db(this.tableId).collection(this.collection),u=yield l.find(y).toArray(),c=(0,QueryModel_1.checkBaseConfig)(this.config,b);let t=!0;const f=[];if(0<u.length&&u.map(e=>{f.push(e._id),(0,QueryModel_1.checkOpsConfig)(this.config,"delete",b,e)||(t=!1)}),p&&(yield p(u)),c&&t){yield l.deleteMany(y);var h={success:f};if(null!=(i=b.body)&&i.stopRes)return h;g.send(h)}else g.status(400).json({err:"Invalid Security Configuration"})}}catch(e){g.status(400).json({err:e})}}),this.initDelete=(r,d,l)=>__awaiter(this,void 0,void 0,function*(){var e;try{var t=r.body.id;if(t){var i=this.mongoDB.db(this.tableId).collection(this.collection),s=yield i.findOne({_id:t}),o=(0,QueryModel_1.checkBaseConfig)(this.config,r),a=(0,QueryModel_1.checkOpsConfig)(this.config,"delete",r,s);if(o&&a){l&&(yield l(s)),yield i.findOneAndDelete({_id:t});var n={success:!0};if(null!=(e=r.body)&&e.stopRes)return n;d.send(n)}else d.status(400).json({err:"Invalid Security Configuration"})}else d.status(400).json({err:"Invalid Fields"})}catch(e){d.status(400).json({err:e})}}),this.initList=(U,z)=>__awaiter(this,void 0,void 0,function*(){var e,t,i,s,o,a,n,r,d;try{var l=(0,QueryModel_1.checkBaseConfig)(this.config,U);if(l){var u=U.body.aggregate,c=null!=(e=U.body.query)?e:[],h=handleParseQueryFilter(c);if(u){var y=[],f=null!=(t=u.queryId)?t:"",b=null!=(i=u.dateId)?i:"baseUpdatedAt";for(const w of null!=(s=u.range)?s:[(0,moment_timezone_1.default)().format("DD/MM/YYYY HH:mm:ss")]){Object.assign({},h);var g=w.split("-"),[p,m=(0,moment_timezone_1.default)().format("DD/MM/YYYY HH:mm:ss")]=g,v=2===g.length,_=(0,moment_timezone_1.default)(p,"DD/MM/YYYY").isValid(),$=(0,moment_timezone_1.default)(m,"DD/MM/YYYY").isValid(),M=(0,moment_timezone_1.default)(p,"DD/MM/YYYY HH:mm:ss").tz("Asia/Kuala_lumpur").toDate(),k=(0,moment_timezone_1.default)(m,"DD/MM/YYYY HH:mm:ss").tz("Asia/Kuala_lumpur").toDate();if(!_||!$)return void z.status(400).json({err:"Invalid Aggregate Range Configuration"});const Q=[v?{$match:Object.assign(Object.assign({},h),{[b]:{$gte:M,$lte:k}})}:{$match:Object.assign(Object.assign({},h),{[b]:{$lte:M}})}];var I=v?w:"Begining until "+(0,moment_timezone_1.default)(p,"DD/MM/YYYY HH:mm:ss").format("DD/MM/YYYY");switch(u.unwind&&0<u.unwind.length&&u.unwind.map((e,t)=>{var i="model"+t;let s="";0<t&&(s="model"+(t-1)+".");t=handleParseQueryFilter(null!=(t=e.query)?t:[],i);Q.push({$lookup:{from:e.collectionId,localField:s+e.localField,foreignField:"_id",as:i}},{$unwind:"$"+i},{$match:t})}),console.log(Q),u.type){case"avg":Q.push({$group:{_id:I,value:{$avg:"$"+f}}});break;case"sum":Q.push({$group:{_id:I,value:{$sum:"$"+f}}});break;case"count":Q.push({$count:"value"})}if(0===Q.length)return void z.status(400).json({err:"Invalid Aggregate Configuration"});var j=this.mongoDB.db(this.tableId).collection(this.collection),D=null!=(a=null==(o=(yield j.aggregate(Q).toArray())[0])?void 0:o.value)?a:0;y.push({_id:I,value:D})}var C={data:y};if(null!=(n=U.body)&&n.stopRes)return C;z.send(C)}else{let e=this.mongoDB.db(this.tableId).collection(this.collection).find(h);var O,Y,A,q=yield e.count({}),B=(U.body.cursor&&(e=e.skip(U.body.cursor)),U.body.stopLimit||(e=e.limit(null!=(r=U.body.limit)?r:10)),U.body.sort&&(Y={[(O=U.body.sort).sortId]:"asc"===O.type?1:-1},e=e.sort(Y)),yield e.toArray());let t=!0;if(0<B.length&&B.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"read",U,e)||(t=!1)}),l&&t)return A={data:B,count:q},null!=(d=U.body)&&d.stopRes?A:void z.send(A);z.status(400).json({err:"Invalid Security Configuration"})}}else z.status(400).json({err:"Invalid Security Configuration"})}catch(e){z.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=o,this.lib=a;e=this.isProd?"prod":"dev";this.tableId=n?n+"-"+e:e,this.debug=r}}exports.MongoWrapper=MongoWrapper;
@@ -24,14 +24,15 @@ export interface Query {
24
24
  * Only mongoDB is supported
25
25
  */
26
26
  export interface Aggregate {
27
- type: "count" | "avg" | "sum" | "value" | "unwind";
27
+ type: "count" | "avg" | "sum" | "value";
28
28
  queryId: string;
29
29
  dateId?: string;
30
30
  range?: string[];
31
31
  unwind?: {
32
+ localField: string;
32
33
  collectionId: string;
33
34
  query: Query[];
34
- };
35
+ }[];
35
36
  }
36
37
  export interface Sort {
37
38
  sortId: string;
@@ -1 +1 @@
1
- {"version":3,"file":"QueryModel.d.ts","sourceRoot":"","sources":["../../src/model/QueryModel.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAE9C;;;;;;;;;;GAUG;AACH,MAAM,WAAW,KAAK;IACpB,IAAI,EAAE,QAAQ,GAAG,GAAG,GAAG,IAAI,GAAG,GAAG,GAAG,GAAG,GAAG,IAAI,GAAG,IAAI,CAAC;IACtD,KAAK,EAAE,GAAG,CAAC;IACX,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;CACtB;AAED;;;;;;GAMG;AACH,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,OAAO,GAAG,KAAK,GAAG,KAAK,GAAG,OAAO,GAAG,QAAQ,CAAC;IACnD,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;IACjB,MAAM,CAAC,EAAE;QACP,YAAY,EAAE,MAAM,CAAC;QACrB,KAAK,EAAE,KAAK,EAAE,CAAC;KAChB,CAAC;CACH;AAED,MAAM,WAAW,IAAI;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,KAAK,GAAG,MAAM,CAAC;CACtB;AAED;;;;;;GAMG;AACH,eAAO,MAAM,eAAe,WAAY,cAAc,OAAO,GAAG,YAmC/D,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,cAAc,WACjB,cAAc,QAChB,MAAM,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,OACxC,GAAG,OACH,GAAG,YAyDT,CAAC"}
1
+ {"version":3,"file":"QueryModel.d.ts","sourceRoot":"","sources":["../../src/model/QueryModel.ts"],"names":[],"mappings":"AAAA,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAE9C;;;;;;;;;;GAUG;AACH,MAAM,WAAW,KAAK;IACpB,IAAI,EAAE,QAAQ,GAAG,GAAG,GAAG,IAAI,GAAG,GAAG,GAAG,GAAG,GAAG,IAAI,GAAG,IAAI,CAAC;IACtD,KAAK,EAAE,GAAG,CAAC;IACX,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;CACtB;AAED;;;;;;GAMG;AACH,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,OAAO,GAAG,KAAK,GAAG,KAAK,GAAG,OAAO,CAAC;IACxC,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;IACjB,MAAM,CAAC,EAAE;QACP,UAAU,EAAE,MAAM,CAAC;QACnB,YAAY,EAAE,MAAM,CAAC;QACrB,KAAK,EAAE,KAAK,EAAE,CAAC;KAChB,EAAE,CAAC;CACL;AAED,MAAM,WAAW,IAAI;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,KAAK,GAAG,MAAM,CAAC;CACtB;AAED;;;;;;GAMG;AACH,eAAO,MAAM,eAAe,WAAY,cAAc,OAAO,GAAG,YAmC/D,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,cAAc,WACjB,cAAc,QAChB,MAAM,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,OACxC,GAAG,OACH,GAAG,YAyDT,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blixify-server",
3
- "version": "0.1.51",
3
+ "version": "0.1.53",
4
4
  "license": "MIT",
5
5
  "main": "dist/apis/index.js",
6
6
  "private": false,