mongoose_helper.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**
  2. * mongodb Helper
  3. *
  4. * @author caiaolin
  5. * @date 2017/5/22.
  6. */
  7. import mongoose from "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. if (option !== null && Object.keys(option).length > 0) {
  59. limit = option.pageSize !== undefined ? option.pageSize : limit;
  60. skip = option.offset !== undefined ? option.offset : skip;
  61. }
  62. return new Promise(function (resolve, reject) {
  63. self.model.find(conditions, fields, option, function (error, data) {
  64. if (error) {
  65. reject(error);
  66. } else {
  67. resolve(data);
  68. }
  69. }).skip(skip).limit(limit);
  70. });
  71. }
  72. /**
  73. * 关联查找数据
  74. *
  75. * @param {object} conditions
  76. * @param {object} fields
  77. * @param {Object} option
  78. * @param {String|Object} populate
  79. * @return {Promise}
  80. */
  81. findWithPopulate(conditions, fields = null, option, populate = '') {
  82. let self = this;
  83. let limit = 0;
  84. let skip = 0;
  85. let sort = {};
  86. if (option !== null && Object.keys(option).length > 0) {
  87. limit = option.pageSize !== undefined ? option.pageSize : limit;
  88. skip = option.offset !== undefined ? option.offset : skip;
  89. sort = option.sort !== undefined ? option.sort : sort;
  90. }
  91. conditions = self._convertId(conditions);
  92. return new Promise(function (resolve, reject) {
  93. self.model.find(conditions, fields).skip(skip).limit(limit).sort(sort)
  94. .populate(populate).exec(function(error, data) {
  95. if (error) {
  96. reject(error);
  97. } else {
  98. resolve(data);
  99. }
  100. });
  101. });
  102. }
  103. /**
  104. * 查找且更新(原子操作)
  105. *
  106. * @param {object} update
  107. * @param {object} condition
  108. * @param {object} options
  109. * @return {Promise}
  110. */
  111. findAndModify(condition, update, options) {
  112. let self = this;
  113. return new Promise(function (resolve, reject) {
  114. self.model.findOneAndUpdate(condition, update, options, function(error, data) {
  115. if (error) {
  116. reject(error);
  117. } else {
  118. resolve(data);
  119. }
  120. });
  121. });
  122. }
  123. /**
  124. * 新增操作
  125. *
  126. * @param {object} data
  127. * @return {Promise}
  128. */
  129. create(data) {
  130. let self = this;
  131. return new Promise(function (resolve, reject) {
  132. self.model.create(data, function(error, data) {
  133. if (error) {
  134. reject(error);
  135. } else {
  136. resolve(data);
  137. }
  138. });
  139. });
  140. }
  141. /**
  142. * 统计数据数量
  143. *
  144. * @param {Object} condition
  145. * @return {Promise}
  146. */
  147. count(condition) {
  148. let self = this;
  149. return new Promise(function(resolve, reject) {
  150. self.model.count(condition, function(error, data) {
  151. if (error) {
  152. reject(error);
  153. } else {
  154. resolve(data);
  155. }
  156. });
  157. });
  158. }
  159. /**
  160. * 删除数据
  161. *
  162. * @param {Object} condition
  163. * @return {Promise}
  164. */
  165. delete(condition) {
  166. let self = this;
  167. return new Promise(function(resolve, reject) {
  168. self.model.remove(condition, function(error, data) {
  169. if (error) {
  170. reject(error);
  171. } else {
  172. resolve(data);
  173. }
  174. });
  175. });
  176. }
  177. /**
  178. * 更新数据
  179. *
  180. * @param {Object} condition
  181. * @param {Object} updateData
  182. * @return {Promise}
  183. */
  184. update(condition, updateData) {
  185. let self = this;
  186. return new Promise(function(resolve, reject) {
  187. condition = self._convertId(condition);
  188. self.model.update(condition, {$set: updateData}, function(error, data) {
  189. if (error) {
  190. reject(error);
  191. } else {
  192. resolve(data);
  193. }
  194. });
  195. });
  196. }
  197. /**
  198. * id转换为objectId
  199. *
  200. * @param {object} condition
  201. * @return {object}
  202. */
  203. _convertId(condition) {
  204. // 对于ID的处理
  205. if (condition === null || condition._id === undefined) {
  206. return condition;
  207. }
  208. let result = mongoose.Types.ObjectId(condition._id);
  209. condition._id = result;
  210. return condition;
  211. }
  212. }
  213. export default MongooseHelper;