forked from xixilive/chinese_regions_db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump.js
62 lines (50 loc) · 1.48 KB
/
dump.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
/*
* dumpdb into json files
*/
'use strict';
//table schema:
//id TEXT PRIMARY KEY ASC, parent_id TEXT, name TEXT, alias TEXT, pinyin TEXT, abbr TEXT, zip TEXT, level INTEGER
(function(db_file){
var fs = require('fs');
function simplify(data){
return {
i: data.id,
n: data.name,
a: data.alias,
y: data.pinyin,
b: data.abbr,
z: data.zip
};
}
function dump(err, rows){
if(err){
console.log(err);
return;
}
var provinces = rows.filter(function(row){
return String(row.parent_id).replace('null','') == "";
});
console.log("Dumping province index......\n");
fs.writeFileSync('json/index.json', JSON.stringify(provinces.map(simplify)));
console.log("Dumping cities and suburbs......\n");
provinces.forEach(function(p){
var regions = rows.filter(function(c){
return c.parent_id == p.id;
}).map(simplify)
.map(function(r){
r.c = rows.filter(function(s){
return s.parent_id == r.i;
}).map(simplify);
if(r.c.length == 0) delete r.c;
return r;
});
fs.writeFileSync('json/'+p.id+'.json', JSON.stringify(regions));
});
fs.writeFileSync('json/last-updated-at', new Date().toString());
console.log("data dumping ok.\n");
}
var sqlite3 = require('sqlite3'), db;
db = new sqlite3.Database(db_file, sqlite3.READ_ONLY);
db.all("select * from `regions` order by `id` ASC", dump);
db.close();
})('regions.db');