Skip to content

Commit 1200edc

Browse files
author
Ankit Rana
committed
Add the Upgrade mechanism in DbContext
1 parent 5924ba4 commit 1200edc

File tree

10 files changed

+342
-311
lines changed

10 files changed

+342
-311
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace IndexedDB {
2+
export interface IDBUpgradeConfiguration {
3+
Version : number;
4+
UpgradeSetting : (db: IDBDatabase) => void
5+
}
6+
}

IndexedDBDAL/Contract/IDbContext.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
namespace IndexedDB {
22
export interface IDBContext {
3+
Begin(): Promise<any>;
34
Reset(): Promise<any>;
45
Delete(): Promise<any>;
5-
CreateObjectSet(model: string): Promise<IDBObjectStore>
6+
Upgrade : IDBUpgradeConfiguration;
7+
}
8+
export interface IModelConfig {
9+
name: string,
10+
keyPath: any,
11+
autoIncrement : any
612
}
713
}

IndexedDBDAL/Contract/IRepository.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
namespace IndexedDB {
2-
export interface IRepository<TObjectStore extends IDBObjectStore, TKey extends any> {
2+
export interface IRepository<TDBContext extends IDBContext, TKey extends any> {
33
Add(TObject: any): Promise<any>;
44
Update(TObject: any): Promise<any>;
55
Delete(TKey: any): Promise<any>;

IndexedDBDAL/DbContext.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,33 @@
44
namespace IndexedDB {
55
export abstract class DbContext implements IDBContext {
66
private _dbNative: IDBFactory;
7+
private _version = 1;
78
constructor(databaseNative: IDBFactory, public dbName?: string) {
89
this._dbNative = databaseNative;
910
this.dbName = this.dbName || 'SampleDB';
10-
this.Begin();
1111
}
12-
13-
public Begin(): Promise<any> {
12+
public Begin = function () {
1413
var self = this;
15-
var creationRequest = self._dbNative.open(this.dbName);
14+
var creationRequest = self._dbNative.open(this.dbName, this.Upgrade != undefined ? this.Upgrade.Version : this._version);
1615
var promise = Util.CreatePromise();
16+
if (creationRequest.readyState === "done") {
17+
promise.resolve(creationRequest.result);
18+
}
19+
1720
creationRequest.onsuccess = function (event: any) {
1821
promise.resolve(event.target.result);
1922
}
2023

2124
creationRequest.onupgradeneeded = function (event: any) {
2225
var db = event.target.result as IDBDatabase;
2326
self.ModelBuilding(db);
24-
promise.resolve(db);
27+
if (self.Upgrade) {
28+
self.Upgrade.UpgradeSetting.call(self, db);
29+
}
30+
}
31+
creationRequest.onerror = () => {
32+
Util.Log('Error while opening the database');
33+
promise.reject();
2534
}
2635

2736
return promise;
@@ -54,15 +63,19 @@ namespace IndexedDB {
5463
return this.Delete().then(this.Begin);
5564
}
5665

57-
public CreateObjectSet(model: string): Promise<IDBObjectStore> {
58-
var self = this;
59-
return this.Begin()
60-
.then(function (db: IDBDatabase) {
61-
var dbStoreReq = db.createObjectStore(model);
62-
return dbStoreReq;
63-
});
66+
public CreateObjectSet(databse: IDBDatabase, model: IModelConfig): void {
67+
if (!databse.objectStoreNames.contains(model.name)) {
68+
var idbOSConf ={ keyPath: model.keyPath, autoIncrement: true };
69+
if (typeof model.autoIncrement !== 'undefined') {
70+
idbOSConf.autoIncrement = model.autoIncrement;
71+
}
72+
databse.createObjectStore(model.name, idbOSConf);
73+
}
6474
}
6575

6676
protected abstract ModelBuilding(databse: IDBDatabase): void;
77+
78+
public Upgrade: IDBUpgradeConfiguration;
79+
6780
}
6881
}

IndexedDBDAL/Repositories/BaseRepository.ts

Lines changed: 98 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,88 +2,122 @@
22
/// <reference path="../Helpers/Util.ts" />
33

44
namespace IndexedDB {
5-
export class BaseRepository<TObjectStore extends IDBObjectStore, TKey extends string | number> implements IRepository<TObjectStore, TKey>{
6-
private _ObjectStore: IDBObjectStore;
7-
constructor(ObjectStore: TObjectStore) {
8-
this._ObjectStore = ObjectStore;
5+
export class BaseRepository<TDBContext extends IDBContext, TKey extends string | number> implements IRepository<TDBContext, TKey>{
6+
private _DBContext: TDBContext;
7+
private _StoreName: string;
8+
constructor(ObjectStore: TDBContext, StoreName: string) {
9+
this._DBContext = ObjectStore;
10+
this._StoreName = StoreName;
911
}
1012

1113
public Add(TObject: any): Promise<any> {
12-
var promise = Util.CreatePromise();
13-
var creationRequest = this._ObjectStore.add(TObject);
14-
creationRequest.onerror = (event: any) => {
15-
promise.reject();
16-
Util.Log("Fail");
17-
}
18-
creationRequest.onsuccess = (event: any) => {
19-
promise.resolve();
20-
Util.Log("Fail");
21-
}
22-
return promise;
14+
var self = this;
15+
return this._DBContext.Begin().then(function (db: IDBDatabase) {
16+
var promise = Util.CreatePromise();
17+
var transaction = db.transaction([self._StoreName], "readwrite");
18+
var objectStore = transaction.objectStore(self._StoreName);
19+
var request = objectStore.add(TObject);
20+
request.onerror = (event: any) => {
21+
promise.reject();
22+
Util.Log("Fail");
23+
}
24+
transaction.oncomplete = (event: any) => {
25+
promise.resolve();
26+
Util.Log("success");
27+
}
28+
return promise;
29+
}, this.ErrorHandler)
30+
2331
}
2432

2533
public Update(TObject: any): Promise<any> {
26-
var promise = Util.CreatePromise();
27-
var creationRequest = this._ObjectStore.put(TObject);
28-
creationRequest.onerror = (event: any) => {
29-
promise.reject();
30-
Util.Log("Fail");
31-
}
32-
creationRequest.onsuccess = (event: any) => {
33-
promise.resolve();
34-
Util.Log("Fail");
35-
}
36-
return promise;
34+
var self = this;
35+
return this._DBContext.Begin().then(function (db: IDBDatabase) {
36+
var promise = Util.CreatePromise();
37+
var transaction = db.transaction([self._StoreName], "readwrite");
38+
var objectStore = transaction.objectStore(self._StoreName);
39+
objectStore.put(TObject);
40+
transaction.onerror = (event: any) => {
41+
promise.reject();
42+
Util.Log("Fail");
43+
}
44+
transaction.oncomplete = (event: any) => {
45+
promise.resolve();
46+
Util.Log("success");
47+
}
48+
return promise;
49+
}, this.ErrorHandler);
50+
3751
}
3852

3953
public Delete(Key: TKey): Promise<any> {
40-
var promise = Util.CreatePromise();
41-
var deleteRequest = this._ObjectStore.delete(Key);
42-
deleteRequest.onerror = (event: any) => {
43-
promise.reject();
44-
Util.Log("Fail");
45-
}
46-
deleteRequest.onsuccess = (event: any) => {
47-
promise.resolve();
48-
Util.Log("success");
49-
}
50-
return promise;
54+
var self = this;
55+
return this._DBContext.Begin().then(function (db: IDBDatabase) {
56+
var promise = Util.CreatePromise();
57+
var transaction = db.transaction([self._StoreName], "readwrite");
58+
var objectStore = transaction.objectStore(self._StoreName);
59+
objectStore.delete(Key);
60+
transaction.onerror = (event: any) => {
61+
promise.reject();
62+
Util.Log("Fail");
63+
}
64+
transaction.oncomplete = (event: any) => {
65+
promise.resolve();
66+
Util.Log("success");
67+
}
68+
return promise;
69+
}, this.ErrorHandler)
5170
}
5271

5372
public Get(TKey: any): Promise<any> {
54-
var promise = Util.CreatePromise();
55-
var retrievalRequest = this._ObjectStore.get(TKey);
56-
retrievalRequest.onsuccess = function (evt: any) {
57-
var data = evt.target.result;
58-
Util.Log("successfully");
59-
promise.resolve(data);
60-
};
61-
retrievalRequest.onerror = function () {
62-
promise.reject();
63-
Util.Log("Fail");
64-
};
65-
return promise;
73+
var self = this;
74+
return this._DBContext.Begin().then(function (db: IDBDatabase) {
75+
var promise = Util.CreatePromise();
76+
var transaction = db.transaction(self._StoreName, IDBTransaction.READ_ONLY);
77+
var objectStore = transaction.objectStore(self._StoreName);
78+
var retrievalRequest = objectStore.get(TKey);
79+
retrievalRequest.onsuccess = function (evt: any) {
80+
var data = evt.target.result;
81+
Util.Log("successfully");
82+
promise.resolve(data);
83+
};
84+
retrievalRequest.onerror = function () {
85+
promise.reject();
86+
Util.Log("Fail");
87+
};
88+
return promise;
89+
}, this.ErrorHandler)
6690
}
6791

6892
public GetAll(): Promise<any> {
69-
var dbCollection : any = [];
70-
var promise = Util.CreatePromise();
71-
var getAllRequest = this._ObjectStore.openCursor();
72-
getAllRequest.onsuccess = function (evt: any) {
73-
var cursor = evt.target.result;
74-
if (cursor) {
75-
dbCollection.push(cursor.value);
76-
cursor.continue()
77-
} else {
93+
var self = this;
94+
return this._DBContext.Begin().then(function (db: IDBDatabase) {
95+
var dbCollection: any = [];
96+
var promise = Util.CreatePromise();
97+
var transaction = db.transaction(self._StoreName, IDBTransaction.READ_ONLY);
98+
var objectStore = transaction.objectStore(self._StoreName);
99+
objectStore.openCursor().onsuccess = function (evt: any) {
100+
var cursor = evt.target.result;
101+
if (cursor) {
102+
dbCollection.push(cursor.value);
103+
cursor.continue()
104+
}
105+
};
106+
transaction.oncomplete = (event: any) => {
78107
Util.Log("success");
108+
Util.Log(dbCollection);
79109
promise.resolve(dbCollection);
80110
}
81-
};
82-
getAllRequest.onerror = function () {
83-
promise.reject();
84-
Util.Log("Fail");
85-
};
86-
return promise;
111+
transaction.onerror = function () {
112+
promise.reject();
113+
Util.Log("Fail");
114+
};
115+
return promise;
116+
}, this.ErrorHandler)
117+
}
118+
119+
public ErrorHandler(ex: string) {
120+
Util.Log(ex);
87121
}
88122
}
89123
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/// <reference path="../SampleContext.ts" />
22

33
namespace IndexedDB {
4-
export class SampleRepository extends BaseRepository<IDBObjectStore, number>{
5-
4+
export class SampleRepository extends BaseRepository<SampleContext, number>{
5+
constructor(ObjectStore: SampleContext) {
6+
super(ObjectStore, "Sample1");
7+
}
68
}
79
}

IndexedDBDAL/SampleContext.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
namespace IndexedDB {
22
export class SampleContext extends DbContext {
3-
public Sample1: IDBObjectStore;
43
constructor(_dbNative: IDBFactory) {
54
super(_dbNative, "SampleContext");
65
}
7-
86
protected ModelBuilding(database: IDBDatabase) {
9-
this.Sample1 = database.createObjectStore("Sample1")
7+
this.CreateObjectSet(database, { name: "Sample1", keyPath: "id", autoIncrement: true });
8+
this.CreateObjectSet(database, { name: "Sample2", keyPath: "id", autoIncrement: true });
9+
this.CreateObjectSet(database, { name: "Sample3", keyPath: "id", autoIncrement: true });
10+
this.CreateObjectSet(database, { name: "Sample4", keyPath: "id", autoIncrement: true });
1011
}
1112
}
1213
}

0 commit comments

Comments
 (0)