text: var mongoose = require( 'mongoose' );
  var Schema   = mongoose.Schema;
  
  var Block = new Schema(
  {
      "number": {type: Number, index: {unique: true}},
      "hash": String,
      "parentHash": String,
      "nonce": String,
      "sha3Uncles": String,
      "logsBloom": String,
      "transactionsRoot": String,
      "stateRoot": String,
      "receiptRoot": String,
      "miner": String,
      "difficulty": String,
      "totalDifficulty": String,
      "size": Number,
      "extraData": String,
      "gasLimit": Number,
      "gasUsed": Number,
      "timestamp": Number,
      "blockTime": Number,
      "uncles": [String]
  });
  
  var Contract = new Schema(
  {
      "address": {type: String, index: {unique: true}},
      "creationTransaction": String,
      "contractName": String,
      "compilerVersion": String,
      "optimization": Boolean,
      "sourceCode": String,
      "abi": String,
      "byteCode": String
  }, {collection: "Contract"});
  
  var Transaction = new Schema(
  {
      "hash": {type: String, index: {unique: true}},
      "nonce": Number,
      "blockHash": String,
      "blockNumber": Number,
      "transactionIndex": Number,
      "from": String,
      "to": String,
      "value": String,
      "gas": Number,
      "gasPrice": String,
      "timestamp": Number,
      "input": String
  }, {collection: "Transaction"});
  
  var BlockStat = new Schema(
  {
      "number": {type: Number, index: {unique: true}},
      "timestamp": Number,
      "difficulty": String,
      "hashrate": String,
      "txCount": Number,
      "gasUsed": Number,
      "gasLimit": Number,
      "miner": String,
      "blockTime": Number,
      "uncleCount": Number
  });
  
  var TransferToken = new Schema(
  {
      "txHash": {type: String, index: {unique: true}},
      "blockNumber": {type: Number, index: {unique: false}},
      "address": String,
      "amount": String,
      "from": String,
      "to": String,
      "gas": String,
      "timestamp": Number
  });
  
  // create indices
  Transaction.index({blockNumber:-1});
  Transaction.index({from:1, blockNumber:-1});
  Transaction.index({to:1, blockNumber:-1});
  Block.index({miner:1});
  
  mongoose.model('BlockStat', BlockStat);
  mongoose.model('Block', Block);
  mongoose.model('Contract', Contract);
  mongoose.model('Transaction', Transaction);
  mongoose.model('TransferToken', TransferToken);
  module.exports.BlockStat = mongoose.model('BlockStat');
  module.exports.Block = mongoose.model('Block');
  module.exports.Contract = mongoose.model('Contract');
  module.exports.Transaction = mongoose.model('Transaction');
  module.exports.TransferToken = mongoose.model('TransferToken');
  
  console.log("mongodb initialize...");
  mongoose.Promise = global.Promise;
  mongoose.connect(process.env.MONGO_URI || 'mongodb://localhost/blockDB',  {
      useMongoClient: true,
      poolSize: 20, // Maintain up to 20 socket connections
      // reconnectTries: 30000
  });
  
  // mongoose.set('debug', true);
  