Skip to content

Commit

Permalink
add clear and fix some typings for aot compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
LFBarreto committed Apr 5, 2017
1 parent 10eed3b commit 6981762
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
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();
}
);
})
});

});
41 changes: 36 additions & 5 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { mergeMap } from 'rxjs/operator/mergeMap';
import { map } from 'rxjs/operator/map';
import { _do } from 'rxjs/operator/do';
import { from } from 'rxjs/observable/from';
import { OpaqueToken, Inject, Injectable, NgModule, ModuleWithProviders } from '@angular/core';
import { OpaqueToken, Inject, NgModule, ModuleWithProviders } from '@angular/core';


const IDB_SUCCESS = 'success';
Expand Down Expand Up @@ -37,19 +37,18 @@ export interface DBSchema {
stores: {[storename: string]: DBStore};
}

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

@Injectable()
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, @Inject(IDB_SCHEMA) schema: DBSchema) {
this._schema = schema;
this._idb = idbBackend;
}
Expand Down Expand Up @@ -270,6 +269,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 6981762

Please sign in to comment.