-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
export-sql.js
executable file
·109 lines (93 loc) · 2.35 KB
/
export-sql.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
#!/usr/bin/env node
'use strict'
const mri = require('mri')
const pkg = require('./package.json')
const argv = mri(process.argv.slice(2), {
boolean: [
'help', 'h',
'version', 'v',
]
})
if (argv.help || argv.h) {
process.stdout.write(`
Usage:
export-hafas-data-as-sql <path-to-leveldb> [event types]
Examples:
export-hafas-data-as-sql db.ldb departure,stopover >export.sql
\n`)
process.exit(0)
}
if (argv.version || argv.v) {
process.stdout.write(`export-hafas-data-as-sql v${pkg.version}\n`)
process.exit(0)
}
const {promisify} = require('util')
const pump = require('pump')
const through2 = require('through2')
const level = require('level')
const {sqlSchemas, exportSql, dbPrefixes} = require('./lib/event-types')
// const NAMESPACE = require('./lib/namespace')
const RANGE_END = String.fromCharCode(0xFFFF)
const showError = (err) => {
console.error(err)
process.exit(1)
}
const pPump = promisify(pump)
// todo: `gt`/`lt` options?
const generateSql = (db, eventType, out) => {
const schema = sqlSchemas[eventType]
const toSql = exportSql[eventType]
const dbPrefix = dbPrefixes[eventType]
if (!schema || !toSql || !dbPrefix) {
throw new Error([
`invalid/unknown event type \`${eventType}\``,
'export-hafas-data-as-sql only supports',
Object.keys(sqlSchemas).join('/')
].join(' '))
}
out.write(schema)
const transformRow = (val, _, cb) => {
if (!Array.isArray(val)) {
const err = new Error('DB value is not an array')
// todo: expose DB key?
err.val = val
throw err
}
cb(null, toSql(val))
}
// todo: expose abort fn
return pPump(
db.createValueStream({
gt: dbPrefix + '-',
lt: dbPrefix + '-' + RANGE_END
}),
through2.obj(transformRow),
out
)
}
const pathToLeveldb = argv._[0]
if (!pathToLeveldb) showError('Missing path to the LevelDB.')
const eventTypes = argv._[1]
? argv._[1].split(/,\s?/)
: Object.keys(exportSql)
for (const eventType of eventTypes) {
if (!(eventType in exportSql)) {
showError([
'invalid/unknown event type:',
'export-hafas-data-as-sql only supports',
Object.keys(exportSql).join('/')
].join(' '))
}
}
const db = level(pathToLeveldb, {valueEncoding: 'json'})
db.open().then(() => {
let p = Promise.resolve()
// chain all tasks
eventTypes.forEach((eventType) => {
p = p.then(() => {
return generateSql(db, eventType, process.stdout)
})
})
return p
})
.catch(showError)