From 8bb4ba11c2e3c248ceabbeedec21a5949cacaa95 Mon Sep 17 00:00:00 2001 From: David Fahlander Date: Wed, 24 Apr 2019 14:24:51 +0200 Subject: [PATCH] Implementation of Table.bulkGet(). (#835) Resolves #833 --- src/classes/table/table.ts | 15 +++++++++++++++ src/public/types/table.d.ts | 1 + test/tests-table.js | 27 ++++++++++++++++++++++++++- 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/classes/table/table.ts b/src/classes/table/table.ts index f38dd1e59..da05c4edb 100644 --- a/src/classes/table/table.ts +++ b/src/classes/table/table.ts @@ -358,6 +358,21 @@ export class Table implements ITable { .then(res => res.numFailures ? Promise.reject(res.failures[0]) : undefined); } + /** Table.bulkGet() + * + * http://dexie.org/docs/Table/Table.bulkGet() + * + * @param keys + */ + bulkGet(keys: IndexableType[]) { + return this._trans('readonly', trans => { + return this.core.getMany({ + keys, + trans + }); + }); + } + /** Table.bulkAdd() * * http://dexie.org/docs/Table/Table.bulkAdd() diff --git a/src/public/types/table.d.ts b/src/public/types/table.d.ts index f74f07790..7b9a2a37c 100644 --- a/src/public/types/table.d.ts +++ b/src/public/types/table.d.ts @@ -44,6 +44,7 @@ export interface Table { put(item: T, key?: TKey): PromiseExtended; delete(key: TKey): PromiseExtended; clear(): PromiseExtended; + bulkGet(keys: TKey[]): PromiseExtended; bulkAdd(items: T[], keys?: IndexableTypeArrayReadonly): PromiseExtended; bulkPut(items: T[], keys?: IndexableTypeArrayReadonly): PromiseExtended; bulkDelete(keys: IndexableTypeArrayReadonly) : PromiseExtended; diff --git a/test/tests-table.js b/test/tests-table.js index 790000b81..8d8fc38b3 100644 --- a/test/tests-table.js +++ b/test/tests-table.js @@ -1,6 +1,6 @@ import Dexie from 'dexie'; import {module, stop, start, asyncTest, equal, ok} from 'QUnit'; -import {resetDatabase, supports, spawnedTest} from './dexie-unittest-utils'; +import {resetDatabase, supports, spawnedTest, promisedTest} from './dexie-unittest-utils'; var db = new Dexie("TestDBTable"); db.version(1).stores({ @@ -686,3 +686,28 @@ asyncTest("failNotIncludedStoreTrans", () => { ok(false, "Oops: " + e.stack); }).then(start); }); + +// Must use this rather than QUnit's deepEqual() because that one fails on Safari when run via karma-browserstack-launcher +export function deepEqual(a, b, description) { + equal(JSON.stringify(a, null, 2), JSON.stringify(b, null, 2), description); +} + +promisedTest("bulkGet()", async () => { + const bulkData = []; + for (let i=0; i<400; ++i) { + bulkData.push({id: i, first: "Foo"+i, last: "Bar" + i}); + } + ok(`Putting ${bulkData.length} users into the table`); + await db.users.bulkPut(bulkData); + ok(`Done putting users. Now getting them using bulkGet()`); + const keys = bulkData.map(({id}) => id); + const retrieved = await db.users.bulkGet(keys); + deepEqual(retrieved, bulkData, "Put and retrieved should be the same"); + + ok("Now validating that is should be possible to request nonexisting keys but yet get all results in the order of the given keys"); + const [u1, u2, u3, u4] = await db.users.bulkGet(["x", "y", 100, "z"]); + ok(u1 === undefined, "First result should be undefined, as there where no object with that key"); + ok(u2 === undefined, "Second objects -''-"); + ok(u3 && u3.first === 'Foo100', "Third should be Foo100"); + ok(u4 === undefined, "Forth should be undefined"); +});