@ocap/statedb 1.20.15 → 1.21.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.js +45 -1
- package/package.json +4 -4
package/lib/table.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
/* eslint-disable no-shadow */
|
|
1
2
|
/* eslint-disable no-underscore-dangle */
|
|
3
|
+
const crypto = require('crypto');
|
|
2
4
|
const Kareem = require('kareem');
|
|
3
5
|
const omit = require('lodash/omit');
|
|
4
6
|
const pick = require('lodash/pick');
|
|
@@ -7,9 +9,12 @@ const { isValid } = require('@arcblock/did');
|
|
|
7
9
|
const { toAddress } = require('@ocap/util');
|
|
8
10
|
|
|
9
11
|
class StateDBTable extends Ready {
|
|
10
|
-
constructor() {
|
|
12
|
+
constructor(uniqIndex) {
|
|
11
13
|
super();
|
|
12
14
|
|
|
15
|
+
this.uniqIndex = uniqIndex;
|
|
16
|
+
this.primaryKey = typeof uniqIndex === 'string' ? uniqIndex : 'id';
|
|
17
|
+
|
|
13
18
|
const hooks = new Kareem();
|
|
14
19
|
|
|
15
20
|
Object.defineProperty(this, 'pre', { value: (name, fn) => hooks.pre(name, fn) });
|
|
@@ -93,6 +98,45 @@ class StateDBTable extends Ready {
|
|
|
93
98
|
updateOrCreate(id, attrs, context) {
|
|
94
99
|
throw new Error('`updateOrCreate` must be implemented in sub statedb');
|
|
95
100
|
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Generate an ID composed of multiple primary keys
|
|
104
|
+
*/
|
|
105
|
+
generatePrimaryKey(doc) {
|
|
106
|
+
if (typeof doc === 'string') {
|
|
107
|
+
return doc;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// composite primary key
|
|
111
|
+
if (Array.isArray(this.uniqIndex)) {
|
|
112
|
+
const key = this.uniqIndex
|
|
113
|
+
.map((id) => doc[id])
|
|
114
|
+
.map((item) => (typeof item === 'object' ? JSON.stringify(item) : item))
|
|
115
|
+
.join('-');
|
|
116
|
+
return crypto.createHash('md5').update(key).digest('hex');
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return doc[this.uniqIndex];
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** update cache for context */
|
|
123
|
+
updateCache(data, context) {
|
|
124
|
+
if (!context) return;
|
|
125
|
+
if (!context.cacheStates?.[this.name]) {
|
|
126
|
+
context.cacheStates = context.cacheStates || {};
|
|
127
|
+
context.cacheStates[this.name] = {};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const cacheKey = this.generatePrimaryKey(data);
|
|
131
|
+
context.cacheStates[this.name][cacheKey] = data;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** get data from cache for context */
|
|
135
|
+
getCache(key, context) {
|
|
136
|
+
const cacheStates = context.cacheStates || {};
|
|
137
|
+
const cacheKey = this.generatePrimaryKey(key);
|
|
138
|
+
return cacheStates[this.name]?.[cacheKey] || null;
|
|
139
|
+
}
|
|
96
140
|
}
|
|
97
141
|
|
|
98
142
|
module.exports = StateDBTable;
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.
|
|
6
|
+
"version": "1.21.0",
|
|
7
7
|
"description": "Defines the basic interface for OCAP StateDB",
|
|
8
8
|
"main": "lib/index.js",
|
|
9
9
|
"files": [
|
|
@@ -18,9 +18,9 @@
|
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"kareem": "^2.4.1",
|
|
20
20
|
"lodash": "^4.17.21",
|
|
21
|
-
"@arcblock/did": "1.
|
|
22
|
-
"@ocap/state": "1.
|
|
23
|
-
"@ocap/util": "1.
|
|
21
|
+
"@arcblock/did": "1.21.0",
|
|
22
|
+
"@ocap/state": "1.21.0",
|
|
23
|
+
"@ocap/util": "1.21.0"
|
|
24
24
|
},
|
|
25
25
|
"scripts": {
|
|
26
26
|
"lint": "eslint tests lib",
|