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

Commit 62611b7

Browse files
committed
prepub
1 parent d59c30d commit 62611b7

File tree

7 files changed

+69
-53
lines changed

7 files changed

+69
-53
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Place your settings in this file to overwrite default and user settings.
2+
{
3+
}

package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@
22
"name": "@ngrx/db",
33
"version": "1.0.0",
44
"description": "RxJS + IndexedDB for Angular2",
5-
"main": "dist/database.js",
5+
"main": "index.js",
66
"scripts": {
77
"test": "karma start --ci",
8+
"prepare": "npm run clean && npm run build && npm run copy_package",
9+
"copy_package": "cp -r package.json dist/package.json",
810
"watch": "karma start",
9-
"build": "npm run build_test",
11+
"build": "tsc",
1012
"build_test": "tsc -p ./spec",
1113
"clean": "rm -rf dist",
1214
"clean_example": "rm -rf example_dist",
1315
"example": "npm run clean_example && tsc -p ./example && http-server"
1416
},
1517
"author": "Rob Wormald <[email protected]>",
1618
"license": "MIT",
17-
"dependencies": {
18-
"angular2": "^2.0.0-beta.0",
19-
"rxjs": "^5.0.0-beta.0"
19+
"peerDependencies": {
20+
"angular2": "^2.0.0-beta.14",
21+
"rxjs": "^5.0.0-beta.2"
2022
},
2123
"devDependencies": {
2224
"es6-promise": "^3.0.2",

spec/tsconfig.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"outDir": "../tmp",
99
"declaration": true
1010
},
11-
"exclude": [
12-
"node_modules"
11+
"files": [
12+
"../typings/browser.d.ts",
13+
"db_spec.ts"
1314
]
14-
}
15+
}

src/database.ts

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import 'rxjs/Rx';
21
import {Observable} from 'rxjs/Observable';
32
import {Subscriber} from 'rxjs/Subscriber';
43
import {Subject} from 'rxjs/Subject';
54
import {OpaqueToken, Inject, provide} from 'angular2/core';
5+
import 'rxjs/add/operator/mergeMap'
6+
import 'rxjs/add/operator/do'
7+
import 'rxjs/add/operator/toArray'
8+
import 'rxjs/add/observable/fromArray'
69

710
const IDB_SUCCESS = 'success';
811
const IDB_COMPLETE = 'complete';
@@ -35,17 +38,17 @@ export interface DBSchema {
3538
export const getIDBFactory = () => window.indexedDB || self.indexedDB;
3639

3740
export class Database {
38-
41+
3942
public changes:Subject<any> = new Subject();
40-
43+
4144
private _idb:IDBFactory;
4245
private _schema: DBSchema;
4346

4447
constructor(@Inject(DatabaseBackend) idbBackend, @Inject(IDB_SCHEMA) schema){
4548
this._schema = schema;
4649
this._idb = idbBackend;
4750
}
48-
51+
4952
private _upgradeDB(observer, db:IDBDatabase){
5053
for(var storeName in this._schema.stores){
5154
if(db.objectStoreNames.contains(storeName)){
@@ -56,17 +59,17 @@ export class Database {
5659
observer.next(db);
5760
observer.complete();
5861
}
59-
62+
6063
private _createObjectStore(db:IDBDatabase, key:string, schema:DBStore){
6164
let objectStore = db.createObjectStore(key, {autoIncrement: true});
6265
}
63-
66+
6467
open(dbName:string, version:number = 1, upgradeHandler?:DBUpgradeHandler):Observable<IDBDatabase> {
6568
const idb = this._idb;
66-
return new Observable((observer:Subscriber<any>) => {
67-
69+
return Observable.create((observer:Subscriber<any>) => {
70+
6871
const openReq = idb.open(dbName, this._schema.version);
69-
72+
7073
const onSuccess = (event) => {
7174
observer.next(event.target.result);
7275
observer.complete();
@@ -75,90 +78,90 @@ export class Database {
7578
console.log(err);
7679
observer.error(err);
7780
}
78-
81+
7982
const onUpgradeNeeded = (event) => {
8083
this._upgradeDB(observer, event.target.result);
8184
}
82-
85+
8386
openReq.addEventListener(IDB_SUCCESS, onSuccess);
8487
openReq.addEventListener(IDB_ERROR, onError);
8588
openReq.addEventListener(IDB_UPGRADE_NEEDED, onUpgradeNeeded);
86-
89+
8790
return () => {
8891
openReq.removeEventListener(IDB_SUCCESS, onSuccess);
8992
openReq.removeEventListener(IDB_ERROR, onError);
9093
openReq.removeEventListener(IDB_UPGRADE_NEEDED, onUpgradeNeeded);
9194
}
92-
95+
9396
});
9497
}
95-
98+
9699
deleteDatabase(dbName:string){
97100
return new Observable((deletionObserver:Subscriber<any>) => {
98-
101+
99102
const deleteRequest = this._idb.deleteDatabase(dbName);
100-
103+
101104
const onSuccess = (event) => {
102105
deletionObserver.next(null);
103106
deletionObserver.complete();
104107
}
105-
108+
106109
const onError = (err) => deletionObserver.error(err);
107-
110+
108111
deleteRequest.addEventListener(IDB_SUCCESS, onSuccess);
109112
deleteRequest.addEventListener(IDB_ERROR, onError);
110-
113+
111114
return () => {
112115
deleteRequest.removeEventListener(IDB_SUCCESS, onSuccess);
113116
deleteRequest.removeEventListener(IDB_ERROR, onError);
114117
}
115118
})
116119
}
117-
120+
118121
insert(storeName:string, records:any[], notify:boolean = true){
119122
return this.executeWrite(storeName, 'add', records)
120123
.do(payload => notify ? this.changes.next({type: DB_INSERT, payload }) : ({}));
121124
}
122-
125+
123126
get(storeName:string, key:any){
124127
return this.open(this._schema.name)
125128
.mergeMap(db => {
126129
return new Observable(txnObserver => {
127130
const txn = db.transaction([storeName], IDB_TXN_READ);
128131
const objectStore = txn.objectStore(storeName);
129-
132+
130133
const getRequest = objectStore.get(key);
131-
134+
132135
const onTxnError = (err) => txnObserver.error(err);
133136
const onTxnComplete = () => txnObserver.complete();
134137
const onRecordFound = (ev) => txnObserver.next(getRequest.result);
135-
138+
136139
txn.addEventListener(IDB_COMPLETE, onTxnComplete);
137140
txn.addEventListener(IDB_ERROR, onTxnError);
138-
141+
139142
getRequest.addEventListener(IDB_SUCCESS, onRecordFound);
140143
getRequest.addEventListener(IDB_ERROR, onTxnError);
141-
144+
142145
return () => {
143146
getRequest.removeEventListener(IDB_SUCCESS, onRecordFound);
144147
getRequest.removeEventListener(IDB_ERROR, onTxnError);
145148
txn.removeEventListener(IDB_COMPLETE, onTxnComplete);
146149
txn.removeEventListener(IDB_ERROR, onTxnError);
147150
}
148-
151+
149152
});
150153
});
151154
}
152-
155+
153156
query(storeName:string, predicate?:(rec:any) => boolean){
154157
return this.open(this._schema.name)
155158
.mergeMap(db => {
156159
return new Observable(txnObserver => {
157160
const txn = db.transaction([storeName], IDB_TXN_READ);
158161
const objectStore = txn.objectStore(storeName);
159-
162+
160163
const getRequest = objectStore.openCursor();
161-
164+
162165
const onTxnError = (err) => txnObserver.error(err);
163166
const onRecordFound = (ev) => {
164167
let cursor = ev.target.result;
@@ -178,36 +181,36 @@ export class Database {
178181
txnObserver.complete();
179182
}
180183
}
181-
184+
182185
txn.addEventListener(IDB_ERROR, onTxnError);
183-
186+
184187
getRequest.addEventListener(IDB_SUCCESS, onRecordFound);
185188
getRequest.addEventListener(IDB_ERROR, onTxnError);
186-
189+
187190
return () => {
188191
getRequest.removeEventListener(IDB_SUCCESS, onRecordFound);
189192
getRequest.removeEventListener(IDB_ERROR, onTxnError);
190193
txn.removeEventListener(IDB_ERROR, onTxnError);
191194
}
192-
195+
193196
});
194197
});
195198
}
196-
199+
197200
executeWrite(storeName:string, actionType:string, records:any[]){
198201
const changes = this.changes;
199202
return this.open(this._schema.name)
200203
.mergeMap(db => {
201204
return new Observable(txnObserver => {
202205
const txn = db.transaction([storeName], IDB_TXN_READWRITE);
203206
const objectStore = txn.objectStore(storeName);
204-
207+
205208
const onTxnError = (err) => txnObserver.error(err);
206209
const onTxnComplete = () => txnObserver.complete();
207-
210+
208211
txn.addEventListener(IDB_COMPLETE, onTxnComplete);
209212
txn.addEventListener(IDB_ERROR, onTxnError);
210-
213+
211214
const makeRequest = (record) => {
212215
return new Observable(reqObserver => {
213216
let req = objectStore[actionType](record);
@@ -220,11 +223,11 @@ export class Database {
220223
});
221224
});
222225
}
223-
224-
let requestSubscriber = Observable.from(records)
226+
227+
let requestSubscriber = Observable.fromArray(records)
225228
.mergeMap(makeRequest)
226229
.subscribe(txnObserver);
227-
230+
228231
return () => {
229232
requestSubscriber.unsubscribe();
230233
txn.removeEventListener(IDB_COMPLETE, onTxnComplete);
@@ -233,7 +236,7 @@ export class Database {
233236
});
234237
});
235238
}
236-
239+
237240
compare(a:any, b:any):number{
238241
return this._idb.cmp(a, b);
239242
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './database'

tsconfig.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"declaration": true
1010
},
1111
"files": [
12-
"src/database.ts",
13-
"spec/db_spec.ts"
12+
"typings/browser.d.ts",
13+
"src/index.ts",
14+
"src/database.ts"
1415
]
15-
}
16+
}

typings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "@ngrx/db",
3+
"version": false,
4+
"dependencies": {}
5+
}

0 commit comments

Comments
 (0)