Skip to main content

Crate bevy_stdb

Crate bevy_stdb 

Source
Expand description

Bevy integration for SpacetimeDB.

bevy_stdb adapts SpacetimeDB’s connection and callback model into Bevy-style resources, systems, and messages. Everything is configured through StdbPlugin.

§Architecture

The crate is organized around four lifecycle concerns:

§Quick start

use bevy::prelude::*;
use bevy_stdb::prelude::*;
// Generated by `spacetime generate`:
use module_bindings::{DbConnection, RemoteModule, PlayerRow};

#[derive(Clone, Eq, PartialEq, Hash)]
enum SubKey { Players }

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_plugins(
            StdbPlugin::<DbConnection, RemoteModule>::default()
                .with_database_name("my_module")
                .with_uri("http://localhost:3000")
                .with_background_driver(DbConnection::run_threaded)
                .with_reconnect(StdbReconnectOptions::default())
                .with_subscriptions::<SubKey>()
                .add_table::<PlayerRow>(|reg, db| reg.bind(db.player()))
        )
        .add_systems(Update, (on_player_insert, subscribe_players_on_connect))
        .run();
}

fn subscribe_players_on_connect(
    mut connected: ReadStdbConnectedMessage,
    mut subs: ResMut<StdbSubscriptions<SubKey, RemoteModule>>,
) {
    if connected.read().next().is_some() {
        subs.subscribe_sql(SubKey::Players, "SELECT * FROM player");
    }
}

fn on_player_insert(mut msgs: ReadInsertMessage<PlayerRow>) {
    for msg in msgs.read() {
        info!("player inserted: {:?}", msg.row);
    }
}

See the StdbPlugin docs for the full builder API and the README for detailed guides on connection driving, table registration, subscriptions, and delayed connections.

Modules§

prelude
Common imports for bevy_stdb.