Skip to content

Commit 4006f8a

Browse files
committed
Add the DbContext and generate compiled version of indexedDB.js
1 parent 81ee371 commit 4006f8a

22 files changed

+424
-100
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/DataBase

.vscode/tasks.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"version": "0.1.0",
55
"command": "tsc",
66
"isShellCommand": true,
7-
"args": ["-p", "./DataBase"],
7+
"args": ["-p", "./IndexedDBDAL"],
88
"showOutput": "silent",
99
"problemMatcher": "$tsc"
10-
}
10+
}

IndexedDBDAL/Contract/IDbContext.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace IndexedDB {
2+
export interface IDBContext {
3+
Reset(): Promise<any>;
4+
Delete(): Promise<any>;
5+
CreateObjectSet(model: string): Promise<IDBObjectStore>
6+
}
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace IndexedDB {
2+
export interface IDbContextStorageContainer {
3+
Get(): IDBContext;
4+
}
5+
}

IndexedDBDAL/Contract/IRepository.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace IndexedDB {
2+
export interface IRepository<TObjectStore extends IDBObjectStore, TKey extends any> {
3+
Add(TObject: any): Promise<any>;
4+
Update(TObject: any): Promise<any>;
5+
Delete(TKey: any): Promise<any>;
6+
Get(TKey: any): Promise<any>;
7+
GetAll(): Promise<any>;
8+
}
9+
}

IndexedDb.DAL/DbContext.ts renamed to IndexedDBDAL/DbContext.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
import * as Helpers from "./Helpers/Util";
2-
import * as DbContext from "./Contract/IDbContext";
3-
export namespace IndexedDb {
4-
export abstract class DbContext implements DbContext.IndexedDb.IDBContext {
1+
/// <reference path="./Contract/IDbContext.ts" />
2+
/// <reference path="./Helpers/Util.ts" />
3+
4+
namespace IndexedDB {
5+
export abstract class DbContext implements IDBContext {
56
private _dbNative: IDBFactory;
67
constructor(databaseNative: IDBFactory, public dbName?: string) {
78
this._dbNative = databaseNative;
89
this.dbName = this.dbName || 'SampleDB';
910
this.Begin();
1011
}
11-
12+
1213
public Begin(): Promise<any> {
1314
var self = this;
1415
var creationRequest = self._dbNative.open(this.dbName);
15-
var promise = Helpers.IndexedDb.Helpers.Util.CreatePromise();
16+
var promise = Util.CreatePromise();
1617
creationRequest.onsuccess = function (event: any) {
1718
promise.resolve(event.target.result);
1819
}
19-
20+
2021
creationRequest.onupgradeneeded = function (event: any) {
2122
var db = event.target.result as IDBDatabase;
2223
self.ModelBuilding(db);
@@ -31,19 +32,19 @@ export namespace IndexedDb {
3132
return this.Begin()
3233
.then(function (db: IDBDatabase) {
3334
db.close();
34-
var promise = Helpers.IndexedDb.Helpers.Util.CreatePromise();
35+
var promise = Util.CreatePromise();
3536
var req = self._dbNative.deleteDatabase(self.dbName);
3637
req.onsuccess = function () {
3738
promise.resolve();
38-
Helpers.IndexedDb.Helpers.Util.Log("Deleted database successfully");
39+
Util.Log("Deleted database successfully");
3940
};
4041
req.onerror = function () {
4142
promise.reject();
42-
Helpers.IndexedDb.Helpers.Util.Log("Couldn't delete database");
43+
Util.Log("Couldn't delete database");
4344
};
4445
req.onblocked = function () {
4546
promise.reject();
46-
Helpers.IndexedDb.Helpers.Util.Log("Couldn't delete database due to the operation being blocked");
47+
Util.Log("Couldn't delete database due to the operation being blocked");
4748
};
4849
return promise;
4950
});
@@ -62,6 +63,6 @@ export namespace IndexedDb {
6263
});
6364
}
6465

65-
protected abstract ModelBuilding(databse :IDBDatabase);
66+
protected abstract ModelBuilding(databse: IDBDatabase): void;
6667
}
6768
}

IndexedDBDAL/DbContextContainer.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path="./Contract/IDbContextStorageContainer.ts" />
2+
/// <reference path="./SampleContext.ts" />
3+
4+
namespace IndexedDB {
5+
export class DbContextConatiner implements IDbContextStorageContainer {
6+
private _context: SampleContext;
7+
constructor(databaseNative: IDBFactory) {
8+
this._context = new SampleContext(databaseNative);
9+
}
10+
public Get(): IDBContext {
11+
return this._context;
12+
}
13+
}
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace IndexedDB {
2+
export class DbContextStorageFactory {
3+
private ContextContainer: IDbContextStorageContainer;
4+
constructor(dbNative: IDBFactory) {
5+
this.ContextContainer = new DbContextConatiner(dbNative);
6+
}
7+
get Context(): IDBContext {
8+
return this.ContextContainer.Get();
9+
}
10+
11+
}
12+
}

IndexedDb.DAL/Helpers/Util.ts renamed to IndexedDBDAL/Helpers/Util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export namespace IndexedDb.Helpers {
1+
namespace IndexedDB {
22
export class Util {
33
static CreatePromise(): any {
44
var promiseHandler = <IPromiseHandler>{};
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/// <reference path="../Contract/IRepository.ts" />
2+
/// <reference path="../Helpers/Util.ts" />
3+
4+
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;
9+
}
10+
11+
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;
23+
}
24+
25+
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;
37+
}
38+
39+
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;
51+
}
52+
53+
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;
66+
}
67+
68+
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 {
78+
Util.Log("success");
79+
promise.resolve(dbCollection);
80+
}
81+
};
82+
getAllRequest.onerror = function () {
83+
promise.reject();
84+
Util.Log("Fail");
85+
};
86+
return promise;
87+
}
88+
}
89+
}

0 commit comments

Comments
 (0)