diff options
author | Florian Dold <florian.dold@gmail.com> | 2016-11-18 04:09:04 +0100 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2016-11-18 04:09:04 +0100 |
commit | 356ebd2137eb5ca31486ed49ab8223c8256b1554 (patch) | |
tree | f3aa2214b928119b3d6e5ba22b8fa348fa929c25 /src/query.ts | |
parent | 2986afb3d4e5f750f34e68cd46e394e2da392a4a (diff) |
persistent logging
Diffstat (limited to 'src/query.ts')
-rw-r--r-- | src/query.ts | 41 |
1 files changed, 39 insertions, 2 deletions
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<L,R> { export class Store<T> { 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<void> { return new IterQueryStream(this, store.name, {}); } + count<T>(store: Store<T>): Promise<number> { + 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<T>(store: Store<T>, 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<S extends IDBValidKey,T>(index: Index<S,T>, only?: S): QueryStream<T> { this.stores.add(index.storeName); |