DAO.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. var path = require("path");
  2. // 获取数据库模型
  3. databaseModule = require(path.join(process.cwd(),"modules/database"));
  4. var logger = require('../modules/logger').logger();
  5. /**
  6. * 创建对象数据
  7. *
  8. * @param {[type]} modelName 模型名称
  9. * @param {[type]} obj 模型对象
  10. * @param {Function} cb 回调函数
  11. */
  12. module.exports.create = function(modelName,obj,cb) {
  13. var db = databaseModule.getDatabase();
  14. var Model = db.models[modelName];
  15. Model.create(obj,cb);
  16. }
  17. /**
  18. * 获取所有数据
  19. *
  20. * @param {[type]} conditions 查询条件
  21. * 查询条件统一规范
  22. * conditions
  23. {
  24. "columns" : {
  25. 字段条件
  26. "字段名" : "条件值"
  27. },
  28. "offset" : "偏移",
  29. "omit" : ["字段"],
  30. "only" : ["需要字段"],
  31. "limit" : "",
  32. "order" :[
  33. "字段" , A | Z,
  34. ...
  35. ]
  36. }
  37. * @param {Function} cb 回调函数
  38. */
  39. module.exports.list = function(modelName,conditions,cb) {
  40. var db = databaseModule.getDatabase();
  41. var model = db.models[modelName];
  42. if(!model) return cb("模型不存在",null);
  43. if(conditions) {
  44. if(conditions["columns"]) {
  45. model = model.find(conditions["columns"]);
  46. } else {
  47. model = model.find();
  48. }
  49. if(conditions["offset"]) {
  50. model = model.offset(parseInt(conditions["offset"]));
  51. }
  52. if(conditions["limit"]) {
  53. model = model.limit(parseInt(conditions["limit"]));
  54. }
  55. if(conditions["only"]) {
  56. model = model.only(conditions["only"]);
  57. }
  58. if(conditions["omit"]) {
  59. model = model.omit(conditions["omit"]);
  60. }
  61. if(conditions["order"]) {
  62. model = model.order(conditions["order"]);
  63. }
  64. } else {
  65. model = model.find();
  66. }
  67. model.run(function(err,models) {
  68. if(err) {
  69. console.log(err);
  70. return cb("查询失败",null);
  71. }
  72. cb(null,models);
  73. });
  74. };
  75. module.exports.countByConditions = function(modelName,conditions,cb) {
  76. var db = databaseModule.getDatabase();
  77. var model = db.models[modelName];
  78. if(!model) return cb("模型不存在",null);
  79. var resultCB = function(err,count){
  80. if(err) {
  81. return cb("查询失败",null);
  82. }
  83. cb(null,count);
  84. }
  85. if(conditions) {
  86. if(conditions["columns"]) {
  87. model = model.count(conditions["columns"],resultCB);
  88. } else {
  89. model = model.count(resultCB);
  90. }
  91. } else {
  92. model = model.count(resultCB);
  93. }
  94. };
  95. /**
  96. * 获取一条数据
  97. * @param {[type]} modelName 模型名称
  98. * @param {[数组]} conditions 条件集合
  99. * @param {Function} cb 回调函数
  100. */
  101. module.exports.findOne = function(modelName,conditions,cb) {
  102. var db = databaseModule.getDatabase();
  103. var Model = db.models[modelName];
  104. if(!Model) return cb("模型不存在",null);
  105. if(!conditions) return cb("条件为空",null);
  106. Model.one(conditions,function(err,obj){
  107. logger.debug(err);
  108. if(err) {
  109. return cb("查询失败",null);
  110. }
  111. return cb(null,obj);
  112. });
  113. }
  114. /**
  115. * 更新对象数据
  116. *
  117. * @param {[type]} modelName 模型名称
  118. * @param {[type]} id 数据关键ID
  119. * @param {[type]} updateObj 更新对象数据
  120. * @param {Function} cb 回调函数
  121. */
  122. module.exports.update = function(modelName,id,updateObj,cb) {
  123. var db = databaseModule.getDatabase();
  124. var Model = db.models[modelName];
  125. Model.get(id,function(err,obj){
  126. if(err) return cb("更新失败",null);
  127. obj.save(updateObj,cb);
  128. });
  129. }
  130. /**
  131. * 通过主键ID获取对象
  132. * @param {[type]} modelName 模型名称
  133. * @param {[type]} id 主键ID
  134. * @param {Function} cb 回调函数
  135. */
  136. module.exports.show = function(modelName,id,cb) {
  137. var db = databaseModule.getDatabase();
  138. var Model = db.models[modelName];
  139. Model.get(id,function(err,obj){
  140. cb(err,obj);
  141. });
  142. }
  143. /**
  144. * 通过主键ID删除对象
  145. *
  146. * @param {[type]} modelName 模型名称
  147. * @param {[type]} id 主键ID
  148. * @param {Function} cb 回调函数
  149. */
  150. module.exports.destroy = function(modelName,id,cb) {
  151. var db = databaseModule.getDatabase();
  152. var Model = db.models[modelName];
  153. Model.get(id,function(err,obj){
  154. if(err) return cb("无模型ID");
  155. obj.remove(function(err) {
  156. if(err) return cb("删除失败");
  157. return cb(null);
  158. });
  159. });
  160. }
  161. /**
  162. * 通过模型名称获取数据库数量
  163. *
  164. * @param {[type]} modelName 模型名称
  165. * @param {Function} cb 回调函数
  166. */
  167. module.exports.count = function(modelName,cb) {
  168. var db = databaseModule.getDatabase();
  169. var Model = db.models[modelName];
  170. Model.count(cb);
  171. }
  172. /**
  173. * 通过条件判断数据是否存在
  174. *
  175. * @param {[type]} modelName 模块名
  176. * @param {[type]} conditions 条件
  177. * @param {Function} cb 回调函数
  178. */
  179. module.exports.exists = function(modelName,conditions,cb) {
  180. var db = databaseModule.getDatabase();
  181. var Model = db.models[modelName];
  182. Model.exists(conditions,function(err,isExists){
  183. if(err) return cb("查询失败");
  184. cb(null,isExists);
  185. });
  186. }
  187. module.exports.getModel = function(modelName) {
  188. var db = databaseModule.getDatabase();
  189. return db.models[modelName];
  190. }