@naturalcycles/db-lib 10.60.0 → 10.61.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/cachedb/cache.db.d.ts +2 -2
- package/dist/adapter/file/file.db.d.ts +6 -6
- package/dist/adapter/file/file.db.js +2 -2
- package/dist/adapter/file/file.db.model.d.ts +1 -1
- package/dist/adapter/file/inMemory.persistence.plugin.d.ts +1 -1
- package/dist/adapter/file/localFile.persistence.plugin.d.ts +2 -2
- package/dist/adapter/file/noop.persistence.plugin.d.ts +1 -1
- package/dist/commondao/common.dao.d.ts +24 -18
- package/dist/commondao/common.dao.js +43 -28
- package/dist/commondao/commonDaoTransaction.d.ts +3 -3
- package/dist/commondb/base.common.db.d.ts +3 -3
- package/dist/commondb/common.db.d.ts +3 -3
- package/dist/db.model.d.ts +2 -2
- package/dist/inmemory/inMemory.db.d.ts +6 -6
- package/dist/inmemory/inMemoryKeyValueDB.d.ts +4 -4
- package/dist/inmemory/queryInMemory.js +1 -1
- package/dist/kv/commonKeyValueDB.d.ts +4 -4
- package/dist/kv/commonKeyValueDao.d.ts +5 -5
- package/dist/query/dbQuery.d.ts +4 -4
- package/dist/query/dbQuery.js +2 -2
- package/dist/timeseries/commonTimeSeriesDao.d.ts +3 -3
- package/dist/transaction/dbTransaction.util.d.ts +3 -3
- package/package.json +1 -1
- package/src/adapter/cachedb/cache.db.ts +2 -2
- package/src/adapter/file/file.db.model.ts +1 -1
- package/src/adapter/file/file.db.ts +9 -7
- package/src/adapter/file/inMemory.persistence.plugin.ts +1 -1
- package/src/adapter/file/localFile.persistence.plugin.ts +2 -2
- package/src/adapter/file/noop.persistence.plugin.ts +1 -1
- package/src/commondao/common.dao.ts +73 -48
- package/src/commondao/commonDaoTransaction.ts +3 -3
- package/src/commondb/base.common.db.ts +10 -3
- package/src/commondb/common.db.ts +7 -3
- package/src/db.model.ts +2 -2
- package/src/inmemory/inMemory.db.ts +10 -6
- package/src/inmemory/inMemoryKeyValueDB.ts +7 -4
- package/src/inmemory/queryInMemory.ts +5 -5
- package/src/kv/commonKeyValueDB.ts +4 -4
- package/src/kv/commonKeyValueDao.ts +5 -5
- package/src/query/dbQuery.ts +6 -6
- package/src/timeseries/commonTimeSeriesDao.ts +3 -3
- package/src/transaction/dbTransaction.util.ts +3 -3
|
@@ -24,19 +24,19 @@ export class InMemoryKeyValueDB implements CommonKeyValueDB {
|
|
|
24
24
|
|
|
25
25
|
async createTable(_table: string, _opt?: CommonDBCreateOptions): Promise<void> {}
|
|
26
26
|
|
|
27
|
-
async deleteByIds(table: string, ids: string[]): Promise<void> {
|
|
27
|
+
async deleteByIds(table: string, ids: readonly string[]): Promise<void> {
|
|
28
28
|
this.data[table] ||= {}
|
|
29
29
|
for (const id of ids) {
|
|
30
30
|
delete this.data[table][id]
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
async getByIds(table: string, ids: string[]): Promise<KeyValueDBTuple[]> {
|
|
34
|
+
async getByIds(table: string, ids: readonly string[]): Promise<KeyValueDBTuple[]> {
|
|
35
35
|
this.data[table] ||= {}
|
|
36
36
|
return ids.map(id => [id, this.data[table]![id]!] as KeyValueDBTuple).filter(e => e[1])
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
async saveBatch(table: string, entries: KeyValueDBTuple[]): Promise<void> {
|
|
39
|
+
async saveBatch(table: string, entries: readonly KeyValueDBTuple[]): Promise<void> {
|
|
40
40
|
this.data[table] ||= {}
|
|
41
41
|
for (const [id, v] of entries) {
|
|
42
42
|
this.data[table][id] = v
|
|
@@ -60,7 +60,10 @@ export class InMemoryKeyValueDB implements CommonKeyValueDB {
|
|
|
60
60
|
return Object.keys(this.data[table]).length
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
async incrementBatch(
|
|
63
|
+
async incrementBatch(
|
|
64
|
+
table: string,
|
|
65
|
+
entries: readonly IncrementTuple[],
|
|
66
|
+
): Promise<IncrementTuple[]> {
|
|
64
67
|
this.data[table] ||= {}
|
|
65
68
|
|
|
66
69
|
return entries.map(([id, by]) => {
|
|
@@ -11,10 +11,10 @@ const FILTER_FNS: Record<DBQueryFilterOperator, FilterFn> = {
|
|
|
11
11
|
'<=': (v, val) => v <= val,
|
|
12
12
|
'>': (v, val) => v > val,
|
|
13
13
|
'>=': (v, val) => v >= val,
|
|
14
|
-
in: (v, val?: any[]) => (val || []).includes(v),
|
|
15
|
-
'not-in': (v, val?: any[]) => !(val || []).includes(v),
|
|
16
|
-
'array-contains': (v?: any[], val?: any) => (v || []).includes(val),
|
|
17
|
-
'array-contains-any': (v?: any[], val?: any[]) =>
|
|
14
|
+
in: (v, val?: readonly any[]) => (val || []).includes(v),
|
|
15
|
+
'not-in': (v, val?: readonly any[]) => !(val || []).includes(v),
|
|
16
|
+
'array-contains': (v?: readonly any[], val?: any) => (v || []).includes(val),
|
|
17
|
+
'array-contains-any': (v?: readonly any[], val?: readonly any[]) =>
|
|
18
18
|
(v && val && v.some(item => val.includes(item))) || false,
|
|
19
19
|
}
|
|
20
20
|
|
|
@@ -39,7 +39,7 @@ export function queryInMemory<ROW extends ObjectWithId>(q: DBQuery<ROW>, rows: R
|
|
|
39
39
|
const [order] = q._orders
|
|
40
40
|
if (order) {
|
|
41
41
|
const { name, descending } = order
|
|
42
|
-
rows = rows.
|
|
42
|
+
rows = rows.toSorted(
|
|
43
43
|
comparators.by(r => r[name] as string | number, { dir: descending ? 'desc' : 'asc' }),
|
|
44
44
|
)
|
|
45
45
|
}
|
|
@@ -29,13 +29,13 @@ export interface CommonKeyValueDB {
|
|
|
29
29
|
*
|
|
30
30
|
* Currently it is NOT required to maintain the same order as input `ids`.
|
|
31
31
|
*/
|
|
32
|
-
getByIds: (table: string, ids: string[]) => Promise<KeyValueDBTuple[]>
|
|
32
|
+
getByIds: (table: string, ids: readonly string[]) => Promise<KeyValueDBTuple[]>
|
|
33
33
|
|
|
34
|
-
deleteByIds: (table: string, ids: string[]) => Promise<void>
|
|
34
|
+
deleteByIds: (table: string, ids: readonly string[]) => Promise<void>
|
|
35
35
|
|
|
36
36
|
saveBatch: (
|
|
37
37
|
table: string,
|
|
38
|
-
entries: KeyValueDBTuple[],
|
|
38
|
+
entries: readonly KeyValueDBTuple[],
|
|
39
39
|
opt?: CommonKeyValueDBSaveBatchOptions,
|
|
40
40
|
) => Promise<void>
|
|
41
41
|
|
|
@@ -60,7 +60,7 @@ export interface CommonKeyValueDB {
|
|
|
60
60
|
*
|
|
61
61
|
* @experimental
|
|
62
62
|
*/
|
|
63
|
-
incrementBatch: (table: string, entries: IncrementTuple[]) => Promise<IncrementTuple[]>
|
|
63
|
+
incrementBatch: (table: string, entries: readonly IncrementTuple[]) => Promise<IncrementTuple[]>
|
|
64
64
|
}
|
|
65
65
|
|
|
66
66
|
export type KeyValueDBTuple = [key: string, value: Buffer]
|
|
@@ -155,14 +155,14 @@ export class CommonKeyValueDao<K extends string = string, V = Buffer> {
|
|
|
155
155
|
return r[1]
|
|
156
156
|
}
|
|
157
157
|
|
|
158
|
-
async getByIds(ids: K[]): Promise<KeyValueTuple<string, V>[]> {
|
|
158
|
+
async getByIds(ids: readonly K[]): Promise<KeyValueTuple<string, V>[]> {
|
|
159
159
|
const entries = await this.cfg.db.getByIds(this.cfg.table, ids)
|
|
160
160
|
if (!this.cfg.transformer) return entries as any
|
|
161
161
|
|
|
162
162
|
return entries.map(([id, raw]) => [id, this.cfg.transformer!.bufferToValue(raw)])
|
|
163
163
|
}
|
|
164
164
|
|
|
165
|
-
async getByIdsAsBuffer(ids: K[]): Promise<KeyValueDBTuple[]> {
|
|
165
|
+
async getByIdsAsBuffer(ids: readonly K[]): Promise<KeyValueDBTuple[]> {
|
|
166
166
|
return await this.cfg.db.getByIds(this.cfg.table, ids)
|
|
167
167
|
}
|
|
168
168
|
|
|
@@ -171,7 +171,7 @@ export class CommonKeyValueDao<K extends string = string, V = Buffer> {
|
|
|
171
171
|
}
|
|
172
172
|
|
|
173
173
|
async saveBatch(
|
|
174
|
-
entries: KeyValueTuple<K, V>[],
|
|
174
|
+
entries: readonly KeyValueTuple<K, V>[],
|
|
175
175
|
opt?: CommonKeyValueDaoSaveOptions,
|
|
176
176
|
): Promise<void> {
|
|
177
177
|
const { transformer } = this.cfg
|
|
@@ -186,7 +186,7 @@ export class CommonKeyValueDao<K extends string = string, V = Buffer> {
|
|
|
186
186
|
await this.cfg.db.saveBatch(this.cfg.table, rawEntries, opt)
|
|
187
187
|
}
|
|
188
188
|
|
|
189
|
-
async deleteByIds(ids: K[]): Promise<void> {
|
|
189
|
+
async deleteByIds(ids: readonly K[]): Promise<void> {
|
|
190
190
|
await this.cfg.db.deleteByIds(this.cfg.table, ids)
|
|
191
191
|
}
|
|
192
192
|
|
|
@@ -255,7 +255,7 @@ export class CommonKeyValueDao<K extends string = string, V = Buffer> {
|
|
|
255
255
|
return t![1]
|
|
256
256
|
}
|
|
257
257
|
|
|
258
|
-
async incrementBatch(entries: IncrementTuple[]): Promise<IncrementTuple[]> {
|
|
258
|
+
async incrementBatch(entries: readonly IncrementTuple[]): Promise<IncrementTuple[]> {
|
|
259
259
|
return await this.cfg.db.incrementBatch(this.cfg.table, entries)
|
|
260
260
|
}
|
|
261
261
|
}
|
package/src/query/dbQuery.ts
CHANGED
|
@@ -42,7 +42,7 @@ export type DBQueryFilterOperator =
|
|
|
42
42
|
| 'array-contains'
|
|
43
43
|
| 'array-contains-any'
|
|
44
44
|
|
|
45
|
-
export const dbQueryFilterOperatorValues: DBQueryFilterOperator[] = [
|
|
45
|
+
export const dbQueryFilterOperatorValues: readonly DBQueryFilterOperator[] = [
|
|
46
46
|
'<',
|
|
47
47
|
'<=',
|
|
48
48
|
'==',
|
|
@@ -118,7 +118,7 @@ export class DBQuery<ROW extends ObjectWithId> {
|
|
|
118
118
|
return this
|
|
119
119
|
}
|
|
120
120
|
|
|
121
|
-
filterIn(name: keyof ROW, val: any[]): this {
|
|
121
|
+
filterIn(name: keyof ROW, val: readonly any[]): this {
|
|
122
122
|
this._filters.push({ name, op: 'in', val })
|
|
123
123
|
return this
|
|
124
124
|
}
|
|
@@ -144,13 +144,13 @@ export class DBQuery<ROW extends ObjectWithId> {
|
|
|
144
144
|
return this
|
|
145
145
|
}
|
|
146
146
|
|
|
147
|
-
select(fieldNames: (keyof ROW)[]): this {
|
|
148
|
-
this._selectedFieldNames = fieldNames
|
|
147
|
+
select(fieldNames: readonly (keyof ROW)[]): this {
|
|
148
|
+
this._selectedFieldNames = fieldNames.slice()
|
|
149
149
|
return this
|
|
150
150
|
}
|
|
151
151
|
|
|
152
|
-
groupBy(fieldNames: (keyof ROW)[]): this {
|
|
153
|
-
this._groupByFieldNames = fieldNames
|
|
152
|
+
groupBy(fieldNames: readonly (keyof ROW)[]): this {
|
|
153
|
+
this._groupByFieldNames = fieldNames.slice()
|
|
154
154
|
return this
|
|
155
155
|
}
|
|
156
156
|
|
|
@@ -35,7 +35,7 @@ export class CommonTimeSeriesDao {
|
|
|
35
35
|
await this.saveBatch(series, [[tsMillis, value]])
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
async saveBatch(series: string, dataPoints: TimeSeriesDataPoint[]): Promise<void> {
|
|
38
|
+
async saveBatch(series: string, dataPoints: readonly TimeSeriesDataPoint[]): Promise<void> {
|
|
39
39
|
if (!dataPoints.length) return
|
|
40
40
|
|
|
41
41
|
const rows: ObjectWithId[] = dataPoints.map(([ts, v]) => ({
|
|
@@ -50,7 +50,7 @@ export class CommonTimeSeriesDao {
|
|
|
50
50
|
/**
|
|
51
51
|
* All ops are executed as a single CommonDB Transaction.
|
|
52
52
|
*/
|
|
53
|
-
async commitTransaction(ops: TimeSeriesSaveBatchOp[]): Promise<void> {
|
|
53
|
+
async commitTransaction(ops: readonly TimeSeriesSaveBatchOp[]): Promise<void> {
|
|
54
54
|
if (!ops.length) return
|
|
55
55
|
|
|
56
56
|
await this.cfg.db.runInTransaction(async tx => {
|
|
@@ -70,7 +70,7 @@ export class CommonTimeSeriesDao {
|
|
|
70
70
|
await this.deleteByIds(series, [tsMillis])
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
async deleteByIds(series: string, ids: number[]): Promise<void> {
|
|
73
|
+
async deleteByIds(series: string, ids: readonly number[]): Promise<void> {
|
|
74
74
|
// Save to _RAW table with v=null
|
|
75
75
|
await this.saveBatch(
|
|
76
76
|
series,
|
|
@@ -103,7 +103,7 @@ export class FakeDBTransaction implements DBTransaction {
|
|
|
103
103
|
|
|
104
104
|
async getByIds<ROW extends ObjectWithId>(
|
|
105
105
|
table: string,
|
|
106
|
-
ids: string[],
|
|
106
|
+
ids: readonly string[],
|
|
107
107
|
opt?: CommonDBOptions,
|
|
108
108
|
): Promise<ROW[]> {
|
|
109
109
|
return await this.db.getByIds(table, ids, opt)
|
|
@@ -117,13 +117,13 @@ export class FakeDBTransaction implements DBTransaction {
|
|
|
117
117
|
// }
|
|
118
118
|
async saveBatch<ROW extends ObjectWithId>(
|
|
119
119
|
table: string,
|
|
120
|
-
rows: ROW[],
|
|
120
|
+
rows: readonly ROW[],
|
|
121
121
|
opt?: CommonDBSaveOptions<ROW>,
|
|
122
122
|
): Promise<void> {
|
|
123
123
|
await this.db.saveBatch(table, rows, opt)
|
|
124
124
|
}
|
|
125
125
|
|
|
126
|
-
async deleteByIds(table: string, ids: string[], opt?: CommonDBOptions): Promise<number> {
|
|
126
|
+
async deleteByIds(table: string, ids: readonly string[], opt?: CommonDBOptions): Promise<number> {
|
|
127
127
|
return await this.db.deleteByIds(table, ids, opt)
|
|
128
128
|
}
|
|
129
129
|
}
|