forked from hgwood/hash-code-tooling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pg-load.js
52 lines (44 loc) · 1.45 KB
/
pg-load.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
const _ = require("lodash");
const debug = require("debug")("pg-load");
const { Client } = require("pg");
const insertDataSet = async (client, dataSet, dataSetName) => {
// insert the data set in the db here
};
const main = async files => {
if (files.length === 0) {
debug("no input files");
return;
}
const dataSets = _(files)
.keyBy(file => file.split(".")[0].replace("-", "_"))
.mapValues(file => require(`./${file}`))
.value();
const client = new Client({
connectionString: "postgres://postgres@localhost:5432/postgres"
});
await client.connect();
await Promise.all(
Object.entries(dataSets).map(async ([dataSetName, dataSet]) => {
const database = dataSetName;
await client.query(`drop database if exists ${database}`);
await client.query(`create database ${database}`);
debug(dataSetName, "database created");
const dataSetClient = new Client({
connectionString: `postgres://postgres@localhost:5432/${database}`
});
await dataSetClient.connect();
await dataSetClient.query("begin");
try {
await insertDataSet(client, dataSet, dataSetName);
await dataSetClient.query("commit");
debug(dataSetName, "dataset committed");
} catch (err) {
await dataSetClient.query("rollback");
debug(dataSetName, "rolled back", err);
}
await dataSetClient.end();
})
);
await client.end();
};
main(process.argv.slice(2));