mongoose_helper.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**
  2. * mongodb Helper
  3. *
  4. * @author caiaolin
  5. * @date 2017/5/22.
  6. */
  7. const mongoose = require('mongoose');
  8. class MongooseHelper {
  9. /**
  10. * mongoose连接对象
  11. *
  12. * @var {object}
  13. */
  14. connect = null;
  15. /**
  16. * mongoose数据模型
  17. *
  18. * @var {object}
  19. */
  20. model = null;
  21. /**
  22. * 查找单一数据
  23. *
  24. * @param {object} conditions
  25. * @param {object} fields
  26. * @param {Object} option
  27. * @return {Promise}
  28. */
  29. findOne(conditions, fields = null, option = null) {
  30. let self = this;
  31. let sort = {};
  32. if (option !== null && Object.keys(option).length > 0) {
  33. sort = option.sort !== undefined ? option.sort : sort;
  34. }
  35. return new Promise(function (resolve, reject) {
  36. conditions = self._convertId(conditions);
  37. self.model.findOne(conditions, fields, function (error, data) {
  38. if (error) {
  39. reject(null);
  40. } else {
  41. resolve(data);
  42. }
  43. }).sort(sort);
  44. });
  45. }
  46. /**
  47. * 查找数据
  48. *
  49. * @param {object} conditions
  50. * @param {object} fields
  51. * @param {object} option
  52. * @return {Promise}
  53. */
  54. find(conditions, fields = null, option = null) {
  55. let self = this;
  56. let limit = 0;
  57. let skip = 0;
  58. let sort = {_id:1};
  59. if (option !== null && Object.keys(option).length > 0) {
  60. limit = option.pageSize !== undefined ? option.pageSize : limit;
  61. skip = option.offset !== undefined ? option.offset : skip;
  62. sort = option.sort !== undefined ? option.sort : sort;
  63. }
  64. return new Promise(function (resolve, reject) {
  65. self.model.find(conditions, fields, option, function (error, data) {
  66. if (error) {
  67. reject(error);
  68. } else {
  69. resolve(data);
  70. }
  71. }).sort(sort).skip(skip).limit(limit);
  72. });
  73. }
  74. /**
  75. * 关联查找数据
  76. *
  77. * @param {object} conditions
  78. * @param {object} fields
  79. * @param {Object} option
  80. * @param {String|Object} populate
  81. * @return {Promise}
  82. */
  83. findWithPopulate(conditions, fields = null, option, populate = '') {
  84. let self = this;
  85. let limit = 0;
  86. let skip = 0;
  87. let sort = {};
  88. if (option !== null && Object.keys(option).length > 0) {
  89. limit = option.pageSize !== undefined ? option.pageSize : limit;
  90. skip = option.offset !== undefined ? option.offset : skip;
  91. sort = option.sort !== undefined ? option.sort : sort;
  92. }
  93. conditions = self._convertId(conditions);
  94. return new Promise(function (resolve, reject) {
  95. self.model.find(conditions, fields).skip(skip).limit(limit).sort(sort)
  96. .populate(populate).exec(function(error, data) {
  97. if (error) {
  98. reject(error);
  99. } else {
  100. resolve(data);
  101. }
  102. });
  103. });
  104. }
  105. /**
  106. * 查找且更新(原子操作)
  107. *
  108. * @param {object} update
  109. * @param {object} condition
  110. * @param {object} options
  111. * @return {Promise}
  112. */
  113. findAndModify(condition, update, options) {
  114. let self = this;
  115. return new Promise(function (resolve, reject) {
  116. self.model.findOneAndUpdate(condition, update, options, function(error, data) {
  117. if (error) {
  118. reject(error);
  119. } else {
  120. resolve(data);
  121. }
  122. });
  123. });
  124. }
  125. /**
  126. * 新增操作
  127. *
  128. * @param {object} data
  129. * @return {Promise}
  130. */
  131. create(data) {
  132. let self = this;
  133. return new Promise(function (resolve, reject) {
  134. self.model.create(data, function(error, data) {
  135. if (error) {
  136. reject(error);
  137. } else {
  138. resolve(data);
  139. }
  140. });
  141. });
  142. }
  143. /**
  144. * 统计数据数量
  145. *
  146. * @param {Object} condition
  147. * @return {Promise}
  148. */
  149. count(condition) {
  150. let self = this;
  151. return new Promise(function(resolve, reject) {
  152. self.model.count(condition, function(error, data) {
  153. if (error) {
  154. reject(error);
  155. } else {
  156. resolve(data);
  157. }
  158. });
  159. });
  160. }
  161. /**
  162. * 删除数据
  163. *
  164. * @param {Object} condition
  165. * @return {Promise}
  166. */
  167. delete(condition) {
  168. let self = this;
  169. return new Promise(function(resolve, reject) {
  170. self.model.remove(condition, function(error, data) {
  171. if (error) {
  172. reject(error);
  173. } else {
  174. resolve(data);
  175. }
  176. });
  177. });
  178. }
  179. /**
  180. * 更新数据
  181. *
  182. * @param {Object} condition
  183. * @param {Object} updateData
  184. * @return {Promise}
  185. */
  186. update(condition, updateData) {
  187. let self = this;
  188. return new Promise(function(resolve, reject) {
  189. condition = self._convertId(condition);
  190. self.model.update(condition, {$set: updateData}, function(error, data) {
  191. if (error) {
  192. reject(error);
  193. } else {
  194. resolve(data);
  195. }
  196. });
  197. });
  198. }
  199. /**
  200. * id转换为objectId
  201. *
  202. * @param {object} condition
  203. * @return {object}
  204. */
  205. _convertId(condition) {
  206. // 对于ID的处理
  207. if (condition === null || condition._id === undefined) {
  208. return condition;
  209. }
  210. condition._id = condition._id.toString();
  211. let result = mongoose.Types.ObjectId(condition._id);
  212. condition._id = result;
  213. return condition;
  214. }
  215. }
  216. module.exports = MongooseHelper;