From 437fbc3f7e49ff9934f0ad1c86a4849096d8d767 Mon Sep 17 00:00:00 2001 From: bradleyshep <148254416+bradleyshep@users.noreply.github.com> Date: Wed, 28 Jan 2026 09:15:39 -0500 Subject: [PATCH 1/3] init --- .../00200-quickstarts/00250-bun.md | 240 ++++++++++++++++++ templates/bun-ts/.template.json | 5 + templates/bun-ts/LICENSE | 1 + templates/bun-ts/package.json | 19 ++ templates/bun-ts/spacetimedb/package.json | 15 ++ templates/bun-ts/spacetimedb/src/index.ts | 33 +++ templates/bun-ts/spacetimedb/tsconfig.json | 24 ++ templates/bun-ts/src/main.ts | 192 ++++++++++++++ .../bun-ts/src/module_bindings/add_reducer.ts | 15 ++ .../bun-ts/src/module_bindings/add_type.ts | 15 ++ templates/bun-ts/src/module_bindings/index.ts | 151 +++++++++++ .../bun-ts/src/module_bindings/init_type.ts | 13 + .../src/module_bindings/on_connect_reducer.ts | 13 + .../src/module_bindings/on_connect_type.ts | 13 + .../module_bindings/on_disconnect_reducer.ts | 13 + .../src/module_bindings/on_disconnect_type.ts | 13 + .../src/module_bindings/person_table.ts | 15 ++ .../bun-ts/src/module_bindings/person_type.ts | 15 ++ .../src/module_bindings/say_hello_reducer.ts | 13 + .../src/module_bindings/say_hello_type.ts | 13 + templates/bun-ts/tsconfig.json | 22 ++ 21 files changed, 853 insertions(+) create mode 100644 docs/docs/00100-intro/00200-quickstarts/00250-bun.md create mode 100644 templates/bun-ts/.template.json create mode 100644 templates/bun-ts/LICENSE create mode 100644 templates/bun-ts/package.json create mode 100644 templates/bun-ts/spacetimedb/package.json create mode 100644 templates/bun-ts/spacetimedb/src/index.ts create mode 100644 templates/bun-ts/spacetimedb/tsconfig.json create mode 100644 templates/bun-ts/src/main.ts create mode 100644 templates/bun-ts/src/module_bindings/add_reducer.ts create mode 100644 templates/bun-ts/src/module_bindings/add_type.ts create mode 100644 templates/bun-ts/src/module_bindings/index.ts create mode 100644 templates/bun-ts/src/module_bindings/init_type.ts create mode 100644 templates/bun-ts/src/module_bindings/on_connect_reducer.ts create mode 100644 templates/bun-ts/src/module_bindings/on_connect_type.ts create mode 100644 templates/bun-ts/src/module_bindings/on_disconnect_reducer.ts create mode 100644 templates/bun-ts/src/module_bindings/on_disconnect_type.ts create mode 100644 templates/bun-ts/src/module_bindings/person_table.ts create mode 100644 templates/bun-ts/src/module_bindings/person_type.ts create mode 100644 templates/bun-ts/src/module_bindings/say_hello_reducer.ts create mode 100644 templates/bun-ts/src/module_bindings/say_hello_type.ts create mode 100644 templates/bun-ts/tsconfig.json diff --git a/docs/docs/00100-intro/00200-quickstarts/00250-bun.md b/docs/docs/00100-intro/00200-quickstarts/00250-bun.md new file mode 100644 index 00000000000..48a0bd39f89 --- /dev/null +++ b/docs/docs/00100-intro/00200-quickstarts/00250-bun.md @@ -0,0 +1,240 @@ +--- +title: Bun Quickstart +sidebar_label: Bun +slug: /quickstarts/bun +hide_table_of_contents: true +--- + +import { InstallCardLink } from "@site/src/components/InstallCardLink"; +import { StepByStep, Step, StepText, StepCode } from "@site/src/components/Steps"; + + +Get a SpacetimeDB Bun app running in under 5 minutes. + +## Prerequisites + +- [Bun](https://bun.sh/) installed +- [SpacetimeDB CLI](https://spacetimedb.com/install) installed + + + +--- + + + + + Run the `spacetime dev` command to create a new project with a SpacetimeDB module and Bun client. + + This will start the local SpacetimeDB server, publish your module, and generate TypeScript bindings. + + +```bash +spacetime dev --template bun-ts +``` + + + + + + Your project contains both server and client code. + + Edit `spacetimedb/src/index.ts` to add tables and reducers. Edit `src/main.ts` to build your Bun client. + + +``` +my-spacetime-app/ +├── spacetimedb/ # Your SpacetimeDB module +│ └── src/ +│ └── index.ts # Server-side logic +├── src/ +│ ├── main.ts # Bun client script +│ └── module_bindings/ # Auto-generated types +└── package.json +``` + + + + + + Open `spacetimedb/src/index.ts` to see the module code. The template includes a `person` table and two reducers: `add` to insert a person, and `say_hello` to greet everyone. + + Tables store your data. Reducers are functions that modify data — they're the only way to write to the database. + + +```typescript +import { schema, table, t } from 'spacetimedb/server'; + +export const spacetimedb = schema( + table( + { name: 'person', public: true }, + { + name: t.string(), + } + ) +); + +spacetimedb.reducer('add', { name: t.string() }, (ctx, { name }) => { + ctx.db.person.insert({ name }); +}); + +spacetimedb.reducer('say_hello', (ctx) => { + for (const person of ctx.db.person.iter()) { + console.info(`Hello, ${person.name}!`); + } + console.info('Hello, World!'); +}); +``` + + + + + + In a new terminal, run the Bun client. It will connect to SpacetimeDB and start an interactive CLI where you can add people and query the database. + + +```bash +# Run with auto-reload during development +bun run dev + +# Or run once +bun run start +``` + + + + + + The client provides a command-line interface to interact with your SpacetimeDB module. Type a name to add a person, or use the built-in commands. + + +``` +Connecting to SpacetimeDB... + URI: ws://localhost:3000 + Module: bun-ts + +Connected to SpacetimeDB! +Identity: abc123def456... + +Current people (0): + (none yet) + +Commands: + - Add a person with that name + list - Show all people + hello - Greet everyone (check server logs) + Ctrl+C - Quit + +> Alice +[Added] Alice + +> Bob +[Added] Bob + +> list +People in database: + - Alice + - Bob + +> hello +Called say_hello reducer (check server logs) +``` + + + + + + Open `src/main.ts` to see the Bun client. It uses `DbConnection.builder()` to connect to SpacetimeDB, subscribes to tables, and sets up the interactive CLI using Bun's native APIs. + + Unlike browser apps, Bun stores the authentication token in a file using `Bun.file()` and `Bun.write()`. + + +```typescript +import { DbConnection } from './module_bindings/index.js'; + +// Build and establish connection +const conn = DbConnection.builder() + .withUri(HOST) + .withModuleName(DB_NAME) + .withToken(await loadToken()) // Load saved token from file + .onConnect((conn, identity, token) => { + console.log('Connected! Identity:', identity.toHexString()); + saveToken(token); // Save token for future connections + + // Subscribe to all tables + conn.subscriptionBuilder() + .onApplied((ctx) => { + // Show current data, start CLI + setupCLI(); + }) + .subscribeToAllTables(); + + // Listen for table changes + conn.db.person.onInsert((ctx, person) => { + console.log(`[Added] ${person.name}`); + }); + }) + .build(); +``` + + + + + + You can also use the SpacetimeDB CLI to call reducers and query your data directly. Changes made via the CLI will appear in your Bun client in real-time. + + +```bash +# Call the add reducer to insert a person +spacetime call add Charlie + +# Query the person table +spacetime sql "SELECT * FROM person" + name +--------- + "Alice" + "Bob" + "Charlie" + +# Call say_hello to greet everyone +spacetime call say_hello + +# View the module logs +spacetime logs +2025-01-13T12:00:00.000000Z INFO: Hello, Alice! +2025-01-13T12:00:00.000000Z INFO: Hello, Bob! +2025-01-13T12:00:00.000000Z INFO: Hello, Charlie! +2025-01-13T12:00:00.000000Z INFO: Hello, World! +``` + + + + + + **Native WebSocket:** Bun has built-in WebSocket support, so no additional packages like `undici` are needed. + + **Built-in TypeScript:** Bun runs TypeScript directly without transpilation, making startup faster and eliminating the need for `tsx` or `ts-node`. + + **Environment variables:** Bun automatically loads `.env` files. Configure the connection using `SPACETIMEDB_HOST` and `SPACETIMEDB_DB_NAME` environment variables. + + **File APIs:** The template uses `Bun.file()` and `Bun.write()` for token persistence, which are faster than Node.js `fs` operations. + + +```bash +# Configure via environment variables +SPACETIMEDB_HOST=ws://localhost:3000 \ +SPACETIMEDB_DB_NAME=my-app \ +bun run start + +# Or create a .env file (Bun loads it automatically) +echo "SPACETIMEDB_HOST=ws://localhost:3000" > .env +echo "SPACETIMEDB_DB_NAME=my-app" >> .env +bun run start +``` + + + + +## Next steps + +- See the [Chat App Tutorial](/tutorials/chat-app) for a complete example +- Read the [TypeScript SDK Reference](/sdks/typescript) for detailed API docs diff --git a/templates/bun-ts/.template.json b/templates/bun-ts/.template.json new file mode 100644 index 00000000000..f6a2ab3b67e --- /dev/null +++ b/templates/bun-ts/.template.json @@ -0,0 +1,5 @@ +{ + "description": "Bun TypeScript client and server template", + "client_lang": "typescript", + "server_lang": "typescript" +} diff --git a/templates/bun-ts/LICENSE b/templates/bun-ts/LICENSE new file mode 100644 index 00000000000..039e117dde2 --- /dev/null +++ b/templates/bun-ts/LICENSE @@ -0,0 +1 @@ +../../licenses/apache2.txt \ No newline at end of file diff --git a/templates/bun-ts/package.json b/templates/bun-ts/package.json new file mode 100644 index 00000000000..8bd8697a0d3 --- /dev/null +++ b/templates/bun-ts/package.json @@ -0,0 +1,19 @@ +{ + "name": "@clockworklabs/bun-ts", + "private": true, + "version": "0.0.1", + "type": "module", + "scripts": { + "dev": "bun --watch src/main.ts", + "start": "bun src/main.ts", + "build": "bun build src/main.ts --outdir dist", + "spacetime:generate": "spacetime generate --lang typescript --out-dir src/module_bindings --project-path spacetimedb" + }, + "dependencies": { + "spacetimedb": "workspace:*" + }, + "devDependencies": { + "@types/bun": "latest", + "typescript": "~5.6.2" + } +} diff --git a/templates/bun-ts/spacetimedb/package.json b/templates/bun-ts/spacetimedb/package.json new file mode 100644 index 00000000000..214ccc569bf --- /dev/null +++ b/templates/bun-ts/spacetimedb/package.json @@ -0,0 +1,15 @@ +{ + "name": "spacetime-module", + "version": "1.0.0", + "description": "", + "scripts": { + "build": "spacetime build", + "publish": "spacetime publish" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "spacetimedb": "1.*" + } +} diff --git a/templates/bun-ts/spacetimedb/src/index.ts b/templates/bun-ts/spacetimedb/src/index.ts new file mode 100644 index 00000000000..3a5ddbc8257 --- /dev/null +++ b/templates/bun-ts/spacetimedb/src/index.ts @@ -0,0 +1,33 @@ +import { schema, table, t } from 'spacetimedb/server'; + +export const spacetimedb = schema( + table( + { name: 'person', public: true }, + { + name: t.string(), + } + ) +); + +spacetimedb.init(_ctx => { + // Called when the module is initially published +}); + +spacetimedb.clientConnected(_ctx => { + // Called every time a new client connects +}); + +spacetimedb.clientDisconnected(_ctx => { + // Called every time a client disconnects +}); + +spacetimedb.reducer('add', { name: t.string() }, (ctx, { name }) => { + ctx.db.person.insert({ name }); +}); + +spacetimedb.reducer('say_hello', ctx => { + for (const person of ctx.db.person.iter()) { + console.info(`Hello, ${person.name}!`); + } + console.info('Hello, World!'); +}); diff --git a/templates/bun-ts/spacetimedb/tsconfig.json b/templates/bun-ts/spacetimedb/tsconfig.json new file mode 100644 index 00000000000..6283107337e --- /dev/null +++ b/templates/bun-ts/spacetimedb/tsconfig.json @@ -0,0 +1,24 @@ + +/* + * This tsconfig is used for TypeScript projects created with `spacetimedb init + * --lang typescript`. You can modify it as needed for your project, although + * some options are required by SpacetimeDB. + */ +{ + "compilerOptions": { + "strict": true, + "skipLibCheck": true, + "moduleResolution": "bundler", + "jsx": "react-jsx", + + /* The following options are required by SpacetimeDB + * and should not be modified + */ + "target": "ESNext", + "lib": ["ES2021", "dom"], + "module": "ESNext", + "isolatedModules": true, + "noEmit": true + }, + "include": ["./**/*"] +} diff --git a/templates/bun-ts/src/main.ts b/templates/bun-ts/src/main.ts new file mode 100644 index 00000000000..01e81bd2951 --- /dev/null +++ b/templates/bun-ts/src/main.ts @@ -0,0 +1,192 @@ +import { Identity } from 'spacetimedb'; +import { + DbConnection, + ErrorContext, + EventContext, +} from './module_bindings/index.js'; + +// Configuration - Bun supports .env files natively +const HOST = process.env.SPACETIMEDB_HOST ?? 'ws://localhost:3000'; +const DB_NAME = process.env.SPACETIMEDB_DB_NAME ?? 'bun-ts'; + +// Token persistence using Bun APIs +const TOKEN_FILE = '.spacetimedb-token'; + +async function loadToken(): Promise { + try { + const file = Bun.file(TOKEN_FILE); + if (await file.exists()) { + const text = await file.text(); + return text.trim() || undefined; + } + } catch (err) { + console.warn('Could not load token:', err); + } + return undefined; +} + +async function saveToken(token: string): Promise { + try { + await Bun.write(TOKEN_FILE, token); + } catch (err) { + console.warn('Could not save token:', err); + } +} + +// Connection state +let conn: DbConnection | null = null; +let isReady = false; + +// Setup interactive CLI using Bun's console +function setupCLI(): void { + console.log('\nCommands:'); + console.log(' - Add a person with that name'); + console.log(' list - Show all people'); + console.log(' hello - Greet everyone (check server logs)'); + console.log(' Ctrl+C - Quit\n'); + + const prompt = () => process.stdout.write('> '); + prompt(); + + // Use Bun's stdin for reading input + const decoder = new TextDecoder(); + const stdin = Bun.stdin.stream(); + const reader = stdin.getReader(); + + const readLoop = async () => { + while (true) { + const { done, value } = await reader.read(); + if (done) { + shutdown(); + break; + } + + const text = decoder.decode(value).trim(); + if (!text || !conn || !isReady) { + prompt(); + continue; + } + + if (text.toLowerCase() === 'list') { + console.log('\nPeople in database:'); + let count = 0; + for (const person of conn.db.person.iter()) { + console.log(` - ${person.name}`); + count++; + } + if (count === 0) { + console.log(' (none)'); + } + console.log(); + } else if (text.toLowerCase() === 'hello') { + conn.reducers.sayHello({}); + console.log('Called say_hello reducer (check server logs)\n'); + } else { + conn.reducers.add({ name: text }); + } + prompt(); + } + }; + + readLoop().catch(err => { + console.error('CLI error:', err); + shutdown(); + }); +} + +// Connection callbacks +function onConnect( + _conn: DbConnection, + identity: Identity, + token: string +): void { + console.log('\nConnected to SpacetimeDB!'); + console.log(`Identity: ${identity.toHexString().slice(0, 16)}...`); + + // Save token for future connections + saveToken(token); + + // Subscribe to all tables + _conn + .subscriptionBuilder() + .onApplied(ctx => { + isReady = true; + + // Show current people + const people = [...ctx.db.person.iter()]; + console.log(`\nCurrent people (${people.length}):`); + if (people.length === 0) { + console.log(' (none yet)'); + } else { + for (const person of people) { + console.log(` - ${person.name}`); + } + } + + setupCLI(); + }) + .onError((_ctx, err) => { + console.error('Subscription error:', err); + }) + .subscribeToAllTables(); + + // Register callbacks for table changes + _conn.db.person.onInsert((_ctx: EventContext, person) => { + console.log(`[Added] ${person.name}`); + }); + + _conn.db.person.onDelete((_ctx: EventContext, person) => { + console.log(`[Removed] ${person.name}`); + }); +} + +function onDisconnect(_ctx: ErrorContext, error?: Error): void { + isReady = false; + if (error) { + console.error('Disconnected with error:', error); + } else { + console.log('Disconnected from SpacetimeDB'); + } +} + +function onConnectError(_ctx: ErrorContext, error: Error): void { + console.error('Connection error:', error); + process.exit(1); +} + +// Main entry point +async function main(): Promise { + console.log(`Connecting to SpacetimeDB...`); + console.log(` URI: ${HOST}`); + console.log(` Module: ${DB_NAME}`); + + const token = await loadToken(); + + // Build and establish connection + conn = DbConnection.builder() + .withUri(HOST) + .withModuleName(DB_NAME) + .withToken(token) + .onConnect(onConnect) + .onDisconnect(onDisconnect) + .onConnectError(onConnectError) + .build(); +} + +// Graceful shutdown +function shutdown(): void { + console.log('\nShutting down...'); + if (conn) { + conn.disconnect(); + } + process.exit(0); +} + +process.on('SIGINT', shutdown); +process.on('SIGTERM', shutdown); + +// Run the main function +main().catch(err => { + console.error('Fatal error:', err); + process.exit(1); +}); diff --git a/templates/bun-ts/src/module_bindings/add_reducer.ts b/templates/bun-ts/src/module_bindings/add_reducer.ts new file mode 100644 index 00000000000..85081559c7d --- /dev/null +++ b/templates/bun-ts/src/module_bindings/add_reducer.ts @@ -0,0 +1,15 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from 'spacetimedb'; + +export default { + name: __t.string(), +}; diff --git a/templates/bun-ts/src/module_bindings/add_type.ts b/templates/bun-ts/src/module_bindings/add_type.ts new file mode 100644 index 00000000000..638f62cea39 --- /dev/null +++ b/templates/bun-ts/src/module_bindings/add_type.ts @@ -0,0 +1,15 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from 'spacetimedb'; + +export default __t.object('Add', { + name: __t.string(), +}); diff --git a/templates/bun-ts/src/module_bindings/index.ts b/templates/bun-ts/src/module_bindings/index.ts new file mode 100644 index 00000000000..5e83bc75b20 --- /dev/null +++ b/templates/bun-ts/src/module_bindings/index.ts @@ -0,0 +1,151 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +// This was generated using spacetimedb cli version 1.11.3 (commit f9bca6a8df856d950360b40cbce744fcbffc9a63). + +/* eslint-disable */ +/* tslint:disable */ +import { + DbConnectionBuilder as __DbConnectionBuilder, + DbConnectionImpl as __DbConnectionImpl, + SubscriptionBuilderImpl as __SubscriptionBuilderImpl, + TypeBuilder as __TypeBuilder, + Uuid as __Uuid, + convertToAccessorMap as __convertToAccessorMap, + makeQueryBuilder as __makeQueryBuilder, + procedureSchema as __procedureSchema, + procedures as __procedures, + reducerSchema as __reducerSchema, + reducers as __reducers, + schema as __schema, + t as __t, + table as __table, + type AlgebraicTypeType as __AlgebraicTypeType, + type DbConnectionConfig as __DbConnectionConfig, + type ErrorContextInterface as __ErrorContextInterface, + type Event as __Event, + type EventContextInterface as __EventContextInterface, + type Infer as __Infer, + type QueryBuilder as __QueryBuilder, + type ReducerEventContextInterface as __ReducerEventContextInterface, + type RemoteModule as __RemoteModule, + type SubscriptionEventContextInterface as __SubscriptionEventContextInterface, + type SubscriptionHandleImpl as __SubscriptionHandleImpl, +} from 'spacetimedb'; + +// Import and reexport all reducer arg types +import OnConnectReducer from './on_connect_reducer.js'; +export { OnConnectReducer }; +import OnDisconnectReducer from './on_disconnect_reducer.js'; +export { OnDisconnectReducer }; +import AddReducer from './add_reducer.js'; +export { AddReducer }; +import SayHelloReducer from './say_hello_reducer.js'; +export { SayHelloReducer }; + +// Import and reexport all procedure arg types + +// Import and reexport all table handle types +import PersonRow from './person_table.js'; +export { PersonRow }; + +// Import and reexport all types +import Add from './add_type.js'; +export { Add }; +import Init from './init_type.js'; +export { Init }; +import OnConnect from './on_connect_type.js'; +export { OnConnect }; +import OnDisconnect from './on_disconnect_type.js'; +export { OnDisconnect }; +import Person from './person_type.js'; +export { Person }; +import SayHello from './say_hello_type.js'; +export { SayHello }; + +/** The schema information for all tables in this module. This is defined the same was as the tables would have been defined in the server. */ +const tablesSchema = __schema( + __table( + { + name: 'person', + indexes: [], + constraints: [], + }, + PersonRow + ) +); + +/** The schema information for all reducers in this module. This is defined the same way as the reducers would have been defined in the server, except the body of the reducer is omitted in code generation. */ +const reducersSchema = __reducers( + __reducerSchema('add', AddReducer), + __reducerSchema('say_hello', SayHelloReducer) +); + +/** The schema information for all procedures in this module. This is defined the same way as the procedures would have been defined in the server. */ +const proceduresSchema = __procedures(); + +/** The remote SpacetimeDB module schema, both runtime and type information. */ +const REMOTE_MODULE = { + versionInfo: { + cliVersion: '1.11.3' as const, + }, + tables: tablesSchema.schemaType.tables, + reducers: reducersSchema.reducersType.reducers, + ...proceduresSchema, +} satisfies __RemoteModule< + typeof tablesSchema.schemaType, + typeof reducersSchema.reducersType, + typeof proceduresSchema +>; + +/** The tables available in this remote SpacetimeDB module. */ +export const tables = __convertToAccessorMap(tablesSchema.schemaType.tables); + +/** A typed query builder for this remote SpacetimeDB module. */ +export const query: __QueryBuilder = + __makeQueryBuilder(tablesSchema.schemaType); + +/** The reducers available in this remote SpacetimeDB module. */ +export const reducers = __convertToAccessorMap( + reducersSchema.reducersType.reducers +); + +/** The context type returned in callbacks for all possible events. */ +export type EventContext = __EventContextInterface; +/** The context type returned in callbacks for reducer events. */ +export type ReducerEventContext = __ReducerEventContextInterface< + typeof REMOTE_MODULE +>; +/** The context type returned in callbacks for subscription events. */ +export type SubscriptionEventContext = __SubscriptionEventContextInterface< + typeof REMOTE_MODULE +>; +/** The context type returned in callbacks for error events. */ +export type ErrorContext = __ErrorContextInterface; +/** The subscription handle type to manage active subscriptions created from a {@link SubscriptionBuilder}. */ +export type SubscriptionHandle = __SubscriptionHandleImpl; + +/** Builder class to configure a new subscription to the remote SpacetimeDB instance. */ +export class SubscriptionBuilder extends __SubscriptionBuilderImpl< + typeof REMOTE_MODULE +> {} + +/** Builder class to configure a new database connection to the remote SpacetimeDB instance. */ +export class DbConnectionBuilder extends __DbConnectionBuilder {} + +/** The typed database connection to manage connections to the remote SpacetimeDB instance. This class has type information specific to the generated module. */ +export class DbConnection extends __DbConnectionImpl { + /** Creates a new {@link DbConnectionBuilder} to configure and connect to the remote SpacetimeDB instance. */ + static builder = (): DbConnectionBuilder => { + return new DbConnectionBuilder( + REMOTE_MODULE, + (config: __DbConnectionConfig) => + new DbConnection(config) + ); + }; + + /** Creates a new {@link SubscriptionBuilder} to configure a subscription to the remote SpacetimeDB instance. */ + override subscriptionBuilder = (): SubscriptionBuilder => { + return new SubscriptionBuilder(this); + }; +} diff --git a/templates/bun-ts/src/module_bindings/init_type.ts b/templates/bun-ts/src/module_bindings/init_type.ts new file mode 100644 index 00000000000..52ed691ed94 --- /dev/null +++ b/templates/bun-ts/src/module_bindings/init_type.ts @@ -0,0 +1,13 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from 'spacetimedb'; + +export default __t.object('Init', {}); diff --git a/templates/bun-ts/src/module_bindings/on_connect_reducer.ts b/templates/bun-ts/src/module_bindings/on_connect_reducer.ts new file mode 100644 index 00000000000..2ca99c88fea --- /dev/null +++ b/templates/bun-ts/src/module_bindings/on_connect_reducer.ts @@ -0,0 +1,13 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from 'spacetimedb'; + +export default {}; diff --git a/templates/bun-ts/src/module_bindings/on_connect_type.ts b/templates/bun-ts/src/module_bindings/on_connect_type.ts new file mode 100644 index 00000000000..d36362515de --- /dev/null +++ b/templates/bun-ts/src/module_bindings/on_connect_type.ts @@ -0,0 +1,13 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from 'spacetimedb'; + +export default __t.object('OnConnect', {}); diff --git a/templates/bun-ts/src/module_bindings/on_disconnect_reducer.ts b/templates/bun-ts/src/module_bindings/on_disconnect_reducer.ts new file mode 100644 index 00000000000..2ca99c88fea --- /dev/null +++ b/templates/bun-ts/src/module_bindings/on_disconnect_reducer.ts @@ -0,0 +1,13 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from 'spacetimedb'; + +export default {}; diff --git a/templates/bun-ts/src/module_bindings/on_disconnect_type.ts b/templates/bun-ts/src/module_bindings/on_disconnect_type.ts new file mode 100644 index 00000000000..efda71ebcfd --- /dev/null +++ b/templates/bun-ts/src/module_bindings/on_disconnect_type.ts @@ -0,0 +1,13 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from 'spacetimedb'; + +export default __t.object('OnDisconnect', {}); diff --git a/templates/bun-ts/src/module_bindings/person_table.ts b/templates/bun-ts/src/module_bindings/person_table.ts new file mode 100644 index 00000000000..0f70f74f617 --- /dev/null +++ b/templates/bun-ts/src/module_bindings/person_table.ts @@ -0,0 +1,15 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from 'spacetimedb'; + +export default __t.row({ + name: __t.string(), +}); diff --git a/templates/bun-ts/src/module_bindings/person_type.ts b/templates/bun-ts/src/module_bindings/person_type.ts new file mode 100644 index 00000000000..1156775a3cf --- /dev/null +++ b/templates/bun-ts/src/module_bindings/person_type.ts @@ -0,0 +1,15 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from 'spacetimedb'; + +export default __t.object('Person', { + name: __t.string(), +}); diff --git a/templates/bun-ts/src/module_bindings/say_hello_reducer.ts b/templates/bun-ts/src/module_bindings/say_hello_reducer.ts new file mode 100644 index 00000000000..2ca99c88fea --- /dev/null +++ b/templates/bun-ts/src/module_bindings/say_hello_reducer.ts @@ -0,0 +1,13 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from 'spacetimedb'; + +export default {}; diff --git a/templates/bun-ts/src/module_bindings/say_hello_type.ts b/templates/bun-ts/src/module_bindings/say_hello_type.ts new file mode 100644 index 00000000000..6293ca6bd09 --- /dev/null +++ b/templates/bun-ts/src/module_bindings/say_hello_type.ts @@ -0,0 +1,13 @@ +// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE +// WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. + +/* eslint-disable */ +/* tslint:disable */ +import { + TypeBuilder as __TypeBuilder, + t as __t, + type AlgebraicTypeType as __AlgebraicTypeType, + type Infer as __Infer, +} from 'spacetimedb'; + +export default __t.object('SayHello', {}); diff --git a/templates/bun-ts/tsconfig.json b/templates/bun-ts/tsconfig.json new file mode 100644 index 00000000000..e50bb6d56b4 --- /dev/null +++ b/templates/bun-ts/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "target": "ES2022", + "module": "ESNext", + "moduleResolution": "bundler", + "lib": ["ES2022"], + "outDir": "dist", + "rootDir": "src", + "strict": true, + "esModuleInterop": true, + "skipLibCheck": true, + "forceConsistentCasingInFileNames": true, + "resolveJsonModule": true, + "declaration": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noFallthroughCasesInSwitch": true, + "types": ["bun-types"] + }, + "include": ["src"], + "exclude": ["node_modules", "dist"] +} From 364407e7835a3e4e53126706fbc55cbcad367399 Mon Sep 17 00:00:00 2001 From: bradleyshep <148254416+bradleyshep@users.noreply.github.com> Date: Wed, 28 Jan 2026 13:45:35 -0500 Subject: [PATCH 2/3] Update tsconfig.json --- templates/bun-ts/spacetimedb/tsconfig.json | 1 - 1 file changed, 1 deletion(-) diff --git a/templates/bun-ts/spacetimedb/tsconfig.json b/templates/bun-ts/spacetimedb/tsconfig.json index 6283107337e..b6f79b99474 100644 --- a/templates/bun-ts/spacetimedb/tsconfig.json +++ b/templates/bun-ts/spacetimedb/tsconfig.json @@ -1,4 +1,3 @@ - /* * This tsconfig is used for TypeScript projects created with `spacetimedb init * --lang typescript`. You can modify it as needed for your project, although From 64f61d12bd7da52f079e795142bd728589eddcb2 Mon Sep 17 00:00:00 2001 From: bradleyshep <148254416+bradleyshep@users.noreply.github.com> Date: Mon, 2 Feb 2026 17:03:13 -0500 Subject: [PATCH 3/3] code reorg + docs tweak --- .../00200-quickstarts/00250-bun.md | 85 +++++--- templates/bun-ts/src/main.ts | 205 ++++++++---------- 2 files changed, 150 insertions(+), 140 deletions(-) diff --git a/docs/docs/00100-intro/00200-quickstarts/00250-bun.md b/docs/docs/00100-intro/00200-quickstarts/00250-bun.md index 48a0bd39f89..f65db619dab 100644 --- a/docs/docs/00100-intro/00200-quickstarts/00250-bun.md +++ b/docs/docs/00100-intro/00200-quickstarts/00250-bun.md @@ -8,7 +8,6 @@ hide_table_of_contents: true import { InstallCardLink } from "@site/src/components/InstallCardLink"; import { StepByStep, Step, StepText, StepCode } from "@site/src/components/Steps"; - Get a SpacetimeDB Bun app running in under 5 minutes. ## Prerequisites @@ -28,10 +27,13 @@ Get a SpacetimeDB Bun app running in under 5 minutes. This will start the local SpacetimeDB server, publish your module, and generate TypeScript bindings. + ```bash spacetime dev --template bun-ts ``` + + @@ -41,6 +43,7 @@ spacetime dev --template bun-ts Edit `spacetimedb/src/index.ts` to add tables and reducers. Edit `src/main.ts` to build your Bun client. + ``` my-spacetime-app/ ├── spacetimedb/ # Your SpacetimeDB module @@ -51,7 +54,9 @@ my-spacetime-app/ │ └── module_bindings/ # Auto-generated types └── package.json ``` + + @@ -61,6 +66,7 @@ my-spacetime-app/ Tables store your data. Reducers are functions that modify data — they're the only way to write to the database. + ```typescript import { schema, table, t } from 'spacetimedb/server'; @@ -77,14 +83,16 @@ spacetimedb.reducer('add', { name: t.string() }, (ctx, { name }) => { ctx.db.person.insert({ name }); }); -spacetimedb.reducer('say_hello', (ctx) => { +spacetimedb.reducer('say_hello', ctx => { for (const person of ctx.db.person.iter()) { console.info(`Hello, ${person.name}!`); } console.info('Hello, World!'); }); ``` + + @@ -97,7 +105,9 @@ spacetimedb.reducer('say_hello', (ctx) => { bun run dev # Or run once + bun run start + ``` @@ -108,36 +118,39 @@ bun run start ``` + Connecting to SpacetimeDB... - URI: ws://localhost:3000 - Module: bun-ts +URI: ws://localhost:3000 +Module: bun-ts Connected to SpacetimeDB! Identity: abc123def456... Current people (0): - (none yet) +(none yet) Commands: - - Add a person with that name - list - Show all people - hello - Greet everyone (check server logs) - Ctrl+C - Quit + - Add a person with that name +list - Show all people +hello - Greet everyone (check server logs) +Ctrl+C - Quit > Alice -[Added] Alice +> [Added] Alice > Bob -[Added] Bob +> [Added] Bob > list -People in database: - - Alice - - Bob +> People in database: + +- Alice +- Bob > hello -Called say_hello reducer (check server logs) -``` +> Called say_hello reducer (check server logs) + +```` @@ -152,7 +165,7 @@ Called say_hello reducer (check server logs) import { DbConnection } from './module_bindings/index.js'; // Build and establish connection -const conn = DbConnection.builder() +DbConnection.builder() .withUri(HOST) .withModuleName(DB_NAME) .withToken(await loadToken()) // Load saved token from file @@ -164,7 +177,7 @@ const conn = DbConnection.builder() conn.subscriptionBuilder() .onApplied((ctx) => { // Show current data, start CLI - setupCLI(); + setupCLI(conn); }) .subscribeToAllTables(); @@ -174,8 +187,10 @@ const conn = DbConnection.builder() }); }) .build(); -``` +```` + + @@ -188,23 +203,29 @@ const conn = DbConnection.builder() spacetime call add Charlie # Query the person table -spacetime sql "SELECT * FROM person" - name ---------- - "Alice" - "Bob" - "Charlie" + +spacetime sql "SELECT \* FROM person" +name + +--- + +"Alice" +"Bob" +"Charlie" # Call say_hello to greet everyone + spacetime call say_hello # View the module logs + spacetime logs -2025-01-13T12:00:00.000000Z INFO: Hello, Alice! -2025-01-13T12:00:00.000000Z INFO: Hello, Bob! -2025-01-13T12:00:00.000000Z INFO: Hello, Charlie! -2025-01-13T12:00:00.000000Z INFO: Hello, World! -``` +2025-01-13T12:00:00.000000Z INFO: Hello, Alice! +2025-01-13T12:00:00.000000Z INFO: Hello, Bob! +2025-01-13T12:00:00.000000Z INFO: Hello, Charlie! +2025-01-13T12:00:00.000000Z INFO: Hello, World! + +```` @@ -229,8 +250,10 @@ bun run start echo "SPACETIMEDB_HOST=ws://localhost:3000" > .env echo "SPACETIMEDB_DB_NAME=my-app" >> .env bun run start -``` +```` + + diff --git a/templates/bun-ts/src/main.ts b/templates/bun-ts/src/main.ts index 01e81bd2951..4344826024d 100644 --- a/templates/bun-ts/src/main.ts +++ b/templates/bun-ts/src/main.ts @@ -9,36 +9,82 @@ import { const HOST = process.env.SPACETIMEDB_HOST ?? 'ws://localhost:3000'; const DB_NAME = process.env.SPACETIMEDB_DB_NAME ?? 'bun-ts'; -// Token persistence using Bun APIs -const TOKEN_FILE = '.spacetimedb-token'; +// Main entry point +async function main(): Promise { + console.log(`Connecting to SpacetimeDB...`); + console.log(` URI: ${HOST}`); + console.log(` Module: ${DB_NAME}`); -async function loadToken(): Promise { - try { - const file = Bun.file(TOKEN_FILE); - if (await file.exists()) { - const text = await file.text(); - return text.trim() || undefined; - } - } catch (err) { - console.warn('Could not load token:', err); - } - return undefined; + const token = await loadToken(); + + // Build and establish connection + DbConnection.builder() + .withUri(HOST) + .withModuleName(DB_NAME) + .withToken(token) + .onConnect(onConnect) + .onDisconnect(onDisconnect) + .onConnectError(onConnectError) + .build(); } -async function saveToken(token: string): Promise { - try { - await Bun.write(TOKEN_FILE, token); - } catch (err) { - console.warn('Could not save token:', err); +function onConnect( + conn: DbConnection, + identity: Identity, + token: string +): void { + console.log('\nConnected to SpacetimeDB!'); + console.log(`Identity: ${identity.toHexString().slice(0, 16)}...`); + + // Save token for future connections + saveToken(token); + + // Subscribe to all tables + conn + .subscriptionBuilder() + .onApplied(ctx => { + // Show current people + const people = [...ctx.db.person.iter()]; + console.log(`\nCurrent people (${people.length}):`); + if (people.length === 0) { + console.log(' (none yet)'); + } else { + for (const person of people) { + console.log(` - ${person.name}`); + } + } + + setupCLI(conn); + }) + .onError((_ctx, err) => { + console.error('Subscription error:', err); + }) + .subscribeToAllTables(); + + // Register callbacks for table changes + conn.db.person.onInsert((_ctx: EventContext, person) => { + console.log(`[Added] ${person.name}`); + }); + + conn.db.person.onDelete((_ctx: EventContext, person) => { + console.log(`[Removed] ${person.name}`); + }); +} + +function onDisconnect(_ctx: ErrorContext, error?: Error): void { + if (error) { + console.error('Disconnected with error:', error); + } else { + console.log('Disconnected from SpacetimeDB'); } } -// Connection state -let conn: DbConnection | null = null; -let isReady = false; +function onConnectError(_ctx: ErrorContext, error: Error): void { + console.error('Connection error:', error); + process.exit(1); +} -// Setup interactive CLI using Bun's console -function setupCLI(): void { +function setupCLI(conn: DbConnection): void { console.log('\nCommands:'); console.log(' - Add a person with that name'); console.log(' list - Show all people'); @@ -53,6 +99,15 @@ function setupCLI(): void { const stdin = Bun.stdin.stream(); const reader = stdin.getReader(); + const shutdown = (): void => { + console.log('\nShutting down...'); + conn.disconnect(); + process.exit(0); + }; + + process.on('SIGINT', shutdown); + process.on('SIGTERM', shutdown); + const readLoop = async () => { while (true) { const { done, value } = await reader.read(); @@ -62,7 +117,7 @@ function setupCLI(): void { } const text = decoder.decode(value).trim(); - if (!text || !conn || !isReady) { + if (!text) { prompt(); continue; } @@ -94,98 +149,30 @@ function setupCLI(): void { }); } -// Connection callbacks -function onConnect( - _conn: DbConnection, - identity: Identity, - token: string -): void { - console.log('\nConnected to SpacetimeDB!'); - console.log(`Identity: ${identity.toHexString().slice(0, 16)}...`); - - // Save token for future connections - saveToken(token); - - // Subscribe to all tables - _conn - .subscriptionBuilder() - .onApplied(ctx => { - isReady = true; - - // Show current people - const people = [...ctx.db.person.iter()]; - console.log(`\nCurrent people (${people.length}):`); - if (people.length === 0) { - console.log(' (none yet)'); - } else { - for (const person of people) { - console.log(` - ${person.name}`); - } - } - - setupCLI(); - }) - .onError((_ctx, err) => { - console.error('Subscription error:', err); - }) - .subscribeToAllTables(); - - // Register callbacks for table changes - _conn.db.person.onInsert((_ctx: EventContext, person) => { - console.log(`[Added] ${person.name}`); - }); - - _conn.db.person.onDelete((_ctx: EventContext, person) => { - console.log(`[Removed] ${person.name}`); - }); -} +// Token persistence using Bun APIs +const TOKEN_FILE = '.spacetimedb-token'; -function onDisconnect(_ctx: ErrorContext, error?: Error): void { - isReady = false; - if (error) { - console.error('Disconnected with error:', error); - } else { - console.log('Disconnected from SpacetimeDB'); +async function loadToken(): Promise { + try { + const file = Bun.file(TOKEN_FILE); + if (await file.exists()) { + const text = await file.text(); + return text.trim() || undefined; + } + } catch (err) { + console.warn('Could not load token:', err); } + return undefined; } -function onConnectError(_ctx: ErrorContext, error: Error): void { - console.error('Connection error:', error); - process.exit(1); -} - -// Main entry point -async function main(): Promise { - console.log(`Connecting to SpacetimeDB...`); - console.log(` URI: ${HOST}`); - console.log(` Module: ${DB_NAME}`); - - const token = await loadToken(); - - // Build and establish connection - conn = DbConnection.builder() - .withUri(HOST) - .withModuleName(DB_NAME) - .withToken(token) - .onConnect(onConnect) - .onDisconnect(onDisconnect) - .onConnectError(onConnectError) - .build(); -} - -// Graceful shutdown -function shutdown(): void { - console.log('\nShutting down...'); - if (conn) { - conn.disconnect(); +async function saveToken(token: string): Promise { + try { + await Bun.write(TOKEN_FILE, token); + } catch (err) { + console.warn('Could not save token:', err); } - process.exit(0); } -process.on('SIGINT', shutdown); -process.on('SIGTERM', shutdown); - -// Run the main function main().catch(err => { console.error('Fatal error:', err); process.exit(1);