-
Notifications
You must be signed in to change notification settings - Fork 152
/
util-heroku-pg.js
executable file
·139 lines (122 loc) · 3.26 KB
/
util-heroku-pg.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const { Client } = require('pg');
const { readConfigurationFromFile } = require('./util-file');
const createConfigurationIfNotExists = () => {
hasConfiguration((error) => {
if (error) {
const client = createClient();
client
.connect()
.then(() => {
return createTableIfNotExists(client);
})
.then((result) => {
return truncateTable(client);
})
.then((result) => {
return readConfigurationFromFile();
})
.then((configurationAsString) => {
return writeConfiguration(client, configurationAsString);
})
.then((result) => {
console.log('table successfully created, configuration saved');
process.exit(0);
})
.catch((error) => {
console.log(error);
process.exit(1);
})
.then(() => client.end());
}
});
};
const hasConfiguration = (callback) => {
const client = createClient();
client
.connect()
.then(() => {
return checkIfTableExists(client);
})
.then((result) => {
if (result.rows[0].exists === true) {
callback(null);
} else {
callback(new Error('table does not exist'));
}
})
.catch((error) => {
return callback(error);
})
.then(() => client.end());
};
const getConfiguration = (callback) => {
const client = createClient();
client
.connect()
.then(() => {
return readConfiguration(client);
})
.then((result) => {
if (result.rows.length === 1) {
callback(null, JSON.parse(result.rows[0].data));
} else {
callback(new Error(`configuration invalid, ${result.rows.length} rows found`));
}
})
.catch((error) => {
return callback(error);
})
.then(() => client.end());
};
const setConfiguration = (configuration, callback) => {
const configurationAsString = JSON.stringify(configuration, null, 4);
const client = createClient();
client
.connect()
.then(() => {
return truncateTable(client);
})
.then((result) => {
return writeConfiguration(client, configurationAsString);
})
.then((result) => {
return callback(null);
})
.catch((error) => {
console.log(error);
return callback(error);
})
.then(() => client.end());
};
const createClient = () => {
return new Client({
connectionString: process.env.DATABASE_URL,
ssl: { rejectUnauthorized: false }
});
};
const checkIfTableExists = (client) => {
return client.query(
"SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'configuration')"
);
};
const createTableIfNotExists = (client) => {
return client.query('CREATE TABLE IF NOT EXISTS configuration (id serial, data text)');
};
const truncateTable = (client) => {
return client.query('TRUNCATE configuration');
};
const writeConfiguration = (client, configuration) => {
const query = {
text: 'INSERT INTO configuration(data) values($1)',
values: [configuration]
};
return client.query(query);
};
const readConfiguration = (client) => {
return client.query('SELECT * FROM configuration');
};
module.exports = {
createConfigurationIfNotExists,
getConfiguration,
setConfiguration
};