@ocap/statedb-memory 1.28.9 → 1.29.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/table/base.js DELETED
@@ -1,135 +0,0 @@
1
- const { StateDBTable } = require('@ocap/statedb');
2
- const omit = require('lodash/omit');
3
-
4
- const debug = require('debug')(require('../../package.json').name);
5
-
6
- class MemoryTable extends StateDBTable {
7
- /**
8
- * @param {Object} param
9
- * @param {string} param.name db name
10
- * @param {string|string[]} param.uniqIndex primary keys
11
- * @param {boolean} param.syncBalance sync balance table when create / update / get
12
- * @param {BalanceTable} param.balanceTable balance table
13
- * @param {import('lokijs')} param.db LokiJS database instance
14
- */
15
- constructor({ name, uniqIndex, balanceTable, syncBalance = false, db }) {
16
- super(uniqIndex);
17
-
18
- this.name = name;
19
- this.uniqIndex = uniqIndex;
20
- this.balanceTable = balanceTable;
21
- this.syncBalance = syncBalance;
22
-
23
- if (this.syncBalance && !this.balanceTable) {
24
- throw new Error('balanceTable is required when syncBalance is true');
25
- }
26
-
27
- if (!db) {
28
- throw new Error('db is required for MemoryTable');
29
- }
30
-
31
- this.collection = db.addCollection(name, { unique: [this.primaryKey], clone: true });
32
- this.markReady();
33
- }
34
-
35
- async _create(key, attrs) {
36
- const id = this.generatePrimaryKey(key);
37
- // Don't call this._get here cause _get may be overwritten
38
- const doc = await MemoryTable.prototype._get.call(this, id);
39
- if (doc) {
40
- throw new Error(`${this.name} already exists: ${key}`);
41
- }
42
-
43
- debug(`insert ${this.name}`, attrs);
44
-
45
- const insertAttrs = { ...attrs };
46
- // insert without tokens cause we've migrate token to balance table
47
- if (this.syncBalance) {
48
- delete insertAttrs.tokens;
49
- }
50
-
51
- const result = await this.collection.insert({ [this.primaryKey]: id, ...insertAttrs });
52
-
53
- if (this.syncBalance && attrs.tokens) {
54
- debug(`update balance for ${this.name}`, attrs);
55
-
56
- result.tokens = await this.balanceTable.updateBalance({
57
- address: attrs.address,
58
- tokens: attrs.tokens,
59
- context: attrs.context,
60
- });
61
- }
62
-
63
- return result;
64
- }
65
-
66
- async _get(key) {
67
- if (!key) return null;
68
-
69
- const id = this.generatePrimaryKey(key);
70
- const result = await this.collection.by(this.primaryKey, id);
71
-
72
- if (result && this.syncBalance) {
73
- const balance = await this.balanceTable.getBalance(result.address);
74
- result.tokens = { ...(result.tokens || {}), ...balance };
75
- }
76
-
77
- return result;
78
- }
79
-
80
- _history() {
81
- return [];
82
- }
83
-
84
- async _update(key, updates) {
85
- const id = this.generatePrimaryKey(key);
86
- // Don't call this._get here cause _get may be overwritten
87
- const doc = await MemoryTable.prototype._get.call(this, id);
88
- if (!doc) {
89
- throw new Error(`${this.name} does not exists: ${key}`);
90
- }
91
-
92
- Object.assign(doc, updates);
93
-
94
- const updateAttrs = { ...doc };
95
- // insert without tokens cause we've migrate token to balance table
96
- if (this.syncBalance) {
97
- delete updateAttrs.tokens;
98
- }
99
-
100
- await this.collection.update(updateAttrs);
101
-
102
- if (this.syncBalance && doc.tokens) {
103
- debug(`update balance for ${this.name}`, doc);
104
-
105
- doc.tokens = await this.balanceTable.updateBalance({
106
- address: doc.address,
107
- tokens: doc.tokens,
108
- context: doc.context,
109
- });
110
- }
111
-
112
- return doc;
113
- }
114
-
115
- updateOrCreate(exist, state, ctx) {
116
- const id = this.generatePrimaryKey(state);
117
- const attrs = omit(state, this.uniqIndex);
118
-
119
- if (!id) {
120
- throw new Error('Cannot update or create without uniq index');
121
- }
122
-
123
- if (exist) {
124
- return this.update(id, attrs, ctx);
125
- }
126
-
127
- return this.create(id, attrs, ctx);
128
- }
129
-
130
- _reset() {
131
- this.collection.removeWhere({});
132
- }
133
- }
134
-
135
- module.exports = MemoryTable;
@@ -1,13 +0,0 @@
1
- const MemoryTable = require('./base');
2
-
3
- class RollupTable extends MemoryTable {
4
- async existByToken(token) {
5
- if (!token) {
6
- return false;
7
- }
8
-
9
- return this.collection.count({ tokenAddress: token, paused: false }) > 0;
10
- }
11
- }
12
-
13
- module.exports = RollupTable;
@@ -1,13 +0,0 @@
1
- const MemoryTable = require('./base');
2
-
3
- class TokenTable extends MemoryTable {
4
- async existBySymbol(symbol) {
5
- if (!symbol) {
6
- throw new Error('param symbol is required');
7
- }
8
-
9
- return this.collection.count({ symbol }) > 0;
10
- }
11
- }
12
-
13
- module.exports = TokenTable;