Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var mongo_utils = require('./utils');
- /**
- * @var db
- */
- exports.Provider = Provider = function(db, coll_name, callback, mixins) {
- this._db = db;
- this._coll_name = coll_name;
- this._coll = null;
- if (mixins){
- for(var p in mixins){
- this[p] = mixins[p];
- }
- }
- var prov = this;
- var on_coll = function(coll) {
- this._coll = coll;
- if (callback) {
- callback.call(prov);
- }
- }
- this._db.collection(this._coll_name, on_coll);
- }
- Provider.prototype.collection = function(callback) {
- var prov = this;
- if (this._coll) {
- callback.call(this._coll);
- } else {
- var on_coll = function(err, coll) {
- if (err) {
- throw(err);
- } else {
- prov._coll = coll;
- callback.call(coll);
- }
- }
- this._db.collection(this._coll_name, on_coll);
- }
- }
- Provider.prototype.find = function(query, cond, callback) {
- this.collection(function(){
- mongo_utils.find_cond(this, query, cond, callback);
- })
- }
- Provider.prototype._post_find = false;
- Provider.prototype.find_all = function(config, callback){
- var prov = this;
- var _on_find = function(e, cursor){
- callback.call(this, e, cursor);
- };
- this.collection(function(){
- mongo_utils.find_all(this, config, _on_find)
- });
- }
- /*
- * data must be an array of 1 or more
- */
- Provider.prototype._pre_insert = false;
- Provider.prototype._post_insert = false;
- Provider.prototype.insert = function(data, callback) {
- if (this._pre_insert){
- this._pre_insert(data);
- }
- var prov = this;
- var _on_callback = function(e, d){
- if (e){
- console.log('insert error: ');
- console.log(e);
- } else {
- if (prov._post_insert){
- prov._post_insert(d);
- }
- if (callback){
- callback.call(prov, e, d);
- }
- }
- }
- this.collection(function(){
- this.insert(data, _on_callback);
- });
- }
Add Comment
Please, Sign In to add comment