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
|
/**
* OKQuery!
* Takes configuration and gives you something that can run a DB query
* based on the configurations.
*/
function OKQuery(db, options) {
if (!(this instanceof OKQuery)) return new OKQuery(db, options);
options = options || {};
if (!db)
throw new Error('No DB provided to query.');
if (!options.name)
throw new Error('No name type provided to query');
this.name = options.name;
this.get = createQuery(db, options);
}
function createQuery(db, config) {
var name = config.name;
var id = config.id || '*';
if (isDynamic(id)) {
return queryDynamic(db, name);
} else if (isSet(id)) {
return queryAll(db, name);
} else {
return querySingle(db, name, id);
}
}
function queryDynamic(db, name) {
return function(options) {
options = options || {};
return db.get(name, options.id);
}
}
function queryAll(db, name) {
return function() {
return db.getAll(name);
}
}
function querySingle(db, name, id) {
return function() {
return db.get(name, id);
}
}
function isDynamic(id) {
return id && id.charAt(0) === ':';
}
function isSet(id) {
return id && id === '*';
}
module.exports = OKQuery;
|