blixify-server 0.1.34 → 0.1.35

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.
@@ -17,7 +17,7 @@ export declare class FirebaseWrapper {
17
17
  initCreate: (req: any, res: any, workflow?: ((data: any) => Promise<any>) | undefined) => Promise<void>;
18
18
  initGet: (req: any, res: any) => Promise<void>;
19
19
  initBatchUpdate: (req: any, res: any) => Promise<void>;
20
- initUpdate: (req: any, res: any) => Promise<void>;
20
+ initUpdate: (req: any, res: any, workflow?: ((data: any) => Promise<any>) | undefined) => Promise<void>;
21
21
  initBatchDelete: (req: any, res: any, workflow?: ((dataList: any[]) => Promise<any>) | undefined) => Promise<void>;
22
22
  initDelete: (req: any, res: any, workflow?: ((data: any) => Promise<any>) | undefined) => Promise<void>;
23
23
  initList: (req: any, res: any) => Promise<void>;
@@ -1 +1 @@
1
- {"version":3,"file":"fbWrapper.d.ts","sourceRoot":"","sources":["../../src/apis/fbWrapper.ts"],"names":[],"mappings":"AAQA,OAAO,cAAc,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAE5C;;;GAGG;AACH,qBAAa,eAAe;IAC1B,OAAO,EAAE,GAAG,CAAM;IAClB,UAAU,SAAM;IAChB,MAAM,UAAS;IACf,MAAM,EAAE,cAAc,CAQpB;IACF,GAAG,EAAE,UAAU,CAAC;IAEhB,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;IAUjB,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,mBA6BjC;IAEF,eAAe,QAAe,GAAG,OAAO,GAAG,mBA2HzC;IAEF,UAAU,QAAe,GAAG,OAAO,GAAG,mBA2DpC;IAEF,eAAe,QACR,GAAG,OACH,GAAG,yBACc,GAAG,EAAE,KAAK,QAAQ,GAAG,CAAC,gCAsH5C;IAEF,UAAU,QACH,GAAG,OACH,GAAG,qBACU,GAAG,KAAK,QAAQ,GAAG,CAAC,gCA4CtC;IAEF,QAAQ,QAAe,GAAG,OAAO,GAAG,mBA+MlC;IAEF,IAAI,YA2EF;CACH"}
1
+ {"version":3,"file":"fbWrapper.d.ts","sourceRoot":"","sources":["../../src/apis/fbWrapper.ts"],"names":[],"mappings":"AAQA,OAAO,cAAc,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAE5C;;;GAGG;AACH,qBAAa,eAAe;IAC1B,OAAO,EAAE,GAAG,CAAM;IAClB,UAAU,SAAM;IAChB,MAAM,UAAS;IACf,MAAM,EAAE,cAAc,CAQpB;IACF,GAAG,EAAE,UAAU,CAAC;IAEhB,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;IAUjB,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,mBA6BjC;IAEF,eAAe,QAAe,GAAG,OAAO,GAAG,mBA2HzC;IAEF,UAAU,QACH,GAAG,OACH,GAAG,qBACU,GAAG,KAAK,QAAQ,GAAG,CAAC,gCA8DtC;IAEF,eAAe,QACR,GAAG,OACH,GAAG,yBACc,GAAG,EAAE,KAAK,QAAQ,GAAG,CAAC,gCAsH5C;IAEF,UAAU,QACH,GAAG,OACH,GAAG,qBACU,GAAG,KAAK,QAAQ,GAAG,CAAC,gCA4CtC;IAEF,QAAQ,QAAe,GAAG,OAAO,GAAG,mBA+MlC;IAEF,IAAI,YA2EF;CACH"}
@@ -1 +1 @@
1
- "use strict";var __awaiter=this&&this.__awaiter||function(e,a,d,n){return new(d=d||Promise)(function(i,t){function s(e){try{o(n.next(e))}catch(e){t(e)}}function r(e){try{o(n.throw(e))}catch(e){t(e)}}function o(e){var t;e.done?i(e.value):((t=e.value)instanceof d?t:new d(function(e){e(t)})).then(s,r)}o((n=n.apply(e,a||[])).next())})},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.FirebaseWrapper=void 0;const moment_1=__importDefault(require("moment")),QueryModel_1=require("../model/QueryModel");class FirebaseWrapper{constructor(e,t,i,s,r,o){this.fbAdmin="",this.collection="",this.isProd=!1,this.config={baseConfig:[],opsConfig:{read:[],create:[],update:[],delete:[]}},this.modelChecker=e=>!1,this.parseModel=e=>(delete e.id,e.baseUpdatedAt=(0,moment_1.default)().toDate(),e),this.initBatchCreate=(r,o,a)=>__awaiter(this,void 0,void 0,function*(){try{const s=this.fbAdmin.firestore().batch();if(Array.isArray(r.body.data)){let t=!0;if(r.body.data.map(e=>{this.modelChecker(e)||(t=!1)}),t){var e=r.body.data,i=(0,QueryModel_1.checkBaseConfig)(this.config,r);let t=!0;e.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"update",r,e)||(t=!1)}),i&&t?(a&&(yield a(e)),yield Promise.all(e.map(t=>__awaiter(this,void 0,void 0,function*(){t.baseUpdatedAt=(0,moment_1.default)().toDate();var e=this.fbAdmin.firestore().collection(this.collection).doc(t.id);s.set(e,Object.assign({},t))}))),yield s.commit(),o.send({success:!0})):o.status(400).json({err:"Invalid Security Configuration"})}else o.status(400).json({err:"Invalid Model Structure"})}else o.status(400).json({err:"Invalid Fields"})}catch(e){o.status(400).json({err:e})}}),this.initCreate=(i,s,r)=>__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(),r&&(yield r(i.body.data)),yield this.fbAdmin.firestore().collection(this.collection).doc(i.body.data.id).set(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=(o,a)=>__awaiter(this,void 0,void 0,function*(){try{var e,t,i,s,r=o.body.id;r?(e=yield this.fbAdmin.firestore().collection(this.collection).doc(r).get()).exists&&(t=e.data(),i=(0,QueryModel_1.checkBaseConfig)(this.config,o),s=(0,QueryModel_1.checkOpsConfig)(this.config,"read",o,t),i)&&s?a.send({data:t}):a.status(400).json({err:"Invalid Security Configuration"}):a.status(400).json({err:"Invalid Fields"})}catch(e){a.status(400).json({err:e})}}),this.initBatchUpdate=(y,b)=>__awaiter(this,void 0,void 0,function*(){var e,t;const i=this.fbAdmin.firestore().batch();try{var s=this.modelChecker(y.body.data)&&y.body.query,r=this.parseModel(null!=(e=y.body.data)?e:[]),o=null!=(t=y.body.query)?t:[],a=y.body.isOr||!1,d=y.body.unsetData;if(s){var n=this.fbAdmin.firestore().collection(this.collection);const u=[];o.forEach(e=>{var t,i=null!=(t=e.queryId)?t:"",s=e.value;switch(e.type){case"=":u.push(this.fbAdmin.firestore.Filter.where(i,"==",s));break;case"!=":u.push(this.fbAdmin.firestore.Filter.where(i,"!=",s));break;case">":u.push(this.fbAdmin.firestore.Filter.where(i,">",s));break;case"<":u.push(this.fbAdmin.firestore.Filter.where(i,"<",s));break;case"><":Array.isArray(s)&&2<=s.length&&u.push(this.fbAdmin.firestore.Filter.where(i,">=",s[0]),this.fbAdmin.firestore.Filter.where(i,"<=",s[1]));break;case"in":u.push(this.fbAdmin.firestore.Filter.where(i,"in",s))}});var c=a?this.fbAdmin.firestore.Filter.or(...u):this.fbAdmin.firestore.Filter.and(...u),l=yield n.where(c).get(),h=(0,QueryModel_1.checkBaseConfig)(this.config,y);let t=!0;const f=[];if(l&&l.forEach(e=>{e=e.data();f.push(e.id),y.body.sensitive&&!(0,moment_1.default)(y.body.data.baseUpdatedAt).isAfter((0,moment_1.default)(1e3*e.baseUpdatedAt.seconds))&&b.status(400).json({err:"Refresh Sensitive Model"}),(0,QueryModel_1.checkOpsConfig)(this.config,"update",y,e)||(t=!1)}),h&&t){let t={};d&&d.forEach(e=>{t[e]=this.fbAdmin.firestore.FieldValue.delete()}),r&&(t=Object.assign(Object.assign({},r),t)),yield Promise.all(f.map(e=>{e=this.fbAdmin.firestore().collection(this.collection).doc(e);i.update(e,Object.assign({},t))})),yield i.commit(),b.send({success:!0})}else b.status(400).json({err:"Invalid Security Configuration"})}}catch(e){b.status(400).json({err:e})}}),this.initUpdate=(d,n)=>__awaiter(this,void 0,void 0,function*(){try{var e,t,i,s,r,o,a=this.modelChecker(d.body.data)&&d.body.id;a?(e=yield this.fbAdmin.firestore().collection(this.collection).doc(a).get()).exists&&(t=e.data(),i=(0,QueryModel_1.checkBaseConfig)(this.config,d),s=(0,QueryModel_1.checkOpsConfig)(this.config,"update",d,t),i)&&s?d.body.sensitive?(0,moment_1.default)(d.body.data.baseUpdatedAt).isAfter((0,moment_1.default)(1e3*t.baseUpdatedAt.seconds))?(r=this.parseModel(d.body.data),yield this.fbAdmin.firestore().collection(this.collection).doc(a).update(Object.assign({},r)),n.send({success:!0})):n.status(400).json({err:"Refresh Sensitive Model"}):(o=this.parseModel(d.body.data),yield this.fbAdmin.firestore().collection(this.collection).doc(a).update(Object.assign({},o)),n.send({success:!0})):n.status(400).json({err:"Invalid Security Configuration"}):n.status(400).json({err:"Invalid Model Structure"})}catch(e){n.status(400).json({err:e})}}),this.initBatchDelete=(c,l,h)=>__awaiter(this,void 0,void 0,function*(){try{const d=this.fbAdmin.firestore().batch();if(Array.isArray(c.body.id)){var e=yield this.fbAdmin.firestore().collection(this.collection).where("id","in",c.body.id).get(),i=(0,QueryModel_1.checkBaseConfig)(this.config,c);let t=!0;const r=[];if(e&&e.forEach(e=>{e=e.data();r.push(e),(0,QueryModel_1.checkOpsConfig)(this.config,"delete",c,e)||(t=!1)}),!i||!t)return void l.status(400).json({err:"Invalid Security Configuration"});h&&(yield h(r)),yield Promise.all(c.body.id.map(t=>__awaiter(this,void 0,void 0,function*(){var e=yield this.fbAdmin.firestore().collection(this.collection).doc(t);d.delete(e)}))),yield d.commit(),l.send({success:!0})}else{let r=this.fbAdmin.firestore().collection(this.collection);var s=c.body.query,o=(s&&0<s.length&&s.map(e=>{var t,i=null!=(t=e.queryId)?t:"",s=e.value;switch(e.type){case"search":e.searchIds&&0<e.searchIds.length&&(r=r.orderBy(e.searchIds[0]).startAt(s).endAt(s+""));break;case"=":r=r.where(i,"==",s);break;case"!=":r=r.where(i,"!=",s);break;case">":r=r.where(i,">",s);break;case"<":r=r.where(i,"<",s);break;case"><":Array.isArray(s)&&2<=s.length&&(r=r.where(i,">=",s[0]).where(i,"<=",s[1]));break;case"in":r=r.where(i,"in",s)}}),yield r.get()),a=(0,QueryModel_1.checkBaseConfig)(this.config,c);let t=!0;const n=[];if(o&&o.forEach(e=>{e=e.data();n.push(e),(0,QueryModel_1.checkOpsConfig)(this.config,"delete",c,e)||(t=!1)}),h&&(yield h(n)),!a||!t)return void l.status(400).json({err:"Invalid Security Configuration"});o.forEach(e=>{d.delete(e.ref)}),yield d.commit()}l.send({success:!0})}catch(e){l.status(400).json({err:e})}}),this.initDelete=(o,a,d)=>__awaiter(this,void 0,void 0,function*(){try{var e,t,i,s,r=o.body.id;r?(e=yield this.fbAdmin.firestore().collection(this.collection).doc(r).get()).exists&&(t=e.data(),i=(0,QueryModel_1.checkBaseConfig)(this.config,o),s=(0,QueryModel_1.checkOpsConfig)(this.config,"delete",o,t),i)&&s?(d&&(yield d(t)),yield this.fbAdmin.firestore().collection(this.collection).doc(r).delete(),a.send({success:!0})):a.status(400).json({err:"Invalid Security Configuration"}):a.status(400).json({err:"Invalid Fields"})}catch(e){a.status(400).json({err:e})}}),this.initList=(D,B)=>__awaiter(this,void 0,void 0,function*(){var e,t,i,s;try{var a=(0,QueryModel_1.checkBaseConfig)(this.config,D);if(a){let r=this.fbAdmin.firestore().collection(this.collection);var d=D.body.aggregate,n=null!=(e=D.body.query)?e:[];let o="";if(n&&0<n.length&&n.map(e=>{var t,i=null!=(t=e.queryId)?t:"",s=e.value;switch(e.type){case"search":e.searchIds&&0<e.searchIds.length&&(o=e.searchIds[0],r=r.orderBy(e.searchIds[0]).startAt(s).endAt(s+""));break;case"=":r=r.where(i,"==",s);break;case"!=":r=r.where(i,"!=",s);break;case">":r=r.where(i,">",s);break;case"<":r=r.where(i,"<",s);break;case"><":Array.isArray(s)&&2<=s.length&&(r=r.where(i,">=",s[0]).where(i,"<=",s[1]));break;case"in":r=r.where(i,"in",s)}}),d){const j=null!=(t=d.queryId)?t:"";var c=null!=(i=d.dateId)?i:"baseUpdatedAt",l=null!=(s=d.range)?s:[],h=[];for(const I of l){var u=I.split("-"),[f,y=(0,moment_1.default)().format("DD/MM/YYYY HH:mm:ss")]=u,b=2===u.length,m=(0,moment_1.default)(f,"DD/MM/YYYY").isValid(),g=(0,moment_1.default)(y,"DD/MM/YYYY").isValid(),p=(0,moment_1.default)(f,"DD/MM/YYYY HH:mm:ss").format("YYYY-MM-DD HH:mm:ss"),v=(0,moment_1.default)(y,"DD/MM/YYYY HH:mm:ss").format("YYYY-MM-DD HH:mm:ss");if(!m||!g)return void B.status(400).json({err:"Invalid Aggregate Range Configuration"});var _=b?I:"Begining until "+(0,moment_1.default)(f).format("DD/MM/YYYY");r=b?r.where(c,">=",(0,moment_1.default)(p).toDate()).where(c,"<=",(0,moment_1.default)(v).toDate()):r.where(c,"<",(0,moment_1.default)(p).toDate());let t=0;switch(d.type){case"count":var A=yield r.count().get();t=A.data().count;break;case"value":var M=yield(r=(r=r.limit(1)).orderBy(c,"desc")).get();M&&M.forEach(e=>{e=e.data(),e=JSON.parse(JSON.stringify(e));t=e[j]})}h.push({_id:_,value:t})}B.send({data:h})}else if(D.body.limit){if(r=r.limit(D.body.limit),D.body.sort){var w=D.body.sort;if(o&&w.sortId!==o)return void B.status(400).json({err:"Invalid Search Sort Configuration"});o||(r=r.orderBy(w.sortId,"asc"===w.type?"asc":"desc"))}var C,k=yield(r=D.body.cursor?r.startAfter(D.body.cursor):r).get();let t=!0;const Y=[];if(k&&k.forEach(e=>{e=e.data();Y.push(e),(0,QueryModel_1.checkOpsConfig)(this.config,"read",D,e)||(t=!1)}),a&&t){let t="";if(Y.length===D.body.limit){let e="id";D.body.sort&&(C=D.body.sort,e=C.sortId),t=Y[D.body.limit-1][e]}B.send({data:Y,count:t})}else B.status(400).json({err:"Invalid Security Configuration"})}else B.status(400).json({err:"Invalid Limit 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.fbAdmin=e,this.collection=t,this.isProd=i,this.config=s,this.modelChecker=r,this.lib=o}}exports.FirebaseWrapper=FirebaseWrapper;
1
+ "use strict";var __awaiter=this&&this.__awaiter||function(e,a,d,n){return new(d=d||Promise)(function(i,t){function s(e){try{o(n.next(e))}catch(e){t(e)}}function r(e){try{o(n.throw(e))}catch(e){t(e)}}function o(e){var t;e.done?i(e.value):((t=e.value)instanceof d?t:new d(function(e){e(t)})).then(s,r)}o((n=n.apply(e,a||[])).next())})},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.FirebaseWrapper=void 0;const moment_1=__importDefault(require("moment")),QueryModel_1=require("../model/QueryModel");class FirebaseWrapper{constructor(e,t,i,s,r,o){this.fbAdmin="",this.collection="",this.isProd=!1,this.config={baseConfig:[],opsConfig:{read:[],create:[],update:[],delete:[]}},this.modelChecker=e=>!1,this.parseModel=e=>(delete e.id,e.baseUpdatedAt=(0,moment_1.default)().toDate(),e),this.initBatchCreate=(r,o,a)=>__awaiter(this,void 0,void 0,function*(){try{const s=this.fbAdmin.firestore().batch();if(Array.isArray(r.body.data)){let t=!0;if(r.body.data.map(e=>{this.modelChecker(e)||(t=!1)}),t){var e=r.body.data,i=(0,QueryModel_1.checkBaseConfig)(this.config,r);let t=!0;e.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"update",r,e)||(t=!1)}),i&&t?(a&&(yield a(e)),yield Promise.all(e.map(t=>__awaiter(this,void 0,void 0,function*(){t.baseUpdatedAt=(0,moment_1.default)().toDate();var e=this.fbAdmin.firestore().collection(this.collection).doc(t.id);s.set(e,Object.assign({},t))}))),yield s.commit(),o.send({success:!0})):o.status(400).json({err:"Invalid Security Configuration"})}else o.status(400).json({err:"Invalid Model Structure"})}else o.status(400).json({err:"Invalid Fields"})}catch(e){o.status(400).json({err:e})}}),this.initCreate=(i,s,r)=>__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(),r&&(yield r(i.body.data)),yield this.fbAdmin.firestore().collection(this.collection).doc(i.body.data.id).set(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=(o,a)=>__awaiter(this,void 0,void 0,function*(){try{var e,t,i,s,r=o.body.id;r?(e=yield this.fbAdmin.firestore().collection(this.collection).doc(r).get()).exists&&(t=e.data(),i=(0,QueryModel_1.checkBaseConfig)(this.config,o),s=(0,QueryModel_1.checkOpsConfig)(this.config,"read",o,t),i)&&s?a.send({data:t}):a.status(400).json({err:"Invalid Security Configuration"}):a.status(400).json({err:"Invalid Fields"})}catch(e){a.status(400).json({err:e})}}),this.initBatchUpdate=(y,b)=>__awaiter(this,void 0,void 0,function*(){var e,t;const i=this.fbAdmin.firestore().batch();try{var s=this.modelChecker(y.body.data)&&y.body.query,r=this.parseModel(null!=(e=y.body.data)?e:[]),o=null!=(t=y.body.query)?t:[],a=y.body.isOr||!1,d=y.body.unsetData;if(s){var n=this.fbAdmin.firestore().collection(this.collection);const u=[];o.forEach(e=>{var t,i=null!=(t=e.queryId)?t:"",s=e.value;switch(e.type){case"=":u.push(this.fbAdmin.firestore.Filter.where(i,"==",s));break;case"!=":u.push(this.fbAdmin.firestore.Filter.where(i,"!=",s));break;case">":u.push(this.fbAdmin.firestore.Filter.where(i,">",s));break;case"<":u.push(this.fbAdmin.firestore.Filter.where(i,"<",s));break;case"><":Array.isArray(s)&&2<=s.length&&u.push(this.fbAdmin.firestore.Filter.where(i,">=",s[0]),this.fbAdmin.firestore.Filter.where(i,"<=",s[1]));break;case"in":u.push(this.fbAdmin.firestore.Filter.where(i,"in",s))}});var c=a?this.fbAdmin.firestore.Filter.or(...u):this.fbAdmin.firestore.Filter.and(...u),l=yield n.where(c).get(),h=(0,QueryModel_1.checkBaseConfig)(this.config,y);let t=!0;const f=[];if(l&&l.forEach(e=>{e=e.data();f.push(e.id),y.body.sensitive&&!(0,moment_1.default)(y.body.data.baseUpdatedAt).isAfter((0,moment_1.default)(1e3*e.baseUpdatedAt.seconds))&&b.status(400).json({err:"Refresh Sensitive Model"}),(0,QueryModel_1.checkOpsConfig)(this.config,"update",y,e)||(t=!1)}),h&&t){let t={};d&&d.forEach(e=>{t[e]=this.fbAdmin.firestore.FieldValue.delete()}),r&&(t=Object.assign(Object.assign({},r),t)),yield Promise.all(f.map(e=>{e=this.fbAdmin.firestore().collection(this.collection).doc(e);i.update(e,Object.assign({},t))})),yield i.commit(),b.send({success:!0})}else b.status(400).json({err:"Invalid Security Configuration"})}}catch(e){b.status(400).json({err:e})}}),this.initUpdate=(d,n,c)=>__awaiter(this,void 0,void 0,function*(){try{var e,t,i,s,r,o,a=this.modelChecker(d.body.data)&&d.body.id;a?(e=yield this.fbAdmin.firestore().collection(this.collection).doc(a).get()).exists&&(t=e.data(),i=(0,QueryModel_1.checkBaseConfig)(this.config,d),s=(0,QueryModel_1.checkOpsConfig)(this.config,"update",d,t),i)&&s?d.body.sensitive?(0,moment_1.default)(d.body.data.baseUpdatedAt).isAfter((0,moment_1.default)(1e3*t.baseUpdatedAt.seconds))?(c&&(yield c(d.body.data)),r=this.parseModel(d.body.data),yield this.fbAdmin.firestore().collection(this.collection).doc(a).update(Object.assign({},r)),n.send({success:!0})):n.status(400).json({err:"Refresh Sensitive Model"}):(c&&(yield c(d.body.data)),o=this.parseModel(d.body.data),yield this.fbAdmin.firestore().collection(this.collection).doc(a).update(Object.assign({},o)),n.send({success:!0})):n.status(400).json({err:"Invalid Security Configuration"}):n.status(400).json({err:"Invalid Model Structure"})}catch(e){n.status(400).json({err:e})}}),this.initBatchDelete=(c,l,h)=>__awaiter(this,void 0,void 0,function*(){try{const d=this.fbAdmin.firestore().batch();if(Array.isArray(c.body.id)){var e=yield this.fbAdmin.firestore().collection(this.collection).where("id","in",c.body.id).get(),i=(0,QueryModel_1.checkBaseConfig)(this.config,c);let t=!0;const r=[];if(e&&e.forEach(e=>{e=e.data();r.push(e),(0,QueryModel_1.checkOpsConfig)(this.config,"delete",c,e)||(t=!1)}),!i||!t)return void l.status(400).json({err:"Invalid Security Configuration"});h&&(yield h(r)),yield Promise.all(c.body.id.map(t=>__awaiter(this,void 0,void 0,function*(){var e=yield this.fbAdmin.firestore().collection(this.collection).doc(t);d.delete(e)}))),yield d.commit(),l.send({success:!0})}else{let r=this.fbAdmin.firestore().collection(this.collection);var s=c.body.query,o=(s&&0<s.length&&s.map(e=>{var t,i=null!=(t=e.queryId)?t:"",s=e.value;switch(e.type){case"search":e.searchIds&&0<e.searchIds.length&&(r=r.orderBy(e.searchIds[0]).startAt(s).endAt(s+""));break;case"=":r=r.where(i,"==",s);break;case"!=":r=r.where(i,"!=",s);break;case">":r=r.where(i,">",s);break;case"<":r=r.where(i,"<",s);break;case"><":Array.isArray(s)&&2<=s.length&&(r=r.where(i,">=",s[0]).where(i,"<=",s[1]));break;case"in":r=r.where(i,"in",s)}}),yield r.get()),a=(0,QueryModel_1.checkBaseConfig)(this.config,c);let t=!0;const n=[];if(o&&o.forEach(e=>{e=e.data();n.push(e),(0,QueryModel_1.checkOpsConfig)(this.config,"delete",c,e)||(t=!1)}),h&&(yield h(n)),!a||!t)return void l.status(400).json({err:"Invalid Security Configuration"});o.forEach(e=>{d.delete(e.ref)}),yield d.commit()}l.send({success:!0})}catch(e){l.status(400).json({err:e})}}),this.initDelete=(o,a,d)=>__awaiter(this,void 0,void 0,function*(){try{var e,t,i,s,r=o.body.id;r?(e=yield this.fbAdmin.firestore().collection(this.collection).doc(r).get()).exists&&(t=e.data(),i=(0,QueryModel_1.checkBaseConfig)(this.config,o),s=(0,QueryModel_1.checkOpsConfig)(this.config,"delete",o,t),i)&&s?(d&&(yield d(t)),yield this.fbAdmin.firestore().collection(this.collection).doc(r).delete(),a.send({success:!0})):a.status(400).json({err:"Invalid Security Configuration"}):a.status(400).json({err:"Invalid Fields"})}catch(e){a.status(400).json({err:e})}}),this.initList=(D,B)=>__awaiter(this,void 0,void 0,function*(){var e,t,i,s;try{var a=(0,QueryModel_1.checkBaseConfig)(this.config,D);if(a){let r=this.fbAdmin.firestore().collection(this.collection);var d=D.body.aggregate,n=null!=(e=D.body.query)?e:[];let o="";if(n&&0<n.length&&n.map(e=>{var t,i=null!=(t=e.queryId)?t:"",s=e.value;switch(e.type){case"search":e.searchIds&&0<e.searchIds.length&&(o=e.searchIds[0],r=r.orderBy(e.searchIds[0]).startAt(s).endAt(s+""));break;case"=":r=r.where(i,"==",s);break;case"!=":r=r.where(i,"!=",s);break;case">":r=r.where(i,">",s);break;case"<":r=r.where(i,"<",s);break;case"><":Array.isArray(s)&&2<=s.length&&(r=r.where(i,">=",s[0]).where(i,"<=",s[1]));break;case"in":r=r.where(i,"in",s)}}),d){const j=null!=(t=d.queryId)?t:"";var c=null!=(i=d.dateId)?i:"baseUpdatedAt",l=null!=(s=d.range)?s:[],h=[];for(const I of l){var u=I.split("-"),[f,y=(0,moment_1.default)().format("DD/MM/YYYY HH:mm:ss")]=u,b=2===u.length,m=(0,moment_1.default)(f,"DD/MM/YYYY").isValid(),g=(0,moment_1.default)(y,"DD/MM/YYYY").isValid(),p=(0,moment_1.default)(f,"DD/MM/YYYY HH:mm:ss").format("YYYY-MM-DD HH:mm:ss"),v=(0,moment_1.default)(y,"DD/MM/YYYY HH:mm:ss").format("YYYY-MM-DD HH:mm:ss");if(!m||!g)return void B.status(400).json({err:"Invalid Aggregate Range Configuration"});var _=b?I:"Begining until "+(0,moment_1.default)(f).format("DD/MM/YYYY");r=b?r.where(c,">=",(0,moment_1.default)(p).toDate()).where(c,"<=",(0,moment_1.default)(v).toDate()):r.where(c,"<",(0,moment_1.default)(p).toDate());let t=0;switch(d.type){case"count":var A=yield r.count().get();t=A.data().count;break;case"value":var M=yield(r=(r=r.limit(1)).orderBy(c,"desc")).get();M&&M.forEach(e=>{e=e.data(),e=JSON.parse(JSON.stringify(e));t=e[j]})}h.push({_id:_,value:t})}B.send({data:h})}else if(D.body.limit){if(r=r.limit(D.body.limit),D.body.sort){var w=D.body.sort;if(o&&w.sortId!==o)return void B.status(400).json({err:"Invalid Search Sort Configuration"});o||(r=r.orderBy(w.sortId,"asc"===w.type?"asc":"desc"))}var C,k=yield(r=D.body.cursor?r.startAfter(D.body.cursor):r).get();let t=!0;const Y=[];if(k&&k.forEach(e=>{e=e.data();Y.push(e),(0,QueryModel_1.checkOpsConfig)(this.config,"read",D,e)||(t=!1)}),a&&t){let t="";if(Y.length===D.body.limit){let e="id";D.body.sort&&(C=D.body.sort,e=C.sortId),t=Y[D.body.limit-1][e]}B.send({data:Y,count:t})}else B.status(400).json({err:"Invalid Security Configuration"})}else B.status(400).json({err:"Invalid Limit 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.fbAdmin=e,this.collection=t,this.isProd=i,this.config=s,this.modelChecker=r,this.lib=o}}exports.FirebaseWrapper=FirebaseWrapper;
@@ -30,7 +30,7 @@ export declare class MongoWrapper {
30
30
  initBatchUpdate: (req: any, res: any) => Promise<{
31
31
  success: boolean;
32
32
  } | undefined>;
33
- initUpdate: (req: any, res: any) => Promise<{
33
+ initUpdate: (req: any, res: any, workflow?: ((data: any) => Promise<any>) | undefined) => Promise<{
34
34
  success: boolean;
35
35
  } | undefined>;
36
36
  initBatchDelete: (req: any, res: any, workflow?: ((dataList: any[]) => Promise<any>) | undefined) => Promise<{
@@ -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;;mBAmD5C;IAEF,UAAU,QACH,GAAG,OACH,GAAG,qBACU,GAAG,KAAK,QAAQ,GAAG,CAAC;;mBAkCtC;IAEF,OAAO,QAAe,GAAG,OAAO,GAAG;;mBAgCjC;IAEF,eAAe,QAAe,GAAG,OAAO,GAAG;;mBAsIzC;IAEF,UAAU,QAAe,GAAG,OAAO,GAAG;;mBAmEpC;IAEF,eAAe,QACR,GAAG,OACH,GAAG,yBACc,GAAG,EAAE,KAAK,QAAQ,GAAG,CAAC;;mBA2H5C;IAEF,UAAU,QACH,GAAG,OACH,GAAG,qBACU,GAAG,KAAK,QAAQ,GAAG,CAAC;;mBAqCtC;IAEF,QAAQ,QAAe,GAAG,OAAO,GAAG;;;;;mBA6OlC;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;;mBAmD5C;IAEF,UAAU,QACH,GAAG,OACH,GAAG,qBACU,GAAG,KAAK,QAAQ,GAAG,CAAC;;mBAkCtC;IAEF,OAAO,QAAe,GAAG,OAAO,GAAG;;mBAgCjC;IAEF,eAAe,QAAe,GAAG,OAAO,GAAG;;mBAsIzC;IAEF,UAAU,QACH,GAAG,OACH,GAAG,qBACU,GAAG,KAAK,QAAQ,GAAG,CAAC;;mBAsEtC;IAEF,eAAe,QACR,GAAG,OACH,GAAG,yBACc,GAAG,EAAE,KAAK,QAAQ,GAAG,CAAC;;mBA2H5C;IAEF,UAAU,QACH,GAAG,OACH,GAAG,qBACU,GAAG,KAAK,QAAQ,GAAG,CAAC;;mBAqCtC;IAEF,QAAQ,QAAe,GAAG,OAAO,GAAG;;;;;mBA6OlC;IAEF,IAAI,YA4EF;CACH"}
@@ -1 +1 @@
1
- "use strict";var __awaiter=this&&this.__awaiter||function(e,r,n,d){return new(n=n||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 n?t:new n(function(e){e(t)})).then(i,a)}o((d=d.apply(e,r||[])).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,r,n){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=(r,n,d)=>__awaiter(this,void 0,void 0,function*(){var e;try{if(Array.isArray(r.body.data)){let t=!0;r.body.data.map(e=>{this.modelChecker(e)||(t=!1)}),t||n.status(400).json({err:"Invalid Model Structure"});var i=(0,QueryModel_1.checkBaseConfig)(this.config,r);let s=!0;if(yield Promise.all(r.body.data.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"update",r,e)||(s=!1)})),i&&s){var a=r.body.data.map(e=>Object.assign(Object.assign({},e),{baseUpdatedAt:(0,moment_1.default)().toDate()}));d&&(yield d(a));yield this.mongoDB.db(this.tableId).collection(this.collection).insertMany(a);var o={success:!0};if(null!=(e=r.body)&&e.stopRes)return o;n.send(o)}else 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=(a,o,r)=>__awaiter(this,void 0,void 0,function*(){var e;try{if(this.modelChecker(a.body.data)){var t=(0,QueryModel_1.checkBaseConfig)(this.config,a),s=(0,QueryModel_1.checkOpsConfig)(this.config,"create",a,a.body.data);if(t&&s){a.body.data.baseUpdatedAt=(0,moment_1.default)().toDate(),r&&(yield r(a.body.data));yield this.mongoDB.db(this.tableId).collection(this.collection).insertOne(a.body.data);var i={success:!0};if(null!=(e=a.body)&&e.stopRes)return i;o.send(i)}else o.status(400).json({err:"Invalid Security Configuration"})}else o.status(400).json({err:"Invalid Model Structure"})}catch(e){o.status(400).json({err:e})}}),this.initGet=(r,n)=>__awaiter(this,void 0,void 0,function*(){var e;try{var t=r.body.id;if(t){var s,i=yield this.mongoDB.db(this.tableId).collection(this.collection).findOne({_id:t}),a=(0,QueryModel_1.checkBaseConfig)(this.config,r),o=(0,QueryModel_1.checkOpsConfig)(this.config,"read",r,i);if(a&&o)return s={data:i},null!=(e=r.body)&&e.stopRes?s:void n.send(s);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.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 h=g.body.isOr?"$or":"$and",y={};var r=this.parseModel(null!=(s=g.body.data)?s:[]),n=(o&&0<o.length&&(y[h]=[],o.some(e=>"search"===e.type)&&(y.$or=[]),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=>{y.$or.push({[e]:{$regex:i,$options:"i"}})});break;case"=":y[h].push({[s]:i});break;case"!=":y[h].push({[s]:{$ne:i}});break;case">":y[h].push({[s]:{$gt:i}});break;case"<":y[h].push({[s]:{$lt:i}});break;case"><":Array.isArray(i)&&2<=i.length&&(y[h].push({[s]:{$lt:i}}),y[h]={[s]:{$gte:i[0],$lte:i[1]}});break;case"in":y[h].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({},y))).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)})),n&&t){const f={};var c={},u=(a&&(a.forEach(e=>{f[e]=""}),c.$unset=Object.assign({},f)),r&&(c.$set=Object.assign({},r)),yield d.updateMany(y,c),{success:!0});if(null!=(i=g.body)&&i.stopRes)return u;b.send(u)}else b.status(400).json({err:"Invalid Security Configuration"})}}catch(e){b.status(400).json({err:e})}}),this.initUpdate=(c,u)=>__awaiter(this,void 0,void 0,function*(){var e,t;try{var s=this.modelChecker(c.body.data)&&c.body.id;if(s){var i=this.mongoDB.db(this.tableId).collection(this.collection),a=yield i.findOne({_id:s}),o=(this.debug&&this.debug(c.body,a),(0,QueryModel_1.checkBaseConfig)(this.config,c)),r=(0,QueryModel_1.checkOpsConfig)(this.config,"update",c,a);if(o&&r){var n={success:!0};if(c.body.sensitive)if((0,moment_1.default)(c.body.data.baseUpdatedAt).isAfter((0,moment_1.default)(a.baseUpdatedAt))){var d=this.parseModel(c.body.data);if(yield i.updateOne({_id:s},{$set:Object.assign({},d)}),null!=(e=c.body)&&e.stopRes)return n;u.send(n)}else u.status(400).json({err:"Refresh Sensitive Model"});else{var l=this.parseModel(c.body.data);if(yield i.updateOne({_id:s},{$set:Object.assign({},l)}),null!=(t=c.body)&&t.stopRes)return n;u.send(n)}}else u.status(400).json({err:"Invalid Security Configuration"})}else u.status(400).json({err:"Invalid Model Structure"})}catch(e){this.debug&&this.debug(null,null,e),u.status(400).json({err:e})}}),this.initBatchDelete=(f,g,b)=>__awaiter(this,void 0,void 0,function*(){var e,s;try{if(Array.isArray(f.body.id)){var i=this.mongoDB.db(this.tableId).collection(this.collection),a=yield i.find({_id:{$in:f.body.id}}),o=(0,QueryModel_1.checkBaseConfig)(this.config,f),r=yield a.toArray();let t=!0;if(0<r.length&&(yield Promise.all(r.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"delete",f,e)||(t=!1)}))),o&&t){b&&(yield b(r)),yield i.deleteMany({_id:{$in:f.body.id}});var n={success:!0};if(null!=(e=f.body)&&e.stopRes)return n;g.send(n)}else g.status(400).json({err:"Invalid Security Configuration"})}else{var d=f.body.query;const y={};d&&0<d.length&&d.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&&(y.$or=[],e.searchIds.map(e=>{y.$or.push({[e]:{$regex:i,$options:"i"}})}));break;case"=":y[s]=i;break;case"!=":y[s]={$ne:i};break;case">":y[s]={$gt:i};break;case"<":y[s]={$lt:i};break;case"><":Array.isArray(i)&&2<=i.length&&(y[s]={$gte:i[0],$lte:i[1]});break;case"in":y[s]={$in:i}}});var l=this.mongoDB.db(this.tableId).collection(this.collection),c=yield l.find(y).toArray(),u=(0,QueryModel_1.checkBaseConfig)(this.config,f);let t=!0;if(0<c.length&&c.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"delete",f,e)||(t=!1)}),b&&(yield b(c)),u&&t){yield l.deleteMany(y);var h={success:!0};if(null!=(s=f.body)&&s.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=(n,d,l)=>__awaiter(this,void 0,void 0,function*(){var e;try{var t=n.body.id;if(t){var s=this.mongoDB.db(this.tableId).collection(this.collection),i=yield s.findOne({_id:t}),a=(0,QueryModel_1.checkBaseConfig)(this.config,n),o=(0,QueryModel_1.checkOpsConfig)(this.config,"delete",n,i);if(a&&o){l&&(yield l(i)),yield s.findOneAndDelete({_id:t});var r={success:!0};if(null!=(e=n.body)&&e.stopRes)return r;d.send(r)}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=(R,S)=>__awaiter(this,void 0,void 0,function*(){var e,t,s,i,a,o,r,n;try{var d=(0,QueryModel_1.checkBaseConfig)(this.config,R);if(d){var l=R.body.aggregate,c=null!=(e=R.body.query)?e:[];const w={};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&&(w.$or=[],e.searchIds.map(e=>{w.$or.push({[e]:{$regex:i,$options:"i"}})}));break;case"=":w[s]=i;break;case"!=":w[s]={$ne:i};break;case">":w[s]={$gt:i};break;case"<":w[s]={$lt:i};break;case"><":Array.isArray(i)&&2<=i.length&&(w[s]={$gte:i[0],$lte:i[1]});break;case"in":w[s]={$in:i}}}),l){var u=[],h=null!=(t=l.queryId)?t:"",y=null!=(s=l.dateId)?s:"baseUpdatedAt";for(const Q of null!=(i=l.range)?i:[]){Object.assign({},w);var f=Q.split("-"),[g,b=(0,moment_1.default)().format("DD/MM/YYYY HH:mm:ss")]=f,p=2===f.length,v=(0,moment_1.default)(g,"DD/MM/YYYY").isValid(),m=(0,moment_1.default)(b,"DD/MM/YYYY").isValid(),_=(0,moment_1.default)(g,"DD/MM/YYYY HH:mm:ss").format("YYYY-MM-DD HH:mm:ss"),M=(0,moment_1.default)(b,"DD/MM/YYYY HH:mm:ss").format("YYYY-MM-DD HH:mm:ss");if(!v||!m)return void S.status(400).json({err:"Invalid Aggregate Range Configuration"});var $=p?{$match:Object.assign(Object.assign({},w),{[y]:{$gte:new Date(_),$lte:new Date(M)}})}:{$match:Object.assign(Object.assign({},w),{[y]:{$lte:new Date(_)}})},D=[];let e=!1;var k=p?Q:"Begining until "+(0,moment_1.default)(g,"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 S.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})}var C={data:u};if(null!=(r=R.body)&&r.stopRes)return C;S.send(C)}else{let e=this.mongoDB.db(this.tableId).collection(this.collection).find(w);var j,Y,O,A=yield e.count({}),B=(R.body.cursor&&(e=e.skip(R.body.cursor)),R.body.limit&&(e=e.limit(R.body.limit)),R.body.sort&&(Y={[(j=R.body.sort).sortId]:"asc"===j.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",R,e)||(t=!1)}),d&&t)return O={data:B,count:A},null!=(n=R.body)&&n.stopRes?O:void S.send(O);S.status(400).json({err:"Invalid Security Configuration"})}}else S.status(400).json({err:"Invalid Security Configuration"})}catch(e){S.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=r?r+"-"+e:e,this.debug=n}}exports.MongoWrapper=MongoWrapper;
1
+ "use strict";var __awaiter=this&&this.__awaiter||function(e,r,n,d){return new(n=n||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 n?t:new n(function(e){e(t)})).then(s,a)}o((d=d.apply(e,r||[])).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,r,n){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=(r,n,d)=>__awaiter(this,void 0,void 0,function*(){var e;try{if(Array.isArray(r.body.data)){let t=!0;r.body.data.map(e=>{this.modelChecker(e)||(t=!1)}),t||n.status(400).json({err:"Invalid Model Structure"});var s=(0,QueryModel_1.checkBaseConfig)(this.config,r);let i=!0;if(yield Promise.all(r.body.data.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"update",r,e)||(i=!1)})),s&&i){var a=r.body.data.map(e=>Object.assign(Object.assign({},e),{baseUpdatedAt:(0,moment_1.default)().toDate()}));d&&(yield d(a));yield this.mongoDB.db(this.tableId).collection(this.collection).insertMany(a);var o={success:!0};if(null!=(e=r.body)&&e.stopRes)return o;n.send(o)}else 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=(a,o,r)=>__awaiter(this,void 0,void 0,function*(){var e;try{if(this.modelChecker(a.body.data)){var t=(0,QueryModel_1.checkBaseConfig)(this.config,a),i=(0,QueryModel_1.checkOpsConfig)(this.config,"create",a,a.body.data);if(t&&i){a.body.data.baseUpdatedAt=(0,moment_1.default)().toDate(),r&&(yield r(a.body.data));yield this.mongoDB.db(this.tableId).collection(this.collection).insertOne(a.body.data);var s={success:!0};if(null!=(e=a.body)&&e.stopRes)return s;o.send(s)}else o.status(400).json({err:"Invalid Security Configuration"})}else o.status(400).json({err:"Invalid Model Structure"})}catch(e){o.status(400).json({err:e})}}),this.initGet=(r,n)=>__awaiter(this,void 0,void 0,function*(){var e;try{var t=r.body.id;if(t){var i,s=yield this.mongoDB.db(this.tableId).collection(this.collection).findOne({_id:t}),a=(0,QueryModel_1.checkBaseConfig)(this.config,r),o=(0,QueryModel_1.checkOpsConfig)(this.config,"read",r,s);if(a&&o)return i={data:s},null!=(e=r.body)&&e.stopRes?i:void n.send(i);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.initBatchUpdate=(g,b)=>__awaiter(this,void 0,void 0,function*(){var e,i,s;try{if(this.modelChecker(g.body.data)&&g.body.query){var a=g.body.unsetData,o=null!=(e=g.body.query)?e:[];const h=g.body.isOr?"$or":"$and",y={};var r=this.parseModel(null!=(i=g.body.data)?i:[]),n=(o&&0<o.length&&(y[h]=[],o.some(e=>"search"===e.type)&&(y.$or=[]),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&&e.searchIds.map(e=>{y.$or.push({[e]:{$regex:s,$options:"i"}})});break;case"=":y[h].push({[i]:s});break;case"!=":y[h].push({[i]:{$ne:s}});break;case">":y[h].push({[i]:{$gt:s}});break;case"<":y[h].push({[i]:{$lt:s}});break;case"><":Array.isArray(s)&&2<=s.length&&(y[h].push({[i]:{$lt:s}}),y[h]={[i]:{$gte:s[0],$lte:s[1]}});break;case"in":y[h].push({[i]:{$in:s}})}})),(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({},y))).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)})),n&&t){const f={};var c={},u=(a&&(a.forEach(e=>{f[e]=""}),c.$unset=Object.assign({},f)),r&&(c.$set=Object.assign({},r)),yield d.updateMany(y,c),{success:!0});if(null!=(s=g.body)&&s.stopRes)return u;b.send(u)}else b.status(400).json({err:"Invalid Security Configuration"})}}catch(e){b.status(400).json({err:e})}}),this.initUpdate=(c,u,h)=>__awaiter(this,void 0,void 0,function*(){var e,t;try{var i=this.modelChecker(c.body.data)&&c.body.id;if(i){var s=this.mongoDB.db(this.tableId).collection(this.collection),a=yield s.findOne({_id:i}),o=(this.debug&&this.debug(c.body,a),(0,QueryModel_1.checkBaseConfig)(this.config,c)),r=(0,QueryModel_1.checkOpsConfig)(this.config,"update",c,a);if(o&&r){var n={success:!0};if(c.body.sensitive)if((0,moment_1.default)(c.body.data.baseUpdatedAt).isAfter((0,moment_1.default)(a.baseUpdatedAt))){h&&(yield h(c.body.data));var d=this.parseModel(c.body.data);if(yield s.updateOne({_id:i},{$set:Object.assign({},d)}),null!=(e=c.body)&&e.stopRes)return n;u.send(n)}else u.status(400).json({err:"Refresh Sensitive Model"});else{h&&(yield h(c.body.data));var l=this.parseModel(c.body.data);if(yield s.updateOne({_id:i},{$set:Object.assign({},l)}),null!=(t=c.body)&&t.stopRes)return n;u.send(n)}}else u.status(400).json({err:"Invalid Security Configuration"})}else u.status(400).json({err:"Invalid Model Structure"})}catch(e){this.debug&&this.debug(null,null,e),u.status(400).json({err:e})}}),this.initBatchDelete=(f,g,b)=>__awaiter(this,void 0,void 0,function*(){var e,i;try{if(Array.isArray(f.body.id)){var s=this.mongoDB.db(this.tableId).collection(this.collection),a=yield s.find({_id:{$in:f.body.id}}),o=(0,QueryModel_1.checkBaseConfig)(this.config,f),r=yield a.toArray();let t=!0;if(0<r.length&&(yield Promise.all(r.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"delete",f,e)||(t=!1)}))),o&&t){b&&(yield b(r)),yield s.deleteMany({_id:{$in:f.body.id}});var n={success:!0};if(null!=(e=f.body)&&e.stopRes)return n;g.send(n)}else g.status(400).json({err:"Invalid Security Configuration"})}else{var d=f.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),c=yield l.find(y).toArray(),u=(0,QueryModel_1.checkBaseConfig)(this.config,f);let t=!0;if(0<c.length&&c.map(e=>{(0,QueryModel_1.checkOpsConfig)(this.config,"delete",f,e)||(t=!1)}),b&&(yield b(c)),u&&t){yield l.deleteMany(y);var h={success:!0};if(null!=(i=f.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=(n,d,l)=>__awaiter(this,void 0,void 0,function*(){var e;try{var t=n.body.id;if(t){var i=this.mongoDB.db(this.tableId).collection(this.collection),s=yield i.findOne({_id:t}),a=(0,QueryModel_1.checkBaseConfig)(this.config,n),o=(0,QueryModel_1.checkOpsConfig)(this.config,"delete",n,s);if(a&&o){l&&(yield l(s)),yield i.findOneAndDelete({_id:t});var r={success:!0};if(null!=(e=n.body)&&e.stopRes)return r;d.send(r)}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=(R,S)=>__awaiter(this,void 0,void 0,function*(){var e,t,i,s,a,o,r,n;try{var d=(0,QueryModel_1.checkBaseConfig)(this.config,R);if(d){var l=R.body.aggregate,c=null!=(e=R.body.query)?e:[];const w={};if(c&&0<c.length&&c.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&&(w.$or=[],e.searchIds.map(e=>{w.$or.push({[e]:{$regex:s,$options:"i"}})}));break;case"=":w[i]=s;break;case"!=":w[i]={$ne:s};break;case">":w[i]={$gt:s};break;case"<":w[i]={$lt:s};break;case"><":Array.isArray(s)&&2<=s.length&&(w[i]={$gte:s[0],$lte:s[1]});break;case"in":w[i]={$in:s}}}),l){var u=[],h=null!=(t=l.queryId)?t:"",y=null!=(i=l.dateId)?i:"baseUpdatedAt";for(const Q of null!=(s=l.range)?s:[]){Object.assign({},w);var f=Q.split("-"),[g,b=(0,moment_1.default)().format("DD/MM/YYYY HH:mm:ss")]=f,p=2===f.length,v=(0,moment_1.default)(g,"DD/MM/YYYY").isValid(),m=(0,moment_1.default)(b,"DD/MM/YYYY").isValid(),_=(0,moment_1.default)(g,"DD/MM/YYYY HH:mm:ss").format("YYYY-MM-DD HH:mm:ss"),M=(0,moment_1.default)(b,"DD/MM/YYYY HH:mm:ss").format("YYYY-MM-DD HH:mm:ss");if(!v||!m)return void S.status(400).json({err:"Invalid Aggregate Range Configuration"});var $=p?{$match:Object.assign(Object.assign({},w),{[y]:{$gte:new Date(_),$lte:new Date(M)}})}:{$match:Object.assign(Object.assign({},w),{[y]:{$lte:new Date(_)}})},D=[];let e=!1;var k=p?Q:"Begining until "+(0,moment_1.default)(g,"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 S.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})}var C={data:u};if(null!=(r=R.body)&&r.stopRes)return C;S.send(C)}else{let e=this.mongoDB.db(this.tableId).collection(this.collection).find(w);var j,Y,O,A=yield e.count({}),B=(R.body.cursor&&(e=e.skip(R.body.cursor)),R.body.limit&&(e=e.limit(R.body.limit)),R.body.sort&&(Y={[(j=R.body.sort).sortId]:"asc"===j.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",R,e)||(t=!1)}),d&&t)return O={data:B,count:A},null!=(n=R.body)&&n.stopRes?O:void S.send(O);S.status(400).json({err:"Invalid Security Configuration"})}}else S.status(400).json({err:"Invalid Security Configuration"})}catch(e){S.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=r?r+"-"+e:e,this.debug=n}}exports.MongoWrapper=MongoWrapper;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blixify-server",
3
- "version": "0.1.34",
3
+ "version": "0.1.35",
4
4
  "license": "MIT",
5
5
  "main": "dist/apis/index.js",
6
6
  "private": false,