-
Notifications
You must be signed in to change notification settings - Fork 3
/
DbConnectionsManager.js
139 lines (124 loc) · 4.3 KB
/
DbConnectionsManager.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
import { decorate, observable, flow, action, computed } from 'mobx';
import { boundMethod } from 'autobind-decorator'
import keytar from 'keytar';
import persistentStorage from '../persistentStorage';
import DbConnection from './DbConnection';
import type { TDbConnectionData } from './DbConnection';
import DbTable from './DbTable';
import DbSchema from './DbSchema';
class DbConnectionsManager {
constructor() {
this.currentConnectionName = '';
this.editingConnectionName = '';
this.currentSchemaName = '';
this.currentTableName = '';
this.currentRoleName = '';
this.connections = {};
this.connectionsNames = [];
this.error = null;
}
addConnection(connectionData: TDbConnectionData, fromStorage = false) {
const { password, ...publicConnectionData } = connectionData;
const connection = new DbConnection(connectionData);
const rewrite = this.connections[connection.name];
this.connections[connection.name] = connection;
if (!rewrite) {
this.connectionsNames.push(connection.name);
}
if (!fromStorage) {
persistentStorage.set('connections', {
...(persistentStorage.get('connections') || {}),
[connection.name]: publicConnectionData,
});
return keytar.setPassword(connectionData.name, connectionData.user, password);
}
}
addConnections(connectionsData, fromStorage) {
Object.keys(connectionsData).forEach(key => {
this.addConnection(connectionsData[key], fromStorage);
});
}
getCurrentConnection(): DbConnection {
return this.connections[this.currentConnectionName];
}
connect = flow(function* connect({ connectionName }) {
this.currentConnectionName = connectionName;
const connection = this.getCurrentConnection();
yield Promise.all([
connection.fetchSchemas(),
connection.fetchRoles(),
]);
if (!connection.isFetched) {
this.currentConnectionName = '';
this.error = connection.schemasFetcher.error || connection.rolesFetcher.error;
return this;
}
this.currentRoleName = connection.user;
const firstSchemaName = connection.schemasNames.find((name) => name === 'public') || connection.schemasNames[0];
yield this.setCurrentSchema({ schemaName: firstSchemaName });
return this;
}).bind(this);
getCurrentSchema(): DbSchema {
return this.getCurrentConnection().schemas[this.currentSchemaName];
}
setCurrentSchema = flow(function* setCurrentSchema({ schemaName }) {
this.currentSchemaName = schemaName;
const schema = this.getCurrentSchema();
if (!schema.tablesFetcher.inSuccessState) {
yield schema.fetchTables();
}
this.error = schema.tablesFetcher.error;
if (schema.tablesNames.length) {
yield this.setCurrentTable({ tableName: schema.tablesNames[0] });
}
}).bind(this);
getCurrentTable(): ?DbTable {
return this.getCurrentSchema().tables[this.currentTableName];
}
setCurrentTable = flow(function* setCurrentTable({ tableName }) {
this.currentTableName = tableName;
const table = this.getCurrentTable();
if (table.isFetched) {
return;
}
yield Promise.all([
table.fetchColumns(),
table.fetchPrivileges(),
table.fetchPolicies(),
]);
this.error = table.columnsFetcher.error || table.privilegesFetcher.error || table.policiesFetcher.error;
}).bind(this);
setCurrentRole = flow(function* setCurrentRole({ roleName }) {
this.currentRoleName = roleName;
}).bind(this);
setEditingConnectionName (connectionName) {
this.editingConnectionName = connectionName;
};
getEditingConnection() {
return this.connections[this.editingConnectionName];
}
get isFetched() {
try {
this.getCurrentTable().isFetched;
} catch (error) {
return false;
}
}
}
decorate(DbConnectionsManager, {
currentConnectionName: observable,
editingConnectionName: observable,
currentSchemaName: observable,
currentTableName: observable,
currentRoleName: observable,
connections: observable,
connectionsNames: observable,
addConnection: action.bound,
addConnections: action.bound,
getCurrentConnection: [boundMethod, computed],
getCurrentSchema: [boundMethod, computed],
getCurrentTable: [boundMethod, computed],
getEditingConnection: [boundMethod, computed],
isFetched: computed,
});
export default DbConnectionsManager;