UNPKG

@ocap/statedb-memory

Version:

OCAP statedb adapter that uses memory as backend statedb, just for test purpose

116 lines (114 loc) 2.77 kB
import package_default from "./package.mjs"; import base_default from "./table/base.mjs"; import account_default from "./table/account.mjs"; import balance_default from "./table/balance.mjs"; import rollup_default from "./table/rollup.mjs"; import token_default from "./table/token.mjs"; import { StateDB } from "@ocap/statedb"; import Lokijs from "lokijs"; //#region src/db.ts const { name, version } = package_default; let instanceCounter = 0; /** * 内存 StateDB 实现 * 使用 LokiJS 作为内存存储引擎 */ var MemoryStateDB = class extends StateDB { constructor() { super(); this.name = name; this.version = version; instanceCounter += 1; this.db = new Lokijs(`ocap-memory-statedb-${instanceCounter}.db`); this.balance = new balance_default({ name: "balance", uniqIndex: ["address", "tokenAddress"], db: this.db }); this.account = new account_default({ name: "account", uniqIndex: "address", balanceTable: this.balance, syncBalance: true, db: this.db }); this.factory = new base_default({ name: "factory", uniqIndex: "address", syncBalance: true, balanceTable: this.balance, db: this.db }); this.stake = new base_default({ name: "stake", uniqIndex: "address", syncBalance: true, balanceTable: this.balance, db: this.db }); this.asset = new base_default({ name: "asset", uniqIndex: "address", db: this.db }); this.delegation = new base_default({ name: "delegation", uniqIndex: "address", db: this.db }); this.tx = new base_default({ name: "tx", uniqIndex: "hash", db: this.db }); this.token = new token_default({ name: "token", uniqIndex: "address", db: this.db }); this.chain = new base_default({ name: "chain", uniqIndex: "address", db: this.db }); this.rollup = new rollup_default({ name: "rollup", uniqIndex: "address", db: this.db }); this.rollupBlock = new base_default({ name: "rollupBlock", uniqIndex: "hash", db: this.db }); this.evidence = new base_default({ name: "evidence", uniqIndex: "hash", db: this.db }); this.tokenFactory = new base_default({ name: "tokenFactory", uniqIndex: "address", db: this.db }); this.attachReadyListeners(); } /** * Execute function - Memory adapter has no real transaction support * This is a no-op wrapper that simply executes the callback * * @param fn - Function to execute (txn parameter will be null) * @returns Result of the function */ async runAsLambda(fn, options) { const result = await fn(null); if (options?.onCommit) try { await options.onCommit(); } catch (_err) {} return result; } async close() {} }; var db_default = MemoryStateDB; //#endregion export { db_default as default };