Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions crates/bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
///
Expand All @@ -1461,6 +1464,7 @@ pub trait DbContext {
fn db_read_only(&self) -> &LocalReadOnly;
}

#[allow(deprecated)]
impl DbContext for AnonymousViewContext {
type DbView = LocalReadOnly;

Expand All @@ -1473,6 +1477,7 @@ impl DbContext for AnonymousViewContext {
}
}

#[allow(deprecated)]
impl DbContext for ReducerContext {
type DbView = Local;

Expand All @@ -1485,6 +1490,7 @@ impl DbContext for ReducerContext {
}
}

#[allow(deprecated)]
impl DbContext for TxContext {
type DbView = Local;

Expand All @@ -1497,6 +1503,7 @@ impl DbContext for TxContext {
}
}

#[allow(deprecated)]
impl DbContext for ViewContext {
type DbView = LocalReadOnly;

Expand Down Expand Up @@ -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
Expand Down