-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload
More file actions
executable file
·88 lines (75 loc) · 2.03 KB
/
load
File metadata and controls
executable file
·88 lines (75 loc) · 2.03 KB
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
#! /usr/bin/env node
const Promise = require('bluebird')
const Client = require('pg').Client
const fs = require('fs')
const program = require('commander')
const readline = require('readline')
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
program
.usage('[options] <file ...>')
.option('-s, --schema <schema>', 'schema name to store the data under (default \'ds\')')
.parse(process.argv)
const schema = program.schema || 'ds'
if (program.args.length === 0) {
console.error('no files provided!')
program.help()
}
const client = new Client()
const filenames = program.args
const useJsonb = true
const co = Promise.coroutine
const readFile = Promise.promisify(fs.readFile)
const load = co(function* (filename) {
let data = yield readFile(filename, 'utf8')
try {
data = JSON.parse(data)
} catch (err) {
console.error(err)
}
try {
yield client.query(`CREATE SCHEMA "${schema}";`)
console.log('created schema', schema)
} catch (err) {
// schema already exists
}
for (const tableName in data) {
try {
yield client.query(`
CREATE TABLE "${schema}"."${tableName}" (
id text NOT NULL,
val ${useJsonb ? 'jsonb' : 'text'} NOT NULL,
PRIMARY KEY (id)
);`
)
console.log('created table', tableName)
} catch (err) {
// table already exists
console.log(`WARNING: the table ${tableName}, already exists`)
}
const tableData = data[tableName]
let recordCount = 0
for (const recordName in tableData) {
yield client.query(`
INSERT INTO "${schema}"."${tableName}" (id, val) VALUES
($1, $2)
ON CONFLICT (id)
DO UPDATE SET val = EXCLUDED.val;`,
[recordName, { _d: tableData[recordName], _v: 1 }]
)
recordCount++
}
console.log('inserted', recordCount, 'records into table', tableName)
}
})
const loadAll = co(function* () {
for (const filename of filenames) {
yield load(filename)
}
})
client.connect()
.then(loadAll)
.then(client.end)
.catch(console.error.bind(null, 'an error occurred:'))