@ocap/indexdb-elasticsearch 1.20.16 → 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/db.js CHANGED
@@ -361,7 +361,10 @@ class ESIndexDB extends BaseIndexDB {
361
361
  }
362
362
  });
363
363
 
364
- return { accounts: items.map((x) => Account.formatAfterRead(x)), paging: nextPaging };
364
+ return {
365
+ accounts: await Promise.all(items.map((x) => Account.formatAfterRead(x))),
366
+ paging: nextPaging,
367
+ };
365
368
  }
366
369
 
367
370
  async listTokens({ issuerAddress, paging, timeFilter = {} } = {}) {
@@ -19,6 +19,7 @@ class Account extends ESIndex {
19
19
 
20
20
  static formatBeforeUpdate(data, tokenLength) {
21
21
  data.balance = super.padBalance(data.balance, tokenLength);
22
+
22
23
  if (Array.isArray(data.tokens)) {
23
24
  data.tokens = data.tokens.map((x) => {
24
25
  x.balance = super.padBalance(x.balance, tokenLength);
@@ -30,13 +31,9 @@ class Account extends ESIndex {
30
31
  }
31
32
 
32
33
  static formatAfterRead(data) {
33
- if (!data) {
34
- return data;
35
- }
34
+ if (!data) return data;
36
35
 
37
- if (data) {
38
- data.balance = super.trimBalance(data.balance);
39
- }
36
+ data.balance = super.trimBalance(data.balance);
40
37
 
41
38
  if (Array.isArray(data.tokens)) {
42
39
  data.tokens = data.tokens.map((x) => {
@@ -50,7 +47,6 @@ class Account extends ESIndex {
50
47
 
51
48
  batchInsert(rows) {
52
49
  const formatted = rows.map((x) => Account.formatBeforeUpdate(x, this.tokenLength));
53
-
54
50
  return super.batchInsert(formatted);
55
51
  }
56
52
 
package/lib/table/base.js CHANGED
@@ -10,12 +10,13 @@ const formatEsResponseError = (err) => (get(err, 'meta.body') ? err.meta.body :
10
10
 
11
11
  class ESIndex extends BaseIndex {
12
12
  constructor({ name, docId, client, http, indexParams }) {
13
- super(name);
13
+ super(name, docId);
14
14
 
15
15
  this.docId = docId;
16
16
  this.client = client;
17
17
  this.http = http;
18
18
  this.indexParams = indexParams;
19
+
19
20
  this.ensureIndex(indexParams).then(() => {
20
21
  debug(`index ready ${name}`);
21
22
  this.markReady();
@@ -91,30 +92,35 @@ class ESIndex extends BaseIndex {
91
92
  }
92
93
 
93
94
  batchInsert(rows) {
94
- const body = rows.flatMap((row) => [{ index: { _index: this.name, _id: row[this.docId] } }, row]);
95
+ const body = rows.flatMap((row) => [{ index: { _index: this.name, _id: this.generatePrimaryKey(row) } }, row]);
95
96
  return this.client.bulk({ refresh: 'wait_for', body });
96
97
  }
97
98
 
98
99
  batchUpdate(rows) {
99
- const body = rows.flatMap((row) => [{ update: { _index: this.name, _id: row[this.docId] } }, { doc: row }]);
100
+ const body = rows.flatMap((row) => [
101
+ { update: { _index: this.name, _id: this.generatePrimaryKey(row) } },
102
+ { doc: row },
103
+ ]);
100
104
  return this.client.bulk({ refresh: 'wait_for', body });
101
105
  }
102
106
 
103
107
  async _insert(row) {
104
108
  const copy = { ...row };
105
109
  try {
106
- const doc = await this._get(row[this.docId]);
110
+ const id = this.generatePrimaryKey(copy);
111
+ const doc = await this._get(id);
107
112
  if (doc) {
108
113
  return null;
109
114
  }
110
115
 
111
116
  const { body } = await this.client.create({
112
117
  index: this.name,
113
- id: copy[this.docId],
118
+ id,
114
119
  body: copy,
115
120
  refresh: process.env.NODE_ENV === 'test' ? 'wait_for' : undefined,
116
121
  });
117
122
  debug(`insert ${this.name} ${body.result}`, copy);
123
+
118
124
  return row;
119
125
  } catch (err) {
120
126
  console.warn(`failed to insert ${this.name}`, formatEsResponseError(err));
@@ -123,8 +129,10 @@ class ESIndex extends BaseIndex {
123
129
  }
124
130
 
125
131
  async _get(docId) {
132
+ const id = this.generatePrimaryKey(docId);
133
+
126
134
  try {
127
- const { body } = await this.client.get({ index: this.name, id: docId });
135
+ const { body } = await this.client.get({ index: this.name, id });
128
136
  return body ? body._source : null;
129
137
  } catch {
130
138
  return null;
@@ -137,18 +145,21 @@ class ESIndex extends BaseIndex {
137
145
  return null;
138
146
  }
139
147
 
140
- delete updates[this.docId];
148
+ [].concat(this.docId).forEach((id) => {
149
+ delete updates[id];
150
+ });
141
151
 
142
152
  try {
153
+ const id = this.generatePrimaryKey(docId);
143
154
  const { body } = await this.client.update({
144
155
  index: this.name,
145
- id: docId,
156
+ id,
146
157
  retry_on_conflict: 5,
147
158
  // partial doc merge is supported in opensearch, so we just pass the updates here
148
159
  // @link https://www.elastic.co/guide/en/elasticsearch/reference/7.16/docs-update.html#update-api-desc
149
160
  body: { doc: { ...updates } },
150
161
  });
151
- debug(`update ${this.name}`, { docId, result: body.result });
162
+ debug(`update ${this.name}`, { docId: id, result: body.result });
152
163
  } catch (err) {
153
164
  console.warn(`failed to update ${this.name}`, formatEsResponseError(err));
154
165
  throw err;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ocap/indexdb-elasticsearch",
3
3
  "description": "OCAP indexdb adapter that uses elasticsearch as backend",
4
- "version": "1.20.16",
4
+ "version": "1.21.0",
5
5
  "author": "wangshijun <shijun@arcblock.io> (https://www.arcblock.io)",
6
6
  "bugs": {
7
7
  "url": "https://github.com/ArcBlock/blockchain/issues",
@@ -16,7 +16,7 @@
16
16
  "devDependencies": {
17
17
  "dotenv-flow": "^3.2.0",
18
18
  "jest": "^29.7.0",
19
- "@ocap/indexdb-test": "1.20.16"
19
+ "@ocap/indexdb-test": "1.21.0"
20
20
  },
21
21
  "homepage": "https://github.com/ArcBlock/blockchain/tree/master/indexdb/elasticsearch",
22
22
  "keywords": [
@@ -40,9 +40,9 @@
40
40
  "bn.js": "^5.2.1",
41
41
  "debug": "^4.3.6",
42
42
  "lodash": "^4.17.21",
43
- "@arcblock/did": "1.20.16",
44
- "@ocap/indexdb": "1.20.16",
45
- "@ocap/util": "1.20.16"
43
+ "@ocap/indexdb": "1.21.0",
44
+ "@arcblock/did": "1.21.0",
45
+ "@ocap/util": "1.21.0"
46
46
  },
47
47
  "scripts": {
48
48
  "lint": "eslint tests lib",