@naturalcycles/db-lib 9.22.1 → 9.23.1

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.
@@ -1,7 +1,7 @@
1
- import { KeyValueTuple, StringMap } from '@naturalcycles/js-lib';
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<V>(table: string, ids: string[]): Promise<KeyValueTuple<string, V>[]>;
19
- saveBatch<V>(table: string, entries: KeyValueTuple<string, V>[]): Promise<void>;
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<V>(table: string, limit?: number): ReadableTyped<V>;
22
- streamEntries<V>(table: string, limit?: number): ReadableTyped<KeyValueTuple<string, V>>;
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
  }
@@ -9,7 +9,7 @@ class InMemoryKeyValueDB {
9
9
  this.support = {
10
10
  ...commonKeyValueDB_1.commonKeyValueDBFullSupport,
11
11
  };
12
- // data[table][id] => V
12
+ // data[table][id] => any (can be Buffer, or number)
13
13
  this.data = {};
14
14
  }
15
15
  async ping() { }
@@ -1,4 +1,4 @@
1
- import { Integer, KeyValueTuple, UnixTimestampNumber } from '@naturalcycles/js-lib';
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: <V>(table: string, ids: string[]) => Promise<KeyValueTuple<string, V>[]>;
28
+ getByIds: (table: string, ids: string[]) => Promise<KeyValueDBTuple[]>;
29
29
  deleteByIds: (table: string, ids: string[]) => Promise<void>;
30
- saveBatch: <V>(table: string, entries: KeyValueTuple<string, V>[], opt?: CommonKeyValueDBSaveBatchOptions) => Promise<void>;
30
+ saveBatch: (table: string, entries: KeyValueDBTuple[], opt?: CommonKeyValueDBSaveBatchOptions) => Promise<void>;
31
31
  streamIds: (table: string, limit?: number) => ReadableTyped<string>;
32
- streamValues: <V>(table: string, limit?: number) => ReadableTyped<V>;
33
- streamEntries: <V>(table: string, limit?: number) => ReadableTyped<KeyValueTuple<string, V>>;
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<RAW_V, V = RAW_V> {
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, RAW_V>;
26
+ transformer?: CommonKeyValueDaoTransformer<V>;
27
27
  }
28
28
  export type CommonKeyValueDaoSaveOptions = CommonKeyValueDBSaveBatchOptions;
29
- export interface CommonKeyValueDaoTransformer<V, RAW_V> {
30
- valueToRaw: (v: V) => Promise<RAW_V>;
31
- rawToValue: (raw: RAW_V) => Promise<V>;
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, Buffer>;
34
- export declare class CommonKeyValueDao<K extends string, RAW_V, V = RAW_V> {
35
- constructor(cfg: CommonKeyValueDaoCfg<RAW_V, V>);
36
- cfg: CommonKeyValueDaoCfg<RAW_V, V> & {
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
- getByIdRaw(id?: K): Promise<RAW_V | null>;
42
+ getByIdAsBuffer(id?: K): Promise<Buffer | null>;
43
43
  requireById(id: K): Promise<V>;
44
- requireByIdRaw(id: K): Promise<RAW_V>;
44
+ requireByIdAsBuffer(id: K): Promise<Buffer>;
45
45
  getByIds(ids: K[]): Promise<KeyValueTuple<string, V>[]>;
46
- getByIdsRaw(ids: K[]): Promise<KeyValueTuple<K, RAW_V>[]>;
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
- valueToRaw: async (v) => await (0, nodejs_lib_1.deflateString)(JSON.stringify(v)),
8
- rawToValue: async (raw) => JSON.parse(await (0, nodejs_lib_1.inflateToString)(raw)),
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 getByIdRaw(id) {
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 requireByIdRaw(id) {
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.rawToValue(raw),
65
+ await this.cfg.transformer.bufferToValue(raw),
66
66
  ]);
67
67
  }
68
- async getByIdsRaw(ids) {
69
- return (await this.cfg.db.getByIds(this.cfg.table, ids));
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.valueToRaw(v)]);
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 (raw) => {
99
+ return this.cfg.db.streamValues(this.cfg.table, limit).flatMap(async (buf) => {
106
100
  try {
107
- return [await transformer.rawToValue(raw)];
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, raw]) => {
116
+ return this.cfg.db.streamEntries(this.cfg.table, limit).flatMap(async ([id, buf]) => {
123
117
  try {
124
- return [[id, await transformer.rawToValue(raw)]];
118
+ return [[id, await transformer.bufferToValue(buf)]];
125
119
  }
126
120
  catch (err) {
127
121
  this.cfg.logger.error(err);
@@ -6,7 +6,7 @@ const commonKeyValueDao_1 = require("../kv/commonKeyValueDao");
6
6
  const test_model_1 = require("./test.model");
7
7
  const testItems = (0, test_model_1.createTestItemsBM)(4);
8
8
  const testIds = testItems.map(e => e.id);
9
- const testEntries = testItems.map(e => [e.id, e]);
9
+ const testEntries = testItems.map(e => [e.id, Buffer.from(`${e.id}value`)]);
10
10
  function runCommonKeyValueDaoTest(db) {
11
11
  const dao = new commonKeyValueDao_1.CommonKeyValueDao({
12
12
  db,
package/package.json CHANGED
@@ -45,7 +45,7 @@
45
45
  "engines": {
46
46
  "node": ">=20.13"
47
47
  },
48
- "version": "9.22.1",
48
+ "version": "9.23.1",
49
49
  "description": "Lowest Common Denominator API to supported Databases",
50
50
  "keywords": [
51
51
  "db",
@@ -1,11 +1,12 @@
1
1
  import { Readable } from 'node:stream'
2
- import { KeyValueTuple, StringMap } from '@naturalcycles/js-lib'
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] => V
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<V>(table: string, ids: string[]): Promise<KeyValueTuple<string, V>[]> {
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 KeyValueTuple<string, V>).filter(e => e[1])
35
+ return ids.map(id => [id, this.data[table]![id]!] as KeyValueDBTuple).filter(e => e[1])
35
36
  }
36
37
 
37
- async saveBatch<V>(table: string, entries: KeyValueTuple<string, V>[]): Promise<void> {
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<V>(table: string, limit?: number): ReadableTyped<V> {
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<V>(table: string, limit?: number): ReadableTyped<KeyValueTuple<string, V>> {
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, KeyValueTuple, UnixTimestampNumber } from '@naturalcycles/js-lib'
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: <V>(table: string, ids: string[]) => Promise<KeyValueTuple<string, V>[]>
32
+ getByIds: (table: string, ids: string[]) => Promise<KeyValueDBTuple[]>
33
33
 
34
34
  deleteByIds: (table: string, ids: string[]) => Promise<void>
35
35
 
36
- saveBatch: <V>(
36
+ saveBatch: (
37
37
  table: string,
38
- entries: KeyValueTuple<string, V>[],
38
+ entries: KeyValueDBTuple[],
39
39
  opt?: CommonKeyValueDBSaveBatchOptions,
40
40
  ) => Promise<void>
41
41
 
42
42
  streamIds: (table: string, limit?: number) => ReadableTyped<string>
43
- streamValues: <V>(table: string, limit?: number) => ReadableTyped<V>
44
- streamEntries: <V>(table: string, limit?: number) => ReadableTyped<KeyValueTuple<string, V>>
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<RAW_V, V = RAW_V> {
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, RAW_V>
38
+ transformer?: CommonKeyValueDaoTransformer<V>
38
39
  }
39
40
 
40
41
  export type CommonKeyValueDaoSaveOptions = CommonKeyValueDBSaveBatchOptions
41
42
 
42
- export interface CommonKeyValueDaoTransformer<V, RAW_V> {
43
- valueToRaw: (v: V) => Promise<RAW_V>
44
- rawToValue: (raw: RAW_V) => Promise<V>
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, Buffer> = {
48
- valueToRaw: async v => await deflateString(JSON.stringify(v)),
49
- rawToValue: async raw => JSON.parse(await inflateToString(raw)),
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, RAW_V, V = RAW_V> {
56
- constructor(cfg: CommonKeyValueDaoCfg<RAW_V, V>) {
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<RAW_V, V> & {
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 getByIdRaw(id?: K): Promise<RAW_V | null> {
82
+ async getByIdAsBuffer(id?: K): Promise<Buffer | null> {
82
83
  if (!id) return null
83
- const [r] = await this.cfg.db.getByIds<RAW_V>(this.cfg.table, [id])
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 requireByIdRaw(id: K): Promise<RAW_V> {
102
- const [r] = await this.cfg.db.getByIds<RAW_V>(this.cfg.table, [id])
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<RAW_V>(this.cfg.table, ids)
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!.rawToValue(raw),
122
+ await this.cfg.transformer!.bufferToValue(raw),
122
123
  ])
123
124
  }
124
125
 
125
- async getByIdsRaw(ids: K[]): Promise<KeyValueTuple<K, RAW_V>[]> {
126
- return (await this.cfg.db.getByIds(this.cfg.table, ids)) as KeyValueTuple<K, RAW_V>[]
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: KeyValueTuple<string, RAW_V>[]
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.valueToRaw(v)])
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<V>(this.cfg.table, limit)
166
+ return this.cfg.db.streamValues(this.cfg.table, limit) as ReadableTyped<V>
177
167
  }
178
168
 
179
- return this.cfg.db.streamValues<RAW_V>(this.cfg.table, limit).flatMap(
180
- async raw => {
169
+ return this.cfg.db.streamValues(this.cfg.table, limit).flatMap(
170
+ async buf => {
181
171
  try {
182
- return [await transformer.rawToValue(raw)]
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<V>(this.cfg.table, limit) as any
188
+ return this.cfg.db.streamEntries(this.cfg.table, limit) as ReadableTyped<KeyValueTuple<K, V>>
199
189
  }
200
190
 
201
- return (
202
- this.cfg.db.streamEntries(this.cfg.table, limit) as ReadableTyped<KeyValueTuple<K, RAW_V>>
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.rawToValue(raw)]]
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<Buffer>(TEST_TABLE, testIds)
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<Buffer>(TEST_TABLE).toArray()
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<Buffer>(TEST_TABLE, 2).toArray()
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<Buffer>(TEST_TABLE).toArray()
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<Buffer>(TEST_TABLE, 2).toArray()
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)
@@ -1,14 +1,14 @@
1
- import { _sortBy, KeyValueTuple } from '@naturalcycles/js-lib'
1
+ import { _sortBy } from '@naturalcycles/js-lib'
2
2
  import { CommonKeyValueDao } from '../kv/commonKeyValueDao'
3
- import { CommonKeyValueDB } from '../kv/commonKeyValueDB'
4
- import { createTestItemsBM, TEST_TABLE, TestItemBM } from './test.model'
3
+ import { CommonKeyValueDB, KeyValueDBTuple } from '../kv/commonKeyValueDB'
4
+ import { createTestItemsBM, TEST_TABLE } from './test.model'
5
5
 
6
6
  const testItems = createTestItemsBM(4)
7
7
  const testIds = testItems.map(e => e.id)
8
- const testEntries: KeyValueTuple<string, TestItemBM>[] = testItems.map(e => [e.id, e])
8
+ const testEntries: KeyValueDBTuple[] = testItems.map(e => [e.id, Buffer.from(`${e.id}value`)])
9
9
 
10
10
  export function runCommonKeyValueDaoTest(db: CommonKeyValueDB): void {
11
- const dao = new CommonKeyValueDao<string, TestItemBM>({
11
+ const dao = new CommonKeyValueDao({
12
12
  db,
13
13
  table: TEST_TABLE,
14
14
  // todo: make this test support "deflatedJson" transformer