forked from sqlpad/sqlpad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnections.js
More file actions
209 lines (181 loc) · 5.46 KB
/
connections.js
File metadata and controls
209 lines (181 loc) · 5.46 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
const _ = require('lodash');
const Cryptr = require('cryptr');
const drivers = require('../drivers');
const validateConnection = require('../lib/validate-connection');
class Connections {
/**
* @param {import('../sequelize-db')} sequelizeDb
* @param {import('../lib/config')} config
*/
constructor(sequelizeDb, config) {
this.sequelizeDb = sequelizeDb;
this.config = config;
this.cryptr = new Cryptr(config.get('passphrase'));
}
decorateConnection(connection) {
if (!connection) {
return connection;
}
const copy = _.cloneDeep(connection);
copy.maxRows = Number(this.config.get('queryResultMaxRows'));
const driver = drivers[connection.driver];
if (!driver) {
copy.supportsConnectionClient = false;
copy.isAsynchronous = false;
} else {
copy.supportsConnectionClient = Boolean(
drivers[connection.driver].Client
);
copy.isAsynchronous = Boolean(drivers[connection.driver].asynchronous);
}
// For legacy use, spread driver-field data onto connection object
// This isn't great but needed for backwards compat at this time
if (copy.data) {
Object.assign(copy, copy.data);
}
return copy;
}
decipherConnection(connection) {
if (connection.data && typeof connection.data === 'string') {
connection.data = JSON.parse(this.cryptr.decrypt(connection.data));
}
return connection;
}
async findAll() {
let connectionsFromDb = await this.sequelizeDb.Connections.findAll({
attributes: [
'id',
'name',
'description',
'driver',
'multiStatementTransactionEnabled',
'idleTimeoutSeconds',
'createdAt',
'updatedAt',
],
});
connectionsFromDb = connectionsFromDb.map((conn) => {
let jsonConn = conn.toJSON();
jsonConn.editable = true;
return jsonConn;
});
const allConnections = connectionsFromDb
.concat(this.config.getConnections())
.map((connection) => this.decorateConnection(connection));
return _.sortBy(allConnections, (c) => c.name.toLowerCase());
}
async findOneById(id) {
let connection = await this.sequelizeDb.Connections.findOne({
where: { id },
});
if (connection) {
connection = connection.toJSON();
connection.editable = true;
connection = this.decipherConnection(connection);
return this.decorateConnection(connection);
}
// If connection was not found in db try env
const connectionFromEnv = this.config
.getConnections()
.find((connection) => connection.id === id);
if (!connectionFromEnv) {
return null;
}
return this.decorateConnection(connectionFromEnv);
}
async removeOneById(id) {
return this.sequelizeDb.Connections.destroy({ where: { id } });
}
/**
*
* @param {object} connection
*/
async create(connection) {
const {
id,
name,
description,
driver,
multiStatementTransactionEnabled,
idleTimeoutSeconds,
data,
createdAt,
updatedAt,
...legacyDriverFields
} = connection;
let createObj = {
id,
name,
description,
driver,
multiStatementTransactionEnabled,
idleTimeoutSeconds,
};
// Old connections had driver-specific fields flat on connection object
// With v5 those moved to data, but the old format needs to be supported
// if data is supplied, we assume that this is a new format
// if no data, then we assume all fields we don't know about are driver-specific fields
if (data) {
createObj.data = data;
} else {
createObj.data = legacyDriverFields;
}
createObj = validateConnection(createObj);
// if data is set encrypt it
if (createObj.data) {
createObj.data = this.cryptr.encrypt(JSON.stringify(createObj.data));
}
const created = await this.sequelizeDb.Connections.create(createObj);
return this.findOneById(created.id);
}
/**
*
* @param {string} id - id of connection
* @param {object} connection - connection object with .data field
*/
async update(id, connection) {
if (!connection) {
throw new Error('connection required');
}
// Below uses destructing to deduce legacy driver fields
// id is already declared in function, so it is being cloned and deleted here
const clone = { ...connection };
delete clone.id;
const {
name,
description,
driver,
multiStatementTransactionEnabled,
idleTimeoutSeconds,
data,
createdAt,
updatedAt,
...legacyDriverFields
} = clone;
let updateObj = {
id,
name,
description,
driver,
multiStatementTransactionEnabled,
idleTimeoutSeconds,
};
// Old connections had driver-specific fields flat on connection object
// With v5 those moved to data, but the old format needs to be supported
// if data is supplied, we assume that this is a new format
// if no data, then we assume all fields we don't know about are driver-specific fields
if (data) {
updateObj.data = data;
} else {
updateObj.data = legacyDriverFields;
}
updateObj = validateConnection(updateObj);
// if data is set encrypt it
if (updateObj.data) {
updateObj.data = this.cryptr.encrypt(JSON.stringify(updateObj.data));
}
await this.sequelizeDb.Connections.update(updateObj, { where: { id } });
return this.findOneById(id);
}
}
module.exports = Connections;