forked from 1313-Mockingbird-Lane/data-objects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload-school-data.js
More file actions
76 lines (64 loc) · 1.6 KB
/
load-school-data.js
File metadata and controls
76 lines (64 loc) · 1.6 KB
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
var fs = require('fs')
var PouchDB = require('pouchdb')
var models = require('./models')
var school_data = [];
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream('./data/schools.lsjson')
});
lineReader.on('line', function (line) {
school_data.push(models.School.fromJson(line))
});
var db = new PouchDB('schools', {auto_compaction: true});
function init() {
db.info().then(function (info) {
if(info.doc_count == 0) {
console.log("Empty DB found. Populating with data.")
create_index()
}
})
}
function create_index() {
console.log('Creating Indexes')
var ddoc = {
_id: '_design/index',
views: {
school_name: {
map: function (doc) {
if(doc.type === '_school') {
emit(doc.name);
}
}.toString(),
},
school_zipcode: {
map: function (doc) {
if(doc.type === '_school') {
emit(doc.zipcode);
}
}.toString(),
},
school_zipcode_count: {
map: function (doc) {
if(doc.type === '_school') {
emit(doc.zipcode);
}
}.toString(),
reduce: '_count'
},
}
};
db.put(ddoc).then(function () {
load_schools()
}).catch(function (err) {
console.error(err)
});
}
function load_schools() {
console.log('Loading Schools');
db.bulkDocs(
school_data
).then(function (response) {
}).catch(function (err) {
console.error(err)
});
}
init()