mcpField
S
spacetimedb-reducers

spacetimedb-reducers

github.com/danmossaDevelopment2168 installsUpdated 19w ago
Repository

About spacetimedb-reducers

spacetimedb-reducers is a skill for implementing SpacetimeDB reducers and transactional server logic. It covers table access, rollback behavior, lifecycle reducers, and language-specific patterns for TypeScript, C#, Rust, and C++.

Key features

  1. 01Reducer patterns for multiple languages
  2. 02Transactional mutation and rollback guidance
  3. 03Indexed table access patterns
  4. 04Lifecycle reducer references
  5. 05Error handling guidance

Use cases

  1. 01Implementing transactional table mutations
  2. 02Validating and updating rows in reducers
  3. 03Handling lifecycle events
  4. 04Keeping external I/O out of reducers

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/danmossa/spacetimedb-skills --skill spacetimedb-reducers

After installing, type/spacetimedb-reducersin Claude Code to confirm it loaded.

Bundled files

References and scripts installed along with SKILL.md.

references/
functions-reducers-error-handling.md
functions-reducers-lifecycle.md
functions-reducers-reducer-context.md
functions-reducers.md
functions.md

Details

From the project README

Use this skill for reducer implementation and transactional server logic.

Core reducer patterns

Reducers are the transactional entrypoints for table mutation. A successful reducer commits all table changes; a thrown exception, returned error, or panic rolls all of them back.

TypeScript reducer shape:

export const create_user = spacetimedb.reducer(
  { name: t.string(), email: t.string() },
  (ctx, { name, email }) => {
    if (name === "") throw new Error("Name cannot be empty");
    ctx.db.user.insert({ id: 0n, name, email });
  },
);

Language-specific signatures:

  • TypeScript: spacetimedb.reducer(args, (ctx, args) => { ... }), or spacetimedb.reducer((ctx) => { ... }) for no args.
  • C#: [SpacetimeDB.Reducer] public static void Name(ReducerContext ctx, ...).
  • Rust: #[spacetimedb::reducer] pub fn name(ctx: &ReducerContext, ...) -> Result<(), String>; import spacetimedb::Table for table methods.
  • Lifecycle reducers use init, client_connected, and client_disconnected variants in the language's syntax.

Table access

  • Insert rows through the generated table handle; use the trigger value (0, 0n, etc.) for auto-increment fields.
  • Find one row through a primary key or unique index: TypeScript ctx.db.user.id.find(id), C# ctx.Db.User.Id.Find(id), Rust ctx.db.user().id().find(id).
  • Filter many rows through indexed columns. Avoid full iter() scans on hot paths unless the table is known to be small.
  • Update by finding a row, modifying it, then calling the update method on the same primary-key index.
  • Delete by primary key/unique value or indexed filters when possible.
  • Use ctx.sender in TypeScript/C# and ctx.sender() in Rust/C++ for the caller identity on SpacetimeDB 2.0.

Transaction guidance

  • Keep reducers short and deterministic; move external I/O to procedures.
  • Nested reducer calls share the same transaction; they are not separate commits.
  • Use sender-facing errors for expected validation failures when the language exposes them; reserve panics or unchecked exceptions for programmer errors.
  • Scheduled reducers run from schedule-table rows and may have no client sender.

Reference map

NeedOpen
Reducer basics and table accessreferences/functions-reducers.md
Lifecycle reducersreferences/functions-reducers-lifecycle.md
Reducer contextreferences/functions-reducers-reducer-context.md
Error handlingreferences/functions-reducers-error-handling.md
Function categoriesreferences/functions.md

Guidance

  • Treat reducers as transactions: keep side effects and error behavior aligned with the reference semantics.
  • Use ctx.sender/ctx.sender() according to the project's SpacetimeDB version; load spacetimedb-upgrade for 1.0 to 2.0 migrations.
  • Route schema questions to spacetimedb-tables.

Comments

Markdown supported