From 7a0ec9f11190041d26438f5309865c05356233c6 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Tue, 21 Apr 2015 16:24:01 -0300 Subject: [PATCH] first attempt at integrating tingodb --- lib/storage.js | 29 +++++++++++++++++++++++------ test/integration/server.js | 15 +++++++++------ 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/lib/storage.js b/lib/storage.js index 350fb9a..c9e6ac4 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -77,14 +77,9 @@ Storage.prototype.storeWalletAndUpdateCopayersLookup = function(wallet, cb) { return this.storeWallet(wallet, cb); }; -Storage.prototype.fetchCopayerLookup = function(copayerId, cb) { +Storage.prototype.fetchCopayerLookup2 = function(copayerId, cb) { this.db.collection(collections.WALLETS).findOne({ 'copayers.id': copayerId - }, { - fields: { - id: 1, - copayers: 1, - }, }, function(err, result) { if (err) return cb(err); if (!result) return cb(); @@ -98,6 +93,28 @@ Storage.prototype.fetchCopayerLookup = function(copayerId, cb) { }); }; +Storage.prototype.fetchCopayerLookup = function(copayerId, cb) { + this.db.collection(collections.WALLETS).find({}).toArray(function(err, result) { + if (err) return cb(err); + + result = _.find(result, function(w) { + return _.any(w.copayers, { + id: copayerId + }); + }); + + + if (!result) return cb(); + var copayer = _.find(result.copayers, { + id: copayerId + }); + return cb(null, { + walletId: result.id, + requestPubKey: copayer.requestPubKey, + }); + }); +}; + // TODO: should be done client-side Storage.prototype._completeTxData = function(walletId, txs, cb) { var txList = [].concat(txs); diff --git a/test/integration/server.js b/test/integration/server.js index 086970a..87cae74 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -10,7 +10,8 @@ var should = chai.should(); var log = require('npmlog'); log.debug = log.verbose; -var mongodb = require('mongodb'); +var fs = require('fs'); +var tingodb = require('tingodb')(); var Utils = require('../../lib/utils'); var WalletUtils = require('bitcore-wallet-utils'); @@ -211,10 +212,13 @@ helpers.createAddresses = function(server, wallet, main, change, cb) { var db, storage, blockchainExplorer; function openDb(cb) { - var url = 'mongodb://localhost:27017/bws'; - mongodb.MongoClient.connect(url, function(err, _db) { - should.not.exist(err); - db = _db; + var tingodb = require('tingodb')(); + var dbDir = './db/test/'; + fs.mkdir(dbDir, function(err) { + if (err && err.code != 'EEXIST') { + throw new Error('Could not create test db directory at ./db/test/'); + } + db = new tingodb.Db(dbDir, {}); return cb(); }); }; @@ -222,7 +226,6 @@ function openDb(cb) { function resetDb(cb) { if (!db) return cb(); db.dropDatabase(function(err) { - should.not.exist(err); return cb(); }); };