-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbSelect.js
121 lines (100 loc) · 3.85 KB
/
dbSelect.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
120
121
/*global Q:true,Faker:true,$nameSpace$: true */
(function () {
'use strict';
n$.db.register('select', n$.makeNameSpace());
/*
* Private implementation method to select all records from a table
* @param dbManager {n$.db.Manager} A DB Manager instance
* @param tableName {String} The name of the table to select from
*/
var selectAllImpl = function (dbManager, tableName, ret) {
var deferred = Q.defer();
var doSelect = function () {
var transaction = dbManager.getDb().transaction([tableName]);
var objectStore = transaction.objectStore(tableName);
ret = ret || [];
objectStore.openCursor().onsuccess = function(event) {
var cursor = event.target.result;
if (cursor) {
ret.push(cursor.value);
cursor.continue();
} else {
deferred.resolve(ret);
}
};
return deferred.promise;
};
try {
dbManager.promises.connect.then(doSelect);
}
catch (e) {
console.log(e, e.stack);
deferred.reject(new Error('Could not select records', e));
}
return deferred.promise;
};
/*
* Public implementation method to select all records from a table
* @param dbManager {n$.db.Manager} A DB Manager instance
* @param tableName {String} The name of the table to select from
*/
var selectAll = function (dbWrapper, tableName) {
var ret = [];
var promise = selectAllImpl(dbWrapper, tableName, ret);
promise.return = ret;
return promise;
};
n$.db.select.register('all', selectAll);
/*
* Private implementation method to select all records from a table
* @param dbManager {n$.db.Manager} A DB Manager instance
* @param tableName {String} The name of the table to select from
* @param indexName {String} The name of the index to select from
* @param indexVal {String} The "where" clause: where indexName = indexVal
*/
var selectFromImpl = function (dbManager, tableName, indexName, indexVal, ret) {
var deferred = Q.defer();
var doSelect = function () {
var transaction = dbManager.getDb().transaction([tableName]);
var objectStore = transaction.objectStore(tableName);
var index = objectStore.index(indexName);
ret = ret || [];
var keyRange;
if (indexVal) {
keyRange = IDBKeyRange.only(indexVal);
}
index.openCursor(keyRange).onsuccess = function (event) {
var cursor = event.target.result;
if (cursor) {
ret.push(cursor.value);
cursor.continue();
} else {
deferred.resolve(ret);
}
};
return deferred.promise;
};
try {
dbManager.promises.connect.then(doSelect);
}
catch (e) {
console.log(e, e.stack);
deferred.reject(new Error('Could not select records', e));
}
return deferred.promise;
};
/*
* Public implementation method to select all records from a table
* @param dbManager {n$.db.Manager} A DB Manager instance
* @param tableName {String} The name of the table to select from
* @param indexName {String} The name of the index to select from
* @param indexVal {String} The "where" clause: where indexName = indexVal
*/
var selectFrom = function (dbWrapper, tableName, indexName, indexVal) {
var ret = [];
var promise = selectFromImpl(dbWrapper, tableName, indexName, indexVal, ret);
promise.return = ret;
return promise;
};
n$.db.select.register('from', selectFrom);
} ());