Skip to content

Commit

Permalink
Implementation of Table.bulkGet(). (dexie#835)
Browse files Browse the repository at this point in the history
Resolves dexie#833
  • Loading branch information
dfahlander authored Apr 24, 2019
1 parent a88298c commit 8bb4ba1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/classes/table/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,21 @@ export class Table implements ITable<any, IndexableType> {
.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()
Expand Down
1 change: 1 addition & 0 deletions src/public/types/table.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export interface Table<T=any, TKey=IndexableType> {
put(item: T, key?: TKey): PromiseExtended<TKey>;
delete(key: TKey): PromiseExtended<void>;
clear(): PromiseExtended<void>;
bulkGet(keys: TKey[]): PromiseExtended<T[]>;
bulkAdd(items: T[], keys?: IndexableTypeArrayReadonly): PromiseExtended<TKey>;
bulkPut(items: T[], keys?: IndexableTypeArrayReadonly): PromiseExtended<TKey>;
bulkDelete(keys: IndexableTypeArrayReadonly) : PromiseExtended<void>;
Expand Down
27 changes: 26 additions & 1 deletion test/tests-table.js
Original file line number Diff line number Diff line change
@@ -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({
Expand Down Expand Up @@ -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");
});

0 comments on commit 8bb4ba1

Please sign in to comment.