Skip to content

Commit dbc3d86

Browse files
cloutiertylerbfops
andauthored
First pass at reorganizing the docs and making them nice (#3494)
# Description of Changes This PR is the first in a series of PRs which are an effort the reorganize the docs and make it easier for people to get started using SpacetimeDB. This PR: 1. Renames all folders to be `kebab-case`. This is because `docIds` are set based on folder and file names, and there is no way to override that within a file (all you can change with `id: my-id` is the last component of the id. Because we want to be able to access docs in react components we need stable predictable ids. - This also necessitates setting up a few `_category_.json` files so that the categories do not show up as kebab case in the sidebar. 2. Adds a few React components with images so it's clearer on how to get started 3. Reorganizes the quickstarts to be at the top of the side bar and simplifies the server and client into a single document named for the language 4. Sets up redirections so the old links to the old documents now point to the new documents 5. Copies the entirety of `typography.css` from the website into this repo for the purpose of keeping them identical 6. Makes minor fixes and improvements to the contents of the docs In a future PR, I intend to: 1. Add a tutorial section to the docs which will include two tutorials: Blackholio and Chat 2. Turn the chat quickstart documents into the Chat tutorial and consolidate it into a single file with different language switchers (if this is feasible) 3. Create a new quickstart section, which has a quickstart guide for each platform that we support so far, namely: - Vanilla TypeScript - Vanilla JavaScript - Vanilla Rust - Vanilla C# - React - Unity - Unreal 4. Update the quickstart guides to do the following (in the future I will add more platforms with this scheme): - Use `spacetime dev --template <framework-template>` to create a new project for the given framework - Insert data into the database - Add a new `reducer` to the template - Add a new `table` to the template - Add a new `view` to the template - Call the reducer from the client - Subscribe to the `view` from the template 5. Break up the current per language module reference files into files about SpacetimeDB concepts with a `TabGroup` for each language, namely: - Modules - Tables - Reducers - Procedures - Code generation - DbConnection (and the SDK API) - Auth - etc. The idea being that we should explain our platform by concept, not by language. This will also allow us to reuse a lot of prose that is repeated in the different language documents. # API and ABI breaking changes None # Expected complexity level and risk 1 # Testing - [x] Tested the documents locally to ensure that they render properly --------- Co-authored-by: Zeke Foppa <bfops@users.noreply.github.com>
1 parent b2e37e8 commit dbc3d86

File tree

88 files changed

+4416
-2334
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+4416
-2334
lines changed
File renamed without changes.
File renamed without changes.

docs/docs/01-Intro/01-overview.md renamed to docs/docs/01-intro/01-overview.md

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
11
---
22
title: Overview
3+
id: overview
34
slug: /
45
---
56

67
import Tabs from '@theme/Tabs';
78
import TabItem from '@theme/TabItem';
9+
import { InstallCardLink } from "@site/src/components/InstallCardLink";
10+
import { CardLink } from "@site/src/components/CardLink";
11+
import { CardLinkGrid } from "@site/src/components/CardLinkGrid";
12+
import { QuickstartLinks } from "@site/src/components/QuickstartLinks";
13+
import DocsList from "@site/src/components/DocsList";
814

915
# SpacetimeDB Documentation
1016

1117
## Installation
1218

13-
You can run SpacetimeDB as a standalone database server via the `spacetime` CLI tool.
19+
You can get started by first installing the `spacetime` CLI tool. The `spacetime` CLI tool makes it extremely easy to manage your databases and deployments.
1420

15-
You can find the instructions to install the CLI tool for your platform [here](https://spacetimedb.com/install).
21+
<InstallCardLink />
1622

17-
To get started running your own standalone instance of SpacetimeDB check out our [Getting Started Guide](/getting-started).
23+
## Quickstart Guides
24+
25+
Choose your favorite language and follow one of our quickstart guides to get started building your first app with SpacetimeDB.
26+
27+
<QuickstartLinks />
1828

1929
## What is SpacetimeDB?
2030

@@ -82,20 +92,20 @@ You write SQL queries specifying what information a client is interested in -- f
8292

8393
Every SpacetimeDB database contains a collection of [stored procedures](https://en.wikipedia.org/wiki/Stored_procedure) and schema definitions. Such a collection is called a **module**, which can be written in C# or Rust. They specify a database schema and the business logic that responds to client requests. Modules are administered using the `spacetime` CLI tool.
8494

85-
- [Rust](/modules/rust) - [(Quickstart)](/modules/rust/quickstart)
86-
- [C#](/modules/c-sharp) - [(Quickstart)](/modules/c-sharp/quickstart)
95+
- [Rust](/modules/rust) - [(Quickstart)](/docs/quickstarts/rust)
96+
- [C#](/modules/c-sharp) - [(Quickstart)](/docs/quickstarts/c-sharp)
8797

8898
### Client-side SDKs
8999

90100
**Clients** are applications that connect to SpacetimeDB databases. The `spacetime` CLI tool supports automatically generating interface code that makes it easy to interact with a particular database.
91101

92-
- [Rust](/sdks/rust) - [(Quickstart)](/sdks/rust/quickstart)
93-
- [C#](/sdks/c-sharp) - [(Quickstart)](/sdks/c-sharp/quickstart)
94-
- [TypeScript](/sdks/typescript) - [(Quickstart)](/sdks/typescript/quickstart)
102+
- [Rust](/sdks/rust) - [(Quickstart)](/docs/quickstarts/rust)
103+
- [C#](/sdks/c-sharp) - [(Quickstart)](/docs/quickstarts/c-sharp)
104+
- [TypeScript](/sdks/typescript) - [(Quickstart)](/docs/quickstarts/typescript)
95105

96106
### Unity
97107

98-
SpacetimeDB was designed first and foremost as the backend for multiplayer Unity games. To learn more about using SpacetimeDB with Unity, jump on over to the [SpacetimeDB Unity Tutorial](/unity/part-1).
108+
SpacetimeDB was designed first and foremost as the backend for multiplayer Unity games. To learn more about using SpacetimeDB with Unity, jump on over to the [SpacetimeDB Unity Tutorial](/docs/tutorials/unity/part-1).
99109

100110
## Key architectural concepts
101111

@@ -111,7 +121,7 @@ A database exports [tables](#table), which store data, and [reducers](#reducer),
111121

112122
A database's schema and business logic is specified by a piece of software called a **module**. Modules can be written in C# or Rust.
113123

114-
(Technically, a SpacetimeDB module is a [WebAssembly module](https://developer.mozilla.org/en-US/docs/WebAssembly) that imports a specific low-level [WebAssembly ABI](/webassembly-abi) and exports a small number of special functions. However, the SpacetimeDB [server-side libraries](#module-libraries) hide these low-level details. As a developer, writing a module is mostly like writing any other C# or Rust application, except for the fact that a [special CLI tool](https://spacetimedb.com/install) is used to deploy the application.)
124+
(Technically, a SpacetimeDB module is a [WebAssembly module](https://developer.mozilla.org/en-US/docs/WebAssembly) that imports a specific low-level [WebAssembly ABI](/webassembly-abi) and exports a small number of special functions. However, the SpacetimeDB [server-side libraries](#module-libraries) hide these low-level details. As a developer, writing a module is mostly like writing any other C# or Rust application, except for the fact that a [special CLI tool](pathname:///install) is used to deploy the application.)
115125

116126
### Table
117127

@@ -346,4 +356,4 @@ A user has a single [`Identity`](#identity), but may open multiple connections t
346356
Follow our [Quick Start](/getting-started) guide!
347357

348358
1. How do I create a Unity game with SpacetimeDB?
349-
Follow our [Unity Tutorial](/unity) guide!
359+
Follow our [Unity Tutorial](/docs/tutorials/unity) guide!

docs/docs/01-Intro/02-getting-started.md renamed to docs/docs/01-intro/02-getting-started.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ slug: /getting-started
55

66
To develop SpacetimeDB databases locally, you will need to run the Standalone version of the server.
77

8-
1. [Install](https://spacetimedb.com/install) the SpacetimeDB CLI (Command Line Interface)
8+
1. [Install](pathname:///install) the SpacetimeDB CLI (Command Line Interface)
99
2. Run the start command:
1010

1111
```bash
@@ -33,14 +33,14 @@ You are now ready to start developing SpacetimeDB modules. See below for a quick
3333

3434
### Server (Module)
3535

36-
- [Rust](/modules/rust/quickstart)
37-
- [C#](/modules/c-sharp/quickstart)
36+
- [Rust](/docs/quickstarts/rust)
37+
- [C#](/docs/quickstarts/c-sharp)
3838

3939
**Note:** Rust is [roughly 2x faster](https://faun.dev/c/links/faun/c-vs-rust-vs-go-a-performance-benchmarking-in-kubernetes/) than C#
4040

4141
### Client
4242

43-
- [Rust](/sdks/rust/quickstart)
44-
- [C# (Standalone)](/sdks/c-sharp/quickstart)
45-
- [C# (Unity)](/unity/part-1)
46-
- [Typescript](/sdks/typescript/quickstart)
43+
- [Rust](/docs/quickstarts/rust)
44+
- [C# (Standalone)](/docs/quickstarts/c-sharp)
45+
- [C# (Unity)](/docs/tutorials/unity/part-1)
46+
- [Typescript](/docs/quickstarts/typescript)

docs/docs/07-Client SDK Languages/06-typescript-quickstart.md renamed to docs/docs/02-quickstarts/01-typescript.md

Lines changed: 244 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,256 @@
11
---
2-
title: TypeScript Quickstart
3-
slug: /sdks/typescript/quickstart
2+
title: TypeScript
3+
slug: /quickstarts/typescript
4+
id: typescript
45
---
56

6-
# TypeScript Client SDK Quickstart
7+
import { InstallCardLink } from "@site/src/components/InstallCardLink";
78

8-
In this guide, you'll learn how to use TypeScript to create a SpacetimeDB client application.
9+
# Quickstart Chat App
910

10-
Please note that TypeScript is supported as a client language only. **Before you get started on this guide**, you should complete one of the quickstart guides for creating a SpacetimeDB server module listed below.
11+
In this tutorial, we'll implement a simple chat server as a SpacetimeDB **TypeScript** module.
1112

12-
- [Rust](/modules/rust/quickstart)
13-
- [C#](/modules/c-sharp/quickstart)
13+
A SpacetimeDB module is code that gets bundled to a single JavaScript artifact and uploaded to SpacetimeDB. This code becomes server-side logic that interfaces directly with SpacetimeDB’s relational database.
1414

15-
By the end of this introduction, you will have created a basic single page web app which connects to the `quickstart-chat` database created in the above module quickstart guides.
15+
Each SpacetimeDB module defines a set of **tables** and a set of **reducers**.
16+
17+
- Tables are declared with `table({ ...opts }, { ...columns })`. Each inserted object is a row; each field is a column.
18+
- Tables are **private** by default (readable only by the owner and your module code). Set `{ public: true }` to make them readable by everyone; writes still happen only via reducers.
19+
- A **reducer** is a function that reads/writes the database. Each reducer runs in its own transaction; its writes commit only if it completes without throwing. In TypeScript, reducers are registered with `spacetimedb.reducer(name, argTypes, handler)` and throw `new SenderError("...")` for user-visible errors.
20+
21+
:::note
22+
SpacetimeDB runs your module inside the database host (not Node.js). There’s no direct filesystem or network access from reducers.
23+
:::
24+
25+
## Install SpacetimeDB
26+
27+
If you haven’t already, start by [installing SpacetimeDB](pathname:///install). This installs the `spacetime` CLI used to build, publish, and interact with your database.
28+
29+
<InstallCardLink />
30+
31+
## Project structure
32+
33+
Let's start by running `spacetime init` to initialize our project's directory structure:
34+
35+
```bash
36+
spacetime init --lang typescript quickstart-chat
37+
```
38+
39+
`spacetime init` will ask you for a project path in which to put your project. By default this will be `./quickstart-chat`. This basic project will have a few helper files like Cursor rules for SpacetimeDB and a `spacetimedb` directory which is where your SpacetimeDB module code will go.
40+
41+
Inside the `spacetimedb/` directory will be a `src/index.ts` entrypoint (required for publishing).
42+
43+
## Declare imports
44+
45+
Open `spacetimedb/src/index.ts`. Replace its contents with the following imports to start building a bare-bones real-time chat server:
46+
47+
```ts server
48+
import { schema, t, table, SenderError } from 'spacetimedb/server';
49+
```
50+
51+
From `spacetimedb/server`, we import:
52+
53+
- `table` to define SpacetimeDB tables.
54+
- `t` for column/type builders.
55+
- `schema` to compose our database schema and register reducers.
56+
- `SenderError` to signal user-visible (transaction-aborting) errors.
57+
58+
## Define tables
59+
60+
We’ll store two kinds of data: information about each user, and the messages that have been sent.
61+
62+
For each `User`, we’ll store their `identity` (the caller’s unique identifier), an optional display `name`, and whether they’re currently `online`. We’ll use `identity` as the primary key (unique and indexed).
63+
64+
Add to `spacetimedb/src/index.ts`:
65+
66+
```ts server
67+
const User = table(
68+
{ name: 'user', public: true },
69+
{
70+
identity: t.identity().primaryKey(),
71+
name: t.string().optional(),
72+
online: t.bool(),
73+
}
74+
);
75+
76+
const Message = table(
77+
{ name: 'message', public: true },
78+
{
79+
sender: t.identity(),
80+
sent: t.timestamp(),
81+
text: t.string(),
82+
}
83+
);
84+
85+
// Compose the schema (gives us ctx.db.user and ctx.db.message, etc.)
86+
const spacetimedb = schema(User, Message);
87+
```
88+
89+
## Set users’ names
90+
91+
We’ll allow users to set a display name, since raw identities aren’t user-friendly. Define a reducer `set_name` that validates input, looks up the caller’s `User` row by primary key, and updates it. If there’s no user row (e.g., the caller invoked via CLI without a connection and hasn’t connected before), we’ll return an error.
92+
93+
Add:
94+
95+
```ts server
96+
function validateName(name: string) {
97+
if (!name) {
98+
throw new SenderError('Names must not be empty');
99+
}
100+
}
101+
102+
spacetimedb.reducer('set_name', { name: t.string() }, (ctx, { name }) => {
103+
validateName(name);
104+
const user = ctx.db.user.identity.find(ctx.sender);
105+
if (!user) {
106+
throw new SenderError('Cannot set name for unknown user');
107+
}
108+
ctx.db.user.identity.update({ ...user, name });
109+
});
110+
```
111+
112+
You can extend `validateName` with moderation checks, Unicode normalization, printable-character filtering, max length checks, or duplicate-name rejection.
113+
114+
## Send messages
115+
116+
Define a reducer `send_message` to insert a new `Message` with the caller’s identity and the call timestamp. As with names, we’ll validate that text isn’t empty.
117+
118+
Add:
119+
120+
```ts server
121+
function validateMessage(text: string) {
122+
if (!text) {
123+
throw new SenderError('Messages must not be empty');
124+
}
125+
}
126+
127+
spacetimedb.reducer('send_message', { text: t.string() }, (ctx, { text }) => {
128+
validateMessage(text);
129+
console.info(`User ${ctx.sender}: ${text}`);
130+
ctx.db.message.insert({
131+
sender: ctx.sender,
132+
text,
133+
sent: ctx.timestamp,
134+
});
135+
});
136+
```
137+
138+
Possible extensions:
139+
140+
- Reject messages from users who haven’t set a name.
141+
- Rate-limit messages per user.
142+
143+
## Set users’ online status
144+
145+
SpacetimeDB can invoke lifecycle reducers when clients connect/disconnect. We’ll create or update a `User` row to mark the caller online on connect, and mark them offline on disconnect.
146+
147+
Add:
148+
149+
```ts server
150+
// Called once when the module bundle is installed / updated.
151+
// We'll keep it empty for this quickstart.
152+
spacetimedb.init(_ctx => {});
153+
154+
spacetimedb.clientConnected(ctx => {
155+
const user = ctx.db.user.identity.find(ctx.sender);
156+
if (user) {
157+
// Returning user: set online=true, keep identity/name.
158+
ctx.db.user.identity.update({ ...user, online: true });
159+
} else {
160+
// New user: create a User row with no name yet.
161+
ctx.db.user.insert({
162+
identity: ctx.sender,
163+
name: undefined,
164+
online: true,
165+
});
166+
}
167+
});
168+
169+
spacetimedb.clientDisconnected(ctx => {
170+
const user = ctx.db.user.identity.find(ctx.sender);
171+
if (user) {
172+
ctx.db.user.identity.update({ ...user, online: false });
173+
} else {
174+
// Shouldn't happen (disconnect without prior connect)
175+
console.warn(
176+
`Disconnect event for unknown user with identity ${ctx.sender}`
177+
);
178+
}
179+
});
180+
```
181+
182+
## Start the server
183+
184+
If you haven’t already started the SpacetimeDB host on your machine, run this in a **separate terminal** and leave it running:
185+
186+
```bash
187+
spacetime start
188+
```
189+
190+
(If it’s already running, you can skip this step.)
191+
192+
## Publish the module
193+
194+
From the `quickstart-chat` directory (the parent of `spacetimedb/`):
195+
196+
```bash
197+
spacetime publish --server local --project-path spacetimedb quickstart-chat
198+
```
199+
200+
You can choose any unique, URL-safe database name in place of `quickstart-chat`. The CLI will show the database **Identity** (a hex string) as well; you can use either the name or identity with CLI commands.
201+
202+
## Call reducers
203+
204+
Use the CLI to call reducers. Arguments are passed as JSON (strings may be given bare for single string parameters).
205+
206+
Send a message:
207+
208+
```bash
209+
spacetime call --server local quickstart-chat send_message "Hello, World!"
210+
```
211+
212+
Check that it ran by viewing logs (owner-only):
213+
214+
```bash
215+
spacetime logs --server local quickstart-chat
216+
```
217+
218+
You should see output similar to:
219+
220+
```text
221+
<timestamp> INFO: spacetimedb: Creating table `message`
222+
<timestamp> INFO: spacetimedb: Creating table `user`
223+
<timestamp> INFO: spacetimedb: Database initialized
224+
<timestamp> INFO: console: User 0x...: Hello, World!
225+
```
226+
227+
## SQL queries
228+
229+
SpacetimeDB supports a subset of SQL so you can query your data:
230+
231+
```bash
232+
spacetime sql --server local quickstart-chat "SELECT * FROM message"
233+
```
234+
235+
Output will resemble:
236+
237+
```text
238+
sender | sent | text
239+
--------------------------------------------------------------------+----------------------------------+-----------------
240+
0x93dda09db9a56d8fa6c024d843e805d8262191db3b4ba84c5efcd1ad451fed4e | 2025-04-08T15:47:46.935402+00:00 | "Hello, World!"
241+
```
242+
243+
You've just set up your first TypeScript module in SpacetimeDB! You can find the full code for this client [TypeScript server module example](https://github.com/clockworklabs/SpacetimeDB/tree/master/modules/quickstart-chat-ts).
244+
245+
# Creating the client
246+
247+
Next, you'll learn how to use TypeScript to create a SpacetimeDB client application.
248+
249+
By the end of this introduction, you will have created a basic single page web app which connects to the `quickstart-chat` database you just created.
16250

17251
## Project structure
18252

19-
Enter the directory `quickstart-chat` you created in the [Rust Module Quickstart](/modules/rust/quickstart) or [C# Module Quickstart](/modules/c-sharp/quickstart) guides:
253+
Enter the directory `quickstart-chat` you created in the [Rust Module Quickstart](/docs/quickstarts/rust) or [C# Module Quickstart](/docs/quickstarts/c-sharp) guides:
20254

21255
```bash
22256
cd quickstart-chat
@@ -467,7 +701,7 @@ spacetime generate --lang typescript --out-dir client/src/module_bindings --proj
467701

468702
:::note
469703

470-
This command assumes you've already created a server module in `quickstart-chat/server`. If you haven't completed one of the server module quickstart guides, you can follow either the [Rust](/modules/rust/quickstart) or [C#](/modules/c-sharp/quickstart) module quickstart to create one and then return here.
704+
This command assumes you've already created a server module in `quickstart-chat/server`. If you haven't completed one of the server module quickstart guides, you can follow either the [Rust](/docs/quickstarts/rust) or [C#](/docs/quickstarts/c-sharp) module quickstart to create one and then return here.
471705

472706
:::
473707

0 commit comments

Comments
 (0)