mongoose_helper.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. * mongodb Helper
  3. *
  4. * @author caiaolin
  5. * @date 2017/5/22.
  6. */
  7. class MongooseHelper {
  8. /**
  9. * mongoose连接对象
  10. *
  11. * @var {object}
  12. */
  13. connect = null;
  14. /**
  15. * mongoose数据模型
  16. *
  17. * @var {object}
  18. */
  19. model = null;
  20. /**
  21. * 查找单一数据
  22. *
  23. * @param {object} conditions
  24. * @param {object} fields
  25. * @return {Promise}
  26. */
  27. findOne(conditions, fields = null) {
  28. let self = this;
  29. return new Promise(function (resolve, reject) {
  30. self.model.findOne(conditions, fields, function (error, data) {
  31. if (error) {
  32. reject(null);
  33. } else {
  34. resolve(data);
  35. }
  36. });
  37. });
  38. }
  39. /**
  40. * 查找数据
  41. *
  42. * @param {object} conditions
  43. * @param {object} fields
  44. * @param {object} option
  45. * @return {Promise}
  46. */
  47. find(conditions, fields = null, option = null) {
  48. let self = this;
  49. let limit = 0;
  50. let skip = 0;
  51. if (option !== null && Object.keys(option).length > 0) {
  52. limit = option.pageSize !== undefined ? option.pageSize : limit;
  53. skip = option.offset !== undefined ? option.offset : skip;
  54. }
  55. return new Promise(function (resolve, reject) {
  56. self.model.find(conditions, fields, option, function (error, data) {
  57. if (error) {
  58. reject(error);
  59. } else {
  60. resolve(data);
  61. }
  62. }).skip(skip).limit(limit);
  63. });
  64. }
  65. /**
  66. * 关联查找数据
  67. *
  68. * @param {object} conditions
  69. * @param {object} fields
  70. * @param {String|Object} populate
  71. * @return {Promise}
  72. */
  73. findWithPopulate(conditions, fields = null, populate = '') {
  74. let self = this;
  75. return new Promise(function (resolve, reject) {
  76. self.model.find(conditions, fields).populate(populate).exec(function(error, data) {
  77. if (error) {
  78. reject(error);
  79. } else {
  80. resolve(data);
  81. }
  82. });
  83. });
  84. }
  85. /**
  86. * 查找且更新(原子操作)
  87. *
  88. * @param {object} update
  89. * @param {object} condition
  90. * @param {object} options
  91. * @return {Promise}
  92. */
  93. findAndModify(condition, update, options) {
  94. let self = this;
  95. return new Promise(function (resolve, reject) {
  96. self.model.findOneAndUpdate(condition, update, options, function(error, data) {
  97. if (error) {
  98. reject(error);
  99. } else {
  100. resolve(data);
  101. }
  102. });
  103. });
  104. }
  105. /**
  106. * 新增操作
  107. *
  108. * @param {object} data
  109. * @return {Promise}
  110. */
  111. create(data) {
  112. let self = this;
  113. return new Promise(function (resolve, reject) {
  114. self.model.create(data, function(error, data) {
  115. if (error) {
  116. reject(error);
  117. } else {
  118. resolve(data);
  119. }
  120. });
  121. });
  122. }
  123. /**
  124. * 统计数据数量
  125. *
  126. * @param {Object} condition
  127. * @return {Promise}
  128. */
  129. count(condition) {
  130. let self = this;
  131. return new Promise(function(resolve, reject) {
  132. self.model.count(condition, 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. delete(condition) {
  148. let self = this;
  149. return new Promise(function(resolve, reject) {
  150. self.model.remove(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. * @param {Object} updateData
  164. * @return {Promise}
  165. */
  166. update(condition, updateData) {
  167. let self = this;
  168. return new Promise(function(resolve, reject) {
  169. self.model.update(condition, {$set: updateData}, function(error, data) {
  170. if (error) {
  171. reject(error);
  172. } else {
  173. resolve(data);
  174. }
  175. });
  176. });
  177. }
  178. }
  179. export default MongooseHelper;