@ocap/indexdb-elasticsearch
Version:
OCAP indexdb adapter that uses elasticsearch as backend
50 lines (39 loc) • 1.27 kB
JavaScript
const ESIndex = require('./base');
const getParams = require('../model/token-factory');
const { getTableName } = require('../util');
class TokenFactory extends ESIndex {
constructor(db) {
super({
name: getTableName(db.config.prefix, 'token_factory'),
docId: 'address',
client: db.client,
http: db.http,
indexParams: getParams(),
});
this.tokenLength = db.config.tokenLength;
}
static formatBeforeUpdate(data, tokenLength) {
data.currentSupply = super.padBalance(data.currentSupply, tokenLength);
data.reserveBalance = super.padBalance(data.reserveBalance, tokenLength);
return data;
}
static formatAfterRead(data) {
if (data) {
data.currentSupply = super.trimBalance(data.currentSupply);
data.reserveBalance = super.trimBalance(data.reserveBalance);
}
return data;
}
batchInsert(rows) {
const formatted = rows.map((x) => TokenFactory.formatBeforeUpdate(x, this.tokenLength));
return super.batchInsert(formatted);
}
_insert(row) {
return super._insert(TokenFactory.formatBeforeUpdate(row, this.tokenLength));
}
async _get(key) {
const item = await super._get(key);
return TokenFactory.formatAfterRead(item);
}
}
module.exports = TokenFactory;