Skip to content

Commit ba31c80

Browse files
drogusbfops
andauthored
Fix the basic-rust client template for CLI's init command (#3646)
# Description of Changes This template was never properly tested, and it contains wrong API usage that can't even compile. The only problem is that in order to make it work the person table has to be public, and I'm not sure if we want to make that change. Alternatively we could make basic-rust code not subscribe to anything and just connect to the database. # Expected complexity level and risk 2 # Testing - [x] I compiled the template and checked that it works against Co-authored-by: Zeke Foppa <196249+bfops@users.noreply.github.com>
1 parent 9d57c4d commit ba31c80

2 files changed

Lines changed: 15 additions & 11 deletions

File tree

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
mod module_bindings;
22
use module_bindings::*;
33
use std::env;
4+
use std::io::{self, Read};
45

5-
use spacetimedb_sdk::{DbConnection, Table};
6+
use spacetimedb_sdk::{DbContext, Table};
67

78
fn 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
}

crates/cli/templates/basic-rust/server/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use spacetimedb::{ReducerContext, Table};
22

3-
#[spacetimedb::table(name = person)]
3+
#[spacetimedb::table(name = person, public)]
44
pub struct Person {
55
name: String,
66
}

0 commit comments

Comments
 (0)