Skip to content

Commit fce4df6

Browse files
authored
Update docs for #[spacetimedb(table)] (#61)
* [bfops/public-tables]: update docs for #[spacetimedb(table)] * [bfops/public-tables]: review --------- Co-authored-by: Zeke Foppa <github.com/bfops>
1 parent f813a29 commit fce4df6

File tree

5 files changed

+31
-25
lines changed

5 files changed

+31
-25
lines changed

docs/docs/modules/rust/index.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use spacetimedb::{spacetimedb, println};
3131
// This macro lets us interact with a SpacetimeDB table of Person rows.
3232
// We can insert and delete into, and query, this table by the collection
3333
// of functions generated by the macro.
34-
#[spacetimedb(table)]
34+
#[spacetimedb(table(public))]
3535
pub struct Person {
3636
name: String,
3737
}
@@ -88,10 +88,12 @@ Now we'll get into details on all the macro APIs SpacetimeDB provides, starting
8888

8989
### Defining tables
9090

91-
`#[spacetimedb(table)]` takes no further arguments, and is applied to a Rust struct with named fields:
91+
The `#[spacetimedb(table)]` is applied to a Rust struct with named fields.
92+
By default, tables are considered **private**. This means that they are only readable by the table owner, and by server module code.
93+
The `#[spacetimedb(table(public))]` macro makes a table public. **Public** tables are readable by all users, but can still only be modified by your server module code.
9294

9395
```rust
94-
#[spacetimedb(table)]
96+
#[spacetimedb(table(public))]
9597
struct Table {
9698
field1: String,
9799
field2: u32,
@@ -116,10 +118,10 @@ And common data structures:
116118
- `Option<T> where T: SpacetimeType`
117119
- `Vec<T> where T: SpacetimeType`
118120

119-
All `#[spacetimedb(table)]` types are `SpacetimeType`s, and accordingly, all of their fields have to be.
121+
All `#[spacetimedb(table(...))]` types are `SpacetimeType`s, and accordingly, all of their fields have to be.
120122

121123
```rust
122-
#[spacetimedb(table)]
124+
#[spacetimedb(table(public))]
123125
struct AnotherTable {
124126
// Fine, some builtin types.
125127
id: u64,
@@ -151,7 +153,7 @@ enum Serial {
151153
Once the table is created via the macro, other attributes described below can control more aspects of the table. For instance, a particular column can be indexed, or take on values of an automatically incremented counter. These are described in detail below.
152154

153155
```rust
154-
#[spacetimedb(table)]
156+
#[spacetimedb(table(public))]
155157
struct Person {
156158
#[unique]
157159
id: u64,
@@ -269,7 +271,7 @@ We'll work off these structs to see what functions SpacetimeDB generates:
269271
This table has a plain old column.
270272

271273
```rust
272-
#[spacetimedb(table)]
274+
#[spacetimedb(table(public))]
273275
struct Ordinary {
274276
ordinary_field: u64,
275277
}
@@ -278,7 +280,7 @@ struct Ordinary {
278280
This table has a unique column. Every row in the `Person` table must have distinct values of the `unique_field` column. Attempting to insert a row with a duplicate value will fail.
279281

280282
```rust
281-
#[spacetimedb(table)]
283+
#[spacetimedb(table(public))]
282284
struct Unique {
283285
// A unique column:
284286
#[unique]
@@ -291,7 +293,7 @@ This table has an automatically incrementing column. SpacetimeDB automatically p
291293
Only integer types can be `#[unique]`: `u8`, `u16`, `u32`, `u64`, `u128`, `i8`, `i16`, `i32`, `i64` and `i128`.
292294

293295
```rust
294-
#[spacetimedb(table)]
296+
#[spacetimedb(table(public))]
295297
struct Autoinc {
296298
#[autoinc]
297299
autoinc_field: u64,
@@ -301,7 +303,7 @@ struct Autoinc {
301303
These attributes can be combined, to create an automatically assigned ID usable for filtering.
302304

303305
```rust
304-
#[spacetimedb(table)]
306+
#[spacetimedb(table(public))]
305307
struct Identity {
306308
#[autoinc]
307309
#[unique]
@@ -375,7 +377,7 @@ fn insert_id() {
375377
Given a table, we can iterate over all the rows in it.
376378

377379
```rust
378-
#[spacetimedb(table)]
380+
#[spacetimedb(table(public))]
379381
struct Person {
380382
#[unique]
381383
id: u64,

docs/docs/modules/rust/quickstart.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ A SpacetimeDB module is code that gets compiled to WebAssembly and is uploaded t
66

77
Each SpacetimeDB module defines a set of tables and a set of reducers.
88

9-
Each table is defined as a Rust `struct` annotated with `#[spacetimedb(table)]`, where an instance represents a row, and each field represents a column.
9+
Each table is defined as a Rust `struct` annotated with `#[spacetimedb(table(...))]`, where an instance represents a row, and each field represents a column.
10+
By default, tables are **private**. This means that they are only readable by the table owner, and by server module code.
11+
The `#[spacetimedb(table(public))]` macro makes a table public. **Public** tables are readable by all users, but can still only be modified by your server module code.
12+
13+
_Coming soon: We plan to add much more robust access controls than just `public` or `private`. Stay tuned!_
1014

1115
A reducer is a function which traverses and updates the database. Each reducer call runs in its own transaction, and its updates to the database are only committed if the reducer returns successfully. In Rust, reducers are defined as functions annotated with `#[spacetimedb(reducer)]`, and may return a `Result<()>`, with an `Err` return aborting the transaction.
1216

@@ -67,7 +71,7 @@ For each `User`, we'll store their `Identity`, an optional name they can set to
6771
To `server/src/lib.rs`, add the definition of the table `User`:
6872

6973
```rust
70-
#[spacetimedb(table)]
74+
#[spacetimedb(table(public))]
7175
pub struct User {
7276
#[primarykey]
7377
identity: Identity,
@@ -81,7 +85,7 @@ For each `Message`, we'll store the `Identity` of the user who sent it, the `Tim
8185
To `server/src/lib.rs`, add the definition of the table `Message`:
8286

8387
```rust
84-
#[spacetimedb(table)]
88+
#[spacetimedb(table(public))]
8589
pub struct Message {
8690
sender: Identity,
8791
sent: Timestamp,
@@ -179,7 +183,7 @@ You could extend the validation in `validate_message` in similar ways to `valida
179183

180184
Whenever a client connects, the module will run a special reducer, annotated with `#[spacetimedb(connect)]`, if it's defined. By convention, it's named `identity_connected`. We'll use it to create a `User` record for the client if it doesn't yet exist, and to set its online status.
181185

182-
We'll use `User::filter_by_identity` to look up a `User` row for `ctx.sender`, if one exists. If we find one, we'll use `User::update_by_identity` to overwrite it with a row that has `online: true`. If not, we'll use `User::insert` to insert a new row for our new user. All three of these methods are generated by the `#[spacetimedb(table)]` attribute, with rows and behavior based on the row attributes. `filter_by_identity` returns an `Option<User>`, because the unique constraint from the `#[primarykey]` attribute means there will be either zero or one matching rows. `insert` returns a `Result<(), UniqueConstraintViolation>` because of the same unique constraint; if we want to overwrite a `User` row, we need to do so explicitly using `update_by_identity`.
186+
We'll use `User::filter_by_identity` to look up a `User` row for `ctx.sender`, if one exists. If we find one, we'll use `User::update_by_identity` to overwrite it with a row that has `online: true`. If not, we'll use `User::insert` to insert a new row for our new user. All three of these methods are generated by the `#[spacetimedb(table(...))]` attribute, with rows and behavior based on the row attributes. `filter_by_identity` returns an `Option<User>`, because the unique constraint from the `#[primarykey]` attribute means there will be either zero or one matching rows. `insert` returns a `Result<(), UniqueConstraintViolation>` because of the same unique constraint; if we want to overwrite a `User` row, we need to do so explicitly using `update_by_identity`.
183187

184188
To `server/src/lib.rs`, add the definition of the connect reducer:
185189

docs/docs/unity/part-2a-rust.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ use spacetimedb::{spacetimedb, Identity, SpacetimeType, ReducerContext};
2929
use log;
3030
```
3131

32-
Then we are going to start by adding the global `Config` table. Right now it only contains the "message of the day" but it can be extended to store other configuration variables. This also uses a couple of macros, like `#[spacetimedb(table)]` which you can learn more about in our [Rust module reference](/docs/modules/rust). Simply put, this just tells SpacetimeDB to create a table which uses this struct as the schema for the table.
32+
Then we are going to start by adding the global `Config` table. Right now it only contains the "message of the day" but it can be extended to store other configuration variables. This also uses a couple of macros, like `#[spacetimedb(table(...))]` which you can learn more about in our [Rust module reference](/docs/modules/rust) (including making your tables `private`!). Simply put, this just tells SpacetimeDB to create a table which uses this struct as the schema for the table.
3333

3434
**Append to the bottom of lib.rs:**
3535

3636
```rust
3737
// We're using this table as a singleton, so there should typically only be one element where the version is 0.
38-
#[spacetimedb(table)]
38+
#[spacetimedb(table(public))]
3939
#[derive(Clone)]
4040
pub struct Config {
4141
#[primarykey]
@@ -44,7 +44,7 @@ pub struct Config {
4444
}
4545
```
4646

47-
Next, we're going to define a new `SpacetimeType` called `StdbVector3` which we're going to use to store positions. The difference between a `#[derive(SpacetimeType)]` and a `#[spacetimedb(table)]` is that tables actually store data, whereas the deriving `SpacetimeType` just allows you to create a new column of that type in a SpacetimeDB table. Therefore, `StdbVector3` is not, itself, a table.
47+
Next, we're going to define a new `SpacetimeType` called `StdbVector3` which we're going to use to store positions. The difference between a `#[derive(SpacetimeType)]` and a `#[spacetimedb(table(...))]` is that tables actually store data, whereas the deriving `SpacetimeType` just allows you to create a new column of that type in a SpacetimeDB table. Therefore, `StdbVector3` is not, itself, a table.
4848

4949
**Append to the bottom of lib.rs:**
5050

@@ -64,7 +64,7 @@ Now we're going to create a table which actually uses the `StdbVector3` that we
6464
// This stores information related to all entities in our game. In this tutorial
6565
// all entities must at least have an entity_id, a position, a direction and they
6666
// must specify whether or not they are moving.
67-
#[spacetimedb(table)]
67+
#[spacetimedb(table(public))]
6868
#[derive(Clone)]
6969
pub struct EntityComponent {
7070
#[primarykey]
@@ -87,7 +87,7 @@ Next, we will define the `PlayerComponent` table. The `PlayerComponent` table is
8787
// All players have this component and it associates an entity with the user's
8888
// Identity. It also stores their username and whether or not they're logged in.
8989
#[derive(Clone)]
90-
#[spacetimedb(table)]
90+
#[spacetimedb(table(public))]
9191
pub struct PlayerComponent {
9292
// An entity_id that matches an entity_id in the `EntityComponent` table.
9393
#[primarykey]
@@ -264,7 +264,7 @@ First lets add a new `ChatMessage` table to the SpacetimeDB module. Add the foll
264264
**Append to the bottom of server/src/lib.rs:**
265265

266266
```rust
267-
#[spacetimedb(table)]
267+
#[spacetimedb(table(public))]
268268
pub struct ChatMessage {
269269
// The primary key for this table will be auto-incremented
270270
#[primarykey]

docs/docs/unity/part-2b-c-sharp.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ using SpacetimeDB.Module;
3030
using static SpacetimeDB.Runtime;
3131
```
3232

33-
Then we are going to start by adding the global `Config` table. Right now it only contains the "message of the day" but it can be extended to store other configuration variables. This also uses a couple of macros, like `#[spacetimedb(table)]` which you can learn more about in our [C# module reference](/docs/modules/c-sharp). Simply put, this just tells SpacetimeDB to create a table which uses this struct as the schema for the table.
33+
Then we are going to start by adding the global `Config` table. Right now it only contains the "message of the day" but it can be extended to store other configuration variables. This also uses a couple of attributes, like `[SpacetimeDB.Table]` which you can learn more about in our [C# module reference](/docs/modules/c-sharp). Simply put, this just tells SpacetimeDB to create a table which uses this struct as the schema for the table.
3434

3535
**Append to the bottom of lib.cs:**
3636

docs/docs/unity/part-4.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub enum ResourceNodeType {
3434
Iron,
3535
}
3636

37-
#[spacetimedb(table)]
37+
#[spacetimedb(table(public))]
3838
#[derive(Clone)]
3939
pub struct ResourceNodeComponent {
4040
#[primarykey]
@@ -48,7 +48,7 @@ pub struct ResourceNodeComponent {
4848
Because resource nodes never move, the `MobileEntityComponent` is overkill. Instead, we will add a new entity component named `StaticLocationComponent` that only stores the position and rotation.
4949

5050
```rust
51-
#[spacetimedb(table)]
51+
#[spacetimedb(table(public))]
5252
#[derive(Clone)]
5353
pub struct StaticLocationComponent {
5454
#[primarykey]
@@ -62,7 +62,7 @@ pub struct StaticLocationComponent {
6262
3. We are also going to add a couple of additional column to our Config table. `map_extents` let's our spawner know where it can spawn the nodes. `num_resource_nodes` is the maximum number of nodes to spawn on the map. Update the config table in lib.rs.
6363

6464
```rust
65-
#[spacetimedb(table)]
65+
#[spacetimedb(table(public))]
6666
pub struct Config {
6767
// Config is a global table with a single row. This table will be used to
6868
// store configuration or global variables

0 commit comments

Comments
 (0)