@@ -666,133 +666,6 @@ pub use spacetimedb_bindings_macro::table;
666666#[ doc( inline) ]
667667pub use spacetimedb_bindings_macro:: reducer;
668668
669- /// Marks a function as a spacetimedb view.
670- ///
671- /// A view is a function with read-only access to the database.
672- ///
673- /// The first argument of a view is always a [`&ViewContext`] or [`&AnonymousViewContext`].
674- /// The former can only read from the database whereas latter can also access info about the caller.
675- ///
676- /// After this, a view can take any number of arguments just like reducers.
677- /// These arguments must implement the [`SpacetimeType`], [`Serialize`], and [`Deserialize`] traits.
678- /// All of these traits can be derived at once by marking a type with `#[derive(SpacetimeType)]`.
679- ///
680- /// Views return `Vec<T>` or `Option<T>` where `T` is a `SpacetimeType`.
681- ///
682- /// ```no_run
683- /// # mod demo {
684- /// use spacetimedb::{view, table, AnonymousViewContext, SpacetimeType, ViewContext};
685- /// use spacetimedb_lib::Identity;
686- ///
687- /// #[table(name = player)]
688- /// struct Player {
689- /// #[auto_inc]
690- /// #[primary_key]
691- /// id: u64,
692- ///
693- /// #[unique]
694- /// identity: Identity,
695- ///
696- /// #[index(btree)]
697- /// level: u32,
698- /// }
699- ///
700- /// impl Player {
701- /// fn merge(self, location: Location) -> PlayerAndLocation {
702- /// PlayerAndLocation {
703- /// player_id: self.id,
704- /// level: self.level,
705- /// x: location.x,
706- /// y: location.y,
707- /// }
708- /// }
709- /// }
710- ///
711- /// #[derive(SpacetimeType)]
712- /// struct PlayerId {
713- /// id: u64,
714- /// }
715- ///
716- /// #[table(name = location, index(name = coordinates, btree(columns = [x, y])))]
717- /// struct Location {
718- /// #[unique]
719- /// player_id: u64,
720- /// x: u64,
721- /// y: u64,
722- /// }
723- ///
724- /// #[derive(SpacetimeType)]
725- /// struct PlayerAndLocation {
726- /// player_id: u64,
727- /// level: u32,
728- /// x: u64,
729- /// y: u64,
730- /// }
731- ///
732- /// // A view that selects at most one row from a table
733- /// #[view(public)]
734- /// fn my_player(ctx: &ViewContext) -> Option<Player> {
735- /// ctx.db.player().identity().find(ctx.sender)
736- /// }
737- ///
738- /// // An example of column projection
739- /// #[view(public)]
740- /// fn my_player_id(ctx: &ViewContext) -> Option<PlayerId> {
741- /// ctx.db.player().identity().find(ctx.sender).map(|Player { id, .. }| PlayerId { id })
742- /// }
743- ///
744- /// // An example of a parameterized view
745- /// #[view(public)]
746- /// fn players_at_level(ctx: &AnonymousViewContext, level: u32) -> Vec<Player> {
747- /// ctx.db.player().level().filter(level).collect()
748- /// }
749- ///
750- /// // An example that is analogous to a semijoin in sql
751- /// #[view(public)]
752- /// fn players_at_coordinates(ctx: &AnonymousViewContext, x: u64, y: u64) -> Vec<Player> {
753- /// ctx
754- /// .db
755- /// .location()
756- /// .coordinates()
757- /// .filter((x, y))
758- /// .filter_map(|location| ctx.db.player().id().find(location.player_id))
759- /// .collect()
760- /// }
761- ///
762- /// // An example of a join that combines fields from two different tables
763- /// #[view(public)]
764- /// fn players_with_coordinates(ctx: &AnonymousViewContext, x: u64, y: u64) -> Vec<PlayerAndLocation> {
765- /// ctx
766- /// .db
767- /// .location()
768- /// .coordinates()
769- /// .filter((x, y))
770- /// .filter_map(|location| ctx
771- /// .db
772- /// .player()
773- /// .id()
774- /// .find(location.player_id)
775- /// .map(|player| player.merge(location))
776- /// )
777- /// .collect()
778- /// }
779- /// # }
780- /// ```
781- ///
782- /// Just like reducers, views are limited in their ability to interact with the outside world.
783- /// They have no access to any network or filesystem interfaces.
784- /// Calling methods from [`std::io`], [`std::net`], or [`std::fs`] will result in runtime errors.
785- ///
786- /// Views are callable by reducers and other views simply by passing their `ViewContext`..
787- /// This is a regular function call.
788- /// The callee will run within the caller's transaction.
789- ///
790- ///
791- /// [`&ViewContext`]: `ViewContext`
792- /// [`&AnonymousViewContext`]: `AnonymousViewContext`
793- #[ doc( inline) ]
794- pub use spacetimedb_bindings_macro:: view;
795-
796669/// One of two possible types that can be passed as the first argument to a `#[view]`.
797670/// The other is [`ViewContext`].
798671/// Use this type if the view does not depend on the caller's identity.
0 commit comments