From 356ebd2137eb5ca31486ed49ab8223c8256b1554 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Fri, 18 Nov 2016 04:09:04 +0100 Subject: persistent logging --- src/query.ts | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) (limited to 'src/query.ts') diff --git a/src/query.ts b/src/query.ts index f3ce0d764..f8a6255b4 100644 --- a/src/query.ts +++ b/src/query.ts @@ -38,9 +38,9 @@ export interface JoinLeftResult { export class Store { name: string; validator?: (v: T) => T; - storeParams: IDBObjectStoreParameters; + storeParams?: IDBObjectStoreParameters; - constructor(name: string, storeParams: IDBObjectStoreParameters, + constructor(name: string, storeParams?: IDBObjectStoreParameters, validator?: (v: T) => T) { this.name = name; this.validator = validator; @@ -450,6 +450,43 @@ export class QueryRoot implements PromiseLike { return new IterQueryStream(this, store.name, {}); } + count(store: Store): Promise { + const {resolve, promise} = openPromise(); + + const doCount = (tx: IDBTransaction) => { + const s = tx.objectStore(store.name); + const req = s.count(); + req.onsuccess = () => { + resolve(req.result); + }; + } + + this.addWork(doCount, store.name, false); + return Promise.resolve() + .then(() => this.finish()) + .then(() => promise); + + } + + deleteIf(store: Store, predicate: (x: T, n: number) => boolean): QueryRoot { + const doDeleteIf = (tx: IDBTransaction) => { + const s = tx.objectStore(store.name); + const req = s.openCursor(); + let n = 0; + req.onsuccess = () => { + let cursor: IDBCursorWithValue = req.result; + if (cursor) { + if (predicate(cursor.value, n++)) { + cursor.delete(); + } + cursor.continue(); + } + } + }; + this.addWork(doDeleteIf, store.name, true); + return this; + } + iterIndex(index: Index, only?: S): QueryStream { this.stores.add(index.storeName); -- cgit v1.2.3