()
| 5 | use spacetimedb_sdk::{DbContext, Table}; |
| 6 | |
| 7 | fn main() { |
| 8 | // The URI of the SpacetimeDB instance hosting our chat module. |
| 9 | let host: String = env::var("SPACETIMEDB_HOST").unwrap_or("http://localhost:3000".to_string()); |
| 10 | |
| 11 | // The module name we chose when we published our module. |
| 12 | let db_name: String = env::var("SPACETIMEDB_DB_NAME").unwrap_or("my-db".to_string()); |
| 13 | |
| 14 | // Connect to the database |
| 15 | let conn = DbConnection::builder() |
| 16 | .with_database_name(db_name) |
| 17 | .with_uri(host) |
| 18 | .on_connect(|_, _, _| { |
| 19 | println!("Connected to SpacetimeDB"); |
| 20 | }) |
| 21 | .on_connect_error(|_ctx, e| { |
| 22 | eprintln!("Connection error: {:?}", e); |
| 23 | std::process::exit(1); |
| 24 | }) |
| 25 | .build() |
| 26 | .expect("Failed to connect"); |
| 27 | |
| 28 | conn.run_threaded(); |
| 29 | |
| 30 | // Subscribe to the person table |
| 31 | conn.subscription_builder() |
| 32 | .on_applied(|_ctx| println!("Subscripted to the person table")) |
| 33 | .on_error(|_ctx, e| eprintln!("There was an error when subscring to the person table: {e}")) |
| 34 | .add_query(|q| q.from.person()) |
| 35 | .subscribe(); |
| 36 | |
| 37 | // Register a callback for when rows are inserted into the person table |
| 38 | conn.db().person().on_insert(|_ctx, person| { |
| 39 | println!("New person: {}", person.name); |
| 40 | }); |
| 41 | |
| 42 | // Keep the main thread alive so the connection stays open |
| 43 | loop { |
| 44 | std::thread::sleep(std::time::Duration::from_secs(1)); |
| 45 | } |
| 46 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…