mirror of
https://github.com/zhigang1992/deployd.git
synced 2026-05-13 12:37:17 +08:00
fixed no open connection during db.dropDatabase()
This commit is contained in:
63
lib/db.js
63
lib/db.js
@@ -70,15 +70,11 @@ db.Db = Db;
|
||||
*/
|
||||
|
||||
Db.prototype.drop = function (fn) {
|
||||
var mdb = this._mdb;
|
||||
|
||||
if(!this.connected) {
|
||||
getConnection(this, function (err, mdb) {
|
||||
mdb.open(function () {
|
||||
mdb.dropDatabase(fn);
|
||||
})
|
||||
} else {
|
||||
mdb.dropDatabase(fn);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -113,11 +109,33 @@ function Store(namespace, db) {
|
||||
}
|
||||
module.exports.Store = Store;
|
||||
|
||||
function getConnection(db, fn) {
|
||||
if(db.connected) {
|
||||
fn(null, db._mdb);
|
||||
} else if(db.connecting) {
|
||||
db.once('connection attempted', function (err) {
|
||||
fn(err, db._mdb)
|
||||
});
|
||||
} else {
|
||||
db.connecting = true;
|
||||
db._mdb.open(function (err) {
|
||||
db.connecting = false;
|
||||
db.emit('connection attempted', err);
|
||||
if(err) {
|
||||
db.connected = false;
|
||||
throw err;
|
||||
} else {
|
||||
db.connected = true;
|
||||
fn(null, db._mdb);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function collection(store, fn) {
|
||||
var db = store._db
|
||||
, mdb = db._mdb;
|
||||
var db = store._db;
|
||||
|
||||
function execute(err) {
|
||||
getConnection(db, function (err, mdb) {
|
||||
if(err) return fn(err);
|
||||
|
||||
mdb.collection(store.namespace, function (err, collection) {
|
||||
@@ -127,31 +145,8 @@ function collection(store, fn) {
|
||||
}
|
||||
|
||||
fn(null, collection);
|
||||
});
|
||||
}
|
||||
|
||||
function connect(fn) {
|
||||
db.connecting = true;
|
||||
mdb.open(function (err) {
|
||||
db.connecting = false;
|
||||
db.emit('connection attempted', err);
|
||||
if(err) {
|
||||
db.connected = false;
|
||||
throw err;
|
||||
} else {
|
||||
db.connected = true;
|
||||
fn();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if(db.connected) {
|
||||
execute();
|
||||
} else if(db.connecting) {
|
||||
db.once('connection attempted', execute);
|
||||
} else {
|
||||
connect(execute);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user