@naturalcycles/db-lib 10.14.0 → 10.15.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.
@@ -77,7 +77,7 @@ export class InMemoryDB {
77
77
  async saveBatch(_table, rows, opt = {}) {
78
78
  const table = this.cfg.tablesPrefix + _table;
79
79
  this.data[table] ||= {};
80
- rows.forEach(r => {
80
+ for (const r of rows) {
81
81
  if (!r.id) {
82
82
  this.cfg.logger.warn({ rows });
83
83
  throw new Error(`InMemoryDB doesn't support id auto-generation in saveBatch, row without id was given`);
@@ -92,7 +92,7 @@ export class InMemoryDB {
92
92
  // 1. Not store values "by reference" (avoid mutation bugs)
93
93
  // 2. Simulate real DB that would do something like that in a transport layer anyway
94
94
  this.data[table][r.id] = JSON.parse(JSON.stringify(r), bufferReviver);
95
- });
95
+ }
96
96
  }
97
97
  async deleteByQuery(q, _opt) {
98
98
  const table = this.cfg.tablesPrefix + q.table;
@@ -106,12 +106,12 @@ export class InMemoryDB {
106
106
  if (!this.data[table])
107
107
  return 0;
108
108
  let count = 0;
109
- ids.forEach(id => {
109
+ for (const id of ids) {
110
110
  if (!this.data[table][id])
111
- return;
111
+ continue;
112
112
  delete this.data[table][id];
113
113
  count++;
114
- });
114
+ }
115
115
  return count;
116
116
  }
117
117
  async patchByQuery(q, patch) {
@@ -119,7 +119,9 @@ export class InMemoryDB {
119
119
  return 0;
120
120
  const table = this.cfg.tablesPrefix + q.table;
121
121
  const rows = queryInMemory(q, Object.values(this.data[table] || {}));
122
- rows.forEach(row => Object.assign(row, patch));
122
+ for (const row of rows) {
123
+ Object.assign(row, patch);
124
+ }
123
125
  return rows.length;
124
126
  }
125
127
  async runQuery(q, _opt) {
@@ -14,7 +14,9 @@ export class InMemoryKeyValueDB {
14
14
  async createTable(_table, _opt) { }
15
15
  async deleteByIds(table, ids) {
16
16
  this.data[table] ||= {};
17
- ids.forEach(id => delete this.data[table][id]);
17
+ for (const id of ids) {
18
+ delete this.data[table][id];
19
+ }
18
20
  }
19
21
  async getByIds(table, ids) {
20
22
  this.data[table] ||= {};
@@ -22,7 +24,9 @@ export class InMemoryKeyValueDB {
22
24
  }
23
25
  async saveBatch(table, entries) {
24
26
  this.data[table] ||= {};
25
- entries.forEach(([id, v]) => (this.data[table][id] = v));
27
+ for (const [id, v] of entries) {
28
+ this.data[table][id] = v;
29
+ }
26
30
  }
27
31
  streamIds(table, limit) {
28
32
  return Readable.from(Object.keys(this.data[table] || {}).slice(0, limit));
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/db-lib",
3
3
  "type": "module",
4
- "version": "10.14.0",
4
+ "version": "10.15.0",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@naturalcycles/nodejs-lib": "^15"
@@ -153,7 +153,7 @@ export class InMemoryDB implements CommonDB {
153
153
  const table = this.cfg.tablesPrefix + _table
154
154
  this.data[table] ||= {}
155
155
 
156
- rows.forEach(r => {
156
+ for (const r of rows) {
157
157
  if (!r.id) {
158
158
  this.cfg.logger!.warn({ rows })
159
159
  throw new Error(
@@ -161,19 +161,19 @@ export class InMemoryDB implements CommonDB {
161
161
  )
162
162
  }
163
163
 
164
- if (opt.saveMethod === 'insert' && this.data[table]![r.id]) {
164
+ if (opt.saveMethod === 'insert' && this.data[table][r.id]) {
165
165
  throw new Error(`InMemoryDB: INSERT failed, entity exists: ${table}.${r.id}`)
166
166
  }
167
167
 
168
- if (opt.saveMethod === 'update' && !this.data[table]![r.id]) {
168
+ if (opt.saveMethod === 'update' && !this.data[table][r.id]) {
169
169
  throw new Error(`InMemoryDB: UPDATE failed, entity doesn't exist: ${table}.${r.id}`)
170
170
  }
171
171
 
172
172
  // JSON parse/stringify (deep clone) is to:
173
173
  // 1. Not store values "by reference" (avoid mutation bugs)
174
174
  // 2. Simulate real DB that would do something like that in a transport layer anyway
175
- this.data[table]![r.id] = JSON.parse(JSON.stringify(r), bufferReviver)
176
- })
175
+ this.data[table][r.id] = JSON.parse(JSON.stringify(r), bufferReviver)
176
+ }
177
177
  }
178
178
 
179
179
  async deleteByQuery<ROW extends ObjectWithId>(
@@ -191,11 +191,11 @@ export class InMemoryDB implements CommonDB {
191
191
  if (!this.data[table]) return 0
192
192
 
193
193
  let count = 0
194
- ids.forEach(id => {
195
- if (!this.data[table]![id]) return
196
- delete this.data[table]![id]
194
+ for (const id of ids) {
195
+ if (!this.data[table][id]) continue
196
+ delete this.data[table][id]
197
197
  count++
198
- })
198
+ }
199
199
 
200
200
  return count
201
201
  }
@@ -207,7 +207,9 @@ export class InMemoryDB implements CommonDB {
207
207
  if (_isEmptyObject(patch)) return 0
208
208
  const table = this.cfg.tablesPrefix + q.table
209
209
  const rows = queryInMemory(q, Object.values(this.data[table] || {}) as ROW[])
210
- rows.forEach(row => Object.assign(row, patch))
210
+ for (const row of rows) {
211
+ Object.assign(row, patch)
212
+ }
211
213
  return rows.length
212
214
  }
213
215
 
@@ -23,7 +23,9 @@ export class InMemoryKeyValueDB implements CommonKeyValueDB {
23
23
 
24
24
  async deleteByIds(table: string, ids: string[]): Promise<void> {
25
25
  this.data[table] ||= {}
26
- ids.forEach(id => delete this.data[table]![id])
26
+ for (const id of ids) {
27
+ delete this.data[table][id]
28
+ }
27
29
  }
28
30
 
29
31
  async getByIds(table: string, ids: string[]): Promise<KeyValueDBTuple[]> {
@@ -33,7 +35,9 @@ export class InMemoryKeyValueDB implements CommonKeyValueDB {
33
35
 
34
36
  async saveBatch(table: string, entries: KeyValueDBTuple[]): Promise<void> {
35
37
  this.data[table] ||= {}
36
- entries.forEach(([id, v]) => (this.data[table]![id] = v))
38
+ for (const [id, v] of entries) {
39
+ this.data[table][id] = v
40
+ }
37
41
  }
38
42
 
39
43
  streamIds(table: string, limit?: number): ReadableTyped<string> {