application.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. 'use strict';
  2. const BAR = Symbol('Application#comoBean');
  3. /**
  4. * modelbean
  5. * 用于数据库操作前置、后置
  6. */
  7. class comoBean {
  8. /**
  9. * [constructor 构造数据对象]
  10. * @author szjcomo
  11. * @createTime 2020-08-15
  12. * @param {Object} data [description]
  13. * @return {[type]} [description]
  14. */
  15. constructor(data = {},options = {}) {
  16. this.data = data;
  17. this.options = options;
  18. this.calls = [];
  19. }
  20. /**
  21. * [getOptions 获取数据]
  22. * @author szjcomo
  23. * @createTime 2020-08-15
  24. * @return {[type]} [description]
  25. */
  26. getOptions() {
  27. return this.options;
  28. }
  29. /**
  30. * [getData 返回数据对象]
  31. * @author szjcomo
  32. * @createTime 2020-08-15
  33. * @return {[type]} [description]
  34. */
  35. getData() {
  36. return this.data;
  37. }
  38. /**
  39. * [setData 设置数据对象]
  40. * @author szjcomo
  41. * @createTime 2020-08-15
  42. * @param {Object} data [description]
  43. */
  44. setData(data = {}) {
  45. this.data = data;
  46. }
  47. /**
  48. * [setOptions 设置数据]
  49. * @author szjcomo
  50. * @createTime 2020-08-15
  51. * @param {Object} options [description]
  52. */
  53. setOptions(options = {}) {
  54. this.options = options;
  55. }
  56. /**
  57. * [beforeAction 数据操作前]
  58. * @author szjcomo
  59. * @createTime 2020-08-15
  60. * @return {[type]} [description]
  61. */
  62. async before(ctx) {
  63. let that = this;
  64. let length = that.calls.length;
  65. for(let i = 0;i<length;i++) {
  66. let item = that.calls[i];
  67. if(item.sence == 'before') {
  68. await item.callback.call(that,ctx);
  69. }
  70. }
  71. }
  72. /**
  73. * [afterAction 数据操作后]
  74. * @author szjcomo
  75. * @createTime 2020-08-15
  76. * @return {[type]} [description]
  77. */
  78. async after(ctx,result) {
  79. let that = this;
  80. let length = that.calls.length;
  81. for(let i = 0;i<length;i++) {
  82. let item = that.calls[i];
  83. if(item.sence == 'after') {
  84. let res = await item.callback.call(that,ctx,result);
  85. if(res) result = res;
  86. }
  87. }
  88. return result;
  89. }
  90. /**
  91. * [addCall 注册回调参数]
  92. * @author szjcomo
  93. * @createTime 2020-08-19
  94. * @param {[type]} callable [description]
  95. * @param {String} sence [description]
  96. */
  97. addCall(callable,sence = 'before') {
  98. let that = this;
  99. if(typeof callable !== 'function') return false;
  100. that.calls.push({callback:callable,sence:sence});
  101. }
  102. }
  103. /**
  104. * [exports 导出对象]
  105. * @type {Object}
  106. */
  107. module.exports = {
  108. /**
  109. * [modelBean 获取一个modelBean]
  110. * @author szjcomo
  111. * @createTime 2020-09-05
  112. * @return {[type]} [description]
  113. */
  114. get comoBean() {
  115. let app = this;
  116. if(app[BAR]) return app[BAR];
  117. app[BAR] = {
  118. /**
  119. * [instance 获取一个实例]
  120. * @author szjcomo
  121. * @createTime 2020-09-05
  122. * @param {[type]} data [description]
  123. * @param {[type]} options [description]
  124. * @return {[type]} [description]
  125. */
  126. instance:function(data,options) {
  127. return new comoBean(data,options);
  128. },
  129. /**
  130. * [create 创建数据]
  131. * @author szjcomo
  132. * @createTime 2020-09-05
  133. * @param {[type]} bean [description]
  134. * @param {[type]} model [description]
  135. * @param {String} failMessage [description]
  136. * @return {[type]} [description]
  137. */
  138. create:async function(bean,model,failMessage = '数据创建失败') {
  139. if(!(bean instanceof comoBean)) throw new Error('Bad bean parameter, bean must be comoBean An example of');
  140. let before_bean = await bean.before.call(bean,app);
  141. if(before_bean) bean = before_bean;
  142. let res = await model.create(bean.getData(),bean.getOptions());
  143. if(!res) throw new Error(failMessage);
  144. res = await bean.after.call(bean,app,res);
  145. return res;
  146. },
  147. /**
  148. * [update 更新数据]
  149. * @author szjcomo
  150. * @createTime 2020-09-05
  151. * @param {[type]} bean [description]
  152. * @param {[type]} model [description]
  153. * @param {String} failMessage [description]
  154. * @return {[type]} [description]
  155. */
  156. update:async function(bean,model,failMessage = '数据更新失败') {
  157. if(!(bean instanceof comoBean)) throw new Error('Bad bean parameter, bean must be app.comoBean An example of');
  158. let before_bean = await bean.before.call(bean,app);
  159. if(before_bean) bean = before_bean;
  160. let res = await model.update(bean.getData(),bean.getOptions());
  161. if(!res[0]) throw new Error(failMessage);
  162. res = await bean.after.call(bean,app,res);
  163. return res;
  164. },
  165. /**
  166. * [delete 删除数据]
  167. * @author szjcomo
  168. * @createTime 2020-09-05
  169. * @param {[type]} bean [description]
  170. * @param {[type]} model [description]
  171. * @param {String} failMessage [description]
  172. * @return {[type]} [description]
  173. */
  174. delete:async function(bean,model,failMessage = '数据删除失败') {
  175. if(!(bean instanceof comoBean)) throw new Error('Bad bean parameter, bean must be app.comoBean An example of');
  176. let before_bean = await bean.before.call(bean,app);
  177. if(before_bean) bean = before_bean;
  178. let res = await model.destroy(bean.getOptions());
  179. if(!res) throw new Error(failMessage);
  180. res = await bean.after.call(bean,app,res);
  181. return res;
  182. },
  183. /**
  184. * [select 查询数据]
  185. * @author szjcomo
  186. * @createTime 2020-09-05
  187. * @param {[type]} bean [description]
  188. * @param {[type]} model [description]
  189. * @param {Boolean} all [description]
  190. * @param {Boolean} count [description]
  191. * @param {String} failMessage [description]
  192. * @return {[type]} [description]
  193. */
  194. select:async function(bean,model,count = false,all = false,failMessage = '数据获取失败') {
  195. if(!(bean instanceof comoBean)) throw new Error('Bad bean parameter, bean must be app.comoBean An example of');
  196. let before_bean = await bean.before.call(bean,app);
  197. if(before_bean) bean = before_bean;
  198. let res;
  199. if(count) {
  200. res = await model.findAndCountAll(bean.getOptions());
  201. } else if(all) {
  202. res = await model.findAll(bean.getOptions());
  203. } else {
  204. res = await model.findOne(bean.getOptions());
  205. }
  206. if(!res) throw new Error(failMessage);
  207. res = await bean.after.call(bean,app,res);
  208. return res;
  209. }
  210. }
  211. return app[BAR];
  212. }
  213. };