|
32 | 32 | # --- v3+ Sessions (attach/detach) --- |
33 | 33 |
|
34 | 34 | """ |
35 | | - attach!(client) |
| 35 | + attach!(client) -> SurrealSession |
36 | 36 |
|
37 | | -Create a new ephemeral session on the server (SurrealDB v3+). |
| 37 | +Create a new ephemeral session on the server (SurrealDB v3+) and return a |
| 38 | +[`SurrealSession`](@ref) wrapper. The session is independent: its own |
| 39 | +namespace, database, auth, and variables. Close with [`close!`](@ref). |
38 | 40 |
|
39 | | -Returns a `UUID` session identifier. The session is independent — it has its |
40 | | -own namespace, database, auth, and variables. Use [`detach!`](@ref) to clean up. |
| 41 | +Matches the wrapped-session API used by surrealdb-go (`db.Attach`), |
| 42 | +surrealdb-py (`AsyncSurrealSession` / `BlockingSurrealSession`), and |
| 43 | +surrealdb-js (`newSession`). |
41 | 44 |
|
42 | 45 | WebSocket-only (not supported on HTTP connections). |
43 | 46 | """ |
44 | | -function attach!(client::SurrealClient{<:RemoteWSConnection}) |
| 47 | +function attach!(client::SurrealClient{C}) where {C<:RemoteWSConnection} |
45 | 48 | sid = UUIDs.uuid4() |
46 | 49 | _rpc_call(client, "attach", Any[]; session=sid) |
47 | | - return sid |
| 50 | + return SurrealSession{C}(client, sid) |
48 | 51 | end |
49 | 52 |
|
50 | 53 | """ |
51 | 54 | detach!(client, session_id::UUID) |
52 | 55 |
|
53 | | -Destroy a server-side session (SurrealDB v3+). |
54 | | -
|
55 | | -After detaching, the session cannot be used for further operations. |
| 56 | +Destroy a server-side session by raw UUID (SurrealDB v3+). Prefer |
| 57 | +[`close!`](@ref) on a [`SurrealSession`](@ref); use this when you only have |
| 58 | +a bare UUID (e.g. from [`sessions`](@ref) listing). |
56 | 59 | """ |
57 | 60 | function detach!(client::SurrealClient{<:RemoteWSConnection}, session_id) |
58 | 61 | _rpc_call(client, "detach", Any[]; session=session_id) |
@@ -102,6 +105,19 @@ mutable struct SurrealSession{C<:AbstractConnection} |
102 | 105 | session_id::UUID |
103 | 106 | end |
104 | 107 |
|
| 108 | +Base.show(io::IO, s::SurrealSession) = print(io, "SurrealSession(", s.session_id, ")") |
| 109 | + |
| 110 | +""" |
| 111 | + close!(session::SurrealSession) |
| 112 | +
|
| 113 | +Destroy the server-side session. After closing, the session must not be used. |
| 114 | +Wraps [`detach!`](@ref). |
| 115 | +""" |
| 116 | +function close!(session::SurrealSession{<:RemoteWSConnection}) |
| 117 | + detach!(session.client, session.session_id) |
| 118 | + return nothing |
| 119 | +end |
| 120 | + |
105 | 121 | """ |
106 | 122 | begin!(session::SurrealSession) |
107 | 123 |
|
|
0 commit comments