Cipher-Zero/backend/mongo-db/stake-history-dao.js
2024-07-19 22:15:24 +03:00

43 lines
1.5 KiB
JavaScript

//------------------------------------------------------------------------------
// DAO for daily stake
// Require index: `db.stakeHistory.createIndex({type:1, timestamp:-1})`
// Require index: `db.stakeHistory.createIndex({type:1, height:-1})`
//------------------------------------------------------------------------------
module.exports = class StakeHistoryDAO {
constructor(execDir, client) {
this.client = client;
this.collection = 'stakeHistory';
}
insert(info, callback) {
this.client.insert(this.collection, info, callback);
}
getRecordByTypeAndHeight(type, height, callback) {
const queryObject = { type: type, height: height }
this.client.getRecords(this.collection, queryObject, {}, 0, 0, callback);
}
getRecordByTypeAndTimestamp(type, timestamp, callback) {
const queryObject = { type: type, timestamp: timestamp }
this.client.getRecords(this.collection, queryObject, {}, 0, 0, callback);
}
getLatestTimestamp(timestamp, callback) {
console.log('timestamp:', timestamp, typeof timestamp)
const queryObject = { timestamp: { $lte: timestamp } }
const sortObject = { 'timestamp': -1 };
this.client.getRecords(this.collection, queryObject, sortObject, 0, 1, function (err, list) {
if (list.length === 0) {
console.log('return in length 0')
callback(Error(`NOT_FOUND - Stake records before timestamp ${timestamp}`));
} else if (err) {
callback(err)
} else {
callback(err, list[0].timestamp)
}
})
}
}