insight-ui-zcash/app/models/TransactionItem.js

55 lines
1.1 KiB
JavaScript
Raw Normal View History

2014-01-10 16:42:39 -08:00
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var TransactionItemSchema = new Schema({
txid: String,
index: Number,
addr: {
type: String,
index: true,
},
// OJO: mongoose doesnt accept camelcase for field names
2014-01-11 20:16:22 -08:00
// <0 is Input >0 is Output
value_sat: Number,
ts: Number,
2014-01-10 16:42:39 -08:00
});
2014-01-11 17:57:33 -08:00
// Compound index
TransactionItemSchema.index({txid: 1, index: 1, value_sat: 1}, {unique: true, dropDups: true});
2014-01-11 17:57:33 -08:00
2014-01-10 16:42:39 -08:00
TransactionItemSchema.statics.load = function(id, cb) {
this.findOne({
_id: id
}).exec(cb);
};
TransactionItemSchema.statics.fromTxId = function(txid, cb) {
2014-01-10 16:42:39 -08:00
this.find({
txid: txid,
}).exec(function (err,items) {
// sort by 1) value sign 2) index
return cb(err,items.sort(function(a,b){
var sa= a.value_sat < 0 ? -1 : 1;
var sb= b.value_sat < 0 ? -1 : 1;
2014-01-13 12:21:42 -08:00
if (sa !== sb) {
return sa-sb;
}
else {
return a.index - b.index;
}
}));
});
2014-01-10 16:42:39 -08:00
};
module.exports = mongoose.model('TransactionItem', TransactionItemSchema);