-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpantry_pouchdb.js
85 lines (75 loc) · 1.92 KB
/
pantry_pouchdb.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
var LevelPouchDB = require('pouchdb');
var path = require('path');
var Pantry = function (db_home) {
var pantry_ = path.join(db_home, 'pantry_storage');
var db = new LevelPouchDB(pantry_);
this.SaveFile = SaveFile;
this.GetAll = GetAll;
this.GetByDate = GetByDate;
this.GetByType = GetByType;
SetUpIndex();
function SaveFile(file) {
if (file.id_path) {
file['_id'] = file.id_path;
db.put(file).then(function (response) {
// handle response
}).catch(function (err) {
});
}
}
function GetAll(on_file_record) {
db.allDocs({
include_docs: true,
attachments: true
}).then(function (results) {
on_file_records(results);
}).catch(function (err) {
console.log(err);
});
}
function GetByDate(iso_date, on_file_record) {
db.query('birth_time_index', {
include_docs: true,
attachments: true,
startkey: iso_date,
endkey: iso_date+'\uffff'
}).then(function (results) {
on_file_records(results);
}).catch(function (err) {
console.log(err);
});
}
function GetByType(type, on_file_record) {
db.query('type_index', {
include_docs: true,
attachments: true,
startkey: type,
endkey: type+'\uffff'
}).then(function (results) {
on_file_records(results);
}).catch(function (err) {
console.log(err);
});
}
function SetUpIndex() {
var bt_index = createIndex('birth_time_index', function (doc) {
emit(doc.birth_time);
});
var tp_index = createIndex('type_index', function (doc) {
emit(doc.type);
});
db.put(bt_index).then(function (doc) {}).catch(function (err) {});
db.put(tp_index).then(function (doc) {}).catch(function (err) {});
db.query(bt_index, {stale: 'update_after'});
db.query(tp_index, {stale: 'update_after'});
}
function createIndex(name, mapFunction) {
var doc = {
_id: '_design/' + name,
views: {}
};
doc.views[name] = { map: mapFunction.toString() };
return doc;
}
}
module.exports.Pantry = Pantry;