diff --git a/Sync.js b/Sync.js index 7269c8f..287f8f8 100644 --- a/Sync.js +++ b/Sync.js @@ -94,14 +94,16 @@ function spec(b) { function(cb){ if (opts.destroy) { console.log("Deleting Blocks..."); - Block.remove().exec(cb); + return Block.remove().exec(cb); } + return cb(); }, function(cb){ if (opts.destroy) { console.log("Deleting TXs..."); - Transaction.remove().exec(cb); + return Transaction.remove().exec(cb); } + return cb(); }, function(cb) { that.syncBlocks(opts.reindex, function(err) { diff --git a/app/models/Block.js b/app/models/Block.js index 511cd7c..3ce94ef 100644 --- a/app/models/Block.js +++ b/app/models/Block.js @@ -44,10 +44,10 @@ BlockSchema.methods.explodeTransactions = function(next) { // console.log('exploding %s', this.hash, typeof this.tx); - async.forEach( this.tx, + async.forEach( this.tx, function(tx, callback) { // console.log('procesing TX %s', tx); - Transaction.create({ hash: tx }, function(err) { + Transaction.create({ txid: tx }, function(err) { if (err && ! err.toString().match(/E11000/)) { return callback(); } diff --git a/app/models/Transaction.js b/app/models/Transaction.js index ba8678e..fcd1240 100644 --- a/app/models/Transaction.js +++ b/app/models/Transaction.js @@ -10,15 +10,25 @@ var mongoose = require('mongoose'), /** */ var TransactionSchema = new Schema({ - hash: { + txid: { type: String, index: true, unique: true, }, - parsed: { - type: Boolean, - default: false, + version: Number, + locktime: Number, + vin: { + type: Array, + default: [], }, + vout: { + type: Array, + default: [], + }, + blockhash: String, + confirmations: Number, + time: Number, + blocktime: Number, }); /** @@ -32,10 +42,19 @@ TransactionSchema.statics.load = function(id, cb) { }; -TransactionSchema.statics.fromHash = function(hash, cb) { +TransactionSchema.statics.fromID = function(txid, cb) { this.findOne({ - hash: hash, + txid: txid, }).exec(cb); }; +/* + * virtual + */ + +// ugly? new object every call? +TransactionSchema.virtual('date').get(function () { + return new Date(this.time); +}); + module.exports = mongoose.model('Transaction', TransactionSchema);