@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/README.md +1 -2
- package/esm/db.d.mts +31 -0
- package/esm/db.mjs +101 -0
- package/esm/index.d.mts +2 -0
- package/esm/index.mjs +3 -0
- package/esm/package.mjs +69 -0
- package/esm/table/account.d.mts +17 -0
- package/esm/table/account.mjs +27 -0
- package/esm/table/balance.d.mts +23 -0
- package/esm/table/balance.mjs +43 -0
- package/esm/table/base.d.mts +37 -0
- package/esm/table/base.mjs +94 -0
- package/esm/table/rollup.d.mts +14 -0
- package/esm/table/rollup.mjs +20 -0
- package/esm/table/token.d.mts +14 -0
- package/esm/table/token.mjs +17 -0
- package/lib/_virtual/rolldown_runtime.cjs +29 -0
- package/lib/db.cjs +104 -0
- package/lib/db.d.cts +31 -0
- package/lib/index.cjs +4 -0
- package/lib/index.d.cts +2 -0
- package/lib/package.cjs +81 -0
- package/lib/table/account.cjs +29 -0
- package/lib/table/account.d.cts +17 -0
- package/lib/table/balance.cjs +45 -0
- package/lib/table/balance.d.cts +23 -0
- package/lib/table/base.cjs +98 -0
- package/lib/table/base.d.cts +37 -0
- package/lib/table/rollup.cjs +21 -0
- package/lib/table/rollup.d.cts +14 -0
- package/lib/table/token.cjs +18 -0
- package/lib/table/token.d.cts +14 -0
- package/package.json +35 -9
- package/lib/db.js +0 -60
- package/lib/index.js +0 -1
- package/lib/table/account.js +0 -20
- package/lib/table/balance.js +0 -41
- package/lib/table/base.js +0 -135
- package/lib/table/rollup.js +0 -13
- package/lib/table/token.js +0 -13
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;
|
package/lib/table/rollup.js
DELETED
|
@@ -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;
|
package/lib/table/token.js
DELETED
|
@@ -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;
|