surrealdb_core/rpc/
context.rs

1#[cfg(all(not(target_family = "wasm"), surrealdb_unstable))]
2use crate::gql::SchemaCache;
3use std::sync::Arc;
4use tokio::sync::Semaphore;
5use uuid::Uuid;
6
7use super::Data;
8use super::Method;
9use super::RpcError;
10use super::RpcProtocolV1;
11use super::RpcProtocolV2;
12use crate::dbs::Session;
13use crate::kvs::Datastore;
14use crate::sql::Array;
15
16#[allow(async_fn_in_trait)]
17pub trait RpcContext {
18	/// The datastore for this RPC interface
19	fn kvs(&self) -> &Datastore;
20	/// Retrieves the modification lock for this RPC context
21	fn lock(&self) -> Arc<Semaphore>;
22	/// The current session for this RPC context
23	fn session(&self) -> Arc<Session>;
24	/// Mutable access to the current session for this RPC context
25	fn set_session(&self, session: Arc<Session>);
26	/// The version information for this RPC context
27	fn version_data(&self) -> Data;
28
29	// ------------------------------
30	// Realtime
31	// ------------------------------
32
33	/// Live queries are disabled by default
34	const LQ_SUPPORT: bool = false;
35
36	/// Handles the execution of a LIVE statement
37	fn handle_live(&self, _lqid: &Uuid) -> impl std::future::Future<Output = ()> + Send {
38		async { unimplemented!("handle_live function must be implemented if LQ_SUPPORT = true") }
39	}
40	/// Handles the execution of a KILL statement
41	fn handle_kill(&self, _lqid: &Uuid) -> impl std::future::Future<Output = ()> + Send {
42		async { unimplemented!("handle_kill function must be implemented if LQ_SUPPORT = true") }
43	}
44	/// Handles the cleanup of live queries
45	fn cleanup_lqs(&self) -> impl std::future::Future<Output = ()> + Send {
46		async { unimplemented!("cleanup_lqs function must be implemented if LQ_SUPPORT = true") }
47	}
48
49	// ------------------------------
50	// GraphQL
51	// ------------------------------
52
53	/// GraphQL queries are disabled by default
54	#[cfg(all(not(target_family = "wasm"), surrealdb_unstable))]
55	const GQL_SUPPORT: bool = false;
56
57	/// Returns the GraphQL schema cache used in GraphQL queries
58	#[cfg(all(not(target_family = "wasm"), surrealdb_unstable))]
59	fn graphql_schema_cache(&self) -> &SchemaCache {
60		unimplemented!("graphql_schema_cache function must be implemented if GQL_SUPPORT = true")
61	}
62
63	// ------------------------------
64	// Method execution
65	// ------------------------------
66
67	/// Executes a method on this RPC implementation
68	async fn execute(
69		&self,
70		version: Option<u8>,
71		method: Method,
72		params: Array,
73	) -> Result<Data, RpcError>
74	where
75		Self: RpcProtocolV1,
76		Self: RpcProtocolV2,
77	{
78		match version {
79			Some(1) => RpcProtocolV1::execute(self, method, params).await,
80			Some(2) => RpcProtocolV2::execute(self, method, params).await,
81			_ => RpcProtocolV1::execute(self, method, params).await,
82		}
83	}
84}