forked from sqlpad/sqlpad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection-clients.js
More file actions
74 lines (66 loc) · 2.08 KB
/
connection-clients.js
File metadata and controls
74 lines (66 loc) · 2.08 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
const ConnectionClient = require('../lib/connection-client');
/**
* ConnectionClients is a special in-memory store of connected database clients
* Once a connectionClient disconnects, it should no longer be in this store
* TODO: A UI could be built for admins to see open connections and close them
*/
class ConnectionClients {
/**
* @param {import('../sequelize-db')} sequelizeDb
* @param {import('../lib/config')} config
*/
constructor(sequelizeDb, config) {
this.sequelizeDb = sequelizeDb;
this.config = config;
this.connectionClients = [];
}
/**
* Get all connection clients
* @returns {array}
*/
findAll() {
return this.connectionClients.slice();
}
/**
* Get connected connection client by id
* @param {string} id - id of connection client
* @returns {ConnectionClient}
*/
getOneById(id) {
return this.connectionClients.find((connectionClient) => {
return connectionClient.id === id;
});
}
/**
* Create new connection client and connect
* @param {object} connection
* @param {object} user
* @returns {ConnectionClient}
*/
async createNew(connection, user) {
const connectionClient = new ConnectionClient(connection, user);
await connectionClient.connect();
connectionClient.scheduleCleanupInterval();
this.connectionClients.push(connectionClient);
return this.getOneById(connectionClient.id);
}
/**
* Disconnect connection client for id, and remove it from in-memory store.
* Operates under the assumption that the connection could have been removed since its removal was requested
* @param {string} id
*/
async disconnectForId(id) {
const connectionClient = this.getOneById(id);
// remove client from array immediately
// Disconnecting is async but in-memory state should represent what things will be
this.connectionClients = this.connectionClients.filter(
(connectionClient) => {
return connectionClient.id !== id;
}
);
if (connectionClient) {
await connectionClient.disconnect();
}
}
}
module.exports = ConnectionClients;