-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.js
62 lines (48 loc) · 1.28 KB
/
setup.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
const fs = require('fs');
const crypto = require('crypto')
const connectionString = process.env.DATABASE_URL;
const { Client } = require('pg');
const client = new Client({
connectionString: connectionString,
ssl: { rejectUnauthorized: false }
});
function sleep(milliseconds) {
const date = Date.now();
let currentDate = null;
do {
currentDate = Date.now();
} while (currentDate - date < milliseconds);
}
var storage;
var myData;
try {
storage = fs.readFileSync('./world.zip', { encoding: 'hex' });
} catch(err) {
console.log(err)
return
}
//console.log(storage);
client.connect();
const insertQuery = `
insert into files values('world.zip', bytea('`+ storage +`'))`;
const updateQuery = `
update files set file=bytea('`+ storage +`') where filename='world.zip'`;
const retreiveQuery = `
select convert_from(file, 'UTF-8') as file from files where filename='world.zip'`;
const setupQuery = `
create table files (filename text, file bytea)`
client.query(setupQuery, (err, res, fields) => {
if (err) {
console.error(err);
return;
}
console.log('**script**: created table')
});
client.query(insertQuery, (err, res, fields) => {
if (err) {
console.error(err);
return;
}
console.log('**script**: uploaded db')
client.end();
});