mongoose_helper.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. * @return {Promise}
  27. */
  28. findOne(conditions, fields = null) {
  29. let self = this;
  30. return new Promise(function (resolve, reject) {
  31. conditions = self._convertId(conditions);
  32. self.model.findOne(conditions, fields, function (error, data) {
  33. if (error) {
  34. reject(null);
  35. } else {
  36. resolve(data);
  37. }
  38. });
  39. });
  40. }
  41. /**
  42. * 查找数据
  43. *
  44. * @param {object} conditions
  45. * @param {object} fields
  46. * @param {object} option
  47. * @return {Promise}
  48. */
  49. find(conditions, fields = null, option = null) {
  50. let self = this;
  51. let limit = 0;
  52. let skip = 0;
  53. let sort = { _id: 1 };
  54. if (option !== null && Object.keys(option).length > 0) {
  55. limit = option.pageSize !== undefined ? option.pageSize : limit;
  56. skip = option.offset !== undefined ? option.offset : skip;
  57. sort = option.sort !== undefined ? option.sort : sort;
  58. }
  59. return new Promise(function (resolve, reject) {
  60. self.model.find(conditions, fields, option, function (error, data) {
  61. if (error) {
  62. reject(error);
  63. } else {
  64. resolve(data);
  65. }
  66. }).skip(skip).limit(limit).sort(sort);
  67. });
  68. }
  69. /**
  70. * 关联查找数据
  71. *
  72. * @param {object} conditions
  73. * @param {object} fields
  74. * @param {String|Object} populate
  75. * @return {Promise}
  76. */
  77. findWithPopulate(conditions, fields = null, populate = '') {
  78. let self = this;
  79. return new Promise(function (resolve, reject) {
  80. self.model.find(conditions, fields).populate(populate).exec(function (error, data) {
  81. if (error) {
  82. reject(error);
  83. } else {
  84. resolve(data);
  85. }
  86. });
  87. });
  88. }
  89. /**
  90. * 查找且更新(原子操作)
  91. *
  92. * @param {object} update
  93. * @param {object} condition
  94. * @param {object} options
  95. * @return {Promise}
  96. */
  97. findAndModify(condition, update, options) {
  98. let self = this;
  99. return new Promise(function (resolve, reject) {
  100. self.model.findOneAndUpdate(condition, update, options, function (error, data) {
  101. if (error) {
  102. reject(error);
  103. } else {
  104. resolve(data);
  105. }
  106. });
  107. });
  108. }
  109. /**
  110. * 新增操作
  111. *
  112. * @param {object} data
  113. * @return {Promise}
  114. */
  115. create(data) {
  116. let self = this;
  117. return new Promise(function (resolve, reject) {
  118. self.model.create(data, function (error, data) {
  119. if (error) {
  120. reject(error);
  121. } else {
  122. resolve(data);
  123. }
  124. });
  125. });
  126. }
  127. /**
  128. * 统计数据数量
  129. *
  130. * @param {Object} condition
  131. * @return {Promise}
  132. */
  133. count(condition) {
  134. let self = this;
  135. return new Promise(function (resolve, reject) {
  136. self.model.count(condition, function (error, data) {
  137. if (error) {
  138. reject(error);
  139. } else {
  140. resolve(data);
  141. }
  142. });
  143. });
  144. }
  145. /**
  146. * 删除数据
  147. *
  148. * @param {Object} condition
  149. * @return {Promise}
  150. */
  151. delete(condition) {
  152. let self = this;
  153. return new Promise(function (resolve, reject) {
  154. condition = self._convertId(condition);
  155. self.model.remove(condition, function (error, data) {
  156. if (error) {
  157. reject(error);
  158. } else {
  159. resolve(data);
  160. }
  161. });
  162. });
  163. }
  164. /**
  165. * 更新数据
  166. *
  167. * @param {Object} condition
  168. * @param {Object} updateData
  169. * @return {Promise}
  170. */
  171. update(condition, updateData) {
  172. let self = this;
  173. return new Promise(function (resolve, reject) {
  174. condition = self._convertId(condition);
  175. self.model.update(condition, { $set: updateData }, function (error, data) {
  176. if (error) {
  177. reject(error);
  178. } else {
  179. resolve(data);
  180. }
  181. });
  182. });
  183. }
  184. /**
  185. * 自增
  186. *
  187. * @param {Object} condition
  188. * @param {Object} incData
  189. * @return {Promise}
  190. */
  191. incr(condition, incData) {
  192. let self = this;
  193. return new Promise(function (resolve, reject) {
  194. condition = self._convertId(condition);
  195. self.model.update(condition, { $inc: incData }, function (error, data) {
  196. if (error) {
  197. reject(error);
  198. } else {
  199. resolve(data);
  200. }
  201. });
  202. });
  203. }
  204. /**
  205. * 嵌套结构的新增
  206. *
  207. * @param {Object} condition
  208. * @param {Object} insertData
  209. * @return {Promise}
  210. */
  211. addToSet(condition, insertData) {
  212. let self = this;
  213. return new Promise(function (resolve, reject) {
  214. condition = self._convertId(condition);
  215. self.model.update(condition, { $addToSet: insertData }, function (error, data) {
  216. if (error) {
  217. reject(error);
  218. } else {
  219. resolve(data);
  220. }
  221. });
  222. });
  223. }
  224. /**
  225. * 删除嵌套结构数据
  226. *
  227. * @param {Object} condition
  228. * @param {Object} deleteData
  229. * @return {Promise}
  230. */
  231. deleteSet(condition, deleteData) {
  232. let self = this;
  233. return new Promise(function (resolve, reject) {
  234. condition = self._convertId(condition);
  235. self.model.update(condition, { $pull: deleteData }, function (error, data) {
  236. if (error) {
  237. reject(error);
  238. } else {
  239. resolve(data);
  240. }
  241. });
  242. });
  243. }
  244. /**
  245. * id转换为objectId
  246. *
  247. * @param {object} condition
  248. * @return {object}
  249. */
  250. _convertId(condition) {
  251. // 对于ID的处理
  252. if (condition === null || condition._id === undefined) {
  253. return condition;
  254. }
  255. let result = mongoose.Types.ObjectId(condition._id);
  256. condition._id = result;
  257. return condition;
  258. }
  259. }
  260. export default MongooseHelper;