@naturalcycles/db-lib 9.19.0 → 9.20.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.
|
@@ -36,37 +36,37 @@ export interface CommonKeyValueDaoCfg<T> {
|
|
|
36
36
|
deflatedJsonValue?: boolean;
|
|
37
37
|
}
|
|
38
38
|
export type CommonKeyValueDaoSaveOptions = CommonKeyValueDBSaveBatchOptions;
|
|
39
|
-
export declare class CommonKeyValueDao<
|
|
40
|
-
constructor(cfg: CommonKeyValueDaoCfg<
|
|
41
|
-
cfg: CommonKeyValueDaoCfg<
|
|
42
|
-
hooks: NonNullable<CommonKeyValueDaoCfg<
|
|
39
|
+
export declare class CommonKeyValueDao<V, K extends string = string> {
|
|
40
|
+
constructor(cfg: CommonKeyValueDaoCfg<V>);
|
|
41
|
+
cfg: CommonKeyValueDaoCfg<V> & {
|
|
42
|
+
hooks: NonNullable<CommonKeyValueDaoCfg<V>['hooks']>;
|
|
43
43
|
logger: CommonLogger;
|
|
44
44
|
};
|
|
45
45
|
ping(): Promise<void>;
|
|
46
46
|
createTable(opt?: CommonDBCreateOptions): Promise<void>;
|
|
47
|
-
create(input?: Partial<
|
|
48
|
-
getById(id?:
|
|
49
|
-
getByIdAsBuffer(id?:
|
|
50
|
-
requireById(id:
|
|
51
|
-
requireByIdAsBuffer(id:
|
|
52
|
-
getByIdOrEmpty(id:
|
|
53
|
-
patch(id:
|
|
54
|
-
getByIds(ids:
|
|
55
|
-
getByIdsAsBuffer(ids:
|
|
56
|
-
save(id:
|
|
57
|
-
saveAsBuffer(id:
|
|
58
|
-
saveBatch(entries: KeyValueTuple<
|
|
47
|
+
create(input?: Partial<V>): V;
|
|
48
|
+
getById(id?: K): Promise<V | null>;
|
|
49
|
+
getByIdAsBuffer(id?: K): Promise<Buffer | null>;
|
|
50
|
+
requireById(id: K): Promise<V>;
|
|
51
|
+
requireByIdAsBuffer(id: K): Promise<Buffer>;
|
|
52
|
+
getByIdOrEmpty(id: K, part?: Partial<V>): Promise<V>;
|
|
53
|
+
patch(id: K, patch: Partial<V>, opt?: CommonKeyValueDaoSaveOptions): Promise<V>;
|
|
54
|
+
getByIds(ids: K[]): Promise<KeyValueTuple<string, V>[]>;
|
|
55
|
+
getByIdsAsBuffer(ids: K[]): Promise<KeyValueTuple<K, Buffer>[]>;
|
|
56
|
+
save(id: K, value: V, opt?: CommonKeyValueDaoSaveOptions): Promise<void>;
|
|
57
|
+
saveAsBuffer(id: K, value: Buffer, opt?: CommonKeyValueDaoSaveOptions): Promise<void>;
|
|
58
|
+
saveBatch(entries: KeyValueTuple<K, V>[], opt?: CommonKeyValueDaoSaveOptions): Promise<void>;
|
|
59
59
|
saveBatchAsBuffer(entries: KeyValueDBTuple[], opt?: CommonKeyValueDaoSaveOptions): Promise<void>;
|
|
60
|
-
deleteByIds(ids:
|
|
61
|
-
deleteById(id:
|
|
60
|
+
deleteByIds(ids: K[]): Promise<void>;
|
|
61
|
+
deleteById(id: K): Promise<void>;
|
|
62
62
|
streamIds(limit?: number): ReadableTyped<string>;
|
|
63
|
-
streamValues(limit?: number): ReadableTyped<
|
|
64
|
-
streamEntries(limit?: number): ReadableTyped<KeyValueTuple<
|
|
63
|
+
streamValues(limit?: number): ReadableTyped<V>;
|
|
64
|
+
streamEntries(limit?: number): ReadableTyped<KeyValueTuple<K, V>>;
|
|
65
65
|
/**
|
|
66
66
|
* Increments the `id` field by the amount specified in `by`,
|
|
67
67
|
* or by 1 if `by` is not specified.
|
|
68
68
|
*
|
|
69
69
|
* Returns the new value of the field.
|
|
70
70
|
*/
|
|
71
|
-
increment(id:
|
|
71
|
+
increment(id: K, by?: number): Promise<number>;
|
|
72
72
|
}
|
|
@@ -92,7 +92,7 @@ class CommonKeyValueDao {
|
|
|
92
92
|
]);
|
|
93
93
|
}
|
|
94
94
|
async getByIdsAsBuffer(ids) {
|
|
95
|
-
return await this.cfg.db.getByIds(this.cfg.table, ids);
|
|
95
|
+
return (await this.cfg.db.getByIds(this.cfg.table, ids));
|
|
96
96
|
}
|
|
97
97
|
async save(id, value, opt) {
|
|
98
98
|
await this.saveBatch([[id, value]], opt);
|
package/package.json
CHANGED
|
@@ -53,8 +53,8 @@ export type CommonKeyValueDaoSaveOptions = CommonKeyValueDBSaveBatchOptions
|
|
|
53
53
|
// todo: logging
|
|
54
54
|
// todo: readonly
|
|
55
55
|
|
|
56
|
-
export class CommonKeyValueDao<
|
|
57
|
-
constructor(cfg: CommonKeyValueDaoCfg<
|
|
56
|
+
export class CommonKeyValueDao<V, K extends string = string> {
|
|
57
|
+
constructor(cfg: CommonKeyValueDaoCfg<V>) {
|
|
58
58
|
this.cfg = {
|
|
59
59
|
hooks: {},
|
|
60
60
|
logger: console,
|
|
@@ -70,8 +70,8 @@ export class CommonKeyValueDao<T> {
|
|
|
70
70
|
}
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
cfg: CommonKeyValueDaoCfg<
|
|
74
|
-
hooks: NonNullable<CommonKeyValueDaoCfg<
|
|
73
|
+
cfg: CommonKeyValueDaoCfg<V> & {
|
|
74
|
+
hooks: NonNullable<CommonKeyValueDaoCfg<V>['hooks']>
|
|
75
75
|
logger: CommonLogger
|
|
76
76
|
}
|
|
77
77
|
|
|
@@ -83,25 +83,25 @@ export class CommonKeyValueDao<T> {
|
|
|
83
83
|
await this.cfg.db.createTable(this.cfg.table, opt)
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
-
create(input: Partial<
|
|
86
|
+
create(input: Partial<V> = {}): V {
|
|
87
87
|
return {
|
|
88
88
|
...this.cfg.hooks.beforeCreate?.(input),
|
|
89
|
-
} as
|
|
89
|
+
} as V
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
-
async getById(id?:
|
|
92
|
+
async getById(id?: K): Promise<V | null> {
|
|
93
93
|
if (!id) return null
|
|
94
94
|
const [r] = await this.getByIds([id])
|
|
95
95
|
return r?.[1] || null
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
-
async getByIdAsBuffer(id?:
|
|
98
|
+
async getByIdAsBuffer(id?: K): Promise<Buffer | null> {
|
|
99
99
|
if (!id) return null
|
|
100
100
|
const [r] = await this.cfg.db.getByIds(this.cfg.table, [id])
|
|
101
101
|
return r?.[1] || null
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
-
async requireById(id:
|
|
104
|
+
async requireById(id: K): Promise<V> {
|
|
105
105
|
const [r] = await this.getByIds([id])
|
|
106
106
|
|
|
107
107
|
if (!r) {
|
|
@@ -115,7 +115,7 @@ export class CommonKeyValueDao<T> {
|
|
|
115
115
|
return r[1]
|
|
116
116
|
}
|
|
117
117
|
|
|
118
|
-
async requireByIdAsBuffer(id:
|
|
118
|
+
async requireByIdAsBuffer(id: K): Promise<Buffer> {
|
|
119
119
|
const [r] = await this.cfg.db.getByIds(this.cfg.table, [id])
|
|
120
120
|
|
|
121
121
|
if (!r) {
|
|
@@ -129,18 +129,18 @@ export class CommonKeyValueDao<T> {
|
|
|
129
129
|
return r[1]
|
|
130
130
|
}
|
|
131
131
|
|
|
132
|
-
async getByIdOrEmpty(id:
|
|
132
|
+
async getByIdOrEmpty(id: K, part: Partial<V> = {}): Promise<V> {
|
|
133
133
|
const [r] = await this.getByIds([id])
|
|
134
134
|
if (r) return r[1]
|
|
135
135
|
|
|
136
136
|
return {
|
|
137
137
|
...this.cfg.hooks.beforeCreate?.({}),
|
|
138
138
|
...part,
|
|
139
|
-
} as
|
|
139
|
+
} as V
|
|
140
140
|
}
|
|
141
141
|
|
|
142
|
-
async patch(id:
|
|
143
|
-
const v:
|
|
142
|
+
async patch(id: K, patch: Partial<V>, opt?: CommonKeyValueDaoSaveOptions): Promise<V> {
|
|
143
|
+
const v: V = {
|
|
144
144
|
...(await this.getByIdOrEmpty(id)),
|
|
145
145
|
...patch,
|
|
146
146
|
}
|
|
@@ -150,7 +150,7 @@ export class CommonKeyValueDao<T> {
|
|
|
150
150
|
return v
|
|
151
151
|
}
|
|
152
152
|
|
|
153
|
-
async getByIds(ids:
|
|
153
|
+
async getByIds(ids: K[]): Promise<KeyValueTuple<string, V>[]> {
|
|
154
154
|
const entries = await this.cfg.db.getByIds(this.cfg.table, ids)
|
|
155
155
|
if (!this.cfg.hooks.mapBufferToValue) return entries as any
|
|
156
156
|
|
|
@@ -160,20 +160,20 @@ export class CommonKeyValueDao<T> {
|
|
|
160
160
|
])
|
|
161
161
|
}
|
|
162
162
|
|
|
163
|
-
async getByIdsAsBuffer(ids:
|
|
164
|
-
return await this.cfg.db.getByIds(this.cfg.table, ids)
|
|
163
|
+
async getByIdsAsBuffer(ids: K[]): Promise<KeyValueTuple<K, Buffer>[]> {
|
|
164
|
+
return (await this.cfg.db.getByIds(this.cfg.table, ids)) as KeyValueTuple<K, Buffer>[]
|
|
165
165
|
}
|
|
166
166
|
|
|
167
|
-
async save(id:
|
|
167
|
+
async save(id: K, value: V, opt?: CommonKeyValueDaoSaveOptions): Promise<void> {
|
|
168
168
|
await this.saveBatch([[id, value]], opt)
|
|
169
169
|
}
|
|
170
170
|
|
|
171
|
-
async saveAsBuffer(id:
|
|
171
|
+
async saveAsBuffer(id: K, value: Buffer, opt?: CommonKeyValueDaoSaveOptions): Promise<void> {
|
|
172
172
|
await this.cfg.db.saveBatch(this.cfg.table, [[id, value]], opt)
|
|
173
173
|
}
|
|
174
174
|
|
|
175
175
|
async saveBatch(
|
|
176
|
-
entries: KeyValueTuple<
|
|
176
|
+
entries: KeyValueTuple<K, V>[],
|
|
177
177
|
opt?: CommonKeyValueDaoSaveOptions,
|
|
178
178
|
): Promise<void> {
|
|
179
179
|
const { mapValueToBuffer } = this.cfg.hooks
|
|
@@ -195,11 +195,11 @@ export class CommonKeyValueDao<T> {
|
|
|
195
195
|
await this.cfg.db.saveBatch(this.cfg.table, entries, opt)
|
|
196
196
|
}
|
|
197
197
|
|
|
198
|
-
async deleteByIds(ids:
|
|
198
|
+
async deleteByIds(ids: K[]): Promise<void> {
|
|
199
199
|
await this.cfg.db.deleteByIds(this.cfg.table, ids)
|
|
200
200
|
}
|
|
201
201
|
|
|
202
|
-
async deleteById(id:
|
|
202
|
+
async deleteById(id: K): Promise<void> {
|
|
203
203
|
await this.cfg.db.deleteByIds(this.cfg.table, [id])
|
|
204
204
|
}
|
|
205
205
|
|
|
@@ -207,11 +207,11 @@ export class CommonKeyValueDao<T> {
|
|
|
207
207
|
return this.cfg.db.streamIds(this.cfg.table, limit)
|
|
208
208
|
}
|
|
209
209
|
|
|
210
|
-
streamValues(limit?: number): ReadableTyped<
|
|
210
|
+
streamValues(limit?: number): ReadableTyped<V> {
|
|
211
211
|
const { mapBufferToValue } = this.cfg.hooks
|
|
212
212
|
|
|
213
213
|
if (!mapBufferToValue) {
|
|
214
|
-
return this.cfg.db.streamValues(this.cfg.table, limit) as ReadableTyped<
|
|
214
|
+
return this.cfg.db.streamValues(this.cfg.table, limit) as ReadableTyped<V>
|
|
215
215
|
}
|
|
216
216
|
|
|
217
217
|
return this.cfg.db.streamValues(this.cfg.table, limit).flatMap(
|
|
@@ -229,19 +229,17 @@ export class CommonKeyValueDao<T> {
|
|
|
229
229
|
)
|
|
230
230
|
}
|
|
231
231
|
|
|
232
|
-
streamEntries(limit?: number): ReadableTyped<KeyValueTuple<
|
|
232
|
+
streamEntries(limit?: number): ReadableTyped<KeyValueTuple<K, V>> {
|
|
233
233
|
const { mapBufferToValue } = this.cfg.hooks
|
|
234
234
|
|
|
235
235
|
if (!mapBufferToValue) {
|
|
236
|
-
return this.cfg.db.streamEntries(this.cfg.table, limit) as ReadableTyped<
|
|
237
|
-
KeyValueTuple<string, T>
|
|
238
|
-
>
|
|
236
|
+
return this.cfg.db.streamEntries(this.cfg.table, limit) as ReadableTyped<KeyValueTuple<K, V>>
|
|
239
237
|
}
|
|
240
238
|
|
|
241
239
|
return this.cfg.db.streamEntries(this.cfg.table, limit).flatMap(
|
|
242
240
|
async ([id, buf]) => {
|
|
243
241
|
try {
|
|
244
|
-
return [[id, await mapBufferToValue(buf)]]
|
|
242
|
+
return [[id as K, await mapBufferToValue(buf)]]
|
|
245
243
|
} catch (err) {
|
|
246
244
|
this.cfg.logger.error(err)
|
|
247
245
|
return [] // SKIP
|
|
@@ -259,7 +257,7 @@ export class CommonKeyValueDao<T> {
|
|
|
259
257
|
*
|
|
260
258
|
* Returns the new value of the field.
|
|
261
259
|
*/
|
|
262
|
-
async increment(id:
|
|
260
|
+
async increment(id: K, by = 1): Promise<number> {
|
|
263
261
|
return await this.cfg.db.increment(this.cfg.table, id, by)
|
|
264
262
|
}
|
|
265
263
|
}
|