123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- 'use strict';
- const BAR = Symbol('Application#comoBean');
- /**
- * modelbean
- * 用于数据库操作前置、后置
- */
- class comoBean {
- /**
- * [constructor 构造数据对象]
- * @author szjcomo
- * @createTime 2020-08-15
- * @param {Object} data [description]
- * @return {[type]} [description]
- */
- constructor(data = {},options = {}) {
- this.data = data;
- this.options = options;
- this.calls = [];
- }
- /**
- * [getOptions 获取数据]
- * @author szjcomo
- * @createTime 2020-08-15
- * @return {[type]} [description]
- */
- getOptions() {
- return this.options;
- }
- /**
- * [getData 返回数据对象]
- * @author szjcomo
- * @createTime 2020-08-15
- * @return {[type]} [description]
- */
- getData() {
- return this.data;
- }
- /**
- * [setData 设置数据对象]
- * @author szjcomo
- * @createTime 2020-08-15
- * @param {Object} data [description]
- */
- setData(data = {}) {
- this.data = data;
- }
- /**
- * [setOptions 设置数据]
- * @author szjcomo
- * @createTime 2020-08-15
- * @param {Object} options [description]
- */
- setOptions(options = {}) {
- this.options = options;
- }
- /**
- * [beforeAction 数据操作前]
- * @author szjcomo
- * @createTime 2020-08-15
- * @return {[type]} [description]
- */
- async before(ctx) {
- let that = this;
- let length = that.calls.length;
- for(let i = 0;i<length;i++) {
- let item = that.calls[i];
- if(item.sence == 'before') {
- await item.callback.call(that,ctx);
- }
- }
- }
- /**
- * [afterAction 数据操作后]
- * @author szjcomo
- * @createTime 2020-08-15
- * @return {[type]} [description]
- */
- async after(ctx,result) {
- let that = this;
- let length = that.calls.length;
- for(let i = 0;i<length;i++) {
- let item = that.calls[i];
- if(item.sence == 'after') {
- let res = await item.callback.call(that,ctx,result);
- if(res) result = res;
- }
- }
- return result;
- }
- /**
- * [addCall 注册回调参数]
- * @author szjcomo
- * @createTime 2020-08-19
- * @param {[type]} callable [description]
- * @param {String} sence [description]
- */
- addCall(callable,sence = 'before') {
- let that = this;
- if(typeof callable !== 'function') return false;
- that.calls.push({callback:callable,sence:sence});
- }
- }
- /**
- * [exports 导出对象]
- * @type {Object}
- */
- module.exports = {
- /**
- * [modelBean 获取一个modelBean]
- * @author szjcomo
- * @createTime 2020-09-05
- * @return {[type]} [description]
- */
- get comoBean() {
- let app = this;
- if(app[BAR]) return app[BAR];
- app[BAR] = {
- /**
- * [instance 获取一个实例]
- * @author szjcomo
- * @createTime 2020-09-05
- * @param {[type]} data [description]
- * @param {[type]} options [description]
- * @return {[type]} [description]
- */
- instance:function(data,options) {
- return new comoBean(data,options);
- },
- /**
- * [create 创建数据]
- * @author szjcomo
- * @createTime 2020-09-05
- * @param {[type]} bean [description]
- * @param {[type]} model [description]
- * @param {String} failMessage [description]
- * @return {[type]} [description]
- */
- create:async function(bean,model,failMessage = '数据创建失败') {
- if(!(bean instanceof comoBean)) throw new Error('Bad bean parameter, bean must be comoBean An example of');
- let before_bean = await bean.before.call(bean,app);
- if(before_bean) bean = before_bean;
- let res = await model.create(bean.getData(),bean.getOptions());
- if(!res) throw new Error(failMessage);
- res = await bean.after.call(bean,app,res);
- return res;
- },
- /**
- * [update 更新数据]
- * @author szjcomo
- * @createTime 2020-09-05
- * @param {[type]} bean [description]
- * @param {[type]} model [description]
- * @param {String} failMessage [description]
- * @return {[type]} [description]
- */
- update:async function(bean,model,failMessage = '数据更新失败') {
- if(!(bean instanceof comoBean)) throw new Error('Bad bean parameter, bean must be app.comoBean An example of');
- let before_bean = await bean.before.call(bean,app);
- if(before_bean) bean = before_bean;
- let res = await model.update(bean.getData(),bean.getOptions());
- if(!res[0]) throw new Error(failMessage);
- res = await bean.after.call(bean,app,res);
- return res;
- },
- /**
- * [delete 删除数据]
- * @author szjcomo
- * @createTime 2020-09-05
- * @param {[type]} bean [description]
- * @param {[type]} model [description]
- * @param {String} failMessage [description]
- * @return {[type]} [description]
- */
- delete:async function(bean,model,failMessage = '数据删除失败') {
- if(!(bean instanceof comoBean)) throw new Error('Bad bean parameter, bean must be app.comoBean An example of');
- let before_bean = await bean.before.call(bean,app);
- if(before_bean) bean = before_bean;
- let res = await model.destroy(bean.getOptions());
- if(!res) throw new Error(failMessage);
- res = await bean.after.call(bean,app,res);
- return res;
- },
- /**
- * [select 查询数据]
- * @author szjcomo
- * @createTime 2020-09-05
- * @param {[type]} bean [description]
- * @param {[type]} model [description]
- * @param {Boolean} all [description]
- * @param {Boolean} count [description]
- * @param {String} failMessage [description]
- * @return {[type]} [description]
- */
- select:async function(bean,model,count = false,all = false,failMessage = '数据获取失败') {
- if(!(bean instanceof comoBean)) throw new Error('Bad bean parameter, bean must be app.comoBean An example of');
- let before_bean = await bean.before.call(bean,app);
- if(before_bean) bean = before_bean;
- let res;
- if(count) {
- res = await model.findAndCountAll(bean.getOptions());
- } else if(all) {
- res = await model.findAll(bean.getOptions());
- } else {
- res = await model.findOne(bean.getOptions());
- }
- if(!res) throw new Error(failMessage);
- res = await bean.after.call(bean,app,res);
- return res;
- }
- }
- return app[BAR];
- }
- };
|