UNPKG

@ocap/statedb-qldb

Version:

OCAP statedb adapter that uses amazon qldb as backend statedb

29 lines (24 loc) 1.09 kB
const { ensureChecksumAddress } = require('@ocap/state/lib/states/account'); const debug = require('debug')(require('../../package.json').name); const QLDBTable = require('./base'); class AccountTable extends QLDBTable { async _get(key, { traceMigration = true, txn } = {}) { const results = await txn.execute( `SELECT * FROM ${this.name} WHERE ${this.uniqIndex} = ?`, ensureChecksumAddress(key) ); const current = results.getResultList().shift(); if (current && traceMigration && Array.isArray(current.migratedTo) && current.migratedTo.length) { return this._get(current.migratedTo[0], { traceMigration: true, txn }); } return current ? JSON.parse(JSON.stringify(current)) : null; } async _create(key, attrs, { txn }) { const data = { [this.uniqIndex]: key, ...attrs }; data[this.uniqIndex] = ensureChecksumAddress(data[this.uniqIndex]); const result = await txn.execute(`INSERT INTO ${this.name} ?`, data); debug(`insert ${this.name}`, key, result.getResultList()); return data; } } module.exports = AccountTable;