diff --git a/crates/bindings/src/lib.rs b/crates/bindings/src/lib.rs index 62b78e14be4..f079c9a99d4 100644 --- a/crates/bindings/src/lib.rs +++ b/crates/bindings/src/lib.rs @@ -912,6 +912,8 @@ pub use spacetimedb_bindings_macro::view; pub struct QueryBuilder {} pub use query_builder::{Query, RawQuery}; +use crate::http::{HandlerContext, HttpClient}; + /// One of two possible types that can be passed as the first argument to a `#[view]`. /// The other is [`ViewContext`]. /// Use this type if the view does not depend on the caller's identity. @@ -1440,6 +1442,7 @@ impl ProcedureContext { } /// A handle on a database with a particular table schema. +#[deprecated(note = "Use the capability based traits (CtxDbRead, CtxDbWrite) instead!")] pub trait DbContext { /// A view into the tables of a database. /// @@ -1461,6 +1464,7 @@ pub trait DbContext { fn db_read_only(&self) -> &LocalReadOnly; } +#[allow(deprecated)] impl DbContext for AnonymousViewContext { type DbView = LocalReadOnly; @@ -1473,6 +1477,7 @@ impl DbContext for AnonymousViewContext { } } +#[allow(deprecated)] impl DbContext for ReducerContext { type DbView = Local; @@ -1485,6 +1490,7 @@ impl DbContext for ReducerContext { } } +#[allow(deprecated)] impl DbContext for TxContext { type DbView = Local; @@ -1497,6 +1503,7 @@ impl DbContext for TxContext { } } +#[allow(deprecated)] impl DbContext for ViewContext { type DbView = LocalReadOnly; @@ -1526,6 +1533,111 @@ impl Local { } } +/// This trait allows you to be generic over all contexts that allow you to read from the db. +/// including views, reducers and even procedures and http handlers through [TxContext]. +/// Useful when trying to encapsulate logic in reusable parts. +pub trait CtxDbRead { + fn db_read_only(&self) -> &LocalReadOnly; +} + +impl CtxDbRead for TxContext { + fn db_read_only(&self) -> &LocalReadOnly { + &LocalReadOnly {} + } +} + +impl CtxDbRead for ReducerContext { + fn db_read_only(&self) -> &LocalReadOnly { + &LocalReadOnly {} + } +} + +impl CtxDbRead for ViewContext { + fn db_read_only(&self) -> &LocalReadOnly { + &LocalReadOnly {} + } +} + +impl CtxDbRead for AnonymousViewContext { + fn db_read_only(&self) -> &LocalReadOnly { + &LocalReadOnly {} + } +} + +/// This trait allows you to be generic over all contexts that allow read/write access to the db. +pub trait CtxDbWrite: CtxDbRead { + fn db(&self) -> &Local; +} + +impl CtxDbWrite for TxContext { + fn db(&self) -> &Local { + &Local {} + } +} + +impl CtxDbWrite for ReducerContext { + fn db(&self) -> &Local { + &Local {} + } +} + +/// This trait allows you to be generic over all contexts that allow to retrieve the caller [Identity]. +pub trait CtxWithSender { + fn sender(&self) -> Identity; +} + +impl CtxWithSender for ViewContext { + fn sender(&self) -> Identity { + self.sender + } +} + +impl CtxWithSender for ReducerContext { + fn sender(&self) -> Identity { + self.sender + } +} + +impl CtxWithSender for TxContext { + fn sender(&self) -> Identity { + self.0.sender + } +} + +/// This trait allows you to be generic over all contexts that allow to retrieve the [Timestamp] of calling them. +pub trait CtxWithTimestamp { + fn timestamp(&self) -> Timestamp; +} + +impl CtxWithTimestamp for ReducerContext { + fn timestamp(&self) -> Timestamp { + self.timestamp + } +} + +impl CtxWithTimestamp for TxContext { + fn timestamp(&self) -> Timestamp { + self.timestamp + } +} + +/// This trait allows you to be generic over all contexts that allow to use the [HttpClient]. +pub trait CtxWithHttp { + fn http(&self) -> &HttpClient; +} + +impl CtxWithHttp for HandlerContext { + fn http(&self) -> &HttpClient { + &self.http + } +} + +impl CtxWithHttp for ProcedureContext { + fn http(&self) -> &HttpClient { + &self.http + } +} + /// The [JWT] of an [`AuthCtx`]. /// /// [JWT]: https://en.wikipedia.org/wiki/JSON_Web_Token