diff --git a/lib/storage.js b/lib/storage.js index f964126..8a96663 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -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) {