11mod module_bindings;
22use module_bindings:: * ;
33use std:: env;
4+ use std:: io:: { self , Read } ;
45
5- use spacetimedb_sdk:: { DbConnection , Table } ;
6+ use spacetimedb_sdk:: { DbContext , Table } ;
67
78fn main ( ) {
89 // The URI of the SpacetimeDB instance hosting our chat module.
@@ -14,28 +15,31 @@ fn main() {
1415 // Connect to the database
1516 let conn = DbConnection :: builder ( )
1617 . with_module_name ( db_name)
17- . with_host ( host)
18+ . with_uri ( host)
1819 . on_connect ( |_, _, _| {
1920 println ! ( "Connected to SpacetimeDB" ) ;
2021 } )
21- . on_connect_error ( |e| {
22+ . on_connect_error ( |_ctx , e| {
2223 eprintln ! ( "Connection error: {:?}" , e) ;
2324 std:: process:: exit ( 1 ) ;
2425 } )
2526 . build ( )
2627 . expect ( "Failed to connect" ) ;
2728
29+ conn. run_threaded ( ) ;
30+
2831 // Subscribe to the person table
29- conn. subscribe ( & [
30- "SELECT * FROM person"
31- ] ) ;
32+ conn. subscription_builder ( )
33+ . on_applied ( |_ctx| println ! ( "Subscripted to the person table" ) )
34+ . on_error ( |_ctx, e| eprintln ! ( "There was an error when subscring to the person table: {e}" ) )
35+ . subscribe ( [ "SELECT * FROM person" ] ) ;
3236
3337 // Register a callback for when rows are inserted into the person table
34- Person :: on_insert ( |_ctx, person| {
38+ conn . db ( ) . person ( ) . on_insert ( |_ctx, person| {
3539 println ! ( "New person: {}" , person. name) ;
3640 } ) ;
3741
38- // Run the connection on the current thread
39- // This will block and handle all database events
40- conn . run ( ) ;
42+ println ! ( "Press any key to exit..." ) ;
43+
44+ let _ = io :: stdin ( ) . read ( & mut [ 0u8 ] ) . unwrap ( ) ;
4145}
0 commit comments