-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
119 lines (97 loc) · 3.9 KB
/
db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*global Q:true */
(function(n$) {
n$.register('db', n$.makeNameSpace());
/*
* Instance a DB Manager class which abstracts the mechanics for connecting to and selecting from an IndexedDb database
*/
var dbManager = function(name, version) {
var ret = n$.object();
ret.add('promises', n$.object());
var isNewConnectionRequired = false;
var schemaScripts = [];
/*
* Initiate a promise to connect to a database. When that connection is established, the promise will be resolved.
*/
var connect = function(dbName, dbVersion, dbOnUpgrade) {
isNewConnectionRequired = (!ret.promises.connect || dbName !== name || dbVersion !== version);
if (isNewConnectionRequired) {
var deferred = Q.defer();
ret.promises.connect = deferred.promise;
version = dbVersion || 1;
name = dbName;
dbOnUpgrade = dbOnUpgrade || function() {};
var request = window.indexedDB.open(name, version);
request.onblocked = function(event) {
ret.IDB.close();
alert("A new version of this page is ready. Please reload!");
};
request.onerror = function(event) {
deferred.reject(new Error("Database error: " + event.target.errorCode));
if (ret.IDB) {
ret.IDB.close();
}
};
request.onsuccess = function(event) {
ret.IDB = ret.IDB || request.result;
deferred.resolve(ret.IDB);
};
request.onupgradeneeded = function(event) {
ret.IDB = ret.IDB || request.result;
if (schemaScripts.length > 0) {
n$.iterate(schemaScripts, function(script) {
//debugger;
script(ret.IDB);
});
}
dbOnUpgrade(ret.IDB);
};
}
return ret.promises.connect;
};
/*
* Disconnect from a database
*/
var disconnect = function() {
if (ret.promises.connect.isFulfilled()) {
ret.IDB.close();
}
else if (ret.IDB) {
ret.promises.connect.done(ret.IDB.close);
}
};
//Collect the methods into an API:
ret.add('connect', connect);
ret.add('disconnect', disconnect);
ret.add('getDb', function() {
return ret.IDB;
});
ret.add('schemaScripts', schemaScripts);
ret.add('tables', n$.object());
ret.add('ddl', {
createTable: function(tableName, tablePkColumnName, autoIncrement) {
return n$.fun.shiftRight(n$.db.table.create, ret, arguments, this);
},
dropTable: function(tableName) {
return n$.fun.shiftRight(n$.db.index.drop, ret, arguments, this);
},
createIndex: function(tableName, columnName, indexName, isUnique) {
return n$.fun.shiftRight(n$.db.index.create, ret, arguments, this);
}
});
ret.add('insert', function() {
return n$.fun.shiftRight(n$.db.insert, ret, arguments, this);
});
var select = n$.object();
ret.add('select', select);
select.add('all', function() {
return n$.fun.shiftRight(n$.db.select.all, ret, arguments, this);
});
select.add('from', function() {
return n$.fun.shiftRight(n$.db.select.from, ret, arguments, this);
});
//Connect to the DB automatically
ret.connect(name, version);
return ret;
};
n$.db.register('dbManager', dbManager);
}(window.$nameSpace$));