@ocap/statedb-memory 1.13.61 → 1.13.65

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.
@@ -3,7 +3,7 @@ const MemoryStateDB = require('./base');
3
3
 
4
4
  class AccountStateDB extends MemoryStateDB {
5
5
  _get(address, { final = true } = {}) {
6
- const current = this.collection.by(this.key, address) || null;
6
+ const current = this.collection.by(this.uniqIndex, address) || null;
7
7
  if (current && final && Array.isArray(current.migratedTo) && current.migratedTo.length) {
8
8
  return this._get(current.migratedTo[0]);
9
9
  }
package/lib/table/base.js CHANGED
@@ -7,13 +7,13 @@ const db = new Lokijs('ocap-memory-statedb.db');
7
7
  const debug = require('debug')(require('../../package.json').name);
8
8
 
9
9
  class MemoryTable extends StateDBTable {
10
- constructor(name, key) {
10
+ constructor(name, uniqIndex) {
11
11
  super();
12
12
 
13
13
  this.name = name;
14
- this.key = key;
14
+ this.uniqIndex = uniqIndex;
15
15
 
16
- this.collection = db.addCollection(name, { unique: [key], clone: true });
16
+ this.collection = db.addCollection(name, { unique: [uniqIndex], clone: true });
17
17
  this.markReady();
18
18
  }
19
19
 
@@ -22,13 +22,13 @@ class MemoryTable extends StateDBTable {
22
22
  throw new Error(`${this.name} already exists: ${id}`);
23
23
  }
24
24
 
25
- this.collection.insert({ [this.key]: id, ...attrs });
25
+ this.collection.insert({ [this.uniqIndex]: id, ...attrs });
26
26
  debug(`${this.name} create`, { id, ...attrs });
27
27
  return this._get(id);
28
28
  }
29
29
 
30
30
  _get(id) {
31
- return id ? this.collection.by(this.key, id) : null;
31
+ return id ? this.collection.by(this.uniqIndex, id) : null;
32
32
  }
33
33
 
34
34
  _history() {
@@ -40,12 +40,24 @@ class MemoryTable extends StateDBTable {
40
40
  throw new Error(`${this.name} does not exists: ${id}`);
41
41
  }
42
42
 
43
- const doc = this.collection.by(this.key, id);
43
+ const doc = this.collection.by(this.uniqIndex, id);
44
44
  Object.assign(doc, updates);
45
45
  this.collection.update(doc);
46
46
  return doc;
47
47
  }
48
48
 
49
+ updateOrCreate(exist, state, ctx) {
50
+ if (!state[this.uniqIndex]) {
51
+ throw new Error('Cannot update or create without uniq index');
52
+ }
53
+
54
+ if (exist) {
55
+ return this.update(state[this.uniqIndex], state, ctx);
56
+ }
57
+
58
+ return this.create(state[this.uniqIndex], state, ctx);
59
+ }
60
+
49
61
  _reset() {
50
62
  this.collection.removeWhere({});
51
63
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ocap/statedb-memory",
3
3
  "description": "OCAP statedb adapter that uses memory as backend statedb, just for test purpose",
4
- "version": "1.13.61",
4
+ "version": "1.13.65",
5
5
  "author": "wangshijun <shijun@arcblock.io> (https://www.arcblock.io)",
6
6
  "bugs": {
7
7
  "url": "https://github.com/ArcBlock/asset-chain/issues",
@@ -37,9 +37,9 @@
37
37
  "test": "jest --forceExit --detectOpenHandles",
38
38
  "coverage": "npm run test -- --coverage"
39
39
  },
40
- "gitHead": "79d07bbde4df1d0d37afe76aaa1eaa2f30cc55a7",
40
+ "gitHead": "4011996e1800845142aa5c889b58726129a99ec3",
41
41
  "dependencies": {
42
- "@ocap/statedb": "1.13.61",
42
+ "@ocap/statedb": "1.13.65",
43
43
  "debug": "^4.3.2",
44
44
  "lodash": "^4.17.21",
45
45
  "lokijs": "^1.5.11"