@naturalcycles/db-lib 9.19.0 → 9.20.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.
@@ -36,37 +36,37 @@ export interface CommonKeyValueDaoCfg<T> {
36
36
  deflatedJsonValue?: boolean;
37
37
  }
38
38
  export type CommonKeyValueDaoSaveOptions = CommonKeyValueDBSaveBatchOptions;
39
- export declare class CommonKeyValueDao<T> {
40
- constructor(cfg: CommonKeyValueDaoCfg<T>);
41
- cfg: CommonKeyValueDaoCfg<T> & {
42
- hooks: NonNullable<CommonKeyValueDaoCfg<T>['hooks']>;
39
+ export declare class CommonKeyValueDao<V, K extends string = string> {
40
+ constructor(cfg: CommonKeyValueDaoCfg<V>);
41
+ cfg: CommonKeyValueDaoCfg<V> & {
42
+ hooks: NonNullable<CommonKeyValueDaoCfg<V>['hooks']>;
43
43
  logger: CommonLogger;
44
44
  };
45
45
  ping(): Promise<void>;
46
46
  createTable(opt?: CommonDBCreateOptions): Promise<void>;
47
- create(input?: Partial<T>): T;
48
- getById(id?: string): Promise<T | null>;
49
- getByIdAsBuffer(id?: string): Promise<Buffer | null>;
50
- requireById(id: string): Promise<T>;
51
- requireByIdAsBuffer(id: string): Promise<Buffer>;
52
- getByIdOrEmpty(id: string, part?: Partial<T>): Promise<T>;
53
- patch(id: string, patch: Partial<T>, opt?: CommonKeyValueDaoSaveOptions): Promise<T>;
54
- getByIds(ids: string[]): Promise<KeyValueTuple<string, T>[]>;
55
- getByIdsAsBuffer(ids: string[]): Promise<KeyValueTuple<string, Buffer>[]>;
56
- save(id: string, value: T, opt?: CommonKeyValueDaoSaveOptions): Promise<void>;
57
- saveAsBuffer(id: string, value: Buffer, opt?: CommonKeyValueDaoSaveOptions): Promise<void>;
58
- saveBatch(entries: KeyValueTuple<string, T>[], opt?: CommonKeyValueDaoSaveOptions): Promise<void>;
47
+ create(input?: Partial<V>): V;
48
+ getById(id?: K): Promise<V | null>;
49
+ getByIdAsBuffer(id?: K): Promise<Buffer | null>;
50
+ requireById(id: K): Promise<V>;
51
+ requireByIdAsBuffer(id: K): Promise<Buffer>;
52
+ getByIdOrEmpty(id: K, part?: Partial<V>): Promise<V>;
53
+ patch(id: K, patch: Partial<V>, opt?: CommonKeyValueDaoSaveOptions): Promise<V>;
54
+ getByIds(ids: K[]): Promise<KeyValueTuple<string, V>[]>;
55
+ getByIdsAsBuffer(ids: K[]): Promise<KeyValueTuple<K, Buffer>[]>;
56
+ save(id: K, value: V, opt?: CommonKeyValueDaoSaveOptions): Promise<void>;
57
+ saveAsBuffer(id: K, value: Buffer, opt?: CommonKeyValueDaoSaveOptions): Promise<void>;
58
+ saveBatch(entries: KeyValueTuple<K, V>[], opt?: CommonKeyValueDaoSaveOptions): Promise<void>;
59
59
  saveBatchAsBuffer(entries: KeyValueDBTuple[], opt?: CommonKeyValueDaoSaveOptions): Promise<void>;
60
- deleteByIds(ids: string[]): Promise<void>;
61
- deleteById(id: string): Promise<void>;
60
+ deleteByIds(ids: K[]): Promise<void>;
61
+ deleteById(id: K): Promise<void>;
62
62
  streamIds(limit?: number): ReadableTyped<string>;
63
- streamValues(limit?: number): ReadableTyped<T>;
64
- streamEntries(limit?: number): ReadableTyped<KeyValueTuple<string, T>>;
63
+ streamValues(limit?: number): ReadableTyped<V>;
64
+ streamEntries(limit?: number): ReadableTyped<KeyValueTuple<K, V>>;
65
65
  /**
66
66
  * Increments the `id` field by the amount specified in `by`,
67
67
  * or by 1 if `by` is not specified.
68
68
  *
69
69
  * Returns the new value of the field.
70
70
  */
71
- increment(id: string, by?: number): Promise<number>;
71
+ increment(id: K, by?: number): Promise<number>;
72
72
  }
@@ -92,7 +92,7 @@ class CommonKeyValueDao {
92
92
  ]);
93
93
  }
94
94
  async getByIdsAsBuffer(ids) {
95
- return await this.cfg.db.getByIds(this.cfg.table, ids);
95
+ return (await this.cfg.db.getByIds(this.cfg.table, ids));
96
96
  }
97
97
  async save(id, value, opt) {
98
98
  await this.saveBatch([[id, value]], opt);
package/package.json CHANGED
@@ -45,7 +45,7 @@
45
45
  "engines": {
46
46
  "node": ">=20.13"
47
47
  },
48
- "version": "9.19.0",
48
+ "version": "9.20.0",
49
49
  "description": "Lowest Common Denominator API to supported Databases",
50
50
  "keywords": [
51
51
  "db",
@@ -53,8 +53,8 @@ export type CommonKeyValueDaoSaveOptions = CommonKeyValueDBSaveBatchOptions
53
53
  // todo: logging
54
54
  // todo: readonly
55
55
 
56
- export class CommonKeyValueDao<T> {
57
- constructor(cfg: CommonKeyValueDaoCfg<T>) {
56
+ export class CommonKeyValueDao<V, K extends string = string> {
57
+ constructor(cfg: CommonKeyValueDaoCfg<V>) {
58
58
  this.cfg = {
59
59
  hooks: {},
60
60
  logger: console,
@@ -70,8 +70,8 @@ export class CommonKeyValueDao<T> {
70
70
  }
71
71
  }
72
72
 
73
- cfg: CommonKeyValueDaoCfg<T> & {
74
- hooks: NonNullable<CommonKeyValueDaoCfg<T>['hooks']>
73
+ cfg: CommonKeyValueDaoCfg<V> & {
74
+ hooks: NonNullable<CommonKeyValueDaoCfg<V>['hooks']>
75
75
  logger: CommonLogger
76
76
  }
77
77
 
@@ -83,25 +83,25 @@ export class CommonKeyValueDao<T> {
83
83
  await this.cfg.db.createTable(this.cfg.table, opt)
84
84
  }
85
85
 
86
- create(input: Partial<T> = {}): T {
86
+ create(input: Partial<V> = {}): V {
87
87
  return {
88
88
  ...this.cfg.hooks.beforeCreate?.(input),
89
- } as T
89
+ } as V
90
90
  }
91
91
 
92
- async getById(id?: string): Promise<T | null> {
92
+ async getById(id?: K): Promise<V | null> {
93
93
  if (!id) return null
94
94
  const [r] = await this.getByIds([id])
95
95
  return r?.[1] || null
96
96
  }
97
97
 
98
- async getByIdAsBuffer(id?: string): Promise<Buffer | null> {
98
+ async getByIdAsBuffer(id?: K): Promise<Buffer | null> {
99
99
  if (!id) return null
100
100
  const [r] = await this.cfg.db.getByIds(this.cfg.table, [id])
101
101
  return r?.[1] || null
102
102
  }
103
103
 
104
- async requireById(id: string): Promise<T> {
104
+ async requireById(id: K): Promise<V> {
105
105
  const [r] = await this.getByIds([id])
106
106
 
107
107
  if (!r) {
@@ -115,7 +115,7 @@ export class CommonKeyValueDao<T> {
115
115
  return r[1]
116
116
  }
117
117
 
118
- async requireByIdAsBuffer(id: string): Promise<Buffer> {
118
+ async requireByIdAsBuffer(id: K): Promise<Buffer> {
119
119
  const [r] = await this.cfg.db.getByIds(this.cfg.table, [id])
120
120
 
121
121
  if (!r) {
@@ -129,18 +129,18 @@ export class CommonKeyValueDao<T> {
129
129
  return r[1]
130
130
  }
131
131
 
132
- async getByIdOrEmpty(id: string, part: Partial<T> = {}): Promise<T> {
132
+ async getByIdOrEmpty(id: K, part: Partial<V> = {}): Promise<V> {
133
133
  const [r] = await this.getByIds([id])
134
134
  if (r) return r[1]
135
135
 
136
136
  return {
137
137
  ...this.cfg.hooks.beforeCreate?.({}),
138
138
  ...part,
139
- } as T
139
+ } as V
140
140
  }
141
141
 
142
- async patch(id: string, patch: Partial<T>, opt?: CommonKeyValueDaoSaveOptions): Promise<T> {
143
- const v: T = {
142
+ async patch(id: K, patch: Partial<V>, opt?: CommonKeyValueDaoSaveOptions): Promise<V> {
143
+ const v: V = {
144
144
  ...(await this.getByIdOrEmpty(id)),
145
145
  ...patch,
146
146
  }
@@ -150,7 +150,7 @@ export class CommonKeyValueDao<T> {
150
150
  return v
151
151
  }
152
152
 
153
- async getByIds(ids: string[]): Promise<KeyValueTuple<string, T>[]> {
153
+ async getByIds(ids: K[]): Promise<KeyValueTuple<string, V>[]> {
154
154
  const entries = await this.cfg.db.getByIds(this.cfg.table, ids)
155
155
  if (!this.cfg.hooks.mapBufferToValue) return entries as any
156
156
 
@@ -160,20 +160,20 @@ export class CommonKeyValueDao<T> {
160
160
  ])
161
161
  }
162
162
 
163
- async getByIdsAsBuffer(ids: string[]): Promise<KeyValueTuple<string, Buffer>[]> {
164
- return await this.cfg.db.getByIds(this.cfg.table, ids)
163
+ async getByIdsAsBuffer(ids: K[]): Promise<KeyValueTuple<K, Buffer>[]> {
164
+ return (await this.cfg.db.getByIds(this.cfg.table, ids)) as KeyValueTuple<K, Buffer>[]
165
165
  }
166
166
 
167
- async save(id: string, value: T, opt?: CommonKeyValueDaoSaveOptions): Promise<void> {
167
+ async save(id: K, value: V, opt?: CommonKeyValueDaoSaveOptions): Promise<void> {
168
168
  await this.saveBatch([[id, value]], opt)
169
169
  }
170
170
 
171
- async saveAsBuffer(id: string, value: Buffer, opt?: CommonKeyValueDaoSaveOptions): Promise<void> {
171
+ async saveAsBuffer(id: K, value: Buffer, opt?: CommonKeyValueDaoSaveOptions): Promise<void> {
172
172
  await this.cfg.db.saveBatch(this.cfg.table, [[id, value]], opt)
173
173
  }
174
174
 
175
175
  async saveBatch(
176
- entries: KeyValueTuple<string, T>[],
176
+ entries: KeyValueTuple<K, V>[],
177
177
  opt?: CommonKeyValueDaoSaveOptions,
178
178
  ): Promise<void> {
179
179
  const { mapValueToBuffer } = this.cfg.hooks
@@ -195,11 +195,11 @@ export class CommonKeyValueDao<T> {
195
195
  await this.cfg.db.saveBatch(this.cfg.table, entries, opt)
196
196
  }
197
197
 
198
- async deleteByIds(ids: string[]): Promise<void> {
198
+ async deleteByIds(ids: K[]): Promise<void> {
199
199
  await this.cfg.db.deleteByIds(this.cfg.table, ids)
200
200
  }
201
201
 
202
- async deleteById(id: string): Promise<void> {
202
+ async deleteById(id: K): Promise<void> {
203
203
  await this.cfg.db.deleteByIds(this.cfg.table, [id])
204
204
  }
205
205
 
@@ -207,11 +207,11 @@ export class CommonKeyValueDao<T> {
207
207
  return this.cfg.db.streamIds(this.cfg.table, limit)
208
208
  }
209
209
 
210
- streamValues(limit?: number): ReadableTyped<T> {
210
+ streamValues(limit?: number): ReadableTyped<V> {
211
211
  const { mapBufferToValue } = this.cfg.hooks
212
212
 
213
213
  if (!mapBufferToValue) {
214
- return this.cfg.db.streamValues(this.cfg.table, limit) as ReadableTyped<T>
214
+ return this.cfg.db.streamValues(this.cfg.table, limit) as ReadableTyped<V>
215
215
  }
216
216
 
217
217
  return this.cfg.db.streamValues(this.cfg.table, limit).flatMap(
@@ -229,19 +229,17 @@ export class CommonKeyValueDao<T> {
229
229
  )
230
230
  }
231
231
 
232
- streamEntries(limit?: number): ReadableTyped<KeyValueTuple<string, T>> {
232
+ streamEntries(limit?: number): ReadableTyped<KeyValueTuple<K, V>> {
233
233
  const { mapBufferToValue } = this.cfg.hooks
234
234
 
235
235
  if (!mapBufferToValue) {
236
- return this.cfg.db.streamEntries(this.cfg.table, limit) as ReadableTyped<
237
- KeyValueTuple<string, T>
238
- >
236
+ return this.cfg.db.streamEntries(this.cfg.table, limit) as ReadableTyped<KeyValueTuple<K, V>>
239
237
  }
240
238
 
241
239
  return this.cfg.db.streamEntries(this.cfg.table, limit).flatMap(
242
240
  async ([id, buf]) => {
243
241
  try {
244
- return [[id, await mapBufferToValue(buf)]]
242
+ return [[id as K, await mapBufferToValue(buf)]]
245
243
  } catch (err) {
246
244
  this.cfg.logger.error(err)
247
245
  return [] // SKIP
@@ -259,7 +257,7 @@ export class CommonKeyValueDao<T> {
259
257
  *
260
258
  * Returns the new value of the field.
261
259
  */
262
- async increment(id: string, by = 1): Promise<number> {
260
+ async increment(id: K, by = 1): Promise<number> {
263
261
  return await this.cfg.db.increment(this.cfg.table, id, by)
264
262
  }
265
263
  }