@ocap/statedb 1.6.5 → 1.6.10

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.
Files changed (3) hide show
  1. package/lib/db.js +19 -6
  2. package/lib/table.js +30 -1
  3. package/package.json +8 -6
package/lib/db.js CHANGED
@@ -1,15 +1,28 @@
1
1
  const Ready = require('@ocap/util/lib/ready');
2
+ const { tables } = require('@ocap/state');
2
3
 
3
4
  class StateDB extends Ready {
4
5
  constructor() {
5
6
  super();
6
7
 
7
- this.readyMarks = {
8
- account: false,
9
- asset: false,
10
- delegation: false,
11
- tx: false,
12
- };
8
+ this.readyMarks = tables.reduce((acc, x) => ({ ...acc, [x]: false }), {});
9
+ this.readyListenersAttached = false;
10
+ }
11
+
12
+ attachReadyListeners() {
13
+ if (this.readyListenersAttached) {
14
+ return;
15
+ }
16
+
17
+ for (const table of tables) {
18
+ if (typeof this[table] === 'undefined') {
19
+ throw new Error(`Missing table ${table} in statedb adapter: ${this.name}`);
20
+ }
21
+
22
+ this[table].onReady(() => this.markReady(table));
23
+ }
24
+
25
+ this.readyListenersAttached = true;
13
26
  }
14
27
  }
15
28
 
package/lib/table.js CHANGED
@@ -1,5 +1,7 @@
1
1
  /* eslint-disable no-underscore-dangle */
2
2
  const Kareem = require('kareem');
3
+ const omit = require('lodash/omit');
4
+ const pick = require('lodash/pick');
3
5
  const Ready = require('@ocap/util/lib/ready');
4
6
 
5
7
  class StateDBTable extends Ready {
@@ -11,12 +13,29 @@ class StateDBTable extends Ready {
11
13
  Object.defineProperty(this, 'pre', { value: (name, fn) => hooks.pre(name, fn) });
12
14
  Object.defineProperty(this, 'post', { value: (name, fn) => hooks.post(name, fn) });
13
15
 
14
- ['create', 'get', 'update', 'reset'].forEach((x) => {
16
+ ['create', 'get', 'history', 'update', 'reset'].forEach((x) => {
15
17
  Object.defineProperty(this, x, {
16
18
  value: async (...args) => {
17
19
  hooks.execPreSync(x, args);
18
20
  const result = await this[`_${x}`](...args);
19
21
  hooks.execPostSync(x, result);
22
+
23
+ if (['create', 'update', 'reset'].includes(x)) {
24
+ const ctx = args.slice(-1).pop();
25
+ if (ctx && ctx.tx) {
26
+ ctx.events = ctx.events || [];
27
+ ctx.events.push({
28
+ table: this.name,
29
+ name: x,
30
+ data: omit(result, '$loki'),
31
+ ctx: pick(
32
+ ctx,
33
+ Object.keys(ctx).filter((k) => ['events', 'states', 'statedb'].includes(k) === false)
34
+ ),
35
+ });
36
+ }
37
+ }
38
+
20
39
  return result;
21
40
  },
22
41
  });
@@ -42,6 +61,16 @@ class StateDBTable extends Ready {
42
61
  _reset(context) {
43
62
  throw new Error('`_reset` must be implemented in sub statedb');
44
63
  }
64
+
65
+ // eslint-disable-next-line no-unused-vars
66
+ _history(id, context) {
67
+ throw new Error('`_history` must be implemented in sub statedb');
68
+ }
69
+
70
+ // eslint-disable-next-line no-unused-vars
71
+ updateOrCreate(id, attrs, context) {
72
+ throw new Error('`updateOrCreate` must be implemented in sub statedb');
73
+ }
45
74
  }
46
75
 
47
76
  module.exports = StateDBTable;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.6.5",
6
+ "version": "1.6.10",
7
7
  "description": "Defines the basic interface for OCAP StateDB",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -12,18 +12,20 @@
12
12
  "scripts": {
13
13
  "lint": "eslint tests lib",
14
14
  "lint:fix": "eslint --fix tests lib",
15
- "test": "node tools/jest.js",
15
+ "test": "jest --forceExit --detectOpenHandles",
16
16
  "coverage": "npm run test -- --coverage"
17
17
  },
18
18
  "keywords": [],
19
19
  "author": "wangshijun <wangshijun2010@gmail.com> (http://github.com/wangshijun)",
20
20
  "license": "MIT",
21
21
  "devDependencies": {
22
- "jest": "^26.6.3"
22
+ "jest": "^27.3.1"
23
23
  },
24
24
  "dependencies": {
25
- "@ocap/util": "^1.6.5",
26
- "kareem": "^2.3.2"
25
+ "@ocap/state": "1.6.10",
26
+ "@ocap/util": "1.6.10",
27
+ "kareem": "^2.3.2",
28
+ "lodash": "^4.17.21"
27
29
  },
28
- "gitHead": "5779448f13824de38978df3c84c9da0c1e1ad989"
30
+ "gitHead": "ab272e8db3a15c6571cc7fae7cc3d3e0fdd4bdb1"
29
31
  }