Skip to content

Instantly share code, notes, and snippets.

@tvl83
Created October 1, 2017 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tvl83/55378bae699769ebfa596ca5e562df08 to your computer and use it in GitHub Desktop.
Save tvl83/55378bae699769ebfa596ca5e562df08 to your computer and use it in GitHub Desktop.
transaction schema for blockchain explorer
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