From efe19444d5807ff75b566aed7a16e892149e31d1 Mon Sep 17 00:00:00 2001 From: tenthirtyone Date: Mon, 21 Aug 2017 21:08:41 -0400 Subject: [PATCH] getTxById added to model --- server/lib/db/transactions.js | 8 +++----- server/models/transaction.js | 17 +++++++++++++++-- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/server/lib/db/transactions.js b/server/lib/db/transactions.js index fe12e0b..af02bb5 100644 --- a/server/lib/db/transactions.js +++ b/server/lib/db/transactions.js @@ -2,6 +2,8 @@ const Transactions = require('../../models/transaction.js'); const logger = require('../logger'); const config = require('../../config'); +const txs = new Transactions(); + // For now, blocks handles these calls. // These will be replaced with more advanced mongo // No optimization yet. @@ -84,11 +86,7 @@ function getTopTransactions(cb) { } function getTxById(txid, cb) { - getTransaction( - { hash: txid }, - { }, - 1, - 0, + txs.byId(txid, (err, transaction) => { if (err) { logger.log('error', diff --git a/server/models/transaction.js b/server/models/transaction.js index e4e3416..1d5c842 100644 --- a/server/models/transaction.js +++ b/server/models/transaction.js @@ -1,6 +1,7 @@ const mongoose = require('mongoose'); const Input = require('./input'); const Output = require('./output'); +const logger = require('../lib/logger'); const Schema = mongoose.Schema; @@ -24,6 +25,18 @@ const TransactionSchema = new Schema({ TransactionSchema.index({ hash: 1 }); -const Transaction = mongoose.model('Transaction', TransactionSchema); +TransactionSchema.methods.byId = function txById(txid, cb) { + return this.model('Transaction').findOne( + { hash: txid }, + (err, tx) => { + if (err) { + logger.log('error', + `TransactionSchema.methods.byId: ${err}`); + return cb(err); + } + return cb(null, tx); + }); +}; -module.exports = Transaction; + +module.exports = mongoose.model('Transaction', TransactionSchema);