Skip to content

Commit

Permalink
Merge Clear Db
Browse files Browse the repository at this point in the history
See: ngrx#4 and commit:
6981762
  • Loading branch information
LucidityWaver committed Mar 21, 2018
2 parents 056b279 + 6981762 commit 008d93d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ngrx/db",
"version": "2.1.0",
"version": "2.1.1-alpha",
"description": "RxJS + IndexedDB for Angular",
"main": "bundles/db.umd.js",
"module": "index.js",
Expand Down
27 changes: 26 additions & 1 deletion spec/database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,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();
}
);
})
});

});
38 changes: 35 additions & 3 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export interface DBSchema {
stores: {[storename: string]: DBStore};
}

export function getIDBFactory(): IDBFactory {
export function getIDBFactory() {
return typeof window !== 'undefined' ? window.indexedDB : self.indexedDB;
}

Expand All @@ -46,10 +46,10 @@ export class Database {

public changes: Subject<any> = new Subject();

private _idb: IDBFactory;
private _idb;
private _schema: DBSchema;

constructor(@Inject(DatabaseBackend) idbBackend: any, @Inject(IDB_SCHEMA) schema: any) {
constructor(@Inject(DatabaseBackend) idbBackend: any, @Inject(IDB_SCHEMA) schema: DBSchema) {
this._schema = schema;
this._idb = idbBackend;
}
Expand Down Expand Up @@ -270,6 +270,38 @@ export class Database {
compare(a: any, b: any): number {
return this._idb.cmp(a, b);
}

clear(storeName: string) {
const open$ = this.open(this._schema.name);
return mergeMap.call(open$, (db: IDBDatabase) => {
return new Observable( (txnObserver: Observer<any>) => {
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: any) => txnObserver.error(err);
const onTxnComplete = () => txnObserver.complete();
const onClear = () => txnObserver.next(null);

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);
};

});
});
}
}


Expand Down

0 comments on commit 008d93d

Please sign in to comment.