Skip to content

Commit d6db799

Browse files
author
Ankit Rana
committed
working with all existing feature and new UpgradeMapper feature
1 parent 1200edc commit d6db799

File tree

5 files changed

+37
-11
lines changed

5 files changed

+37
-11
lines changed

IndexedDBDAL/Contract/IDbContext.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ namespace IndexedDB {
77
}
88
export interface IModelConfig {
99
name: string,
10-
keyPath: any,
11-
autoIncrement : any
10+
keyPath: string,
11+
autoIncrement : any,
12+
indexes? : IIndexConfig[]
13+
}
14+
export interface IIndexConfig{
15+
name :string ;
16+
keyPath: string,
17+
options: any
1218
}
1319
}

IndexedDBDAL/DbContext.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@ namespace IndexedDB {
55
export abstract class DbContext implements IDBContext {
66
private _dbNative: IDBFactory;
77
private _version = 1;
8+
private _DBPromise: Promise<any>;
89
constructor(databaseNative: IDBFactory, public dbName?: string) {
910
this._dbNative = databaseNative;
1011
this.dbName = this.dbName || 'SampleDB';
1112
}
1213
public Begin = function () {
14+
if (!this._DBPromise) {
15+
this._DBPromise = this.begin();
16+
}
17+
return this._DBPromise;
18+
}
19+
public begin = function () {
1320
var self = this;
1421
var creationRequest = self._dbNative.open(this.dbName, this.Upgrade != undefined ? this.Upgrade.Version : this._version);
1522
var promise = Util.CreatePromise();
@@ -28,9 +35,9 @@ namespace IndexedDB {
2835
self.Upgrade.UpgradeSetting.call(self, db);
2936
}
3037
}
31-
creationRequest.onerror = () => {
38+
creationRequest.onerror = (event: any) => {
3239
Util.Log('Error while opening the database');
33-
promise.reject();
40+
promise.reject(event);
3441
}
3542

3643
return promise;
@@ -65,11 +72,17 @@ namespace IndexedDB {
6572

6673
public CreateObjectSet(databse: IDBDatabase, model: IModelConfig): void {
6774
if (!databse.objectStoreNames.contains(model.name)) {
68-
var idbOSConf ={ keyPath: model.keyPath, autoIncrement: true };
75+
var idbOSConf = { keyPath: model.keyPath, autoIncrement: true };
6976
if (typeof model.autoIncrement !== 'undefined') {
7077
idbOSConf.autoIncrement = model.autoIncrement;
7178
}
72-
databse.createObjectStore(model.name, idbOSConf);
79+
var os = databse.createObjectStore(model.name, idbOSConf);
80+
81+
if ((typeof model.indexes !== 'undefined') && (model.indexes.length > 0)) {
82+
for (var i = 0; i < model.indexes.length; i++) {
83+
os.createIndex(model.indexes[i].name, model.indexes[i].keyPath, model.indexes[i].options);
84+
}
85+
}
7386
}
7487
}
7588

build/indexedDB.js

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)