@naturalcycles/db-lib 10.38.0 → 10.39.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.
@@ -1,5 +1,5 @@
1
1
  import type { CommonLogger } from '@naturalcycles/js-lib/log';
2
- import { type KeyValueTuple } from '@naturalcycles/js-lib/types';
2
+ import { type Integer, type KeyValueTuple } from '@naturalcycles/js-lib/types';
3
3
  import type { Pipeline } from '@naturalcycles/nodejs-lib/stream';
4
4
  import type { CommonDaoLogLevel } from '../commondao/common.dao.model.js';
5
5
  import type { CommonDBCreateOptions } from '../db.model.js';
@@ -31,13 +31,16 @@ export interface CommonKeyValueDaoTransformer<V> {
31
31
  valueToBuffer: (v: V) => Promise<Buffer>;
32
32
  bufferToValue: (buf: Buffer) => Promise<V>;
33
33
  }
34
- export declare const commonKeyValueDaoDeflatedJsonTransformer: CommonKeyValueDaoTransformer<any>;
35
- export declare const commonKeyValueDaoZstdJsonTransformer: CommonKeyValueDaoTransformer<any>;
34
+ /**
35
+ * @deprecated use zstd instead, gzip is obsolete
36
+ */
37
+ export declare function commonKeyValueDaoDeflatedJsonTransformer<T = any>(): CommonKeyValueDaoTransformer<T>;
38
+ export declare function commonKeyValueDaoZstdJsonTransformer<T = any>(level: Integer | undefined): CommonKeyValueDaoTransformer<T>;
36
39
  /**
37
40
  * Saves: zstd
38
41
  * Reads: zstd or deflate (backwards compatible)
39
42
  */
40
- export declare const commonKeyValueDaoCompressedTransformer: CommonKeyValueDaoTransformer<any>;
43
+ export declare function commonKeyValueDaoCompressedTransformer<T = any>(): CommonKeyValueDaoTransformer<T>;
41
44
  export declare class CommonKeyValueDao<K extends string = string, V = Buffer> {
42
45
  constructor(cfg: CommonKeyValueDaoCfg<V>);
43
46
  cfg: CommonKeyValueDaoCfg<V> & {
@@ -2,22 +2,31 @@ import { AppError } from '@naturalcycles/js-lib/error/error.util.js';
2
2
  import { pMap } from '@naturalcycles/js-lib/promise/pMap.js';
3
3
  import { SKIP } from '@naturalcycles/js-lib/types';
4
4
  import { decompressZstdOrInflateToString, deflateString, inflateToString, zstdCompress, zstdDecompressToString, } from '@naturalcycles/nodejs-lib/zip';
5
- export const commonKeyValueDaoDeflatedJsonTransformer = {
6
- valueToBuffer: async (v) => await deflateString(JSON.stringify(v)),
7
- bufferToValue: async (buf) => JSON.parse(await inflateToString(buf)),
8
- };
9
- export const commonKeyValueDaoZstdJsonTransformer = {
10
- valueToBuffer: async (v) => await zstdCompress(JSON.stringify(v)),
11
- bufferToValue: async (buf) => JSON.parse(await zstdDecompressToString(buf)),
12
- };
5
+ /**
6
+ * @deprecated use zstd instead, gzip is obsolete
7
+ */
8
+ export function commonKeyValueDaoDeflatedJsonTransformer() {
9
+ return {
10
+ valueToBuffer: async (v) => await deflateString(JSON.stringify(v)),
11
+ bufferToValue: async (buf) => JSON.parse(await inflateToString(buf)),
12
+ };
13
+ }
14
+ export function commonKeyValueDaoZstdJsonTransformer(level) {
15
+ return {
16
+ valueToBuffer: async (v) => await zstdCompress(JSON.stringify(v), level),
17
+ bufferToValue: async (buf) => JSON.parse(await zstdDecompressToString(buf)),
18
+ };
19
+ }
13
20
  /**
14
21
  * Saves: zstd
15
22
  * Reads: zstd or deflate (backwards compatible)
16
23
  */
17
- export const commonKeyValueDaoCompressedTransformer = {
18
- valueToBuffer: async (v) => await zstdCompress(JSON.stringify(v)),
19
- bufferToValue: async (buf) => JSON.parse(await decompressZstdOrInflateToString(buf)),
20
- };
24
+ export function commonKeyValueDaoCompressedTransformer() {
25
+ return {
26
+ valueToBuffer: async (v) => await zstdCompress(JSON.stringify(v)),
27
+ bufferToValue: async (buf) => JSON.parse(await decompressZstdOrInflateToString(buf)),
28
+ };
29
+ }
21
30
  // todo: logging
22
31
  // todo: readonly
23
32
  export class CommonKeyValueDao {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/db-lib",
3
3
  "type": "module",
4
- "version": "10.38.0",
4
+ "version": "10.39.0",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@naturalcycles/nodejs-lib": "^15"
@@ -1,7 +1,7 @@
1
1
  import { AppError } from '@naturalcycles/js-lib/error/error.util.js'
2
2
  import type { CommonLogger } from '@naturalcycles/js-lib/log'
3
3
  import { pMap } from '@naturalcycles/js-lib/promise/pMap.js'
4
- import { type KeyValueTuple, SKIP } from '@naturalcycles/js-lib/types'
4
+ import { type Integer, type KeyValueTuple, SKIP } from '@naturalcycles/js-lib/types'
5
5
  import type { Pipeline } from '@naturalcycles/nodejs-lib/stream'
6
6
  import {
7
7
  decompressZstdOrInflateToString,
@@ -55,23 +55,36 @@ export interface CommonKeyValueDaoTransformer<V> {
55
55
  bufferToValue: (buf: Buffer) => Promise<V>
56
56
  }
57
57
 
58
- export const commonKeyValueDaoDeflatedJsonTransformer: CommonKeyValueDaoTransformer<any> = {
59
- valueToBuffer: async v => await deflateString(JSON.stringify(v)),
60
- bufferToValue: async buf => JSON.parse(await inflateToString(buf)),
58
+ /**
59
+ * @deprecated use zstd instead, gzip is obsolete
60
+ */
61
+ export function commonKeyValueDaoDeflatedJsonTransformer<
62
+ T = any,
63
+ >(): CommonKeyValueDaoTransformer<T> {
64
+ return {
65
+ valueToBuffer: async v => await deflateString(JSON.stringify(v)),
66
+ bufferToValue: async buf => JSON.parse(await inflateToString(buf)),
67
+ }
61
68
  }
62
69
 
63
- export const commonKeyValueDaoZstdJsonTransformer: CommonKeyValueDaoTransformer<any> = {
64
- valueToBuffer: async v => await zstdCompress(JSON.stringify(v)),
65
- bufferToValue: async buf => JSON.parse(await zstdDecompressToString(buf)),
70
+ export function commonKeyValueDaoZstdJsonTransformer<T = any>(
71
+ level: Integer | undefined, // defaults to 3
72
+ ): CommonKeyValueDaoTransformer<T> {
73
+ return {
74
+ valueToBuffer: async v => await zstdCompress(JSON.stringify(v), level),
75
+ bufferToValue: async buf => JSON.parse(await zstdDecompressToString(buf)),
76
+ }
66
77
  }
67
78
 
68
79
  /**
69
80
  * Saves: zstd
70
81
  * Reads: zstd or deflate (backwards compatible)
71
82
  */
72
- export const commonKeyValueDaoCompressedTransformer: CommonKeyValueDaoTransformer<any> = {
73
- valueToBuffer: async v => await zstdCompress(JSON.stringify(v)),
74
- bufferToValue: async buf => JSON.parse(await decompressZstdOrInflateToString(buf)),
83
+ export function commonKeyValueDaoCompressedTransformer<T = any>(): CommonKeyValueDaoTransformer<T> {
84
+ return {
85
+ valueToBuffer: async v => await zstdCompress(JSON.stringify(v)),
86
+ bufferToValue: async buf => JSON.parse(await decompressZstdOrInflateToString(buf)),
87
+ }
75
88
  }
76
89
 
77
90
  // todo: logging