Skip to content

Commit

Permalink
make it work with db connection object
Browse files Browse the repository at this point in the history
  • Loading branch information
zemirco committed Apr 17, 2014
1 parent f753b68 commit 7764520
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ var moment = require('moment');

module.exports = function(config) {

// short form for collection name
var coll = config.db.collection;

// create connection string
var url = config.db.url + config.db.name;

// create connection as soon as module is required and share global db object
var db;
MongoClient.connect(config.db, function(err, database) {
MongoClient.connect(url, function(err, database) {
if (err) throw err;
db = database;
});
Expand Down Expand Up @@ -37,7 +43,7 @@ module.exports = function(config) {
if (err) return done(err);
user.salt = salt;
user.derived_key = hash;
db.collection(config.dbCollection).save(user, done);
db.collection(coll).save(user, done);
});

};
Expand All @@ -48,19 +54,19 @@ module.exports = function(config) {
var qry = {};
qry[match] = query;

db.collection(config.dbCollection).find(qry).nextObject(done);
db.collection(coll).find(qry).nextObject(done);

};

// update an existing user and return updated user object
adapter.update = function(user, done) {

// update user in db
db.collection(config.dbCollection).save(user, function(err, res) {
db.collection(coll).save(user, function(err, res) {
if (err) console.log(err);

// res is not the updated user object! -> find manually
db.collection(config.dbCollection).find({_id: user._id}).nextObject(done);
db.collection(coll).find({_id: user._id}).nextObject(done);

});

Expand All @@ -69,7 +75,7 @@ module.exports = function(config) {
// remove an existing user from db
adapter.remove = function(name, done) {

db.collection(config.dbCollection).remove({name: name}, function(err, numberOfRemovedDocs) {
db.collection(coll).remove({name: name}, function(err, numberOfRemovedDocs) {
if (err) return done(err);
if (numberOfRemovedDocs === 0) return done(new Error('lockit - Cannot find user "' + name + '"'));
done(null, true);
Expand Down

0 comments on commit 7764520

Please sign in to comment.