This repository has been archived by the owner on Jan 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
248 lines (228 loc) · 7.08 KB
/
mod.ts
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import Surreal, { Auth, Patch, Result } from "https://deno.land/x/[email protected]/mod.ts";
type TokenInput = string | undefined | null | void;
export default class AwaitedSurreal {
public readonly endpoint: string;
public readonly instance: Surreal;
private queue: Promise<unknown>[] = [];
public constructor({
endpoint,
namespace,
database,
token,
initialAuthSuccess,
initialAuthFailed,
}: {
endpoint: string;
namespace: string;
database: string;
token?: TokenInput | (() => Promise<TokenInput>);
initialAuthSuccess?: () => unknown;
initialAuthFailed?: (e: any) => unknown;
}) {
this.endpoint = new URL(endpoint).origin;
const instance = new Surreal(`${this.endpoint}/rpc`);
this.instance = instance;
this.queue.push(
(async () => {
await instance.use(namespace, database);
token = typeof token == "function" ? await token() : token;
if (token)
await instance
.authenticate(token)
.then(() => initialAuthSuccess?.())
.catch((e) => {
console.log("Failed to authenticate user with provided token");
initialAuthFailed?.(e);
});
})()
);
}
public async waitForConnection(): Promise<true> {
await Promise.allSettled(this.queue);
return true;
}
/**
* Switch to a specific namespace and database.
* @param ns - Switches to a specific namespace.
* @param db - Switches to a specific database.
*/
public async use(ns: string, db: string): Promise<void> {
await this.waitForConnection();
const task = this.instance.use(ns, db);
this.queue.push(task);
return task;
}
/**
* Signs up to a specific authentication scope.
* @param vars - Variables used in a signup query.
* @return The authenication token.
*/
public async signup(vars: Auth): Promise<string> {
await this.waitForConnection();
const task = this.instance.signup(vars);
this.queue.push(task);
return task;
}
/**
* Signs in to a specific authentication scope.
* @param vars - Variables used in a signin query.
* @return The authenication token.
*/
public async signin(vars: Auth): Promise<string> {
await this.waitForConnection();
const task = this.instance.signin(vars);
this.queue.push(task);
return task;
}
/**
* Invalidates the authentication for the current connection.
*/
public async invalidate(): Promise<void> {
await this.waitForConnection();
const task = this.instance.invalidate();
this.queue.push(task);
return task;
}
/**
* Authenticates the current connection with a JWT token.
* @param token - The JWT authentication token.
*/
public async authenticate(token: string): Promise<void> {
await this.waitForConnection();
const task = this.instance.authenticate(token);
this.queue.push(task);
return task;
}
public async live(table: string): Promise<string> {
await this.waitForConnection();
return this.instance.live(table);
}
/**
* Kill a specific query.
* @param query - The query to kill.
*/
public async kill(query: string): Promise<void> {
await this.waitForConnection();
return this.instance.kill(query);
}
/**
* Define a variable for the current session.
* @param key - Specifies the name of the variable.
* @param val - Assigns the value to the variable name.
*/
public async let(key: string, val: unknown): Promise<string> {
await this.waitForConnection();
return this.instance.let(key, val);
}
/**
* Runs a set of SurrealQL statements against the database.
* @param query - Specifies the SurrealQL statements.
* @param vars - Assigns variables which can be used in the query.
*/
public async query<T = Result[]>(
query: string,
vars?: Record<string, unknown>
): Promise<T> {
await this.waitForConnection();
return this.instance.query<T>(query, vars);
}
/**
* Runs a set of SurrealQL statements against the database. Typing for this function is opiniated.
* @param query - Specifies the SurrealQL statements.
* @param vars - Assigns variables which can be used in the query.
*/
public async opiniatedQuery<T = unknown>(
query: string,
vars?: Record<string, unknown>
): Promise<Result<T[]>[]> {
await this.waitForConnection();
return this.instance.query<Result<T[]>[]>(query, vars);
}
/**
* Selects all records in a table, or a specific record, from the database.
* @param thing - The table name or a record ID to select.
*/
public async select<T>(thing: string): Promise<T[]> {
await this.waitForConnection();
return this.instance.select<T>(thing);
}
/**
* Creates a record in the database.
* @param thing - The table name or the specific record ID to create.
* @param data - The document / record data to insert.
*/
public async create<T extends Record<string, unknown>>(
thing: string,
data?: T
): Promise<
T & {
id: string;
}
> {
await this.waitForConnection();
return this.instance.create<T>(thing, data);
}
/**
* Updates all records in a table, or a specific record, in the database.
*
* ***NOTE: This function replaces the current document / record data with the specified data.***
* @param thing - The table name or the specific record ID to update.
* @param data - The document / record data to insert.
*/
public async update<T extends Record<string, unknown>>(
thing: string,
data?: T
): Promise<
T & {
id: string;
}
> {
await this.waitForConnection();
return this.instance.update<T>(thing, data);
}
/**
* Modifies all records in a table, or a specific record, in the database.
*
* ***NOTE: This function merges the current document / record data with the specified data.***
* @param thing - The table name or the specific record ID to change.
* @param data - The document / record data to insert.
*/
public async change<
T extends Record<string, unknown>,
U extends Record<string, unknown> = T
>(
thing: string,
data?: Partial<T> & U
): Promise<
| (T &
U & {
id: string;
})
| (T &
U & {
id: string;
})[]
> {
await this.waitForConnection();
return this.instance.change<T, U>(thing, data);
}
/**
* Applies JSON Patch changes to all records, or a specific record, in the database.
*
* ***NOTE: This function patches the current document / record data with the specified JSON Patch data.***
* @param thing - The table name or the specific record ID to modify.
* @param data - The JSON Patch data with which to modify the records.
*/
public async modify(thing: string, data?: Patch[]): Promise<Patch[]> {
await this.waitForConnection();
return this.instance.modify(thing, data);
}
/**
* Deletes all records in a table, or a specific record, from the database.
* @param thing - The table name or a record ID to select.
*/
public async delete(thing: string): Promise<void> {
await this.waitForConnection();
return this.instance.delete(thing);
}
}