@naturalcycles/db-lib 8.32.0 → 8.33.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/index.d.ts CHANGED
@@ -17,5 +17,6 @@ import { dbPipelineRestore, DBPipelineRestoreOptions } from './pipeline/dbPipeli
17
17
  import { DBQuery, DBQueryFilter, DBQueryFilterOperator, dbQueryFilterOperatorValues, DBQueryOrder, RunnableDBQuery } from './query/dbQuery';
18
18
  import { DBTransaction, RunnableDBTransaction } from './transaction/dbTransaction';
19
19
  import { commitDBTransactionSimple, mergeDBOperations } from './transaction/dbTransaction.util';
20
+ export * from './kv/commonKeyValueDaoMemoCache';
20
21
  export type { DBQueryFilterOperator, DBQueryFilter, DBQueryOrder, CommonDaoCreateOptions, CommonDaoOptions, CommonDaoSaveOptions, CommonDaoStreamForEachOptions, CommonDaoStreamOptions, CommonDBOptions, CommonDBSaveOptions, CommonDBStreamOptions, CommonDBCreateOptions, CommonDB, RunQueryResult, CommonDaoCfg, CommonDaoCreateIdHook, CommonDaoParseNaturalIdHook, CommonDaoBeforeCreateHook, CommonDaoBeforeDBMValidateHook, CommonDaoBeforeDBMToBMHook, CommonDaoBeforeBMToDBMHook, CommonDaoBeforeTMToBMHook, CommonDaoBeforeBMToTMHook, CommonDaoAnonymizeHook, InMemoryDBCfg, InMemoryKeyValueDBCfg, DBPipelineBackupOptions, DBPipelineRestoreOptions, DBPipelineCopyOptions, CommonDBAdapter, DBOperation, DBSaveBatchOperation, DBDeleteByIdsOperation, CommonKeyValueDB, CommonKeyValueDaoCfg, KeyValueDBTuple, };
21
22
  export { DBQuery, dbQueryFilterOperatorValues, RunnableDBQuery, CommonDaoLogLevel, DBRelation, DBModelType, CommonDao, createdUpdatedFields, createdUpdatedIdFields, InMemoryDB, InMemoryKeyValueDB, queryInMemory, serializeJsonField, deserializeJsonField, dbPipelineBackup, dbPipelineRestore, dbPipelineCopy, getDB, DBLibError, BaseCommonDB, DBTransaction, RunnableDBTransaction, mergeDBOperations, commitDBTransactionSimple, CommonKeyValueDao, };
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CommonKeyValueDao = exports.commitDBTransactionSimple = exports.mergeDBOperations = exports.RunnableDBTransaction = exports.DBTransaction = exports.BaseCommonDB = exports.DBLibError = exports.getDB = exports.dbPipelineCopy = exports.dbPipelineRestore = exports.dbPipelineBackup = exports.deserializeJsonField = exports.serializeJsonField = exports.queryInMemory = exports.InMemoryKeyValueDB = exports.InMemoryDB = exports.createdUpdatedIdFields = exports.createdUpdatedFields = exports.CommonDao = exports.DBModelType = exports.DBRelation = exports.CommonDaoLogLevel = exports.RunnableDBQuery = exports.dbQueryFilterOperatorValues = exports.DBQuery = void 0;
4
+ const tslib_1 = require("tslib");
4
5
  const inMemory_db_1 = require("./adapter/inmemory/inMemory.db");
5
6
  Object.defineProperty(exports, "InMemoryDB", { enumerable: true, get: function () { return inMemory_db_1.InMemoryDB; } });
6
7
  const inMemoryKeyValueDB_1 = require("./adapter/inmemory/inMemoryKeyValueDB");
@@ -43,3 +44,4 @@ Object.defineProperty(exports, "RunnableDBTransaction", { enumerable: true, get:
43
44
  const dbTransaction_util_1 = require("./transaction/dbTransaction.util");
44
45
  Object.defineProperty(exports, "commitDBTransactionSimple", { enumerable: true, get: function () { return dbTransaction_util_1.commitDBTransactionSimple; } });
45
46
  Object.defineProperty(exports, "mergeDBOperations", { enumerable: true, get: function () { return dbTransaction_util_1.mergeDBOperations; } });
47
+ (0, tslib_1.__exportStar)(require("./kv/commonKeyValueDaoMemoCache"), exports);
@@ -25,6 +25,12 @@ export interface CommonKeyValueDaoCfg<T> {
25
25
  mapBufferToValue?: (b: Buffer) => Promise<T>;
26
26
  beforeCreate?: (v: Partial<T>) => Partial<T>;
27
27
  };
28
+ /**
29
+ * Set to `true` to conveniently enable zipping+JSON.stringify
30
+ * (and unzipping+JSON.parse) of the Buffer value via hooks.
31
+ * Custom hooks will override these hooks (if provided).
32
+ */
33
+ deflatedJsonValue?: boolean;
28
34
  }
29
35
  export declare class CommonKeyValueDao<T> {
30
36
  cfg: CommonKeyValueDaoCfg<T>;
@@ -9,6 +9,13 @@ const cnst_1 = require("../cnst");
9
9
  class CommonKeyValueDao {
10
10
  constructor(cfg) {
11
11
  this.cfg = cfg;
12
+ if (cfg.deflatedJsonValue) {
13
+ cfg.hooks = {
14
+ mapValueToBuffer: async (v) => await (0, nodejs_lib_1.deflateString)(JSON.stringify(v)),
15
+ mapBufferToValue: async (buf) => JSON.parse(await (0, nodejs_lib_1.inflateToString)(buf)),
16
+ ...cfg.hooks,
17
+ };
18
+ }
12
19
  }
13
20
  async ping() {
14
21
  await this.cfg.db.ping();
@@ -0,0 +1,9 @@
1
+ import { AsyncMemoCache } from '@naturalcycles/js-lib';
2
+ import { CommonKeyValueDao } from './commonKeyValueDao';
3
+ export declare class CommonKeyValueDaoMemoCache<VALUE = any> implements AsyncMemoCache<string, VALUE> {
4
+ private dao;
5
+ constructor(dao: CommonKeyValueDao<VALUE>);
6
+ get(k: string): Promise<VALUE | undefined>;
7
+ set(k: string, v: VALUE): Promise<void>;
8
+ clear(): Promise<void>;
9
+ }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CommonKeyValueDaoMemoCache = void 0;
4
+ class CommonKeyValueDaoMemoCache {
5
+ constructor(dao) {
6
+ this.dao = dao;
7
+ }
8
+ async get(k) {
9
+ return (await this.dao.getById(k)) || undefined;
10
+ }
11
+ async set(k, v) {
12
+ await this.dao.save(k, v);
13
+ }
14
+ async clear() {
15
+ throw new Error('CommonKeyValueDaoCacheFactory.clear is not supported, because cache is expected to be persistent');
16
+ }
17
+ }
18
+ exports.CommonKeyValueDaoMemoCache = CommonKeyValueDaoMemoCache;
package/package.json CHANGED
@@ -43,7 +43,7 @@
43
43
  "engines": {
44
44
  "node": ">=14.15"
45
45
  },
46
- "version": "8.32.0",
46
+ "version": "8.33.0",
47
47
  "description": "Lowest Common Denominator API to supported Databases",
48
48
  "keywords": [
49
49
  "db",
package/src/index.ts CHANGED
@@ -58,6 +58,7 @@ import {
58
58
  } from './query/dbQuery'
59
59
  import { DBTransaction, RunnableDBTransaction } from './transaction/dbTransaction'
60
60
  import { commitDBTransactionSimple, mergeDBOperations } from './transaction/dbTransaction.util'
61
+ export * from './kv/commonKeyValueDaoMemoCache'
61
62
 
62
63
  export type {
63
64
  DBQueryFilterOperator,
@@ -1,5 +1,10 @@
1
1
  import { AppError, ErrorMode, KeyValueTuple, pMap } from '@naturalcycles/js-lib'
2
- import { ReadableTyped, transformMap } from '@naturalcycles/nodejs-lib'
2
+ import {
3
+ deflateString,
4
+ inflateToString,
5
+ ReadableTyped,
6
+ transformMap,
7
+ } from '@naturalcycles/nodejs-lib'
3
8
  import { DBLibError } from '../cnst'
4
9
  import { CommonDaoLogLevel } from '../commondao/common.dao.model'
5
10
  import { CommonDBCreateOptions } from '../db.model'
@@ -31,13 +36,28 @@ export interface CommonKeyValueDaoCfg<T> {
31
36
  mapBufferToValue?: (b: Buffer) => Promise<T>
32
37
  beforeCreate?: (v: Partial<T>) => Partial<T>
33
38
  }
39
+
40
+ /**
41
+ * Set to `true` to conveniently enable zipping+JSON.stringify
42
+ * (and unzipping+JSON.parse) of the Buffer value via hooks.
43
+ * Custom hooks will override these hooks (if provided).
44
+ */
45
+ deflatedJsonValue?: boolean
34
46
  }
35
47
 
36
48
  // todo: logging
37
49
  // todo: readonly
38
50
 
39
51
  export class CommonKeyValueDao<T> {
40
- constructor(public cfg: CommonKeyValueDaoCfg<T>) {}
52
+ constructor(public cfg: CommonKeyValueDaoCfg<T>) {
53
+ if (cfg.deflatedJsonValue) {
54
+ cfg.hooks = {
55
+ mapValueToBuffer: async v => await deflateString(JSON.stringify(v)),
56
+ mapBufferToValue: async buf => JSON.parse(await inflateToString(buf)),
57
+ ...cfg.hooks,
58
+ }
59
+ }
60
+ }
41
61
 
42
62
  async ping(): Promise<void> {
43
63
  await this.cfg.db.ping()
@@ -0,0 +1,20 @@
1
+ import { AsyncMemoCache } from '@naturalcycles/js-lib'
2
+ import { CommonKeyValueDao } from './commonKeyValueDao'
3
+
4
+ export class CommonKeyValueDaoMemoCache<VALUE = any> implements AsyncMemoCache<string, VALUE> {
5
+ constructor(private dao: CommonKeyValueDao<VALUE>) {}
6
+
7
+ async get(k: string): Promise<VALUE | undefined> {
8
+ return (await this.dao.getById(k)) || undefined
9
+ }
10
+
11
+ async set(k: string, v: VALUE): Promise<void> {
12
+ await this.dao.save(k, v)
13
+ }
14
+
15
+ async clear(): Promise<void> {
16
+ throw new Error(
17
+ 'CommonKeyValueDaoCacheFactory.clear is not supported, because cache is expected to be persistent',
18
+ )
19
+ }
20
+ }