@naturalcycles/db-lib 8.50.2 → 8.51.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/adapter/file/file.db.d.ts +8 -8
- package/dist/adapter/file/file.db.model.d.ts +4 -4
- package/dist/adapter/file/inMemory.persistence.plugin.d.ts +1 -1
- package/dist/adapter/file/localFile.persistence.plugin.d.ts +1 -1
- package/dist/adapter/file/noop.persistence.plugin.d.ts +1 -1
- package/dist/adapter/inmemory/inMemory.db.d.ts +8 -8
- package/dist/adapter/inmemory/queryInMemory.d.ts +1 -1
- package/dist/common.db.d.ts +12 -12
- package/dist/commondao/common.dao.d.ts +17 -6
- package/dist/commondao/common.dao.js +75 -1
- package/dist/commondao/common.dao.model.d.ts +20 -23
- package/dist/db.model.d.ts +2 -1
- package/dist/db.model.js +1 -0
- package/dist/kv/commonKeyValueDB.d.ts +9 -9
- package/dist/kv/commonKeyValueDao.d.ts +3 -3
- package/dist/query/dbQuery.d.ts +10 -8
- package/dist/query/dbQuery.js +6 -0
- package/dist/testing/daoTest.js +1 -0
- package/package.json +1 -1
- package/src/adapter/file/file.db.model.ts +4 -4
- package/src/adapter/file/file.db.ts +12 -12
- package/src/adapter/file/inMemory.persistence.plugin.ts +1 -1
- package/src/adapter/file/localFile.persistence.plugin.ts +1 -1
- package/src/adapter/file/noop.persistence.plugin.ts +1 -1
- package/src/adapter/inmemory/inMemory.db.ts +11 -11
- package/src/adapter/inmemory/queryInMemory.ts +1 -4
- package/src/common.db.ts +24 -18
- package/src/commondao/common.dao.model.ts +32 -27
- package/src/commondao/common.dao.ts +108 -10
- package/src/db.model.ts +1 -0
- package/src/kv/commonKeyValueDB.ts +9 -9
- package/src/kv/commonKeyValueDao.ts +3 -3
- package/src/query/dbQuery.ts +18 -8
- package/src/testing/daoTest.ts +2 -0
package/src/db.model.ts
CHANGED
|
@@ -12,28 +12,28 @@ export interface CommonKeyValueDB {
|
|
|
12
12
|
/**
|
|
13
13
|
* Check that DB connection is working properly.
|
|
14
14
|
*/
|
|
15
|
-
ping()
|
|
15
|
+
ping: () => Promise<void>
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* Will do like `create table ...` for mysql.
|
|
19
19
|
* Caution! dropIfExists defaults to false. If set to true - will actually DROP the table!
|
|
20
20
|
*/
|
|
21
|
-
createTable(table: string, opt?: CommonDBCreateOptions)
|
|
21
|
+
createTable: (table: string, opt?: CommonDBCreateOptions) => Promise<void>
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
24
|
* Returns an array of tuples [key, value]. Not found values are not returned (no error is thrown).
|
|
25
25
|
*
|
|
26
26
|
* Currently it is NOT required to maintain the same order as input `ids`.
|
|
27
27
|
*/
|
|
28
|
-
getByIds(table: string, ids: string[])
|
|
28
|
+
getByIds: (table: string, ids: string[]) => Promise<KeyValueDBTuple[]>
|
|
29
29
|
|
|
30
|
-
deleteByIds(table: string, ids: string[])
|
|
30
|
+
deleteByIds: (table: string, ids: string[]) => Promise<void>
|
|
31
31
|
|
|
32
|
-
saveBatch(table: string, entries: KeyValueDBTuple[])
|
|
32
|
+
saveBatch: (table: string, entries: KeyValueDBTuple[]) => Promise<void>
|
|
33
33
|
|
|
34
|
-
streamIds(table: string, limit?: number)
|
|
35
|
-
streamValues(table: string, limit?: number)
|
|
36
|
-
streamEntries(table: string, limit?: number)
|
|
34
|
+
streamIds: (table: string, limit?: number) => ReadableTyped<string>
|
|
35
|
+
streamValues: (table: string, limit?: number) => ReadableTyped<Buffer>
|
|
36
|
+
streamEntries: (table: string, limit?: number) => ReadableTyped<KeyValueDBTuple>
|
|
37
37
|
|
|
38
|
-
count(table: string)
|
|
38
|
+
count: (table: string) => Promise<number>
|
|
39
39
|
}
|
|
@@ -32,9 +32,9 @@ export interface CommonKeyValueDaoCfg<T> {
|
|
|
32
32
|
logStarted?: boolean
|
|
33
33
|
|
|
34
34
|
hooks?: {
|
|
35
|
-
mapValueToBuffer
|
|
36
|
-
mapBufferToValue
|
|
37
|
-
beforeCreate
|
|
35
|
+
mapValueToBuffer?: (v: T) => Promise<Buffer>
|
|
36
|
+
mapBufferToValue?: (b: Buffer) => Promise<T>
|
|
37
|
+
beforeCreate?: (v: Partial<T>) => Partial<T>
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
/**
|
package/src/query/dbQuery.ts
CHANGED
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
AsyncMapper,
|
|
5
5
|
_truncate,
|
|
6
6
|
Saved,
|
|
7
|
+
AnyObject,
|
|
7
8
|
_objectAssign,
|
|
8
9
|
} from '@naturalcycles/js-lib'
|
|
9
10
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib'
|
|
@@ -60,13 +61,13 @@ export const dbQueryFilterOperatorValues: DBQueryFilterOperator[] = [
|
|
|
60
61
|
'array-contains-any',
|
|
61
62
|
]
|
|
62
63
|
|
|
63
|
-
export interface DBQueryFilter<ROW extends
|
|
64
|
+
export interface DBQueryFilter<ROW extends ObjectWithId = AnyObjectWithId> {
|
|
64
65
|
name: keyof ROW
|
|
65
66
|
op: DBQueryFilterOperator
|
|
66
67
|
val: any
|
|
67
68
|
}
|
|
68
69
|
|
|
69
|
-
export interface DBQueryOrder<ROW extends
|
|
70
|
+
export interface DBQueryOrder<ROW extends ObjectWithId = AnyObjectWithId> {
|
|
70
71
|
name: keyof ROW
|
|
71
72
|
descending?: boolean
|
|
72
73
|
}
|
|
@@ -81,7 +82,7 @@ export interface DBQueryOrder<ROW extends Partial<ObjectWithId> = AnyObjectWithI
|
|
|
81
82
|
*
|
|
82
83
|
* <DBM> is the type of **queried** object (so e.g `key of DBM` can be used), not **returned** object.
|
|
83
84
|
*/
|
|
84
|
-
export class DBQuery<ROW extends
|
|
85
|
+
export class DBQuery<ROW extends ObjectWithId = AnyObjectWithId> {
|
|
85
86
|
constructor(public table: string) {}
|
|
86
87
|
|
|
87
88
|
/**
|
|
@@ -91,7 +92,7 @@ export class DBQuery<ROW extends Partial<ObjectWithId> = AnyObjectWithId> {
|
|
|
91
92
|
return new DBQuery(table)
|
|
92
93
|
}
|
|
93
94
|
|
|
94
|
-
static fromPlainObject<ROW extends
|
|
95
|
+
static fromPlainObject<ROW extends ObjectWithId = AnyObjectWithId>(
|
|
95
96
|
obj: Partial<DBQuery<ROW>> & { table: string },
|
|
96
97
|
): DBQuery<ROW> {
|
|
97
98
|
return Object.assign(new DBQuery<ROW>(obj.table), obj)
|
|
@@ -235,14 +236,15 @@ export class DBQuery<ROW extends Partial<ObjectWithId> = AnyObjectWithId> {
|
|
|
235
236
|
* DBQuery that has additional method to support Fluent API style.
|
|
236
237
|
*/
|
|
237
238
|
export class RunnableDBQuery<
|
|
238
|
-
BM extends ObjectWithId<ID
|
|
239
|
-
DBM extends ObjectWithId<ID> = BM
|
|
240
|
-
|
|
239
|
+
BM extends Partial<ObjectWithId<ID>>,
|
|
240
|
+
DBM extends ObjectWithId<ID> = Saved<BM>,
|
|
241
|
+
TM extends AnyObject = BM,
|
|
242
|
+
ID extends string | number = string,
|
|
241
243
|
> extends DBQuery<DBM> {
|
|
242
244
|
/**
|
|
243
245
|
* Pass `table` to override table.
|
|
244
246
|
*/
|
|
245
|
-
constructor(public dao: CommonDao<BM, DBM, ID>, table?: string) {
|
|
247
|
+
constructor(public dao: CommonDao<BM, DBM, TM, ID>, table?: string) {
|
|
246
248
|
super(table || dao.cfg.table)
|
|
247
249
|
}
|
|
248
250
|
|
|
@@ -258,6 +260,10 @@ export class RunnableDBQuery<
|
|
|
258
260
|
return await this.dao.runQueryAsDBM(this, opt)
|
|
259
261
|
}
|
|
260
262
|
|
|
263
|
+
async runQueryAsTM(opt?: CommonDaoOptions): Promise<TM[]> {
|
|
264
|
+
return await this.dao.runQueryAsTM(this, opt)
|
|
265
|
+
}
|
|
266
|
+
|
|
261
267
|
async runQueryExtended(opt?: CommonDaoOptions): Promise<RunQueryResult<Saved<BM>>> {
|
|
262
268
|
return await this.dao.runQueryExtended(this, opt)
|
|
263
269
|
}
|
|
@@ -266,6 +272,10 @@ export class RunnableDBQuery<
|
|
|
266
272
|
return await this.dao.runQueryExtendedAsDBM(this, opt)
|
|
267
273
|
}
|
|
268
274
|
|
|
275
|
+
async runQueryExtendedAsTM(opt?: CommonDaoOptions): Promise<RunQueryResult<TM>> {
|
|
276
|
+
return await this.dao.runQueryExtendedAsTM(this, opt)
|
|
277
|
+
}
|
|
278
|
+
|
|
269
279
|
async runQueryCount(opt?: CommonDaoOptions): Promise<number> {
|
|
270
280
|
return await this.dao.runQueryCount(this, opt)
|
|
271
281
|
}
|
package/src/testing/daoTest.ts
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
createTestItemsBM,
|
|
9
9
|
testItemBMSchema,
|
|
10
10
|
testItemDBMSchema,
|
|
11
|
+
testItemTMSchema,
|
|
11
12
|
TEST_TABLE,
|
|
12
13
|
createTestItemBM,
|
|
13
14
|
testItemDBMJsonSchema,
|
|
@@ -24,6 +25,7 @@ export function runCommonDaoTest(
|
|
|
24
25
|
db,
|
|
25
26
|
dbmSchema: testItemDBMSchema,
|
|
26
27
|
bmSchema: testItemBMSchema,
|
|
28
|
+
tmSchema: testItemTMSchema,
|
|
27
29
|
logStarted: true,
|
|
28
30
|
logLevel: CommonDaoLogLevel.DATA_FULL,
|
|
29
31
|
})
|