-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
35 lines (28 loc) · 891 Bytes
/
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
const fs = require("fs");
const eta = require("eta");
const pg = require("pg");
const { execFileSync: sh } = require("child_process");
const query = fs.readFileSync("./query.sql").toString();
const template = fs.readFileSync("./template.eta").toString();
const args = process.argv.slice(2);
async function getSchema() {
const { Client } = pg;
const client = new Client(args[0]);
await client.connect();
const res = await client.query(query);
await client.end();
return res.rows.map((result) => ({
...result,
// postgres sometimes returns [null] for some reason
foreign_relations: result.foreign_relations.filter(
(relation) => !!relation
),
}));
}
async function main() {
const schema = await getSchema();
const output = eta.render(template, { schema });
fs.writeFileSync("output.d2", output);
sh("d2", ["output.d2", "out.svg"]);
}
main();