-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostgresql.js
158 lines (134 loc) · 4.94 KB
/
postgresql.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
* Manages the loading and saving of configuration data to a PostgreSQL database.
*
* Written By:
* Dion Woolley
*
* License:
* MIT License. All code unless otherwise specified is
* Copyright (c) Matthew Knox and Contributors 2016.
*/
const Pool = require('pg-pool'),
SQL = require('sql-template-strings'),
deasync = require('deasync'),
path = require('path'),
url = require('url');
let pool = null,
config = {
host: 'localhost',
port: 0,
max: 20,
user: 'postgres',
password: 'postgres',
database: 'concierge'
};
class PostgreSQLService {
load() {
if (process.env.DATABASE_URL) {
const params = url.parse(process.env.DATABASE_URL),
auth = params.auth.split(':');
config.host = params.hostname;
config.port = params.port;
config.ssl = true;
config.user = auth[0];
config.password = auth[1];
config.database = params.pathname.split('/')[1];
}
pool = new Pool(config);
pool.on('error', (error, client) => {
console.error($$ `database screwed up, error:"${error.message}"`);
});
let done = false,
done2 = false;
pool.connect((err, client, cb) => {
if (err) {
console.error($$ `database screwed up, error:"${err.message}"`);
cb(err);
done = true;
}
client.query('CREATE TABLE IF NOT EXISTS module (id text PRIMARY KEY, config json NOT NULL)', (err, res) => {
cb();
if (err) {
console.error($$ `database screwed up, error:"${err.message}"`);
}
done = true;
});
client.query('CREATE INDEX IF NOT EXISTS id_index ON module (id)', (err, res) => {
cb();
if (err) {
console.error($$ `database screwed up, error:"${err.message}"`);
}
done = true;
});
});
global.currentPlatform.config.setInterceptor(this);
deasync.loopWhile(() => { return !done && !done2; });
}
_pathFromDescriptor(descriptor) {
if (descriptor === global.currentPlatform.config.getGlobalIndicator()) {
return global.__runAsLocal ? global.rootPathJoin('') : global.__modulesPath;
} else {
if (!descriptor.folderPath) {
descriptor.folderPath = path.join(global.__modulesPath, descriptor.name);
}
return descriptor.folderPath;
}
}
unload() {
pool.end();
global.currentPlatform.config.setInterceptor(null);
}
loadConfig(descriptor) {
descriptor = this._pathFromDescriptor(descriptor);
let done = false,
output = {};
pool.connect((err, client, cb) => {
if (err) {
console.error($$ `database screwed up, error:"${err.message}"`);
cb(err);
done = true;
}
client.query(SQL `SELECT config FROM module WHERE id = ${descriptor}`, (err, res) => {
cb();
if (err) {
console.error($$ `database screwed up, error:"${err.message}"`);
done = true;
}
if (res.rows.length === 0) {
done = true;
} else if (res.rows.length === 1) {
output = res.rows[0];
done = true;
} else {
console.error($$ `The number of rows returned for the query: "${res.command}", was greater than 1`);
done = true;
}
});
});
deasync.loopWhile(() => { return !done; });
return output;
}
saveConfig(descriptor, config) {
descriptor = this._pathFromDescriptor(descriptor);
const data = JSON.stringify(config, (key, value) => {
// deliberate use of undefined, will cause property to be deleted.
return value === null || typeof value === 'object' && Object.keys(value).length === 0 ? void(0) : value;
}, 4);
if (!!data && data !== 'undefined') { // there is data to write
let done = false;
pool.connect((err, client, cb) => {
if (err) {
console.error($$ `database screwed up, error:"${err.message}"`);
cb(err);
done = true;
}
client.query(SQL `INSERT INTO module (id, config) VALUES (${descriptor}, ${data}) ON CONFLICT (id) DO UPDATE SET config = ${data}`, (err, res) => {
cb();
done = true;
});
});
deasync.loopWhile(() => { return !done; });
}
}
}
module.exports = new PostgreSQLService();