@naturalcycles/db-lib 8.45.0 → 8.46.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.
|
@@ -93,7 +93,7 @@ class InMemoryDB {
|
|
|
93
93
|
}
|
|
94
94
|
async deleteByIds(_table, ids, _opt) {
|
|
95
95
|
const table = this.cfg.tablesPrefix + _table;
|
|
96
|
-
this.data[table]
|
|
96
|
+
this.data[table] ||= {};
|
|
97
97
|
return ids
|
|
98
98
|
.map(id => {
|
|
99
99
|
const exists = !!this.data[table][id];
|
|
@@ -3,7 +3,7 @@ import { KeyValueTuple } from '@naturalcycles/js-lib';
|
|
|
3
3
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib';
|
|
4
4
|
import { CommonDaoLogLevel } from '../commondao/common.dao.model';
|
|
5
5
|
import { CommonDBCreateOptions } from '../db.model';
|
|
6
|
-
import { CommonKeyValueDB } from './commonKeyValueDB';
|
|
6
|
+
import { CommonKeyValueDB, KeyValueDBTuple } from './commonKeyValueDB';
|
|
7
7
|
export interface CommonKeyValueDaoCfg<T> {
|
|
8
8
|
db: CommonKeyValueDB;
|
|
9
9
|
table: string;
|
|
@@ -39,15 +39,20 @@ export declare class CommonKeyValueDao<T> {
|
|
|
39
39
|
createTable(opt?: CommonDBCreateOptions): Promise<void>;
|
|
40
40
|
create(input?: Partial<T>): T;
|
|
41
41
|
getById(id?: string): Promise<T | null>;
|
|
42
|
+
getByIdAsBuffer(id?: string): Promise<Buffer | null>;
|
|
42
43
|
requireById(id: string): Promise<T>;
|
|
44
|
+
requireByIdAsBuffer(id: string): Promise<Buffer>;
|
|
43
45
|
getByIdOrEmpty(id: string, part?: Partial<T>): Promise<T>;
|
|
44
46
|
patch(id: string, patch: Partial<T>): Promise<T>;
|
|
45
47
|
getByIds(ids: string[]): Promise<KeyValueTuple<string, T>[]>;
|
|
48
|
+
getByIdsAsBuffer(ids: string[]): Promise<KeyValueTuple<string, Buffer>[]>;
|
|
46
49
|
save(id: string, value: T): Promise<void>;
|
|
50
|
+
saveAsBuffer(id: string, value: Buffer): Promise<void>;
|
|
47
51
|
saveBatch(entries: KeyValueTuple<string, T>[]): Promise<void>;
|
|
52
|
+
saveBatchAsBuffer(entries: KeyValueDBTuple[]): Promise<void>;
|
|
48
53
|
deleteByIds(ids: string[]): Promise<void>;
|
|
49
54
|
deleteById(id: string): Promise<void>;
|
|
50
55
|
streamIds(limit?: number): ReadableTyped<string>;
|
|
51
|
-
streamValues(limit?: number): ReadableTyped<
|
|
56
|
+
streamValues(limit?: number): ReadableTyped<T>;
|
|
52
57
|
streamEntries(limit?: number): ReadableTyped<KeyValueTuple<string, T>>;
|
|
53
58
|
}
|
|
@@ -34,6 +34,12 @@ class CommonKeyValueDao {
|
|
|
34
34
|
const [r] = await this.getByIds([id]);
|
|
35
35
|
return r?.[1] || null;
|
|
36
36
|
}
|
|
37
|
+
async getByIdAsBuffer(id) {
|
|
38
|
+
if (!id)
|
|
39
|
+
return null;
|
|
40
|
+
const [r] = await this.cfg.db.getByIds(this.cfg.table, [id]);
|
|
41
|
+
return r?.[1] || null;
|
|
42
|
+
}
|
|
37
43
|
async requireById(id) {
|
|
38
44
|
const [r] = await this.getByIds([id]);
|
|
39
45
|
if (!r) {
|
|
@@ -46,6 +52,18 @@ class CommonKeyValueDao {
|
|
|
46
52
|
}
|
|
47
53
|
return r[1];
|
|
48
54
|
}
|
|
55
|
+
async requireByIdAsBuffer(id) {
|
|
56
|
+
const [r] = await this.cfg.db.getByIds(this.cfg.table, [id]);
|
|
57
|
+
if (!r) {
|
|
58
|
+
const { table } = this.cfg;
|
|
59
|
+
throw new js_lib_1.AppError(`DB row required, but not found: ${table}.${id}`, {
|
|
60
|
+
code: cnst_1.DBLibError.DB_ROW_REQUIRED,
|
|
61
|
+
table,
|
|
62
|
+
id,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
return r[1];
|
|
66
|
+
}
|
|
49
67
|
async getByIdOrEmpty(id, part = {}) {
|
|
50
68
|
const [r] = await this.getByIds([id]);
|
|
51
69
|
if (r)
|
|
@@ -72,9 +90,15 @@ class CommonKeyValueDao {
|
|
|
72
90
|
await this.cfg.hooks.mapBufferToValue(buf),
|
|
73
91
|
]);
|
|
74
92
|
}
|
|
93
|
+
async getByIdsAsBuffer(ids) {
|
|
94
|
+
return await this.cfg.db.getByIds(this.cfg.table, ids);
|
|
95
|
+
}
|
|
75
96
|
async save(id, value) {
|
|
76
97
|
await this.saveBatch([[id, value]]);
|
|
77
98
|
}
|
|
99
|
+
async saveAsBuffer(id, value) {
|
|
100
|
+
await this.cfg.db.saveBatch(this.cfg.table, [[id, value]]);
|
|
101
|
+
}
|
|
78
102
|
async saveBatch(entries) {
|
|
79
103
|
let bufferEntries = [];
|
|
80
104
|
if (!this.cfg.hooks?.mapValueToBuffer) {
|
|
@@ -88,6 +112,9 @@ class CommonKeyValueDao {
|
|
|
88
112
|
}
|
|
89
113
|
await this.cfg.db.saveBatch(this.cfg.table, bufferEntries);
|
|
90
114
|
}
|
|
115
|
+
async saveBatchAsBuffer(entries) {
|
|
116
|
+
await this.cfg.db.saveBatch(this.cfg.table, entries);
|
|
117
|
+
}
|
|
91
118
|
async deleteByIds(ids) {
|
|
92
119
|
await this.cfg.db.deleteByIds(this.cfg.table, ids);
|
|
93
120
|
}
|
package/package.json
CHANGED
|
@@ -189,7 +189,7 @@ export class InMemoryDB implements CommonDB {
|
|
|
189
189
|
_opt?: CommonDBOptions,
|
|
190
190
|
): Promise<number> {
|
|
191
191
|
const table = this.cfg.tablesPrefix + _table
|
|
192
|
-
this.data[table]
|
|
192
|
+
this.data[table] ||= {}
|
|
193
193
|
|
|
194
194
|
return ids
|
|
195
195
|
.map(id => {
|
|
@@ -79,6 +79,12 @@ export class CommonKeyValueDao<T> {
|
|
|
79
79
|
return r?.[1] || null
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
+
async getByIdAsBuffer(id?: string): Promise<Buffer | null> {
|
|
83
|
+
if (!id) return null
|
|
84
|
+
const [r] = await this.cfg.db.getByIds(this.cfg.table, [id])
|
|
85
|
+
return r?.[1] || null
|
|
86
|
+
}
|
|
87
|
+
|
|
82
88
|
async requireById(id: string): Promise<T> {
|
|
83
89
|
const [r] = await this.getByIds([id])
|
|
84
90
|
|
|
@@ -94,6 +100,21 @@ export class CommonKeyValueDao<T> {
|
|
|
94
100
|
return r[1]
|
|
95
101
|
}
|
|
96
102
|
|
|
103
|
+
async requireByIdAsBuffer(id: string): Promise<Buffer> {
|
|
104
|
+
const [r] = await this.cfg.db.getByIds(this.cfg.table, [id])
|
|
105
|
+
|
|
106
|
+
if (!r) {
|
|
107
|
+
const { table } = this.cfg
|
|
108
|
+
throw new AppError(`DB row required, but not found: ${table}.${id}`, {
|
|
109
|
+
code: DBLibError.DB_ROW_REQUIRED,
|
|
110
|
+
table,
|
|
111
|
+
id,
|
|
112
|
+
})
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return r[1]
|
|
116
|
+
}
|
|
117
|
+
|
|
97
118
|
async getByIdOrEmpty(id: string, part: Partial<T> = {}): Promise<T> {
|
|
98
119
|
const [r] = await this.getByIds([id])
|
|
99
120
|
if (r) return r[1]
|
|
@@ -125,10 +146,18 @@ export class CommonKeyValueDao<T> {
|
|
|
125
146
|
])
|
|
126
147
|
}
|
|
127
148
|
|
|
149
|
+
async getByIdsAsBuffer(ids: string[]): Promise<KeyValueTuple<string, Buffer>[]> {
|
|
150
|
+
return await this.cfg.db.getByIds(this.cfg.table, ids)
|
|
151
|
+
}
|
|
152
|
+
|
|
128
153
|
async save(id: string, value: T): Promise<void> {
|
|
129
154
|
await this.saveBatch([[id, value]])
|
|
130
155
|
}
|
|
131
156
|
|
|
157
|
+
async saveAsBuffer(id: string, value: Buffer): Promise<void> {
|
|
158
|
+
await this.cfg.db.saveBatch(this.cfg.table, [[id, value]])
|
|
159
|
+
}
|
|
160
|
+
|
|
132
161
|
async saveBatch(entries: KeyValueTuple<string, T>[]): Promise<void> {
|
|
133
162
|
let bufferEntries: KeyValueDBTuple[] = []
|
|
134
163
|
|
|
@@ -144,6 +173,10 @@ export class CommonKeyValueDao<T> {
|
|
|
144
173
|
await this.cfg.db.saveBatch(this.cfg.table, bufferEntries)
|
|
145
174
|
}
|
|
146
175
|
|
|
176
|
+
async saveBatchAsBuffer(entries: KeyValueDBTuple[]): Promise<void> {
|
|
177
|
+
await this.cfg.db.saveBatch(this.cfg.table, entries)
|
|
178
|
+
}
|
|
179
|
+
|
|
147
180
|
async deleteByIds(ids: string[]): Promise<void> {
|
|
148
181
|
await this.cfg.db.deleteByIds(this.cfg.table, ids)
|
|
149
182
|
}
|
|
@@ -156,14 +189,14 @@ export class CommonKeyValueDao<T> {
|
|
|
156
189
|
return this.cfg.db.streamIds(this.cfg.table, limit)
|
|
157
190
|
}
|
|
158
191
|
|
|
159
|
-
streamValues(limit?: number): ReadableTyped<
|
|
192
|
+
streamValues(limit?: number): ReadableTyped<T> {
|
|
160
193
|
if (!this.cfg.hooks?.mapBufferToValue) {
|
|
161
194
|
return this.cfg.db.streamValues(this.cfg.table, limit)
|
|
162
195
|
}
|
|
163
196
|
|
|
164
197
|
// todo: consider it when readableMap supports `errorMode: SUPPRESS`
|
|
165
198
|
// readableMap(this.cfg.db.streamValues(this.cfg.table, limit), async buf => await this.cfg.hooks!.mapBufferToValue(buf))
|
|
166
|
-
const stream: ReadableTyped<
|
|
199
|
+
const stream: ReadableTyped<T> = this.cfg.db
|
|
167
200
|
.streamValues(this.cfg.table, limit)
|
|
168
201
|
.on('error', err => stream.emit('error', err))
|
|
169
202
|
.pipe(
|