Skip to content

Commit

Permalink
feature(internal-client): allow access to underlying resource from dp…
Browse files Browse the repository at this point in the history
…d internal client

Allows script syntax such as:

```javascript
dpd.colname.resource.store.collection(function(err, col) {
  // use col directly here, see mongodb documentation
});
```
  • Loading branch information
andreialecu authored and ericfong committed Jul 18, 2015
1 parent 89cd1f0 commit d478b34
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
29 changes: 21 additions & 8 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,20 +155,33 @@ function getConnection(db) {
function collection(store, fn) {
var db = store._db;

getConnection(db).then(function (mdb) {
mdb.collection(store.namespace, function (err, collection) {
if(err || !collection) {
error(err || new Error('Unable to get ' + store.namespace + ' collection'));
process.exit(1);
}
return getConnection(db).then(function (mdb) {
return new Promise(function(resolve, reject) {
mdb.collection(store.namespace, function (err, collection) {
if(err || !collection) {
error(err || new Error('Unable to get ' + store.namespace + ' collection'));
process.exit(1);
}

fn(null, collection);
if (fn) fn(null, collection);
resolve(collection);
});
});
}).catch(function (err) {
return fn(err);
if (fn) fn(err);
throw err;
});
}

/**
* Returns a promise, or calls fn with the mongo collection served by this store
* @param {Function} fn a callback that will receive the mongo collection as the second parameter
* @return {Promise} returns a promise with the mongo collection
*/
Store.prototype.getCollection = function(fn){
return collection(this, fn);
};

/**
* Change public IDs to private IDs.
*
Expand Down
2 changes: 2 additions & 0 deletions lib/internal-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ function createResourceClient(resource, baseMethods) {
};
});

r.getResource = function() { return resource; };

return r;
}

Expand Down

0 comments on commit d478b34

Please sign in to comment.