Skip to content

Commit 471880c

Browse files
author
Ankit Rana
committed
Add the DbContextBuilder
1 parent d6db799 commit 471880c

File tree

13 files changed

+174
-139
lines changed

13 files changed

+174
-139
lines changed

IndexedDBDAL/Contract/IDbContext.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@ namespace IndexedDB {
33
Begin(): Promise<any>;
44
Reset(): Promise<any>;
55
Delete(): Promise<any>;
6-
Upgrade : IDBUpgradeConfiguration;
6+
Upgrade: IDBUpgradeConfiguration;
77
}
88
export interface IModelConfig {
99
name: string,
1010
keyPath: string,
11-
autoIncrement : any,
12-
indexes? : IIndexConfig[]
11+
autoIncrement: any,
12+
indexes?: IIndexConfig[],
13+
upgradeMapping?: (model: any) => void;
1314
}
14-
export interface IIndexConfig{
15-
name :string ;
15+
export interface IIndexConfig {
16+
name: string;
1617
keyPath: string,
1718
options: any
1819
}

IndexedDBDAL/DbContext.ts

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

44
namespace IndexedDB {
5-
export abstract class DbContext implements IDBContext {
5+
export class DbContext implements IDBContext {
66
private _dbNative: IDBFactory;
77
private _version = 1;
88
private _DBPromise: Promise<any>;
@@ -30,7 +30,9 @@ namespace IndexedDB {
3030

3131
creationRequest.onupgradeneeded = function (event: any) {
3232
var db = event.target.result as IDBDatabase;
33-
self.ModelBuilding(db);
33+
if (self.ModelBuilding) {
34+
self.ModelBuilding(db);
35+
}
3436
if (self.Upgrade) {
3537
self.Upgrade.UpgradeSetting.call(self, db);
3638
}
@@ -83,12 +85,13 @@ namespace IndexedDB {
8385
os.createIndex(model.indexes[i].name, model.indexes[i].keyPath, model.indexes[i].options);
8486
}
8587
}
88+
} else {
89+
8690
}
8791
}
8892

89-
protected abstract ModelBuilding(databse: IDBDatabase): void;
93+
public ModelBuilding: (databse: IDBDatabase) => void;
9094

9195
public Upgrade: IDBUpgradeConfiguration;
92-
9396
}
9497
}

IndexedDBDAL/DbContextBuilder.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
namespace IndexedDB {
2+
export class DbContextBuilder {
3+
_dbName: string;
4+
_models: IModelConfig[] = [];
5+
_upgradeConfig: IDBUpgradeConfiguration;
6+
_isCreated: boolean = false;
7+
_repositories: any;
8+
constructor(private _dbNative: IDBFactory) {
9+
}
10+
11+
public CreateDB(dbName: string): DbContextBuilder {
12+
this._dbName = dbName;
13+
return this;
14+
}
15+
16+
public ConfigureModel(model: IModelConfig): DbContextBuilder {
17+
if (model === undefined || model === null)
18+
throw new Error('Please mention model detail');
19+
this._models.push(model);
20+
return this;
21+
}
22+
23+
public UpgradeConfiguration(UpgradeConfiguration: IDBUpgradeConfiguration): DbContextBuilder {
24+
if (this._upgradeConfig !== undefined || this._upgradeConfig !== null)
25+
throw new Error('Upgrade Configuration already provided');
26+
this._upgradeConfig = UpgradeConfiguration;
27+
return this;
28+
}
29+
30+
public Build() {
31+
if (this._isCreated)
32+
throw new Error('Context is already Build');
33+
var that = this;
34+
var object = Object.create(null);
35+
var container = new DbContext(this._dbNative, this._dbName);
36+
container.Upgrade = this._upgradeConfig;
37+
container.ModelBuilding = function (db: IDBDatabase) {
38+
for (var model of that._models) {
39+
this.CreateObjectSet(db, model);
40+
}
41+
}
42+
for (var model of this._models) {
43+
object[model.name] = new BaseRepository(container, model.name);
44+
}
45+
this._repositories = object;
46+
this._isCreated = true;
47+
return object;
48+
}
49+
50+
public GetRepositories(): any {
51+
if (!this._isCreated)
52+
throw new Error('Please build the DbContext first');
53+
return this._repositories;
54+
}
55+
56+
public static Debug(flag: boolean) {
57+
Util.enableDebug = flag;
58+
}
59+
}
60+
}

IndexedDBDAL/DbContextContainer.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

IndexedDBDAL/DbContextStorageFactory.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

IndexedDBDAL/Helpers/Util.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace IndexedDB {
22
export class Util {
3+
static enableDebug: boolean = true;
34
static CreatePromise(): any {
45
var promiseHandler = <IPromiseHandler>{};
56
var promise = new Promise(function (resolve, reject) {
@@ -9,7 +10,8 @@ namespace IndexedDB {
910
return Object.assign(promise, promiseHandler);
1011
}
1112
static Log(ex: any): void {
12-
console.log(ex);
13+
if (Util.enableDebug)
14+
console.log(ex);
1315
}
1416
}
1517
export interface IPromiseHandler {

IndexedDBDAL/Repositories/BaseRepository.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ namespace IndexedDB {
55
export class BaseRepository<TDBContext extends IDBContext, TKey extends string | number> implements IRepository<TDBContext, TKey>{
66
private _DBContext: TDBContext;
77
private _StoreName: string;
8-
constructor(ObjectStore: TDBContext, StoreName: string) {
9-
this._DBContext = ObjectStore;
8+
constructor(TContext: TDBContext, StoreName: string) {
9+
this._DBContext = TContext;
1010
this._StoreName = StoreName;
1111
}
1212

IndexedDBDAL/Repositories/SampleRepository.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

IndexedDBDAL/SampleContext.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1-
## INDEXEDdb.JS
1+
## IndexedDB.js
2+
inspired from **[proto-db](https://stackedit.io/)**.
3+
IndexedDB.js enable you to access the indexed Database feature of browser with promise-api. IndexedDB.js create a repository for each Object store in IndexedDB database.
4+
5+
Sample Code of Usage :
6+
```javascript
7+
var sampleContextBuilder = new IndexedDB.DbContextBuilder(window.indexedDB);
8+
sampleContextBuilder.CreateDB("Sample");
9+
sampleContextBuilder.ConfigureModel({
10+
name: "Sample",
11+
keyPath: "id",
12+
autoIncrement: false
13+
});
14+
sampleContextBuilder.Build();
15+
```
16+
17+
18+
19+
220

321

0 commit comments

Comments
 (0)