-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
134 lines (98 loc) · 2.67 KB
/
index.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
122
123
124
125
126
127
128
129
130
131
132
133
134
const {
mkdirSync,
writeFileSync,
} = require('fs');
const path = require('path');
const {
camelCase,
} = require('change-case');
const Sqlite = require('better-sqlite3');
const db = new Sqlite('./lds-scriptures-sqlite3.db', {
readonly: true,
});
function cleanRecord(obj, prefix) {
return Object.keys(obj).reduce((newObj, key) => ({
...newObj,
[camelCase(key.replace(prefix + '_', '')).replace('ldsUrl', 'name')]: obj[key],
}), {});
}
function sortKeys(obj) {
if (typeof obj === 'string') {
return obj;
}
if (typeof (obj.id) !== 'undefined') {
delete obj.id;
}
return Object.keys(obj).sort()
.reduce((newObj, key) => ({
...newObj,
[key]: obj[key],
}), {});
}
function makeFile(pathname, content) {
if (Array.isArray(pathname)) {
pathname = path.join(...pathname);
}
mkdirSync(path.dirname(pathname), { recursive: true });
if (typeof content !== 'string') {
if (Array.isArray(content)) {
content = content.map(sortKeys);
} else {
content = sortKeys(content);
}
content = JSON.stringify(content);
}
writeFileSync(pathname, content);
}
function select(table, where = null) {
let vals = [];
if (where !== null) {
const keys = Object.keys(where);
vals = Object.values(where);
where = ` WHERE ${ keys.map(key => `${ key } = ?`).join(' AND ') }`;
} else {
where = '';
}
const query = `SELECT * FROM ${ table + where }`;
return db.prepare(query).all(...vals)
.map((val) => cleanRecord(val, table.replace(/s$/, '')));
}
const volumes = [];
const rootDir = path.join(__dirname, 'json');
select('volumes').forEach((volume) => {
volume.books = [];
volume.bookCount = 0;
volume.chapterCount = 0;
volume.verseCount = 0;
select('books', { volume_id: volume.id }).forEach((book) => {
volume.books.push(book.name);
volume.bookCount++;
delete book.volumeId;
book.volume = volume.name;
book.chapterCount = 0;
book.verseCount = 0;
const bookDir = path.join(rootDir, book.name);
select('chapters', { book_id: book.id }).forEach((chapter) => {
volume.chapterCount++;
book.chapterCount++;
delete chapter.bookId;
chapter.volume = volume.name;
chapter.verseCount = 0;
chapter.verses = select('verses', { chapter_id: chapter.id })
.map((verse) => {
delete verse.chapterId;
return sortKeys(verse);
});
chapter.verseCount = chapter.verses.length;
volume.verseCount += chapter.verseCount;
book.verseCount += chapter.verseCount;
makeFile([bookDir, `${ chapter.number }.json`], chapter);
});
makeFile([rootDir, `${ book.name }.json`], book);
console.log(book);
});
volumes.push(volume);
});
makeFile([rootDir, 'volumes.json'], volumes);
console.log(volumes);
console.log('DONE!');