Guest User

Untitled

a guest
Jul 18th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1.  
  2. var mongo_utils = require('./utils');
  3.  
  4. /**
  5. * @var db
  6. */
  7. exports.Provider = Provider = function(db, coll_name, callback, mixins) {
  8. this._db = db;
  9. this._coll_name = coll_name;
  10. this._coll = null;
  11.  
  12. if (mixins){
  13. for(var p in mixins){
  14. this[p] = mixins[p];
  15. }
  16. }
  17.  
  18. var prov = this;
  19.  
  20. var on_coll = function(coll) {
  21. this._coll = coll;
  22. if (callback) {
  23. callback.call(prov);
  24. }
  25.  
  26. }
  27. this._db.collection(this._coll_name, on_coll);
  28. }
  29.  
  30. Provider.prototype.collection = function(callback) {
  31. var prov = this;
  32.  
  33. if (this._coll) {
  34. callback.call(this._coll);
  35. } else {
  36. var on_coll = function(err, coll) {
  37. if (err) {
  38. throw(err);
  39. } else {
  40. prov._coll = coll;
  41. callback.call(coll);
  42. }
  43. }
  44. this._db.collection(this._coll_name, on_coll);
  45. }
  46. }
  47.  
  48. Provider.prototype.find = function(query, cond, callback) {
  49.  
  50. this.collection(function(){
  51. mongo_utils.find_cond(this, query, cond, callback);
  52. })
  53. }
  54.  
  55.  
  56. Provider.prototype._post_find = false;
  57. Provider.prototype.find_all = function(config, callback){
  58. var prov = this;
  59.  
  60. var _on_find = function(e, cursor){
  61. callback.call(this, e, cursor);
  62. };
  63.  
  64. this.collection(function(){
  65. mongo_utils.find_all(this, config, _on_find)
  66. });
  67.  
  68. }
  69.  
  70. /*
  71. * data must be an array of 1 or more
  72. */
  73. Provider.prototype._pre_insert = false;
  74. Provider.prototype._post_insert = false;
  75.  
  76. Provider.prototype.insert = function(data, callback) {
  77. if (this._pre_insert){
  78. this._pre_insert(data);
  79. }
  80.  
  81. var prov = this;
  82. var _on_callback = function(e, d){
  83. if (e){
  84. console.log('insert error: ');
  85. console.log(e);
  86. } else {
  87. if (prov._post_insert){
  88. prov._post_insert(d);
  89. }
  90. if (callback){
  91. callback.call(prov, e, d);
  92. }
  93. }
  94. }
  95. this.collection(function(){
  96. this.insert(data, _on_callback);
  97. });
  98. }
Add Comment
Please, Sign In to add comment