@naturalcycles/db-lib 10.43.0 → 10.45.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/commondao/common.dao.d.ts +1 -1
- package/dist/commondao/common.dao.js +1 -1
- package/dist/kv/commonSyncKeyValueDB.d.ts +56 -0
- package/dist/kv/commonSyncKeyValueDB.js +1 -0
- package/dist/kv/index.d.ts +1 -0
- package/dist/kv/index.js +1 -0
- package/dist/testing/commonDaoTest.js +1 -2
- package/dist/testing/test.model.d.ts +2 -2
- package/dist/validation/index.d.ts +1 -1
- package/dist/validation/index.js +2 -2
- package/package.json +2 -2
- package/src/commondao/common.dao.ts +2 -2
- package/src/kv/commonSyncKeyValueDB.ts +74 -0
- package/src/kv/index.ts +1 -0
- package/src/testing/commonDaoTest.ts +1 -2
- package/src/testing/test.model.ts +13 -14
- package/src/validation/index.ts +4 -5
|
@@ -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
|
|
92
|
+
return await this.anyToDBM(row, opt);
|
|
93
93
|
}
|
|
94
94
|
async getByIds(ids, opt = {}) {
|
|
95
95
|
const dbms = await this.loadByIds(ids, opt);
|
|
@@ -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 {};
|
package/dist/kv/index.d.ts
CHANGED
package/dist/kv/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { _sortBy } from '@naturalcycles/js-lib/array/sort.js';
|
|
2
2
|
import { localTime } from '@naturalcycles/js-lib/datetime/localTime.js';
|
|
3
3
|
import { _deepCopy, _filterObject, _omit, _pick } from '@naturalcycles/js-lib/object';
|
|
4
|
-
import { AjvSchema } from '@naturalcycles/nodejs-lib/ajv';
|
|
5
4
|
import { Pipeline } from '@naturalcycles/nodejs-lib/stream';
|
|
6
5
|
import { CommonDao } from '../commondao/common.dao.js';
|
|
7
6
|
import { DBQuery } from '../query/dbQuery.js';
|
|
@@ -13,7 +12,7 @@ export async function runCommonDaoTest(db, quirks = {}) {
|
|
|
13
12
|
const dao = new CommonDao({
|
|
14
13
|
table: TEST_TABLE,
|
|
15
14
|
db,
|
|
16
|
-
validateBM:
|
|
15
|
+
validateBM: testItemBMSchema.getValidationFunction(),
|
|
17
16
|
});
|
|
18
17
|
const items = createTestItemsBM(3);
|
|
19
18
|
const itemsClone = _deepCopy(items);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { BaseDBEntity } from '@naturalcycles/js-lib/types';
|
|
2
|
-
import type {
|
|
2
|
+
import type { JSchema } from '@naturalcycles/nodejs-lib/ajv';
|
|
3
3
|
export declare const TEST_TABLE = "TEST_TABLE";
|
|
4
4
|
export declare const TEST_TABLE_2 = "TEST_TABLE_2";
|
|
5
5
|
export interface TestItemBM extends BaseDBEntity {
|
|
@@ -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:
|
|
22
|
+
export declare const testItemBMSchema: JSchema<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: () =>
|
|
2
|
+
export declare const commonDBOptionsSchema: () => JBuilder<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/dist/validation/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { j,
|
|
1
|
+
import { j, JBuilder } from '@naturalcycles/nodejs-lib/ajv';
|
|
2
2
|
import { dbQueryFilterOperatorValues } from '../query/dbQuery.js';
|
|
3
3
|
// oxlint-disable typescript/explicit-function-return-type
|
|
4
4
|
// DBTransaction schema - validates presence without deep validation
|
|
5
5
|
const dbTransactionSchema = j.object.any().castAs();
|
|
6
6
|
// Schema that accepts any value (string, number, boolean, object, array, null)
|
|
7
|
-
const anyValueSchema = new
|
|
7
|
+
const anyValueSchema = new JBuilder({});
|
|
8
8
|
export const commonDBOptionsSchema = () => j.object({
|
|
9
9
|
tx: dbTransactionSchema.optional(),
|
|
10
10
|
});
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/db-lib",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "10.
|
|
4
|
+
"version": "10.45.0",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@naturalcycles/js-lib": "^15",
|
|
7
7
|
"@naturalcycles/nodejs-lib": "^15"
|
|
8
8
|
},
|
|
9
9
|
"devDependencies": {
|
|
10
10
|
"@typescript/native-preview": "7.0.0-dev.20260201.1",
|
|
11
|
-
"@naturalcycles/dev-lib": "
|
|
11
|
+
"@naturalcycles/dev-lib": "20.36.0"
|
|
12
12
|
},
|
|
13
13
|
"files": [
|
|
14
14
|
"dist",
|
|
@@ -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
|
|
140
|
+
return await this.anyToDBM(row, opt)
|
|
141
141
|
}
|
|
142
142
|
|
|
143
143
|
async getByIds(ids: ID[], opt: CommonDaoReadOptions = {}): Promise<BM[]> {
|
|
@@ -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,7 +1,6 @@
|
|
|
1
1
|
import { _sortBy } from '@naturalcycles/js-lib/array/sort.js'
|
|
2
2
|
import { localTime } from '@naturalcycles/js-lib/datetime/localTime.js'
|
|
3
3
|
import { _deepCopy, _filterObject, _omit, _pick } from '@naturalcycles/js-lib/object'
|
|
4
|
-
import { AjvSchema } from '@naturalcycles/nodejs-lib/ajv'
|
|
5
4
|
import { Pipeline } from '@naturalcycles/nodejs-lib/stream'
|
|
6
5
|
import { CommonDao } from '../commondao/common.dao.js'
|
|
7
6
|
import type { CommonDB } from '../commondb/common.db.js'
|
|
@@ -21,7 +20,7 @@ export async function runCommonDaoTest(
|
|
|
21
20
|
const dao = new CommonDao({
|
|
22
21
|
table: TEST_TABLE,
|
|
23
22
|
db,
|
|
24
|
-
validateBM:
|
|
23
|
+
validateBM: testItemBMSchema.getValidationFunction(),
|
|
25
24
|
})
|
|
26
25
|
|
|
27
26
|
const items = createTestItemsBM(3)
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { _range } from '@naturalcycles/js-lib/array/range.js'
|
|
2
2
|
import type { BaseDBEntity, UnixTimestamp } from '@naturalcycles/js-lib/types'
|
|
3
|
+
import type { JSchema } from '@naturalcycles/nodejs-lib/ajv'
|
|
3
4
|
import { j } from '@naturalcycles/nodejs-lib/ajv'
|
|
4
|
-
import type { JsonSchemaObjectBuilder } from '@naturalcycles/nodejs-lib/ajv'
|
|
5
5
|
|
|
6
6
|
const MOCK_TS_2018_06_21 = 1529539200 as UnixTimestamp
|
|
7
7
|
|
|
@@ -31,19 +31,18 @@ export const testItemTMSchema = j.object<TestItemTM>({
|
|
|
31
31
|
even: j.boolean().optional(),
|
|
32
32
|
})
|
|
33
33
|
|
|
34
|
-
export const testItemBMSchema:
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
})
|
|
34
|
+
export const testItemBMSchema: JSchema<TestItemBM, false> = j.object.dbEntity<TestItemBM>({
|
|
35
|
+
// todo: figure out how to not copy-paste these 3 fields
|
|
36
|
+
id: j.string(), // todo: not strictly needed here
|
|
37
|
+
created: j.number().integer().unixTimestamp(),
|
|
38
|
+
updated: j.number().integer().unixTimestamp(),
|
|
39
|
+
k1: j.string(),
|
|
40
|
+
k2: j.string().nullable().optional(),
|
|
41
|
+
k3: j.number().optional(),
|
|
42
|
+
even: j.boolean().optional(),
|
|
43
|
+
b1: j.buffer().optional(),
|
|
44
|
+
nested: j.object.infer({ foo: j.number() }).optional(),
|
|
45
|
+
})
|
|
47
46
|
|
|
48
47
|
export function createTestItemDBM(num = 1): TestItemDBM {
|
|
49
48
|
return {
|
package/src/validation/index.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import type { ObjectWithId } from '@naturalcycles/js-lib/types'
|
|
2
|
-
import { j,
|
|
3
|
-
import type { JsonSchemaObjectBuilder } from '@naturalcycles/nodejs-lib/ajv'
|
|
2
|
+
import { j, JBuilder } from '@naturalcycles/nodejs-lib/ajv'
|
|
4
3
|
import type { CommonDBOptions, CommonDBSaveOptions, DBTransaction } from '../db.model.js'
|
|
5
|
-
import { dbQueryFilterOperatorValues } from '../query/dbQuery.js'
|
|
6
4
|
import type { DBQueryFilter, DBQueryOrder } from '../query/dbQuery.js'
|
|
5
|
+
import { dbQueryFilterOperatorValues } from '../query/dbQuery.js'
|
|
7
6
|
|
|
8
7
|
// oxlint-disable typescript/explicit-function-return-type
|
|
9
8
|
|
|
@@ -11,9 +10,9 @@ import type { DBQueryFilter, DBQueryOrder } from '../query/dbQuery.js'
|
|
|
11
10
|
const dbTransactionSchema = j.object.any().castAs<DBTransaction>()
|
|
12
11
|
|
|
13
12
|
// Schema that accepts any value (string, number, boolean, object, array, null)
|
|
14
|
-
const anyValueSchema = new
|
|
13
|
+
const anyValueSchema = new JBuilder<any, false>({})
|
|
15
14
|
|
|
16
|
-
export const commonDBOptionsSchema = ():
|
|
15
|
+
export const commonDBOptionsSchema = (): JBuilder<CommonDBOptions, false> =>
|
|
17
16
|
j.object<CommonDBOptions>({
|
|
18
17
|
tx: dbTransactionSchema.optional(),
|
|
19
18
|
})
|