forked from surrealdb/surrealist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.tsx
163 lines (141 loc) · 3.88 KB
/
database.tsx
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
import { showNotification } from "@mantine/notifications";
import { store } from "./store";
import { closeSurrealConnection, getSurreal, openSurrealConnection } from "./util/connection";
import { showError } from "./util/helpers";
import { fetchDatabaseSchema } from "./util/schema";
import { Text } from "@mantine/core";
import { getActiveEnvironment, getActiveSession, isConnectionValid, mergeConnections } from "./util/environments";
import { uid } from "radash";
import { setIsConnected, setIsConnecting, setQueryActive } from "./stores/database";
import { addHistoryEntry, updateSession } from "./stores/config";
/**
* Open a new connection to the database
*
* @param details Connection details
* @param isSilent Whether to hide error notifications
*/
export function openConnection(isSilent?: boolean) {
const sessionInfo = getActiveSession();
const envInfo = getActiveEnvironment();
const connection = mergeConnections(sessionInfo?.connection || {}, envInfo?.connection || {});
const connectionValid = isConnectionValid(connection);
if (!connectionValid) {
showNotification({
color: "red.4",
bg: "red.6",
message: (
<div>
<Text color="white" weight={600}>
Invalid Connection
</Text>
<Text color="white" opacity={0.8} size="sm">
Please check your connection details
</Text>
</div>
),
});
return;
}
closeConnection();
try {
store.dispatch(setIsConnecting(true));
store.dispatch(setIsConnected(false));
openSurrealConnection({
connection,
onConnect() {
store.dispatch(setIsConnecting(false));
store.dispatch(setIsConnected(true));
fetchDatabaseSchema();
},
onDisconnect(code, reason) {
store.dispatch(setIsConnecting(false));
store.dispatch(setIsConnected(false));
if (code != 1000 && !isSilent) {
const subtitle = code === 1006 ? "Unexpected connection close" : reason || `Unknown reason`;
showNotification({
color: "red.4",
bg: "red.6",
message: (
<div>
<Text color="white" weight={600}>
Connection Closed
</Text>
<Text color="white" opacity={0.8} size="sm">
{subtitle} ({code})
</Text>
</div>
),
});
}
},
});
} catch (err: any) {
showError("Failed to open connection", err.message);
}
}
export interface QueryOptions {
override?: string;
loader?: boolean;
}
/**
* Execute a query against the active connection
*
* @param options Query options
*/
export async function executeQuery(options?: QueryOptions) {
const sessionInfo = getActiveSession();
const isRemote = sessionInfo?.connection?.method === "remote";
if (!sessionInfo || isRemote) {
const { isConnected } = store.getState().database;
if (!isConnected || !sessionInfo) {
showNotification({
message: "You must be connected to send a query",
});
return;
}
}
const { id: tabId, queries, activeQueryId, name, variables } = sessionInfo;
const activeQuery = queries.find((q) => q.id === activeQueryId);
const queryStr = options?.override?.trim() || activeQuery?.text || '';
const variableJson = variables
? JSON.parse(variables)
: undefined;
try {
if (options?.loader) {
store.dispatch(setQueryActive(true));
}
const response = await getSurreal()?.query(queryStr, variableJson);
store.dispatch(updateSession({
id: tabId,
lastResponse: response,
}));
} catch (err: any) {
store.dispatch(updateSession({
id: tabId,
lastResponse: [
{
status: "ERR",
detail: err.message,
},
],
}));
} finally {
if (options?.loader) {
store.dispatch(setQueryActive(false));
}
}
store.dispatch(addHistoryEntry({
id: uid(5),
query: queryStr,
tabName: name,
timestamp: Date.now(),
}));
}
/**
* Terminate the active connection
*/
export function closeConnection() {
closeSurrealConnection();
store.dispatch(setIsConnecting(false));
store.dispatch(setIsConnected(false));
}