-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathsession.go
More file actions
411 lines (348 loc) · 9.68 KB
/
Copy pathsession.go
File metadata and controls
411 lines (348 loc) · 9.68 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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//nolint:dupl // Session methods intentionally mirror DB methods with similar structure
package surrealdb
import (
"context"
"sync"
"github.com/gofrs/uuid"
"github.com/surrealdb/surrealdb.go/pkg/connection"
"github.com/surrealdb/surrealdb.go/pkg/constants"
"github.com/surrealdb/surrealdb.go/pkg/models"
)
// Session represents an additional SurrealDB session on a WebSocket connection.
// Sessions scope live notifications and can have their own transactions.
//
// Sessions are only supported on WebSocket connections (SurrealDB v3+).
// Each session starts unauthenticated and without a selected namespace/database,
// so you must call SignIn/Authenticate and Use after creating a session.
//
// Session satisfies the sendable constraint, so all surrealdb.Query,
// surrealdb.Create, etc. functions work with sessions directly.
type Session struct {
db *DB
id *models.UUID
closed bool
mu sync.RWMutex
}
// Attach creates a new session on the WebSocket connection.
// Sessions are only supported on WebSocket connections (SurrealDB v3+).
//
// The new session starts unauthenticated and without a selected namespace/database.
// You must call SignIn/Authenticate and Use on the session before making queries.
//
// Example:
//
// session, err := db.Attach(ctx)
// if err != nil {
// return err
// }
// defer session.Detach(ctx)
//
// // Authenticate the session
// _, err = session.SignIn(ctx, Auth{Username: "root", Password: "root"})
// if err != nil {
// return err
// }
//
// // Select namespace and database
// err = session.Use(ctx, "test", "test")
// if err != nil {
// return err
// }
//
// // Now the session is ready for queries
// results, err := surrealdb.Query[[]User](ctx, session, "SELECT * FROM users", nil)
func (db *DB) Attach(ctx context.Context) (*Session, error) {
// Check if the connection is a WebSocket connection
if _, ok := db.con.(connection.WebSocketConnection); !ok {
return nil, constants.ErrSessionsNotSupported
}
// Generate a new UUID for the session
newUUID, err := uuid.NewV4()
if err != nil {
return nil, err
}
sessionID := models.UUID{UUID: newUUID}
// Send the attach RPC request with the session UUID as a top-level field
req := &connection.RPCRequest{
Method: string(connection.Attach),
Session: &sessionID,
}
var res connection.RPCResponse[any]
if err := connection.Call(db.con, ctx, &res, req); err != nil {
return nil, err
}
return &Session{
db: db,
id: &sessionID,
}, nil
}
// ID returns the session's UUID.
func (s *Session) ID() *models.UUID {
return s.id
}
// Detach deletes the session from the server.
// After calling Detach, the session cannot be used anymore.
func (s *Session) Detach(ctx context.Context) error {
s.mu.Lock()
defer s.mu.Unlock()
if s.closed {
return constants.ErrSessionClosed
}
// Send the detach RPC request
req := &connection.RPCRequest{
Method: string(connection.Detach),
Session: s.id,
}
var res connection.RPCResponse[any]
if err := connection.Call(s.db.con, ctx, &res, req); err != nil {
return err
}
s.closed = true
return nil
}
// Begin starts a new interactive transaction in this session.
// Interactive transactions are only supported on WebSocket connections (SurrealDB v3+).
func (s *Session) Begin(ctx context.Context) (*Transaction, error) {
s.mu.RLock()
if s.closed {
s.mu.RUnlock()
return nil, constants.ErrSessionClosed
}
s.mu.RUnlock()
// Send the begin RPC request with the session UUID
req := &connection.RPCRequest{
Method: string(connection.Begin),
Session: s.id,
}
var res connection.RPCResponse[models.UUID]
if err := connection.Call(s.db.con, ctx, &res, req); err != nil {
return nil, err
}
return &Transaction{
db: s.db,
id: res.Result,
sessionID: s.id,
}, nil
}
// SignUp signs up a new user in this session.
func (s *Session) SignUp(ctx context.Context, authData any) (string, error) {
s.mu.RLock()
if s.closed {
s.mu.RUnlock()
return "", constants.ErrSessionClosed
}
s.mu.RUnlock()
req := &connection.RPCRequest{
Method: string(connection.SignUp),
Params: []any{authData},
Session: s.id,
}
var res connection.RPCResponse[string]
if err := connection.Call(s.db.con, ctx, &res, req); err != nil {
return "", err
}
if res.Result == nil {
return "", nil
}
return *res.Result, nil
}
// SignUpWithRefresh signs up a new user using a TYPE RECORD access method with WITH REFRESH enabled.
func (s *Session) SignUpWithRefresh(ctx context.Context, authData any) (*Tokens, error) {
s.mu.RLock()
if s.closed {
s.mu.RUnlock()
return nil, constants.ErrSessionClosed
}
s.mu.RUnlock()
req := &connection.RPCRequest{
Method: string(connection.SignUp),
Params: []any{authData},
Session: s.id,
}
var res connection.RPCResponse[Tokens]
if err := connection.Call(s.db.con, ctx, &res, req); err != nil {
return nil, err
}
return res.Result, nil
}
// SignIn signs in an existing user in this session.
func (s *Session) SignIn(ctx context.Context, authData any) (string, error) {
s.mu.RLock()
if s.closed {
s.mu.RUnlock()
return "", constants.ErrSessionClosed
}
s.mu.RUnlock()
req := &connection.RPCRequest{
Method: string(connection.SignIn),
Params: []any{authData},
Session: s.id,
}
var res connection.RPCResponse[string]
if err := connection.Call(s.db.con, ctx, &res, req); err != nil {
return "", err
}
if res.Result == nil {
return "", nil
}
return *res.Result, nil
}
// SignInWithRefresh signs in using a TYPE RECORD access method with WITH REFRESH enabled.
func (s *Session) SignInWithRefresh(ctx context.Context, authData any) (*Tokens, error) {
s.mu.RLock()
if s.closed {
s.mu.RUnlock()
return nil, constants.ErrSessionClosed
}
s.mu.RUnlock()
req := &connection.RPCRequest{
Method: string(connection.SignIn),
Params: []any{authData},
Session: s.id,
}
var res connection.RPCResponse[Tokens]
if err := connection.Call(s.db.con, ctx, &res, req); err != nil {
return nil, err
}
return res.Result, nil
}
// Authenticate authenticates the session with the provided token.
func (s *Session) Authenticate(ctx context.Context, token string) error {
s.mu.RLock()
if s.closed {
s.mu.RUnlock()
return constants.ErrSessionClosed
}
s.mu.RUnlock()
req := &connection.RPCRequest{
Method: string(connection.Authenticate),
Params: []any{token},
Session: s.id,
}
var res connection.RPCResponse[any]
if err := connection.Call(s.db.con, ctx, &res, req); err != nil {
return err
}
return nil
}
// Invalidate invalidates the authentication for this session.
func (s *Session) Invalidate(ctx context.Context) error {
s.mu.RLock()
if s.closed {
s.mu.RUnlock()
return constants.ErrSessionClosed
}
s.mu.RUnlock()
req := &connection.RPCRequest{
Method: string(connection.Invalidate),
Session: s.id,
}
var res connection.RPCResponse[any]
if err := connection.Call(s.db.con, ctx, &res, req); err != nil {
return err
}
return nil
}
// Use selects the namespace and database for this session.
func (s *Session) Use(ctx context.Context, ns, database string) error {
s.mu.RLock()
if s.closed {
s.mu.RUnlock()
return constants.ErrSessionClosed
}
s.mu.RUnlock()
req := &connection.RPCRequest{
Method: string(connection.Use),
Params: []any{ns, database},
Session: s.id,
}
var res connection.RPCResponse[any]
if err := connection.Call(s.db.con, ctx, &res, req); err != nil {
return err
}
return nil
}
// Let sets a variable in this session.
func (s *Session) Let(ctx context.Context, key string, val any) error {
s.mu.RLock()
if s.closed {
s.mu.RUnlock()
return constants.ErrSessionClosed
}
s.mu.RUnlock()
req := &connection.RPCRequest{
Method: string(connection.Let),
Params: []any{key, val},
Session: s.id,
}
var res connection.RPCResponse[any]
if err := connection.Call(s.db.con, ctx, &res, req); err != nil {
return err
}
return nil
}
// Unset removes a variable from this session.
func (s *Session) Unset(ctx context.Context, key string) error {
s.mu.RLock()
if s.closed {
s.mu.RUnlock()
return constants.ErrSessionClosed
}
s.mu.RUnlock()
req := &connection.RPCRequest{
Method: string(connection.Unset),
Params: []any{key},
Session: s.id,
}
var res connection.RPCResponse[any]
if err := connection.Call(s.db.con, ctx, &res, req); err != nil {
return err
}
return nil
}
// Info returns information about the current session state.
func (s *Session) Info(ctx context.Context) (map[string]any, error) {
s.mu.RLock()
if s.closed {
s.mu.RUnlock()
return nil, constants.ErrSessionClosed
}
s.mu.RUnlock()
req := &connection.RPCRequest{
Method: string(connection.Info),
Session: s.id,
}
var res connection.RPCResponse[map[string]any]
if err := connection.Call(s.db.con, ctx, &res, req); err != nil {
return nil, err
}
if res.Result == nil {
return nil, nil
}
return *res.Result, nil
}
// Version returns the SurrealDB version information.
func (s *Session) Version(ctx context.Context) (*VersionData, error) {
s.mu.RLock()
if s.closed {
s.mu.RUnlock()
return nil, constants.ErrSessionClosed
}
s.mu.RUnlock()
// Version doesn't need session context, but we include it for consistency
return s.db.Version(ctx)
}
// LiveNotifications returns a channel for receiving live query notifications.
func (s *Session) LiveNotifications(liveQueryID string) (chan connection.Notification, error) {
return s.db.con.LiveNotifications(liveQueryID)
}
// CloseLiveNotifications closes the notification channel for a live query.
func (s *Session) CloseLiveNotifications(liveQueryID string) error {
return s.db.con.CloseLiveNotifications(liveQueryID)
}
// isClosed returns whether the session is closed (for internal use by send function).
func (s *Session) isClosed() bool {
s.mu.RLock()
defer s.mu.RUnlock()
return s.closed
}