Merge pull request #206 from isocolsky/index_db

Index db
This commit is contained in:
Matias Alejo Garcia 2015-05-07 19:02:27 -03:00
commit a83d788a7f
1 changed files with 30 additions and 8 deletions

View File

@ -25,12 +25,39 @@ var Storage = function(opts) {
this.db = opts.db;
};
Storage.prototype._createIndexes = function() {
this.db.collection(collections.WALLETS).createIndex({
id: 1
});
this.db.collection(collections.COPAYERS_LOOKUP).createIndex({
copayerId: 1
});
this.db.collection(collections.TXS).createIndex({
walletId: 1,
id: 1,
});
this.db.collection(collections.TXS).createIndex({
walletId: 1,
isPending: 1,
});
this.db.collection(collections.NOTIFICATIONS).createIndex({
walletId: 1,
id: 1,
});
this.db.collection(collections.ADDRESSES).createIndex({
walletId: 1
});
this.db.collection(collections.ADDRESSES).createIndex({
address: 1,
});
};
Storage.prototype.connect = function(opts, cb) {
var self = this;
opts = opts || {};
if (this.db) return cb(null);
if (this.db) return cb();
var config = opts.mongoDb || {};
mongodb.MongoClient.connect(config.uri, function(err, db) {
@ -39,8 +66,9 @@ Storage.prototype.connect = function(opts, cb) {
return cb(err);
}
self.db = db;
self._createIndexes();
console.log('Connection established to ', config.uri);
return cb(null);
return cb();
});
};
@ -100,9 +128,6 @@ Storage.prototype.storeWalletAndUpdateCopayersLookup = function(wallet, cb) {
};
Storage.prototype.fetchCopayerLookup = function(copayerId, cb) {
this.db.collection(collections.COPAYERS_LOOKUP).createIndex({
copayerId: 1
});
this.db.collection(collections.COPAYERS_LOOKUP).findOne({
copayerId: copayerId
}, function(err, result) {
@ -318,9 +343,6 @@ Storage.prototype.storeAddressAndWallet = function(wallet, addresses, cb) {
Storage.prototype.fetchAddress = function(address, cb) {
var self = this;
this.db.collection(collections.ADDRESSES).createIndex({
address: 1
});
this.db.collection(collections.ADDRESSES).findOne({
address: address,
}, function(err, result) {