@naturalcycles/db-lib 9.22.1 → 9.23.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/dist/adapter/inmemory/inMemoryKeyValueDB.d.ts +6 -6
- package/dist/adapter/inmemory/inMemoryKeyValueDB.js +1 -1
- package/dist/kv/commonKeyValueDB.d.ts +6 -5
- package/dist/kv/commonKeyValueDao.d.ts +13 -15
- package/dist/kv/commonKeyValueDao.js +12 -18
- package/package.json +1 -1
- package/src/adapter/inmemory/inMemoryKeyValueDB.ts +8 -7
- package/src/kv/commonKeyValueDB.ts +8 -6
- package/src/kv/commonKeyValueDao.ts +30 -42
- package/src/testing/keyValueDBTest.ts +5 -5
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { StringMap } from '@naturalcycles/js-lib';
|
|
2
2
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib';
|
|
3
3
|
import { CommonDBCreateOptions } from '../../db.model';
|
|
4
|
-
import { CommonKeyValueDB, IncrementTuple } from '../../kv/commonKeyValueDB';
|
|
4
|
+
import { CommonKeyValueDB, IncrementTuple, KeyValueDBTuple } from '../../kv/commonKeyValueDB';
|
|
5
5
|
export interface InMemoryKeyValueDBCfg {
|
|
6
6
|
}
|
|
7
7
|
export declare class InMemoryKeyValueDB implements CommonKeyValueDB {
|
|
@@ -15,11 +15,11 @@ export declare class InMemoryKeyValueDB implements CommonKeyValueDB {
|
|
|
15
15
|
ping(): Promise<void>;
|
|
16
16
|
createTable(_table: string, _opt?: CommonDBCreateOptions): Promise<void>;
|
|
17
17
|
deleteByIds(table: string, ids: string[]): Promise<void>;
|
|
18
|
-
getByIds
|
|
19
|
-
saveBatch
|
|
18
|
+
getByIds(table: string, ids: string[]): Promise<KeyValueDBTuple[]>;
|
|
19
|
+
saveBatch(table: string, entries: KeyValueDBTuple[]): Promise<void>;
|
|
20
20
|
streamIds(table: string, limit?: number): ReadableTyped<string>;
|
|
21
|
-
streamValues
|
|
22
|
-
streamEntries
|
|
21
|
+
streamValues(table: string, limit?: number): ReadableTyped<Buffer>;
|
|
22
|
+
streamEntries(table: string, limit?: number): ReadableTyped<KeyValueDBTuple>;
|
|
23
23
|
count(table: string): Promise<number>;
|
|
24
24
|
incrementBatch(table: string, entries: IncrementTuple[]): Promise<IncrementTuple[]>;
|
|
25
25
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Integer,
|
|
1
|
+
import { Integer, UnixTimestampNumber } from '@naturalcycles/js-lib';
|
|
2
2
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib';
|
|
3
3
|
import { CommonDBCreateOptions } from '../db.model';
|
|
4
4
|
/**
|
|
@@ -25,12 +25,12 @@ export interface CommonKeyValueDB {
|
|
|
25
25
|
*
|
|
26
26
|
* Currently it is NOT required to maintain the same order as input `ids`.
|
|
27
27
|
*/
|
|
28
|
-
getByIds:
|
|
28
|
+
getByIds: (table: string, ids: string[]) => Promise<KeyValueDBTuple[]>;
|
|
29
29
|
deleteByIds: (table: string, ids: string[]) => Promise<void>;
|
|
30
|
-
saveBatch:
|
|
30
|
+
saveBatch: (table: string, entries: KeyValueDBTuple[], opt?: CommonKeyValueDBSaveBatchOptions) => Promise<void>;
|
|
31
31
|
streamIds: (table: string, limit?: number) => ReadableTyped<string>;
|
|
32
|
-
streamValues:
|
|
33
|
-
streamEntries:
|
|
32
|
+
streamValues: (table: string, limit?: number) => ReadableTyped<Buffer>;
|
|
33
|
+
streamEntries: (table: string, limit?: number) => ReadableTyped<KeyValueDBTuple>;
|
|
34
34
|
count: (table: string) => Promise<number>;
|
|
35
35
|
/**
|
|
36
36
|
* Perform a batch of Increment operations.
|
|
@@ -49,6 +49,7 @@ export interface CommonKeyValueDB {
|
|
|
49
49
|
*/
|
|
50
50
|
incrementBatch: (table: string, entries: IncrementTuple[]) => Promise<IncrementTuple[]>;
|
|
51
51
|
}
|
|
52
|
+
export type KeyValueDBTuple = [key: string, value: Buffer];
|
|
52
53
|
export type IncrementTuple = [key: string, value: Integer];
|
|
53
54
|
export interface CommonKeyValueDBSaveBatchOptions {
|
|
54
55
|
/**
|
|
@@ -2,8 +2,8 @@ import { CommonLogger, KeyValueTuple } from '@naturalcycles/js-lib';
|
|
|
2
2
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib';
|
|
3
3
|
import { CommonDaoLogLevel } from '../commondao/common.dao.model';
|
|
4
4
|
import { CommonDBCreateOptions } from '../db.model';
|
|
5
|
-
import { CommonKeyValueDB, CommonKeyValueDBSaveBatchOptions, IncrementTuple } from './commonKeyValueDB';
|
|
6
|
-
export interface CommonKeyValueDaoCfg<
|
|
5
|
+
import { CommonKeyValueDB, CommonKeyValueDBSaveBatchOptions, IncrementTuple, KeyValueDBTuple } from './commonKeyValueDB';
|
|
6
|
+
export interface CommonKeyValueDaoCfg<V> {
|
|
7
7
|
db: CommonKeyValueDB;
|
|
8
8
|
table: string;
|
|
9
9
|
/**
|
|
@@ -23,31 +23,29 @@ export interface CommonKeyValueDaoCfg<RAW_V, V = RAW_V> {
|
|
|
23
23
|
* @default false
|
|
24
24
|
*/
|
|
25
25
|
logStarted?: boolean;
|
|
26
|
-
transformer?: CommonKeyValueDaoTransformer<V
|
|
26
|
+
transformer?: CommonKeyValueDaoTransformer<V>;
|
|
27
27
|
}
|
|
28
28
|
export type CommonKeyValueDaoSaveOptions = CommonKeyValueDBSaveBatchOptions;
|
|
29
|
-
export interface CommonKeyValueDaoTransformer<V
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
export interface CommonKeyValueDaoTransformer<V> {
|
|
30
|
+
valueToBuffer: (v: V) => Promise<Buffer>;
|
|
31
|
+
bufferToValue: (buf: Buffer) => Promise<V>;
|
|
32
32
|
}
|
|
33
|
-
export declare const commonKeyValueDaoDeflatedJsonTransformer: CommonKeyValueDaoTransformer<any
|
|
34
|
-
export declare class CommonKeyValueDao<K extends string
|
|
35
|
-
constructor(cfg: CommonKeyValueDaoCfg<
|
|
36
|
-
cfg: CommonKeyValueDaoCfg<
|
|
33
|
+
export declare const commonKeyValueDaoDeflatedJsonTransformer: CommonKeyValueDaoTransformer<any>;
|
|
34
|
+
export declare class CommonKeyValueDao<K extends string = string, V = Buffer> {
|
|
35
|
+
constructor(cfg: CommonKeyValueDaoCfg<V>);
|
|
36
|
+
cfg: CommonKeyValueDaoCfg<V> & {
|
|
37
37
|
logger: CommonLogger;
|
|
38
38
|
};
|
|
39
39
|
ping(): Promise<void>;
|
|
40
40
|
createTable(opt?: CommonDBCreateOptions): Promise<void>;
|
|
41
41
|
getById(id?: K): Promise<V | null>;
|
|
42
|
-
|
|
42
|
+
getByIdAsBuffer(id?: K): Promise<Buffer | null>;
|
|
43
43
|
requireById(id: K): Promise<V>;
|
|
44
|
-
|
|
44
|
+
requireByIdAsBuffer(id: K): Promise<Buffer>;
|
|
45
45
|
getByIds(ids: K[]): Promise<KeyValueTuple<string, V>[]>;
|
|
46
|
-
|
|
46
|
+
getByIdsAsBuffer(ids: K[]): Promise<KeyValueDBTuple[]>;
|
|
47
47
|
save(id: K, value: V, opt?: CommonKeyValueDaoSaveOptions): Promise<void>;
|
|
48
|
-
saveRaw(id: K, value: RAW_V, opt?: CommonKeyValueDaoSaveOptions): Promise<void>;
|
|
49
48
|
saveBatch(entries: KeyValueTuple<K, V>[], opt?: CommonKeyValueDaoSaveOptions): Promise<void>;
|
|
50
|
-
saveBatchRaw(entries: KeyValueTuple<K, RAW_V>[], opt?: CommonKeyValueDaoSaveOptions): Promise<void>;
|
|
51
49
|
deleteByIds(ids: K[]): Promise<void>;
|
|
52
50
|
deleteById(id: K): Promise<void>;
|
|
53
51
|
streamIds(limit?: number): ReadableTyped<K>;
|
|
@@ -4,8 +4,8 @@ exports.CommonKeyValueDao = exports.commonKeyValueDaoDeflatedJsonTransformer = v
|
|
|
4
4
|
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
5
5
|
const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
|
|
6
6
|
exports.commonKeyValueDaoDeflatedJsonTransformer = {
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
valueToBuffer: async (v) => await (0, nodejs_lib_1.deflateString)(JSON.stringify(v)),
|
|
8
|
+
bufferToValue: async (buf) => JSON.parse(await (0, nodejs_lib_1.inflateToString)(buf)),
|
|
9
9
|
};
|
|
10
10
|
// todo: logging
|
|
11
11
|
// todo: readonly
|
|
@@ -28,7 +28,7 @@ class CommonKeyValueDao {
|
|
|
28
28
|
const [r] = await this.getByIds([id]);
|
|
29
29
|
return r?.[1] || null;
|
|
30
30
|
}
|
|
31
|
-
async
|
|
31
|
+
async getByIdAsBuffer(id) {
|
|
32
32
|
if (!id)
|
|
33
33
|
return null;
|
|
34
34
|
const [r] = await this.cfg.db.getByIds(this.cfg.table, [id]);
|
|
@@ -45,7 +45,7 @@ class CommonKeyValueDao {
|
|
|
45
45
|
}
|
|
46
46
|
return r[1];
|
|
47
47
|
}
|
|
48
|
-
async
|
|
48
|
+
async requireByIdAsBuffer(id) {
|
|
49
49
|
const [r] = await this.cfg.db.getByIds(this.cfg.table, [id]);
|
|
50
50
|
if (!r) {
|
|
51
51
|
const { table } = this.cfg;
|
|
@@ -62,18 +62,15 @@ class CommonKeyValueDao {
|
|
|
62
62
|
return entries;
|
|
63
63
|
return await (0, js_lib_1.pMap)(entries, async ([id, raw]) => [
|
|
64
64
|
id,
|
|
65
|
-
await this.cfg.transformer.
|
|
65
|
+
await this.cfg.transformer.bufferToValue(raw),
|
|
66
66
|
]);
|
|
67
67
|
}
|
|
68
|
-
async
|
|
69
|
-
return
|
|
68
|
+
async getByIdsAsBuffer(ids) {
|
|
69
|
+
return await this.cfg.db.getByIds(this.cfg.table, ids);
|
|
70
70
|
}
|
|
71
71
|
async save(id, value, opt) {
|
|
72
72
|
await this.saveBatch([[id, value]], opt);
|
|
73
73
|
}
|
|
74
|
-
async saveRaw(id, value, opt) {
|
|
75
|
-
await this.cfg.db.saveBatch(this.cfg.table, [[id, value]], opt);
|
|
76
|
-
}
|
|
77
74
|
async saveBatch(entries, opt) {
|
|
78
75
|
const { transformer } = this.cfg;
|
|
79
76
|
let rawEntries;
|
|
@@ -81,13 +78,10 @@ class CommonKeyValueDao {
|
|
|
81
78
|
rawEntries = entries;
|
|
82
79
|
}
|
|
83
80
|
else {
|
|
84
|
-
rawEntries = await (0, js_lib_1.pMap)(entries, async ([id, v]) => [id, await transformer.
|
|
81
|
+
rawEntries = await (0, js_lib_1.pMap)(entries, async ([id, v]) => [id, await transformer.valueToBuffer(v)]);
|
|
85
82
|
}
|
|
86
83
|
await this.cfg.db.saveBatch(this.cfg.table, rawEntries, opt);
|
|
87
84
|
}
|
|
88
|
-
async saveBatchRaw(entries, opt) {
|
|
89
|
-
await this.cfg.db.saveBatch(this.cfg.table, entries, opt);
|
|
90
|
-
}
|
|
91
85
|
async deleteByIds(ids) {
|
|
92
86
|
await this.cfg.db.deleteByIds(this.cfg.table, ids);
|
|
93
87
|
}
|
|
@@ -102,9 +96,9 @@ class CommonKeyValueDao {
|
|
|
102
96
|
if (!transformer) {
|
|
103
97
|
return this.cfg.db.streamValues(this.cfg.table, limit);
|
|
104
98
|
}
|
|
105
|
-
return this.cfg.db.streamValues(this.cfg.table, limit).flatMap(async (
|
|
99
|
+
return this.cfg.db.streamValues(this.cfg.table, limit).flatMap(async (buf) => {
|
|
106
100
|
try {
|
|
107
|
-
return [await transformer.
|
|
101
|
+
return [await transformer.bufferToValue(buf)];
|
|
108
102
|
}
|
|
109
103
|
catch (err) {
|
|
110
104
|
this.cfg.logger.error(err);
|
|
@@ -119,9 +113,9 @@ class CommonKeyValueDao {
|
|
|
119
113
|
if (!transformer) {
|
|
120
114
|
return this.cfg.db.streamEntries(this.cfg.table, limit);
|
|
121
115
|
}
|
|
122
|
-
return this.cfg.db.streamEntries(this.cfg.table, limit).flatMap(async ([id,
|
|
116
|
+
return this.cfg.db.streamEntries(this.cfg.table, limit).flatMap(async ([id, buf]) => {
|
|
123
117
|
try {
|
|
124
|
-
return [[id, await transformer.
|
|
118
|
+
return [[id, await transformer.bufferToValue(buf)]];
|
|
125
119
|
}
|
|
126
120
|
catch (err) {
|
|
127
121
|
this.cfg.logger.error(err);
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { Readable } from 'node:stream'
|
|
2
|
-
import {
|
|
2
|
+
import { StringMap } from '@naturalcycles/js-lib'
|
|
3
3
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib'
|
|
4
4
|
import { CommonDBCreateOptions } from '../../db.model'
|
|
5
5
|
import {
|
|
6
6
|
CommonKeyValueDB,
|
|
7
7
|
commonKeyValueDBFullSupport,
|
|
8
8
|
IncrementTuple,
|
|
9
|
+
KeyValueDBTuple,
|
|
9
10
|
} from '../../kv/commonKeyValueDB'
|
|
10
11
|
|
|
11
12
|
export interface InMemoryKeyValueDBCfg {}
|
|
@@ -17,7 +18,7 @@ export class InMemoryKeyValueDB implements CommonKeyValueDB {
|
|
|
17
18
|
...commonKeyValueDBFullSupport,
|
|
18
19
|
}
|
|
19
20
|
|
|
20
|
-
// data[table][id] =>
|
|
21
|
+
// data[table][id] => any (can be Buffer, or number)
|
|
21
22
|
data: StringMap<StringMap<any>> = {}
|
|
22
23
|
|
|
23
24
|
async ping(): Promise<void> {}
|
|
@@ -29,12 +30,12 @@ export class InMemoryKeyValueDB implements CommonKeyValueDB {
|
|
|
29
30
|
ids.forEach(id => delete this.data[table]![id])
|
|
30
31
|
}
|
|
31
32
|
|
|
32
|
-
async getByIds
|
|
33
|
+
async getByIds(table: string, ids: string[]): Promise<KeyValueDBTuple[]> {
|
|
33
34
|
this.data[table] ||= {}
|
|
34
|
-
return ids.map(id => [id, this.data[table]![id]!] as
|
|
35
|
+
return ids.map(id => [id, this.data[table]![id]!] as KeyValueDBTuple).filter(e => e[1])
|
|
35
36
|
}
|
|
36
37
|
|
|
37
|
-
async saveBatch
|
|
38
|
+
async saveBatch(table: string, entries: KeyValueDBTuple[]): Promise<void> {
|
|
38
39
|
this.data[table] ||= {}
|
|
39
40
|
entries.forEach(([id, v]) => (this.data[table]![id] = v))
|
|
40
41
|
}
|
|
@@ -43,11 +44,11 @@ export class InMemoryKeyValueDB implements CommonKeyValueDB {
|
|
|
43
44
|
return Readable.from(Object.keys(this.data[table] || {}).slice(0, limit))
|
|
44
45
|
}
|
|
45
46
|
|
|
46
|
-
streamValues
|
|
47
|
+
streamValues(table: string, limit?: number): ReadableTyped<Buffer> {
|
|
47
48
|
return Readable.from(Object.values(this.data[table] || {}).slice(0, limit))
|
|
48
49
|
}
|
|
49
50
|
|
|
50
|
-
streamEntries
|
|
51
|
+
streamEntries(table: string, limit?: number): ReadableTyped<KeyValueDBTuple> {
|
|
51
52
|
return Readable.from(Object.entries(this.data[table] || {}).slice(0, limit))
|
|
52
53
|
}
|
|
53
54
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Integer,
|
|
1
|
+
import { Integer, UnixTimestampNumber } from '@naturalcycles/js-lib'
|
|
2
2
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib'
|
|
3
3
|
import { CommonDBCreateOptions } from '../db.model'
|
|
4
4
|
|
|
@@ -29,19 +29,19 @@ export interface CommonKeyValueDB {
|
|
|
29
29
|
*
|
|
30
30
|
* Currently it is NOT required to maintain the same order as input `ids`.
|
|
31
31
|
*/
|
|
32
|
-
getByIds:
|
|
32
|
+
getByIds: (table: string, ids: string[]) => Promise<KeyValueDBTuple[]>
|
|
33
33
|
|
|
34
34
|
deleteByIds: (table: string, ids: string[]) => Promise<void>
|
|
35
35
|
|
|
36
|
-
saveBatch:
|
|
36
|
+
saveBatch: (
|
|
37
37
|
table: string,
|
|
38
|
-
entries:
|
|
38
|
+
entries: KeyValueDBTuple[],
|
|
39
39
|
opt?: CommonKeyValueDBSaveBatchOptions,
|
|
40
40
|
) => Promise<void>
|
|
41
41
|
|
|
42
42
|
streamIds: (table: string, limit?: number) => ReadableTyped<string>
|
|
43
|
-
streamValues:
|
|
44
|
-
streamEntries:
|
|
43
|
+
streamValues: (table: string, limit?: number) => ReadableTyped<Buffer>
|
|
44
|
+
streamEntries: (table: string, limit?: number) => ReadableTyped<KeyValueDBTuple>
|
|
45
45
|
|
|
46
46
|
count: (table: string) => Promise<number>
|
|
47
47
|
|
|
@@ -63,6 +63,8 @@ export interface CommonKeyValueDB {
|
|
|
63
63
|
incrementBatch: (table: string, entries: IncrementTuple[]) => Promise<IncrementTuple[]>
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
+
export type KeyValueDBTuple = [key: string, value: Buffer]
|
|
67
|
+
|
|
66
68
|
export type IncrementTuple = [key: string, value: Integer]
|
|
67
69
|
|
|
68
70
|
export interface CommonKeyValueDBSaveBatchOptions {
|
|
@@ -6,9 +6,10 @@ import {
|
|
|
6
6
|
CommonKeyValueDB,
|
|
7
7
|
CommonKeyValueDBSaveBatchOptions,
|
|
8
8
|
IncrementTuple,
|
|
9
|
+
KeyValueDBTuple,
|
|
9
10
|
} from './commonKeyValueDB'
|
|
10
11
|
|
|
11
|
-
export interface CommonKeyValueDaoCfg<
|
|
12
|
+
export interface CommonKeyValueDaoCfg<V> {
|
|
12
13
|
db: CommonKeyValueDB
|
|
13
14
|
|
|
14
15
|
table: string
|
|
@@ -34,33 +35,33 @@ export interface CommonKeyValueDaoCfg<RAW_V, V = RAW_V> {
|
|
|
34
35
|
*/
|
|
35
36
|
logStarted?: boolean
|
|
36
37
|
|
|
37
|
-
transformer?: CommonKeyValueDaoTransformer<V
|
|
38
|
+
transformer?: CommonKeyValueDaoTransformer<V>
|
|
38
39
|
}
|
|
39
40
|
|
|
40
41
|
export type CommonKeyValueDaoSaveOptions = CommonKeyValueDBSaveBatchOptions
|
|
41
42
|
|
|
42
|
-
export interface CommonKeyValueDaoTransformer<V
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
export interface CommonKeyValueDaoTransformer<V> {
|
|
44
|
+
valueToBuffer: (v: V) => Promise<Buffer>
|
|
45
|
+
bufferToValue: (buf: Buffer) => Promise<V>
|
|
45
46
|
}
|
|
46
47
|
|
|
47
|
-
export const commonKeyValueDaoDeflatedJsonTransformer: CommonKeyValueDaoTransformer<any
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
export const commonKeyValueDaoDeflatedJsonTransformer: CommonKeyValueDaoTransformer<any> = {
|
|
49
|
+
valueToBuffer: async v => await deflateString(JSON.stringify(v)),
|
|
50
|
+
bufferToValue: async buf => JSON.parse(await inflateToString(buf)),
|
|
50
51
|
}
|
|
51
52
|
|
|
52
53
|
// todo: logging
|
|
53
54
|
// todo: readonly
|
|
54
55
|
|
|
55
|
-
export class CommonKeyValueDao<K extends string
|
|
56
|
-
constructor(cfg: CommonKeyValueDaoCfg<
|
|
56
|
+
export class CommonKeyValueDao<K extends string = string, V = Buffer> {
|
|
57
|
+
constructor(cfg: CommonKeyValueDaoCfg<V>) {
|
|
57
58
|
this.cfg = {
|
|
58
59
|
logger: console,
|
|
59
60
|
...cfg,
|
|
60
61
|
}
|
|
61
62
|
}
|
|
62
63
|
|
|
63
|
-
cfg: CommonKeyValueDaoCfg<
|
|
64
|
+
cfg: CommonKeyValueDaoCfg<V> & {
|
|
64
65
|
logger: CommonLogger
|
|
65
66
|
}
|
|
66
67
|
|
|
@@ -78,9 +79,9 @@ export class CommonKeyValueDao<K extends string, RAW_V, V = RAW_V> {
|
|
|
78
79
|
return r?.[1] || null
|
|
79
80
|
}
|
|
80
81
|
|
|
81
|
-
async
|
|
82
|
+
async getByIdAsBuffer(id?: K): Promise<Buffer | null> {
|
|
82
83
|
if (!id) return null
|
|
83
|
-
const [r] = await this.cfg.db.getByIds
|
|
84
|
+
const [r] = await this.cfg.db.getByIds(this.cfg.table, [id])
|
|
84
85
|
return r?.[1] || null
|
|
85
86
|
}
|
|
86
87
|
|
|
@@ -98,8 +99,8 @@ export class CommonKeyValueDao<K extends string, RAW_V, V = RAW_V> {
|
|
|
98
99
|
return r[1]
|
|
99
100
|
}
|
|
100
101
|
|
|
101
|
-
async
|
|
102
|
-
const [r] = await this.cfg.db.getByIds
|
|
102
|
+
async requireByIdAsBuffer(id: K): Promise<Buffer> {
|
|
103
|
+
const [r] = await this.cfg.db.getByIds(this.cfg.table, [id])
|
|
103
104
|
|
|
104
105
|
if (!r) {
|
|
105
106
|
const { table } = this.cfg
|
|
@@ -113,50 +114,39 @@ export class CommonKeyValueDao<K extends string, RAW_V, V = RAW_V> {
|
|
|
113
114
|
}
|
|
114
115
|
|
|
115
116
|
async getByIds(ids: K[]): Promise<KeyValueTuple<string, V>[]> {
|
|
116
|
-
const entries = await this.cfg.db.getByIds
|
|
117
|
+
const entries = await this.cfg.db.getByIds(this.cfg.table, ids)
|
|
117
118
|
if (!this.cfg.transformer) return entries as any
|
|
118
119
|
|
|
119
120
|
return await pMap(entries, async ([id, raw]) => [
|
|
120
121
|
id,
|
|
121
|
-
await this.cfg.transformer!.
|
|
122
|
+
await this.cfg.transformer!.bufferToValue(raw),
|
|
122
123
|
])
|
|
123
124
|
}
|
|
124
125
|
|
|
125
|
-
async
|
|
126
|
-
return
|
|
126
|
+
async getByIdsAsBuffer(ids: K[]): Promise<KeyValueDBTuple[]> {
|
|
127
|
+
return await this.cfg.db.getByIds(this.cfg.table, ids)
|
|
127
128
|
}
|
|
128
129
|
|
|
129
130
|
async save(id: K, value: V, opt?: CommonKeyValueDaoSaveOptions): Promise<void> {
|
|
130
131
|
await this.saveBatch([[id, value]], opt)
|
|
131
132
|
}
|
|
132
133
|
|
|
133
|
-
async saveRaw(id: K, value: RAW_V, opt?: CommonKeyValueDaoSaveOptions): Promise<void> {
|
|
134
|
-
await this.cfg.db.saveBatch(this.cfg.table, [[id, value]], opt)
|
|
135
|
-
}
|
|
136
|
-
|
|
137
134
|
async saveBatch(
|
|
138
135
|
entries: KeyValueTuple<K, V>[],
|
|
139
136
|
opt?: CommonKeyValueDaoSaveOptions,
|
|
140
137
|
): Promise<void> {
|
|
141
138
|
const { transformer } = this.cfg
|
|
142
|
-
let rawEntries:
|
|
139
|
+
let rawEntries: KeyValueDBTuple[]
|
|
143
140
|
|
|
144
141
|
if (!transformer) {
|
|
145
142
|
rawEntries = entries as any
|
|
146
143
|
} else {
|
|
147
|
-
rawEntries = await pMap(entries, async ([id, v]) => [id, await transformer.
|
|
144
|
+
rawEntries = await pMap(entries, async ([id, v]) => [id, await transformer.valueToBuffer(v)])
|
|
148
145
|
}
|
|
149
146
|
|
|
150
147
|
await this.cfg.db.saveBatch(this.cfg.table, rawEntries, opt)
|
|
151
148
|
}
|
|
152
149
|
|
|
153
|
-
async saveBatchRaw(
|
|
154
|
-
entries: KeyValueTuple<K, RAW_V>[],
|
|
155
|
-
opt?: CommonKeyValueDaoSaveOptions,
|
|
156
|
-
): Promise<void> {
|
|
157
|
-
await this.cfg.db.saveBatch(this.cfg.table, entries, opt)
|
|
158
|
-
}
|
|
159
|
-
|
|
160
150
|
async deleteByIds(ids: K[]): Promise<void> {
|
|
161
151
|
await this.cfg.db.deleteByIds(this.cfg.table, ids)
|
|
162
152
|
}
|
|
@@ -173,13 +163,13 @@ export class CommonKeyValueDao<K extends string, RAW_V, V = RAW_V> {
|
|
|
173
163
|
const { transformer } = this.cfg
|
|
174
164
|
|
|
175
165
|
if (!transformer) {
|
|
176
|
-
return this.cfg.db.streamValues
|
|
166
|
+
return this.cfg.db.streamValues(this.cfg.table, limit) as ReadableTyped<V>
|
|
177
167
|
}
|
|
178
168
|
|
|
179
|
-
return this.cfg.db.streamValues
|
|
180
|
-
async
|
|
169
|
+
return this.cfg.db.streamValues(this.cfg.table, limit).flatMap(
|
|
170
|
+
async buf => {
|
|
181
171
|
try {
|
|
182
|
-
return [await transformer.
|
|
172
|
+
return [await transformer.bufferToValue(buf)]
|
|
183
173
|
} catch (err) {
|
|
184
174
|
this.cfg.logger.error(err)
|
|
185
175
|
return [] // SKIP
|
|
@@ -195,15 +185,13 @@ export class CommonKeyValueDao<K extends string, RAW_V, V = RAW_V> {
|
|
|
195
185
|
const { transformer } = this.cfg
|
|
196
186
|
|
|
197
187
|
if (!transformer) {
|
|
198
|
-
return this.cfg.db.streamEntries
|
|
188
|
+
return this.cfg.db.streamEntries(this.cfg.table, limit) as ReadableTyped<KeyValueTuple<K, V>>
|
|
199
189
|
}
|
|
200
190
|
|
|
201
|
-
return (
|
|
202
|
-
|
|
203
|
-
).flatMap(
|
|
204
|
-
async ([id, raw]) => {
|
|
191
|
+
return this.cfg.db.streamEntries(this.cfg.table, limit).flatMap(
|
|
192
|
+
async ([id, buf]) => {
|
|
205
193
|
try {
|
|
206
|
-
return [[id, await transformer.
|
|
194
|
+
return [[id as K, await transformer.bufferToValue(buf)]]
|
|
207
195
|
} catch (err) {
|
|
208
196
|
this.cfg.logger.error(err)
|
|
209
197
|
return [] // SKIP
|
|
@@ -50,7 +50,7 @@ export function runCommonKeyValueDBTest(db: CommonKeyValueDB): void {
|
|
|
50
50
|
test('saveBatch, then getByIds', async () => {
|
|
51
51
|
await db.saveBatch(TEST_TABLE, testEntries)
|
|
52
52
|
|
|
53
|
-
const entries = await db.getByIds
|
|
53
|
+
const entries = await db.getByIds(TEST_TABLE, testIds)
|
|
54
54
|
_sortBy(entries, e => e[0], true)
|
|
55
55
|
expect(entries).toEqual(testEntries)
|
|
56
56
|
})
|
|
@@ -76,26 +76,26 @@ export function runCommonKeyValueDBTest(db: CommonKeyValueDB): void {
|
|
|
76
76
|
})
|
|
77
77
|
|
|
78
78
|
test('streamValues', async () => {
|
|
79
|
-
const values = await db.streamValues
|
|
79
|
+
const values = await db.streamValues(TEST_TABLE).toArray()
|
|
80
80
|
values.sort()
|
|
81
81
|
expect(values).toEqual(testEntries.map(e => e[1]))
|
|
82
82
|
})
|
|
83
83
|
|
|
84
84
|
test('streamValues limited', async () => {
|
|
85
|
-
const valuesLimited = await db.streamValues
|
|
85
|
+
const valuesLimited = await db.streamValues(TEST_TABLE, 2).toArray()
|
|
86
86
|
// valuesLimited.sort()
|
|
87
87
|
// expect(valuesLimited).toEqual(testEntries.map(e => e[1]).slice(0, 2))
|
|
88
88
|
expect(valuesLimited.length).toBe(2)
|
|
89
89
|
})
|
|
90
90
|
|
|
91
91
|
test('streamEntries', async () => {
|
|
92
|
-
const entries = await db.streamEntries
|
|
92
|
+
const entries = await db.streamEntries(TEST_TABLE).toArray()
|
|
93
93
|
entries.sort()
|
|
94
94
|
expect(entries).toEqual(testEntries)
|
|
95
95
|
})
|
|
96
96
|
|
|
97
97
|
test('streamEntries limited', async () => {
|
|
98
|
-
const entriesLimited = await db.streamEntries
|
|
98
|
+
const entriesLimited = await db.streamEntries(TEST_TABLE, 2).toArray()
|
|
99
99
|
// entriesLimited.sort()
|
|
100
100
|
// expect(entriesLimited).toEqual(testEntries.slice(0, 2))
|
|
101
101
|
expect(entriesLimited.length).toBe(2)
|