mongoose_helper.js 4.3 KB

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