mongoose_helper.js 4.8 KB

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