Created
March 10, 2018 15:50
-
-
Save jloscos/0adebcd22fd256c0511efdcd4c5ba39a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getByIndex<T>(db: IDBDatabase, storeName: string, indexName: string, key: any): Promise<T[]> { | |
let results = []; | |
return new Promise<T[]>((resolve, reject) => { | |
const transaction = db.transaction(storeName, 'readonly'); | |
const store = transaction.objectStore(storeName); | |
const index = store.index(indexName); | |
//on peut aussi faire index.get si on souhaite lire qu'une seule valeur | |
const request = index.openCursor(key); | |
request.onsuccess = (event: any) => { | |
const cursor = event.target.result; | |
if (cursor) { | |
results.push(cursor.value); | |
cursor.continue(); | |
} else { | |
resolve(results); | |
} | |
}; | |
request.onerror = (event: any) => { | |
reject(event.error); | |
}; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment