Created
October 1, 2017 03:43
-
-
Save tvl83/55378bae699769ebfa596ca5e562df08 to your computer and use it in GitHub Desktop.
transaction schema for blockchain explorer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let mongoose = require('mongoose'); | |
const arrayUniquePlugin = require('mongoose-unique-array'); | |
const voutsArraySchema = mongoose.Schema({ | |
txid: String, | |
n: Number, | |
value: Number, | |
time: Number, | |
address: String | |
}); | |
const vinsArraySchema = mongoose.Schema({ | |
txid: String, | |
voutIndex: Number, | |
value: Number, | |
address: String | |
}); | |
const transactionSchema = mongoose.Schema({ | |
raw: { | |
txid: {type: String, unique: true}, | |
version: Number, | |
time: Number, | |
locktime: Number, | |
vin: Array, | |
vout: Array, | |
blockhash: String, | |
confirmations: Number, | |
blocktime: Number | |
}, | |
txid: {type: String, unique: true, index: true}, | |
totalValueIn: Number, | |
totalValueOut: Number, | |
blockheight: Number, | |
blockhash: String, | |
vins: [{type: vinsArraySchema, unique: true}], | |
vouts: [{type: voutsArraySchema, unique: true}] | |
}); | |
transactionSchema.statics.latestBlock = function (cb) { | |
this.findOne() | |
.sort('-blockheight') | |
.exec(cb); | |
}; | |
transactionSchema.index({"txid": 1, "vouts": [{"n": 1, "value": 1}]}, {unique: true}); | |
transactionSchema.plugin(arrayUniquePlugin); | |
let Transactions = mongoose.model('Transactions', transactionSchema); | |
module.exports = Transactions; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment