Class: SurrealDB::Client
- Inherits:
-
Object
- Object
- SurrealDB::Client
- Defined in:
- lib/surrealdb/client.rb
Overview
Main client for interacting with SurrealDB.
Provides a unified API across WebSocket and HTTP transports, selected automatically based on the connection URL scheme.
Instance Attribute Summary collapse
- #connection ⇒ Connections::Base readonly
Instance Method Summary collapse
-
#attach ⇒ String
Attaches a new session to the connection.
-
#authenticate(token) ⇒ void
Authenticates with an existing JWT token.
-
#begin_transaction ⇒ void
Begins a transaction on the current session.
-
#cancel ⇒ void
Cancels (rolls back) the current transaction.
-
#close ⇒ void
Closes the connection.
-
#commit ⇒ void
Commits the current transaction.
-
#connect ⇒ self
Opens the connection to SurrealDB.
- #connected? ⇒ Boolean
-
#create(resource, data = nil) ⇒ Hash
Creates a new record.
-
#delete(resource) ⇒ Object
Deletes a record or all records from a table.
-
#detach(session_id) ⇒ void
Detaches a session from the connection.
-
#export_data ⇒ String
Exports the database as SurrealQL.
-
#import_data(content) ⇒ Object
Imports SurrealQL data or ML model data into the database.
-
#info ⇒ Hash?
Returns info about the current session.
-
#initialize(url, **options) ⇒ Client
constructor
A new instance of Client.
-
#insert(table, data) ⇒ Array
Inserts one or more records into a table.
-
#insert_relation(table, data) ⇒ Array
Inserts a relation record.
-
#invalidate ⇒ void
Invalidates the current session.
-
#kill(live_query_id) ⇒ void
Kills a live query.
-
#live(resource, diff: false) ⇒ String
Starts a live query on a table (WebSocket only).
-
#merge(resource, data) ⇒ Object
Deep-merges data into a record.
-
#patch(resource, patches) ⇒ Object
Applies JSON Patch operations to a record.
-
#query(sql, vars = {}) ⇒ Array
Executes a SurrealQL query.
-
#query_raw(sql, vars = {}) ⇒ Array<QueryResult>
Executes a SurrealQL query and returns structured per-statement results.
-
#relate(from, relation, to, data = nil) ⇒ Object
Creates a graph relation between two records.
-
#run(function_name, *args, version: nil) ⇒ Object
Runs a SurrealDB function.
-
#select(resource) ⇒ Array, Hash
Selects all records from a table, or a single record by ID.
-
#send_rpc(method, params = []) ⇒ Object
Sends an arbitrary RPC method call.
-
#set(key, value) ⇒ void
(also: #let)
Sets a connection-scoped variable.
-
#signin(credentials = {}) ⇒ String, Hash
Signs in to SurrealDB.
-
#signup(credentials = {}) ⇒ String, Hash
Signs up a new user.
-
#subscribe(live_query_id) {|Hash| ... } ⇒ void
Subscribes to live query notifications.
-
#unset(key) ⇒ void
Removes a connection-scoped variable.
-
#update(resource, data) ⇒ Object
Replaces a record or all records in a table.
-
#upsert(resource, data) ⇒ Object
Upserts a record (insert or update).
-
#use(namespace, database) ⇒ void
Selects the namespace and database to use.
-
#version ⇒ String
Returns the SurrealDB server version.
Constructor Details
#initialize(url, **options) ⇒ Client
Returns a new instance of Client.
24 25 26 27 28 |
# File 'lib/surrealdb/client.rb', line 24 def initialize(url, **) @url = url @options = @connection = build_connection(url, **) end |
Instance Attribute Details
#connection ⇒ Connections::Base (readonly)
20 21 22 |
# File 'lib/surrealdb/client.rb', line 20 def connection @connection end |
Instance Method Details
#attach ⇒ String
Attaches a new session to the connection.
287 288 289 290 |
# File 'lib/surrealdb/client.rb', line 287 def attach require_websocket!('sessions') @connection.send_request(Protocol::Methods::ATTACH) end |
#authenticate(token) ⇒ void
This method returns an undefined value.
Authenticates with an existing JWT token.
77 78 79 |
# File 'lib/surrealdb/client.rb', line 77 def authenticate(token) @connection.send_request(Protocol::Methods::AUTHENTICATE, [token]) end |
#begin_transaction ⇒ void
This method returns an undefined value.
Begins a transaction on the current session.
302 303 304 305 |
# File 'lib/surrealdb/client.rb', line 302 def begin_transaction require_websocket!('transactions') @connection.send_request(Protocol::Methods::BEGIN_TXN) end |
#cancel ⇒ void
This method returns an undefined value.
Cancels (rolls back) the current transaction.
316 317 318 319 |
# File 'lib/surrealdb/client.rb', line 316 def cancel require_websocket!('transactions') @connection.send_request(Protocol::Methods::CANCEL) end |
#close ⇒ void
This method returns an undefined value.
Closes the connection.
39 40 41 |
# File 'lib/surrealdb/client.rb', line 39 def close @connection.close end |
#commit ⇒ void
This method returns an undefined value.
Commits the current transaction.
309 310 311 312 |
# File 'lib/surrealdb/client.rb', line 309 def commit require_websocket!('transactions') @connection.send_request(Protocol::Methods::COMMIT) end |
#connect ⇒ self
Opens the connection to SurrealDB.
32 33 34 35 |
# File 'lib/surrealdb/client.rb', line 32 def connect @connection.connect self end |
#connected? ⇒ Boolean
44 45 46 |
# File 'lib/surrealdb/client.rb', line 44 def connected? @connection.connected? end |
#create(resource, data = nil) ⇒ Hash
Creates a new record.
118 119 120 121 122 |
# File 'lib/surrealdb/client.rb', line 118 def create(resource, data = nil) params = [resource_param(resource)] params << data unless data.nil? @connection.send_request(Protocol::Methods::CREATE, params) end |
#delete(resource) ⇒ Object
Deletes a record or all records from a table.
175 176 177 |
# File 'lib/surrealdb/client.rb', line 175 def delete(resource) @connection.send_request(Protocol::Methods::DELETE, [resource_param(resource)]) end |
#detach(session_id) ⇒ void
This method returns an undefined value.
Detaches a session from the connection.
295 296 297 298 |
# File 'lib/surrealdb/client.rb', line 295 def detach(session_id) require_websocket!('sessions') @connection.send_request(Protocol::Methods::DETACH, [session_id]) end |
#export_data ⇒ String
Exports the database as SurrealQL.
265 266 267 |
# File 'lib/surrealdb/client.rb', line 265 def export_data @connection.send_request(Protocol::Methods::EXPORT) end |
#import_data(content) ⇒ Object
Imports SurrealQL data or ML model data into the database.
259 260 261 |
# File 'lib/surrealdb/client.rb', line 259 def import_data(content) @connection.send_request(Protocol::Methods::IMPORT, [content]) end |
#info ⇒ Hash?
Returns info about the current session.
279 280 281 |
# File 'lib/surrealdb/client.rb', line 279 def info @connection.send_request(Protocol::Methods::INFO) end |
#insert(table, data) ⇒ Array
Inserts one or more records into a table.
128 129 130 |
# File 'lib/surrealdb/client.rb', line 128 def insert(table, data) @connection.send_request(Protocol::Methods::INSERT, [table_param(table), data]) end |
#insert_relation(table, data) ⇒ Array
Inserts a relation record.
136 137 138 |
# File 'lib/surrealdb/client.rb', line 136 def insert_relation(table, data) @connection.send_request(Protocol::Methods::INSERT_RELATION, [table_param(table), data]) end |
#invalidate ⇒ void
This method returns an undefined value.
Invalidates the current session.
83 84 85 |
# File 'lib/surrealdb/client.rb', line 83 def invalidate @connection.send_request(Protocol::Methods::INVALIDATE) end |
#kill(live_query_id) ⇒ void
This method returns an undefined value.
Kills a live query.
248 249 250 251 252 |
# File 'lib/surrealdb/client.rb', line 248 def kill(live_query_id) require_live_queries! @connection.remove_notification_handler(live_query_id.to_s) @connection.send_request(Protocol::Methods::KILL, [live_query_id]) end |
#live(resource, diff: false) ⇒ String
Starts a live query on a table (WebSocket only).
231 232 233 234 |
# File 'lib/surrealdb/client.rb', line 231 def live(resource, diff: false) require_live_queries! @connection.send_request(Protocol::Methods::LIVE, [table_param(resource), diff]) end |
#merge(resource, data) ⇒ Object
Deep-merges data into a record.
160 161 162 |
# File 'lib/surrealdb/client.rb', line 160 def merge(resource, data) @connection.send_request(Protocol::Methods::MERGE, [resource_param(resource), data]) end |
#patch(resource, patches) ⇒ Object
Applies JSON Patch operations to a record.
168 169 170 |
# File 'lib/surrealdb/client.rb', line 168 def patch(resource, patches) @connection.send_request(Protocol::Methods::PATCH, [resource_param(resource), patches]) end |
#query(sql, vars = {}) ⇒ Array
Executes a SurrealQL query.
197 198 199 200 201 |
# File 'lib/surrealdb/client.rb', line 197 def query(sql, vars = {}) params = [sql] params << vars unless vars.empty? @connection.send_request(Protocol::Methods::QUERY, params) end |
#query_raw(sql, vars = {}) ⇒ Array<QueryResult>
Executes a SurrealQL query and returns structured per-statement results. Unlike #query, this returns QueryResult objects with status, timing, and per-statement error information.
209 210 211 212 213 214 |
# File 'lib/surrealdb/client.rb', line 209 def query_raw(sql, vars = {}) params = [sql] params << vars unless vars.empty? raw = @connection.send_request(Protocol::Methods::QUERY, params) Array(raw).map { |stmt| QueryResult.from_response(stmt) } end |
#relate(from, relation, to, data = nil) ⇒ Object
Creates a graph relation between two records.
185 186 187 188 189 |
# File 'lib/surrealdb/client.rb', line 185 def relate(from, relation, to, data = nil) params = [resource_param(from), relation, resource_param(to)] params << data unless data.nil? @connection.send_request(Protocol::Methods::RELATE, params) end |
#run(function_name, *args, version: nil) ⇒ Object
Runs a SurrealDB function.
221 222 223 |
# File 'lib/surrealdb/client.rb', line 221 def run(function_name, *args, version: nil) @connection.send_request(Protocol::Methods::RUN, [function_name, version, args]) end |
#select(resource) ⇒ Array, Hash
Selects all records from a table, or a single record by ID.
110 111 112 |
# File 'lib/surrealdb/client.rb', line 110 def select(resource) @connection.send_request(Protocol::Methods::SELECT, [resource_param(resource)]) end |
#send_rpc(method, params = []) ⇒ Object
Sends an arbitrary RPC method call. Useful as an escape hatch for server methods not yet wrapped by the SDK.
328 329 330 |
# File 'lib/surrealdb/client.rb', line 328 def send_rpc(method, params = []) @connection.send_request(method, params) end |
#set(key, value) ⇒ void Also known as: let
This method returns an undefined value.
Sets a connection-scoped variable.
93 94 95 |
# File 'lib/surrealdb/client.rb', line 93 def set(key, value) @connection.send_request(Protocol::Methods::LET, [key, value]) end |
#signin(credentials = {}) ⇒ String, Hash
Signs in to SurrealDB.
63 64 65 |
# File 'lib/surrealdb/client.rb', line 63 def signin(credentials = {}) @connection.send_request(Protocol::Methods::SIGNIN, [credentials]) end |
#signup(credentials = {}) ⇒ String, Hash
Signs up a new user.
70 71 72 |
# File 'lib/surrealdb/client.rb', line 70 def signup(credentials = {}) @connection.send_request(Protocol::Methods::SIGNUP, [credentials]) end |
#subscribe(live_query_id) {|Hash| ... } ⇒ void
This method returns an undefined value.
Subscribes to live query notifications.
240 241 242 243 |
# File 'lib/surrealdb/client.rb', line 240 def subscribe(live_query_id, &block) require_live_queries! @connection.on_notification(live_query_id.to_s, block) end |
#unset(key) ⇒ void
This method returns an undefined value.
Removes a connection-scoped variable.
101 102 103 |
# File 'lib/surrealdb/client.rb', line 101 def unset(key) @connection.send_request(Protocol::Methods::UNSET, [key]) end |
#update(resource, data) ⇒ Object
Replaces a record or all records in a table.
144 145 146 |
# File 'lib/surrealdb/client.rb', line 144 def update(resource, data) @connection.send_request(Protocol::Methods::UPDATE, [resource_param(resource), data]) end |
#upsert(resource, data) ⇒ Object
Upserts a record (insert or update).
152 153 154 |
# File 'lib/surrealdb/client.rb', line 152 def upsert(resource, data) @connection.send_request(Protocol::Methods::UPSERT, [resource_param(resource), data]) end |