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
+11-13Lines changed: 11 additions & 13 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(public))]
34
+
#[spacetimedb(table)]
35
35
pubstructPerson {
36
36
name:String,
37
37
}
@@ -88,12 +88,10 @@ Now we'll get into details on all the macro APIs SpacetimeDB provides, starting
88
88
89
89
### Defining tables
90
90
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.
91
+
`#[spacetimedb(table)]` takes no further arguments, and is applied to a Rust struct with named fields:
94
92
95
93
```rust
96
-
#[spacetimedb(table(public))]
94
+
#[spacetimedb(table)]
97
95
structTable {
98
96
field1:String,
99
97
field2:u32,
@@ -118,10 +116,10 @@ And common data structures:
118
116
-`Option<T> where T: SpacetimeType`
119
117
-`Vec<T> where T: SpacetimeType`
120
118
121
-
All `#[spacetimedb(table(...))]` types are `SpacetimeType`s, and accordingly, all of their fields have to be.
119
+
All `#[spacetimedb(table)]` types are `SpacetimeType`s, and accordingly, all of their fields have to be.
122
120
123
121
```rust
124
-
#[spacetimedb(table(public))]
122
+
#[spacetimedb(table)]
125
123
structAnotherTable {
126
124
// Fine, some builtin types.
127
125
id:u64,
@@ -153,7 +151,7 @@ enum Serial {
153
151
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.
154
152
155
153
```rust
156
-
#[spacetimedb(table(public))]
154
+
#[spacetimedb(table)]
157
155
structPerson {
158
156
#[unique]
159
157
id:u64,
@@ -271,7 +269,7 @@ We'll work off these structs to see what functions SpacetimeDB generates:
271
269
This table has a plain old column.
272
270
273
271
```rust
274
-
#[spacetimedb(table(public))]
272
+
#[spacetimedb(table)]
275
273
structOrdinary {
276
274
ordinary_field:u64,
277
275
}
@@ -280,7 +278,7 @@ struct Ordinary {
280
278
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.
281
279
282
280
```rust
283
-
#[spacetimedb(table(public))]
281
+
#[spacetimedb(table)]
284
282
structUnique {
285
283
// A unique column:
286
284
#[unique]
@@ -293,7 +291,7 @@ This table has an automatically incrementing column. SpacetimeDB automatically p
293
291
Only integer types can be `#[unique]`: `u8`, `u16`, `u32`, `u64`, `u128`, `i8`, `i16`, `i32`, `i64` and `i128`.
294
292
295
293
```rust
296
-
#[spacetimedb(table(public))]
294
+
#[spacetimedb(table)]
297
295
structAutoinc {
298
296
#[autoinc]
299
297
autoinc_field:u64,
@@ -303,7 +301,7 @@ struct Autoinc {
303
301
These attributes can be combined, to create an automatically assigned ID usable for filtering.
304
302
305
303
```rust
306
-
#[spacetimedb(table(public))]
304
+
#[spacetimedb(table)]
307
305
structIdentity {
308
306
#[autoinc]
309
307
#[unique]
@@ -377,7 +375,7 @@ fn insert_id() {
377
375
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
+4-8Lines changed: 4 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,11 +6,7 @@ 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.
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!_
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.
14
10
15
11
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.
16
12
@@ -71,7 +67,7 @@ For each `User`, we'll store their `Identity`, an optional name they can set to
71
67
To `server/src/lib.rs`, add the definition of the table `User`:
72
68
73
69
```rust
74
-
#[spacetimedb(table(public))]
70
+
#[spacetimedb(table)]
75
71
pubstructUser {
76
72
#[primarykey]
77
73
identity:Identity,
@@ -85,7 +81,7 @@ For each `Message`, we'll store the `Identity` of the user who sent it, the `Tim
85
81
To `server/src/lib.rs`, add the definition of the table `Message`:
86
82
87
83
```rust
88
-
#[spacetimedb(table(public))]
84
+
#[spacetimedb(table)]
89
85
pubstructMessage {
90
86
sender:Identity,
91
87
sent:Timestamp,
@@ -183,7 +179,7 @@ You could extend the validation in `validate_message` in similar ways to `valida
183
179
184
180
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.
185
181
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`.
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`.
187
183
188
184
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) (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.
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.
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(public))]
38
+
#[spacetimedb(table)]
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(public))]
67
+
#[spacetimedb(table)]
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(public))]
90
+
#[spacetimedb(table)]
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(public))]
267
+
#[spacetimedb(table)]
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 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.
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.
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(public))]
65
+
#[spacetimedb(table)]
66
66
pubstructConfig {
67
67
// Config is a global table with a single row. This table will be used to
0 commit comments