This repository has been archived by the owner on Mar 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport.js
79 lines (65 loc) · 1.72 KB
/
export.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
import fs from "fs"
import path from "path"
import readline from "readline"
import sqlite3 from "sqlite3"
import { open } from "sqlite"
import canonize from "rdf-canonize"
import { parseSchemaString, serialize } from "apg"
import { exportInstance } from "./lib/export.js"
function invalidParameters() {
throw new Error(
"Usage: node lib/export.js -s path-to-schema.nq -i input-path.sqlite -o output-path.nq"
)
}
let schemaPath = "",
inputPath = "",
outputPath = ""
if (process.argv.length === 8) {
if (
process.argv[2] === "-s" &&
process.argv[4] === "-i" &&
process.argv[6] === "-o"
) {
schemaPath = process.argv[3]
inputPath = process.argv[5]
outputPath = process.argv[7]
} else {
invalidParameters()
}
} else {
invalidParameters()
}
const filename = path.resolve(outputPath)
if (fs.existsSync(filename)) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
})
await new Promise((resolve) => {
console.log(`The file ${outputPath} already exists!`)
rl.question(
"Press Enter to delete it and continue; or Ctrl-C to abort:",
resolve
)
})
rl.close()
fs.unlinkSync(filename)
}
const schemaFile = fs.readFileSync(schemaPath, "utf-8")
const result = parseSchemaString(schemaFile)
if (result._tag === "Left") {
console.error(result.left)
throw new Error("schema did not parse")
}
const db = await open({
filename: path.resolve(inputPath),
driver: sqlite3.Database,
mode: sqlite3.OPEN_READONLY,
})
const instance = await exportInstance(db, result.right)
const quads = []
for (const quad of serialize(instance, result.right)) {
quads.push(quad.toJSON())
}
const dataset = canonize.canonizeSync(quads, { algorithm: "URDNA2015" })
fs.writeFileSync(filename, dataset)