@naturalcycles/db-lib 10.42.2 → 10.44.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.
@@ -173,8 +173,8 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
173
173
  * Validates (unless `skipValidation=true` passed).
174
174
  */
175
175
  private validateAndConvert;
176
- getTableSchema(): Promise<JsonSchema<any, DBM>>;
177
- createTable(schema: JsonSchema<any, DBM>, opt?: CommonDaoCreateOptions): Promise<void>;
176
+ getTableSchema(): Promise<JsonSchema<DBM>>;
177
+ createTable(schema: JsonSchema<DBM>, opt?: CommonDaoCreateOptions): Promise<void>;
178
178
  /**
179
179
  * Proxy to this.cfg.db.ping
180
180
  */
@@ -201,7 +201,7 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
201
201
  * return dbm
202
202
  * }
203
203
  */
204
- static decompressLegacyRow<T extends ObjectWithId>(row: T): Promise<T>;
204
+ static decompressLegacyRow<T extends ObjectWithId>(this: void, row: T): Promise<T>;
205
205
  /**
206
206
  * Temporary helper to migrate from the old `data` compressed property to the new `__compressed` property.
207
207
  * Use as your `beforeDBMToBM` hook during the migration period.
@@ -89,7 +89,7 @@ export class CommonDao {
89
89
  if (!id)
90
90
  return null;
91
91
  const [row] = await this.loadByIds([id], opt);
92
- return await (this.anyToDBM(row, opt) || null);
92
+ return await this.anyToDBM(row, opt);
93
93
  }
94
94
  async getByIds(ids, opt = {}) {
95
95
  const dbms = await this.loadByIds(ids, opt);
@@ -89,7 +89,7 @@ export interface CommonDaoCfg<BM extends BaseDBEntity, DBM extends BaseDBEntity
89
89
  * It removes the knowledge from CommonDao about the validation library used
90
90
  * and abstracts it away.
91
91
  */
92
- validateBM?: ValidationFunction<BM, BM, any>;
92
+ validateBM?: ValidationFunction<BM, any>;
93
93
  /**
94
94
  * Used by e.g Datastore.
95
95
  */
@@ -0,0 +1,56 @@
1
+ import type { Pipeline } from '@naturalcycles/nodejs-lib/stream';
2
+ import type { CommonDBCreateOptions } from '../db.model.js';
3
+ import type { CommonKeyValueDBSaveBatchOptions, CommonKeyValueDBSupport, IncrementTuple, KeyValueDBTuple } from './commonKeyValueDB.js';
4
+ /**
5
+ * Common interface for Synchronous Key-Value database implementations.
6
+ * Same as CommonKeyValueDB, but with a sync implementation,
7
+ * modeled after e.g SQLite in Node.js, which is designed to be sync.
8
+ *
9
+ * Methods use `Sync` postfix to allow one implementation to support
10
+ * both async and sync CommonKeyValueDB interfaces.
11
+ *
12
+ * @experimental
13
+ */
14
+ export interface CommonSyncKeyValueDB {
15
+ /**
16
+ * Manifest of supported features.
17
+ */
18
+ support: CommonKeyValueDBSupport;
19
+ /**
20
+ * Check that DB connection is working properly.
21
+ */
22
+ pingSync: () => void;
23
+ /**
24
+ * Will do like `create table ...` for mysql.
25
+ * Caution! dropIfExists defaults to false. If set to true - will actually DROP the table!
26
+ */
27
+ createTableSync: (table: string, opt?: CommonDBCreateOptions) => void;
28
+ /**
29
+ * Returns an array of tuples [key, value]. Not found values are not returned (no error is thrown).
30
+ *
31
+ * Currently it is NOT required to maintain the same order as input `ids`.
32
+ */
33
+ getByIdsSync: (table: string, ids: string[]) => KeyValueDBTuple[];
34
+ deleteByIdsSync: (table: string, ids: string[]) => void;
35
+ saveBatchSync: (table: string, entries: KeyValueDBTuple[], opt?: CommonKeyValueDBSaveBatchOptions) => void;
36
+ streamIds: (table: string, limit?: number) => Pipeline<string>;
37
+ streamValues: (table: string, limit?: number) => Pipeline<Buffer>;
38
+ streamEntries: (table: string, limit?: number) => Pipeline<KeyValueDBTuple>;
39
+ countSync: (table: string) => number;
40
+ /**
41
+ * Perform a batch of Increment operations.
42
+ * Given entries array, increment each key of it (1st index of the tuple) by the given amount (2nd index of the tuple).
43
+ *
44
+ * Example:
45
+ * [
46
+ * ['key1', 2],
47
+ * ['key2', 3],
48
+ * ]
49
+ * would increment `key1` by 2, and `key2` by 3.
50
+ *
51
+ * Returns the entries array with tuples of the same structure, with updated numbers.
52
+ *
53
+ * @experimental
54
+ */
55
+ incrementBatchSync: (table: string, entries: IncrementTuple[]) => IncrementTuple[];
56
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -1,3 +1,4 @@
1
1
  export * from './commonKeyValueDao.js';
2
2
  export * from './commonKeyValueDaoMemoCache.js';
3
3
  export * from './commonKeyValueDB.js';
4
+ export * from './commonSyncKeyValueDB.js';
package/dist/kv/index.js CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from './commonKeyValueDao.js';
2
2
  export * from './commonKeyValueDaoMemoCache.js';
3
3
  export * from './commonKeyValueDB.js';
4
+ export * from './commonSyncKeyValueDB.js';
@@ -19,7 +19,7 @@ export interface TestItemTM {
19
19
  even?: boolean;
20
20
  }
21
21
  export declare const testItemTMSchema: any;
22
- export declare const testItemBMSchema: JsonSchemaObjectBuilder<TestItemBM, TestItemBM, false>;
22
+ export declare const testItemBMSchema: JsonSchemaObjectBuilder<TestItemBM, false>;
23
23
  export declare function createTestItemDBM(num?: number): TestItemDBM;
24
24
  export declare function createTestItemBM(num?: number): TestItemBM;
25
25
  export declare function createTestItemsDBM(count?: number): TestItemDBM[];
@@ -1,5 +1,5 @@
1
1
  import type { CommonDBOptions } from '../db.model.js';
2
- export declare const commonDBOptionsSchema: () => JsonSchemaObjectBuilder<CommonDBOptions, CommonDBOptions>;
2
+ export declare const commonDBOptionsSchema: () => JsonSchemaObjectBuilder<CommonDBOptions, false>;
3
3
  export declare const commonDBSaveOptionsSchema: <ROW extends ObjectWithId>() => any;
4
4
  export declare const dbQueryFilterOperatorSchema: any;
5
5
  export declare const dbQueryFilterSchema: <ROW extends ObjectWithId>() => any;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/db-lib",
3
3
  "type": "module",
4
- "version": "10.42.2",
4
+ "version": "10.44.0",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@naturalcycles/nodejs-lib": "^15"
package/readme.md CHANGED
@@ -303,14 +303,6 @@ Passing empty array will actually return an array of empty objects (documented e
303
303
  .select(['id']) //=> [ { id: 'id1' }, { id: 'id2' }, ... ]
304
304
  ```
305
305
 
306
- # Exports
307
-
308
- - `/` root
309
- - `/adapter/file`
310
- - `/adapter/cachedb`
311
- - `/testing`
312
- - dbTest
313
- - daoTest
314
- - Test models, utils, etc
315
- - `/validation`
316
- - Joi validation schemas for DBQuery, CommonDBOptions, CommonSchema, etc
306
+ ## chore-counter
307
+
308
+ 1
@@ -109,7 +109,7 @@ export interface CommonDaoCfg<
109
109
  * It removes the knowledge from CommonDao about the validation library used
110
110
  * and abstracts it away.
111
111
  */
112
- validateBM?: ValidationFunction<BM, BM, any>
112
+ validateBM?: ValidationFunction<BM, any>
113
113
 
114
114
  /**
115
115
  * Used by e.g Datastore.
@@ -137,7 +137,7 @@ export class CommonDao<
137
137
  async getByIdAsDBM(id?: ID | null, opt: CommonDaoReadOptions = {}): Promise<DBM | null> {
138
138
  if (!id) return null
139
139
  const [row] = await this.loadByIds([id], opt)
140
- return await (this.anyToDBM(row, opt) || null)
140
+ return await this.anyToDBM(row, opt)
141
141
  }
142
142
 
143
143
  async getByIds(ids: ID[], opt: CommonDaoReadOptions = {}): Promise<BM[]> {
@@ -926,11 +926,11 @@ export class CommonDao<
926
926
  return convertedValue
927
927
  }
928
928
 
929
- async getTableSchema(): Promise<JsonSchema<any, DBM>> {
929
+ async getTableSchema(): Promise<JsonSchema<DBM>> {
930
930
  return await this.cfg.db.getTableSchema<DBM>(this.cfg.table)
931
931
  }
932
932
 
933
- async createTable(schema: JsonSchema<any, DBM>, opt?: CommonDaoCreateOptions): Promise<void> {
933
+ async createTable(schema: JsonSchema<DBM>, opt?: CommonDaoCreateOptions): Promise<void> {
934
934
  this.requireWriteAccess()
935
935
  await this.cfg.db.createTable(this.cfg.table, schema, opt)
936
936
  }
@@ -989,7 +989,7 @@ export class CommonDao<
989
989
  * return dbm
990
990
  * }
991
991
  */
992
- static async decompressLegacyRow<T extends ObjectWithId>(row: T): Promise<T> {
992
+ static async decompressLegacyRow<T extends ObjectWithId>(this: void, row: T): Promise<T> {
993
993
  // Check both __compressed (current) and data (legacy) for backward compatibility
994
994
  const compressed = (row as any).__compressed ?? (row as any).data
995
995
  if (!Buffer.isBuffer(compressed)) return row
@@ -0,0 +1,74 @@
1
+ import type { Pipeline } from '@naturalcycles/nodejs-lib/stream'
2
+ import type { CommonDBCreateOptions } from '../db.model.js'
3
+ import type {
4
+ CommonKeyValueDBSaveBatchOptions,
5
+ CommonKeyValueDBSupport,
6
+ IncrementTuple,
7
+ KeyValueDBTuple,
8
+ } from './commonKeyValueDB.js'
9
+
10
+ /**
11
+ * Common interface for Synchronous Key-Value database implementations.
12
+ * Same as CommonKeyValueDB, but with a sync implementation,
13
+ * modeled after e.g SQLite in Node.js, which is designed to be sync.
14
+ *
15
+ * Methods use `Sync` postfix to allow one implementation to support
16
+ * both async and sync CommonKeyValueDB interfaces.
17
+ *
18
+ * @experimental
19
+ */
20
+ export interface CommonSyncKeyValueDB {
21
+ /**
22
+ * Manifest of supported features.
23
+ */
24
+ support: CommonKeyValueDBSupport
25
+
26
+ /**
27
+ * Check that DB connection is working properly.
28
+ */
29
+ pingSync: () => void
30
+
31
+ /**
32
+ * Will do like `create table ...` for mysql.
33
+ * Caution! dropIfExists defaults to false. If set to true - will actually DROP the table!
34
+ */
35
+ createTableSync: (table: string, opt?: CommonDBCreateOptions) => void
36
+
37
+ /**
38
+ * Returns an array of tuples [key, value]. Not found values are not returned (no error is thrown).
39
+ *
40
+ * Currently it is NOT required to maintain the same order as input `ids`.
41
+ */
42
+ getByIdsSync: (table: string, ids: string[]) => KeyValueDBTuple[]
43
+
44
+ deleteByIdsSync: (table: string, ids: string[]) => void
45
+
46
+ saveBatchSync: (
47
+ table: string,
48
+ entries: KeyValueDBTuple[],
49
+ opt?: CommonKeyValueDBSaveBatchOptions,
50
+ ) => void
51
+
52
+ streamIds: (table: string, limit?: number) => Pipeline<string>
53
+ streamValues: (table: string, limit?: number) => Pipeline<Buffer>
54
+ streamEntries: (table: string, limit?: number) => Pipeline<KeyValueDBTuple>
55
+
56
+ countSync: (table: string) => number
57
+
58
+ /**
59
+ * Perform a batch of Increment operations.
60
+ * Given entries array, increment each key of it (1st index of the tuple) by the given amount (2nd index of the tuple).
61
+ *
62
+ * Example:
63
+ * [
64
+ * ['key1', 2],
65
+ * ['key2', 3],
66
+ * ]
67
+ * would increment `key1` by 2, and `key2` by 3.
68
+ *
69
+ * Returns the entries array with tuples of the same structure, with updated numbers.
70
+ *
71
+ * @experimental
72
+ */
73
+ incrementBatchSync: (table: string, entries: IncrementTuple[]) => IncrementTuple[]
74
+ }
package/src/kv/index.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from './commonKeyValueDao.js'
2
2
  export * from './commonKeyValueDaoMemoCache.js'
3
3
  export * from './commonKeyValueDB.js'
4
+ export * from './commonSyncKeyValueDB.js'
@@ -31,7 +31,7 @@ export const testItemTMSchema = j.object<TestItemTM>({
31
31
  even: j.boolean().optional(),
32
32
  })
33
33
 
34
- export const testItemBMSchema: JsonSchemaObjectBuilder<TestItemBM, TestItemBM, false> =
34
+ export const testItemBMSchema: JsonSchemaObjectBuilder<TestItemBM, false> =
35
35
  j.object.dbEntity<TestItemBM>({
36
36
  // todo: figure out how to not copy-paste these 3 fields
37
37
  id: j.string(), // todo: not strictly needed here
@@ -11,12 +11,9 @@ import type { DBQueryFilter, DBQueryOrder } from '../query/dbQuery.js'
11
11
  const dbTransactionSchema = j.object.any().castAs<DBTransaction>()
12
12
 
13
13
  // Schema that accepts any value (string, number, boolean, object, array, null)
14
- const anyValueSchema = new JsonSchemaAnyBuilder<any, any, false>({})
14
+ const anyValueSchema = new JsonSchemaAnyBuilder<any, false>({})
15
15
 
16
- export const commonDBOptionsSchema = (): JsonSchemaObjectBuilder<
17
- CommonDBOptions,
18
- CommonDBOptions
19
- > =>
16
+ export const commonDBOptionsSchema = (): JsonSchemaObjectBuilder<CommonDBOptions, false> =>
20
17
  j.object<CommonDBOptions>({
21
18
  tx: dbTransactionSchema.optional(),
22
19
  })