mongoose_helper.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. self.model.findOne(conditions, fields, function (error, data) {
  32. if (error) {
  33. reject(null);
  34. } else {
  35. resolve(data);
  36. }
  37. });
  38. });
  39. }
  40. /**
  41. * 查找数据
  42. *
  43. * @param {object} conditions
  44. * @param {object} fields
  45. * @param {object} option
  46. * @return {Promise}
  47. */
  48. find(conditions, fields = null, option = null) {
  49. let self = this;
  50. let limit = 0;
  51. let skip = 0;
  52. if (Object.keys(option).length > 0) {
  53. limit = option.pageSize !== undefined ? option.pageSize : limit;
  54. skip = option.offset !== undefined ? option.offset : skip;
  55. }
  56. return new Promise(function (resolve, reject) {
  57. self.model.find(conditions, fields, option, function (error, data) {
  58. if (error) {
  59. reject(error);
  60. } else {
  61. resolve(data);
  62. }
  63. }).skip(skip).limit(limit);
  64. });
  65. }
  66. /**
  67. * 关联查找数据
  68. *
  69. * @param {object} conditions
  70. * @param {object} fields
  71. * @param {String|Object} populate
  72. * @return {Promise}
  73. */
  74. findWithPopulate(conditions, fields = null, populate = '') {
  75. let self = this;
  76. return new Promise(function (resolve, reject) {
  77. self.model.find(conditions, fields).populate(populate).exec(function(error, data) {
  78. if (error) {
  79. reject(error);
  80. } else {
  81. resolve(data);
  82. }
  83. });
  84. });
  85. }
  86. /**
  87. * 查找且更新(原子操作)
  88. *
  89. * @param {object} update
  90. * @param {object} condition
  91. * @param {object} options
  92. * @return {Promise}
  93. */
  94. findAndModify(condition, update, options) {
  95. let self = this;
  96. return new Promise(function (resolve, reject) {
  97. self.model.findOneAndUpdate(condition, update, options, function(error, data) {
  98. if (error) {
  99. reject(error);
  100. } else {
  101. resolve(data);
  102. }
  103. });
  104. });
  105. }
  106. /**
  107. * 新增操作
  108. *
  109. * @param {object} data
  110. * @return {Promise}
  111. */
  112. create(data) {
  113. let self = this;
  114. return new Promise(function (resolve, reject) {
  115. self.model.create(data, function(error, data) {
  116. if (error) {
  117. reject(error);
  118. } else {
  119. resolve(data);
  120. }
  121. });
  122. });
  123. }
  124. /**
  125. * 统计数据数量
  126. *
  127. * @param {Object} condition
  128. * @return {Promise}
  129. */
  130. count(condition) {
  131. let self = this;
  132. return new Promise(function(resolve, reject) {
  133. self.model.count(condition, function(error, data) {
  134. if (error) {
  135. reject(error);
  136. } else {
  137. resolve(data);
  138. }
  139. });
  140. });
  141. }
  142. /**
  143. * 删除数据
  144. *
  145. * @param {Object} condition
  146. * @return {Promise}
  147. */
  148. delete(condition) {
  149. let self = this;
  150. return new Promise(function(resolve, reject) {
  151. condition = self._convertId(condition);
  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;