@bbn/bbn 2.0.240 → 2.0.242

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.
@@ -230,6 +230,74 @@ class DbObject {
230
230
  .map(row => row[field])
231
231
  .filter(v => v !== undefined);
232
232
  }
233
+ async count(table, where = null) {
234
+ const [tx, store] = await this.getStore(table, 'readonly');
235
+ const structure = this.structure[table];
236
+ const primary = getPrimaryKey(structure);
237
+ const searchField = isObject(where)
238
+ ? Object.keys(where)[0]
239
+ : (!where || isArray(where) ? null : primary);
240
+ if (!where) {
241
+ const total = await requestToPromise(store.count());
242
+ await transactionDone(tx);
243
+ return total;
244
+ }
245
+ if (!Array.isArray(primary) && searchField === primary) {
246
+ if (Array.isArray(where?.[primary])) {
247
+ const ids = where[primary];
248
+ let total = 0;
249
+ for (const id of ids) {
250
+ const exists = await requestToPromise(store.getKey(id));
251
+ if (exists !== undefined) {
252
+ total++;
253
+ }
254
+ }
255
+ await transactionDone(tx);
256
+ return total;
257
+ }
258
+ const key = isObject(where)
259
+ ? where[primary]
260
+ : where;
261
+ const total = await requestToPromise(store.count(key));
262
+ await transactionDone(tx);
263
+ return total;
264
+ }
265
+ if (typeof searchField === 'string' &&
266
+ isObject(where) &&
267
+ !Array.isArray(where[searchField]) &&
268
+ store.indexNames.contains(searchField)) {
269
+ const index = store.index(searchField);
270
+ const value = where[searchField];
271
+ const total = await requestToPromise(index.count(IDBKeyRange.only(value)));
272
+ await transactionDone(tx);
273
+ return total;
274
+ }
275
+ let total = 0;
276
+ await new Promise((resolve, reject) => {
277
+ const req = store.openCursor();
278
+ req.onsuccess = (e) => {
279
+ const cursor = e.target.result;
280
+ if (!cursor) {
281
+ resolve();
282
+ return;
283
+ }
284
+ const matches = !where || !globalThis.bbn?.fn?.search
285
+ ? true
286
+ : 0 === globalThis.bbn.fn.search([cursor.value], where);
287
+ if (matches) {
288
+ total++;
289
+ }
290
+ cursor.continue();
291
+ };
292
+ req.onerror = () => {
293
+ this.lastErr = req.error;
294
+ log(req.error);
295
+ reject(req.error);
296
+ };
297
+ });
298
+ await transactionDone(tx);
299
+ return total;
300
+ }
233
301
  async copyTable(target, table, fields = [], where = null, order = null, start = 0, limit = null) {
234
302
  if (!this.structure[table]) {
235
303
  throw new Error(_('Source table %s does not exist in structure', table));
@@ -11,6 +11,7 @@ export default class bbnDbObjectProxy implements DbApi {
11
11
  select(table: string, fields?: string[], where?: unknown, order?: OrderClause, start?: number): Promise<unknown>;
12
12
  selectAll(table: string, fields?: string[], where?: unknown, order?: OrderClause, start?: number, limit?: number | null): Promise<unknown>;
13
13
  getColumnValues(table: string, field: string, where?: unknown, order?: OrderClause, start?: number, limit?: number | null): Promise<unknown>;
14
+ count(table: string, where?: unknown): Promise<number>;
14
15
  copyTable(target: string, table: string, fields?: string[], where?: unknown, order?: OrderClause, start?: number, limit?: number | null): Promise<number>;
15
16
  deleteTable(table: string): Promise<boolean>;
16
17
  close(): void;
@@ -64,6 +64,9 @@ export default class bbnDbObjectProxy {
64
64
  getColumnValues(table, field, where, order, start, limit) {
65
65
  return callServiceWorker(this, 'object.getColumnValues', [table, field, where, order, start, limit]);
66
66
  }
67
+ count(table, where) {
68
+ return callServiceWorker(this, 'object.count', [table, where]);
69
+ }
67
70
  copyTable(target, table, fields, where, order, start, limit) {
68
71
  return callServiceWorker(this, 'object.copyTable', [target, table, fields, where, order, start, limit]);
69
72
  }
@@ -34,6 +34,7 @@ export interface DbApi {
34
34
  select(table: string, fields?: string[], where?: unknown, order?: OrderClause, start?: number): Promise<unknown>;
35
35
  selectAll(table: string, fields?: string[], where?: unknown, order?: OrderClause, start?: number, limit?: number | null): Promise<unknown>;
36
36
  getColumnValues(table: string, field: string, where?: unknown, order?: OrderClause, start?: number, limit?: number | null): Promise<unknown>;
37
+ count(table: string, where?: unknown): Promise<number>;
37
38
  copyTable(target: string, table: string, fields?: string[], where?: unknown, order?: OrderClause, start?: number, limit?: number | null): Promise<number>;
38
39
  deleteTable(table: string): Promise<boolean>;
39
40
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "2.0.240",
3
+ "version": "2.0.242",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",