Skip to content
This repository has been archived by the owner on Jul 27, 2020. It is now read-only.

Commit

Permalink
add ability to clear store
Browse files Browse the repository at this point in the history
  • Loading branch information
nastycoder committed Jun 22, 2016
1 parent 22b78a2 commit 0b0d5e8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
25 changes: 25 additions & 0 deletions spec/db_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,29 @@ describe('database functionality', () => {
});
});

it('should clear store', (done) => {
let found;
idb.clear('todos').subscribe(
() => {},
err => {
console.error(err),
done(err);
},
() => {
idb.query('todos').toArray().subscribe(
(records) => {
found = records;
},
err => {
console.error(err);
done(err);
},
() => {
expect(found.length).toEqual(0);
done();
}
);
})
});

});
32 changes: 32 additions & 0 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,38 @@ export class Database {
compare(a: any, b: any): number {
return this._idb.cmp(a, b);
}

clear(storeName: string) {
return this.open(this._schema.name)
.mergeMap(db => {
return new Observable(txnObserver => {
const recordSchema = this._schema.stores[storeName];
const mapper = this._mapRecord(recordSchema);
const txn = db.transaction([storeName], IDB_TXN_READWRITE);
const objectStore = txn.objectStore(storeName);

const clearRequest = objectStore.clear();

const onTxnError = (err) => txnObserver.error(err);
const onTxnComplete = () => txnObserver.complete();
const onClear = (ev) => txnObserver.next();

txn.addEventListener(IDB_COMPLETE, onTxnComplete);
txn.addEventListener(IDB_ERROR, onTxnError);

clearRequest.addEventListener(IDB_SUCCESS, onClear);
clearRequest.addEventListener(IDB_ERROR, onTxnError);

return () => {
clearRequest.removeEventListener(IDB_SUCCESS, onClear);
clearRequest.removeEventListener(IDB_ERROR, onTxnError);
txn.removeEventListener(IDB_COMPLETE, onTxnComplete);
txn.removeEventListener(IDB_ERROR, onTxnError);
};

});
});
}
}

export const DB_PROVIDERS: any[] = [
Expand Down

0 comments on commit 0b0d5e8

Please sign in to comment.