mongoose_helper.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. if (option !== null && Object.keys(option).length > 0) {
  54. limit = option.pageSize !== undefined ? option.pageSize : limit;
  55. skip = option.offset !== undefined ? option.offset : skip;
  56. }
  57. return new Promise(function (resolve, reject) {
  58. self.model.find(conditions, fields, option, function (error, data) {
  59. if (error) {
  60. reject(error);
  61. } else {
  62. resolve(data);
  63. }
  64. }).skip(skip).limit(limit);
  65. });
  66. }
  67. /**
  68. * 关联查找数据
  69. *
  70. * @param {object} conditions
  71. * @param {object} fields
  72. * @param {String|Object} populate
  73. * @return {Promise}
  74. */
  75. findWithPopulate(conditions, fields = null, populate = '') {
  76. let self = this;
  77. return new Promise(function (resolve, reject) {
  78. self.model.find(conditions, fields).populate(populate).exec(function(error, data) {
  79. if (error) {
  80. reject(error);
  81. } else {
  82. resolve(data);
  83. }
  84. });
  85. });
  86. }
  87. /**
  88. * 查找且更新(原子操作)
  89. *
  90. * @param {object} update
  91. * @param {object} condition
  92. * @param {object} options
  93. * @return {Promise}
  94. */
  95. findAndModify(condition, update, options) {
  96. let self = this;
  97. return new Promise(function (resolve, reject) {
  98. self.model.findOneAndUpdate(condition, update, options, function(error, data) {
  99. if (error) {
  100. reject(error);
  101. } else {
  102. resolve(data);
  103. }
  104. });
  105. });
  106. }
  107. /**
  108. * 新增操作
  109. *
  110. * @param {object} data
  111. * @return {Promise}
  112. */
  113. create(data) {
  114. let self = this;
  115. return new Promise(function (resolve, reject) {
  116. self.model.create(data, function(error, data) {
  117. if (error) {
  118. reject(error);
  119. } else {
  120. resolve(data);
  121. }
  122. });
  123. });
  124. }
  125. /**
  126. * 统计数据数量
  127. *
  128. * @param {Object} condition
  129. * @return {Promise}
  130. */
  131. count(condition) {
  132. let self = this;
  133. return new Promise(function(resolve, reject) {
  134. self.model.count(condition, 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. delete(condition) {
  150. let self = this;
  151. return new Promise(function(resolve, reject) {
  152. self.model.remove(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. * @param {Object} updateData
  166. * @return {Promise}
  167. */
  168. update(condition, updateData) {
  169. let self = this;
  170. return new Promise(function(resolve, reject) {
  171. condition = self._convertId(condition);
  172. self.model.update(condition, {$set: updateData}, function(error, data) {
  173. if (error) {
  174. reject(error);
  175. } else {
  176. resolve(data);
  177. }
  178. });
  179. });
  180. }
  181. /**
  182. * id转换为objectId
  183. *
  184. * @param {object} condition
  185. * @return {object}
  186. */
  187. _convertId(condition) {
  188. // 对于ID的处理
  189. if (condition === null || condition._id === undefined) {
  190. return condition;
  191. }
  192. let result = mongoose.Types.ObjectId(condition._id);
  193. condition._id = result;
  194. return condition;
  195. }
  196. }
  197. export default MongooseHelper;