You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/modules/rust/index.md
+13-11Lines changed: 13 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -31,7 +31,7 @@ use spacetimedb::{spacetimedb, println};
31
31
// This macro lets us interact with a SpacetimeDB table of Person rows.
32
32
// We can insert and delete into, and query, this table by the collection
33
33
// of functions generated by the macro.
34
-
#[spacetimedb(table)]
34
+
#[spacetimedb(table(public))]
35
35
pubstructPerson {
36
36
name:String,
37
37
}
@@ -88,10 +88,12 @@ Now we'll get into details on all the macro APIs SpacetimeDB provides, starting
88
88
89
89
### Defining tables
90
90
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.
92
94
93
95
```rust
94
-
#[spacetimedb(table)]
96
+
#[spacetimedb(table(public))]
95
97
structTable {
96
98
field1:String,
97
99
field2:u32,
@@ -116,10 +118,10 @@ And common data structures:
116
118
-`Option<T> where T: SpacetimeType`
117
119
-`Vec<T> where T: SpacetimeType`
118
120
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.
120
122
121
123
```rust
122
-
#[spacetimedb(table)]
124
+
#[spacetimedb(table(public))]
123
125
structAnotherTable {
124
126
// Fine, some builtin types.
125
127
id:u64,
@@ -151,7 +153,7 @@ enum Serial {
151
153
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.
152
154
153
155
```rust
154
-
#[spacetimedb(table)]
156
+
#[spacetimedb(table(public))]
155
157
structPerson {
156
158
#[unique]
157
159
id:u64,
@@ -269,7 +271,7 @@ We'll work off these structs to see what functions SpacetimeDB generates:
269
271
This table has a plain old column.
270
272
271
273
```rust
272
-
#[spacetimedb(table)]
274
+
#[spacetimedb(table(public))]
273
275
structOrdinary {
274
276
ordinary_field:u64,
275
277
}
@@ -278,7 +280,7 @@ struct Ordinary {
278
280
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.
279
281
280
282
```rust
281
-
#[spacetimedb(table)]
283
+
#[spacetimedb(table(public))]
282
284
structUnique {
283
285
// A unique column:
284
286
#[unique]
@@ -291,7 +293,7 @@ This table has an automatically incrementing column. SpacetimeDB automatically p
291
293
Only integer types can be `#[unique]`: `u8`, `u16`, `u32`, `u64`, `u128`, `i8`, `i16`, `i32`, `i64` and `i128`.
292
294
293
295
```rust
294
-
#[spacetimedb(table)]
296
+
#[spacetimedb(table(public))]
295
297
structAutoinc {
296
298
#[autoinc]
297
299
autoinc_field:u64,
@@ -301,7 +303,7 @@ struct Autoinc {
301
303
These attributes can be combined, to create an automatically assigned ID usable for filtering.
302
304
303
305
```rust
304
-
#[spacetimedb(table)]
306
+
#[spacetimedb(table(public))]
305
307
structIdentity {
306
308
#[autoinc]
307
309
#[unique]
@@ -375,7 +377,7 @@ fn insert_id() {
375
377
Given a table, we can iterate over all the rows in it.
Copy file name to clipboardExpand all lines: docs/docs/modules/rust/quickstart.md
+8-4Lines changed: 8 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,11 @@ A SpacetimeDB module is code that gets compiled to WebAssembly and is uploaded t
6
6
7
7
Each SpacetimeDB module defines a set of tables and a set of reducers.
8
8
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!_
10
14
11
15
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.
12
16
@@ -67,7 +71,7 @@ For each `User`, we'll store their `Identity`, an optional name they can set to
67
71
To `server/src/lib.rs`, add the definition of the table `User`:
68
72
69
73
```rust
70
-
#[spacetimedb(table)]
74
+
#[spacetimedb(table(public))]
71
75
pubstructUser {
72
76
#[primarykey]
73
77
identity:Identity,
@@ -81,7 +85,7 @@ For each `Message`, we'll store the `Identity` of the user who sent it, the `Tim
81
85
To `server/src/lib.rs`, add the definition of the table `Message`:
82
86
83
87
```rust
84
-
#[spacetimedb(table)]
88
+
#[spacetimedb(table(public))]
85
89
pubstructMessage {
86
90
sender:Identity,
87
91
sent:Timestamp,
@@ -179,7 +183,7 @@ You could extend the validation in `validate_message` in similar ways to `valida
179
183
180
184
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.
181
185
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`.
183
187
184
188
To `server/src/lib.rs`, add the definition of the connect reducer:
Copy file name to clipboardExpand all lines: docs/docs/unity/part-2a-rust.md
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,13 +29,13 @@ use spacetimedb::{spacetimedb, Identity, SpacetimeType, ReducerContext};
29
29
use log;
30
30
```
31
31
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.
33
33
34
34
**Append to the bottom of lib.rs:**
35
35
36
36
```rust
37
37
// 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))]
39
39
#[derive(Clone)]
40
40
pubstructConfig {
41
41
#[primarykey]
@@ -44,7 +44,7 @@ pub struct Config {
44
44
}
45
45
```
46
46
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.
48
48
49
49
**Append to the bottom of lib.rs:**
50
50
@@ -64,7 +64,7 @@ Now we're going to create a table which actually uses the `StdbVector3` that we
64
64
// This stores information related to all entities in our game. In this tutorial
65
65
// all entities must at least have an entity_id, a position, a direction and they
66
66
// must specify whether or not they are moving.
67
-
#[spacetimedb(table)]
67
+
#[spacetimedb(table(public))]
68
68
#[derive(Clone)]
69
69
pubstructEntityComponent {
70
70
#[primarykey]
@@ -87,7 +87,7 @@ Next, we will define the `PlayerComponent` table. The `PlayerComponent` table is
87
87
// All players have this component and it associates an entity with the user's
88
88
// Identity. It also stores their username and whether or not they're logged in.
89
89
#[derive(Clone)]
90
-
#[spacetimedb(table)]
90
+
#[spacetimedb(table(public))]
91
91
pubstructPlayerComponent {
92
92
// An entity_id that matches an entity_id in the `EntityComponent` table.
93
93
#[primarykey]
@@ -264,7 +264,7 @@ First lets add a new `ChatMessage` table to the SpacetimeDB module. Add the foll
264
264
**Append to the bottom of server/src/lib.rs:**
265
265
266
266
```rust
267
-
#[spacetimedb(table)]
267
+
#[spacetimedb(table(public))]
268
268
pubstructChatMessage {
269
269
// The primary key for this table will be auto-incremented
Copy file name to clipboardExpand all lines: docs/docs/unity/part-2b-c-sharp.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,7 +30,7 @@ using SpacetimeDB.Module;
30
30
usingstaticSpacetimeDB.Runtime;
31
31
```
32
32
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.
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.
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.
63
63
64
64
```rust
65
-
#[spacetimedb(table)]
65
+
#[spacetimedb(table(public))]
66
66
pubstructConfig {
67
67
// Config is a global table with a single row. This table will be used to
0 commit comments