diff --git a/packages/idb-bridge/src/MemoryBackend.test.ts b/packages/idb-bridge/src/MemoryBackend.test.ts index 2e322dfa6..c21c2d064 100644 --- a/packages/idb-bridge/src/MemoryBackend.test.ts +++ b/packages/idb-bridge/src/MemoryBackend.test.ts @@ -214,6 +214,23 @@ test("Spec: Example 1 Part 3", async t => { cursor = request6.result; t.is(cursor, null); + + const request7 = index5.openCursor(null, "prevunique"); + await promiseFromRequest(request7); + cursor = request7.result; + t.is(cursor.value.author, "Fred"); + t.is(cursor.value.isbn, 234567); + cursor.continue(); + + await promiseFromRequest(request7); + cursor = request7.result; + t.is(cursor.value.author, "Barney"); + cursor.continue(); + + await promiseFromRequest(request7); + cursor = request7.result; + t.is(cursor, null); + db.close(); t.pass(); diff --git a/packages/idb-bridge/src/MemoryBackend.ts b/packages/idb-bridge/src/MemoryBackend.ts index 786fa4cf6..694daacfa 100644 --- a/packages/idb-bridge/src/MemoryBackend.ts +++ b/packages/idb-bridge/src/MemoryBackend.ts @@ -640,7 +640,16 @@ export class MemoryBackend implements Backend { objectStoreProperties.indexes.push(indexName); schema.indexes[indexName] = indexProperties; - // FIXME: build index from existing object store! + const objectStore = myConn.objectStoreMap[objectStoreName]; + if (!objectStore) { + throw Error("object store does not exist"); + } + + const storeData = objectStore.modifiedData || objectStore.originalData; + + storeData.forEach((v, k) => { + this.insertIntoIndex(newIndex, k, v.value, indexProperties); + }); } async deleteRecord( diff --git a/packages/idb-bridge/src/tree/interfaces.ts b/packages/idb-bridge/src/tree/interfaces.ts index c708c20b5..6bd0cdf58 100644 --- a/packages/idb-bridge/src/tree/interfaces.ts +++ b/packages/idb-bridge/src/tree/interfaces.ts @@ -53,7 +53,7 @@ export interface IMapSource extends ISetSource /** Calls callbackFn once for each key-value pair present in the map object. * The ES6 Map class sends the value to the callback before the key, so * this interface must do likewise. */ - forEach(callbackFn: (v:V, k:K, map:IMapSource) => void, thisArg: any): void; + forEach(callbackFn: (v:V, k:K, map:IMapSource) => void, thisArg?: any): void; /** Returns an iterator that provides all key-value pairs from the collection (as arrays of length 2). */ entries(): IterableIterator<[K,V]>;