Skip to content

Commit 52642ca

Browse files
committed
session: attach! returns SurrealSession wrapper
1 parent 0ebbf7c commit 52642ca

2 files changed

Lines changed: 32 additions & 16 deletions

File tree

src/session.jl

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,30 @@ end
3232
# --- v3+ Sessions (attach/detach) ---
3333

3434
"""
35-
attach!(client)
35+
attach!(client) -> SurrealSession
3636
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).
3840
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`).
4144
4245
WebSocket-only (not supported on HTTP connections).
4346
"""
44-
function attach!(client::SurrealClient{<:RemoteWSConnection})
47+
function attach!(client::SurrealClient{C}) where {C<:RemoteWSConnection}
4548
sid = UUIDs.uuid4()
4649
_rpc_call(client, "attach", Any[]; session=sid)
47-
return sid
50+
return SurrealSession{C}(client, sid)
4851
end
4952

5053
"""
5154
detach!(client, session_id::UUID)
5255
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).
5659
"""
5760
function detach!(client::SurrealClient{<:RemoteWSConnection}, session_id)
5861
_rpc_call(client, "detach", Any[]; session=session_id)
@@ -102,6 +105,19 @@ mutable struct SurrealSession{C<:AbstractConnection}
102105
session_id::UUID
103106
end
104107

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+
105121
"""
106122
begin!(session::SurrealSession)
107123

test/test_session.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,17 @@ end
7777

7878
# --- v3+ Sessions (attach/detach) ---
7979

80-
@testset "attach!/detach!/sessions roundtrip (v3+)" begin
80+
@testset "attach!/close!/sessions roundtrip (v3+)" begin
8181
# v2 doesn't implement the attach RPC; gate via version probe.
8282
ver = try; string(SurrealDB.version(client)); catch; ""; end
8383
if occursin(r"surrealdb-2\.", ver)
84-
@info "Skipping attach!/detach! — v2 server doesn't support v3 sessions"
84+
@info "Skipping attach!/close! — v2 server doesn't support v3 sessions"
8585
else
86-
sid = SurrealDB.attach!(client)
87-
@test sid isa UUIDs.UUID
88-
@test sid in SurrealDB.sessions(client)
89-
SurrealDB.detach!(client, sid)
90-
@test !(sid in SurrealDB.sessions(client))
86+
sess = SurrealDB.attach!(client)
87+
@test sess isa SurrealDB.SurrealSession
88+
@test sess.session_id in SurrealDB.sessions(client)
89+
SurrealDB.close!(sess)
90+
@test !(sess.session_id in SurrealDB.sessions(client))
9191
end
9292
end
9393

0 commit comments

Comments
 (0)