module

Spacetime::Docs::B_GUIDES::CodegenAndWasmAbi

Schema Codegen & WASM Module ABI

spacetime-cr includes both a schema-driven Crystal code generator (Spacetime::Codegen::CrystalGenerator) for client applications and low-level WebAssembly host ABI declarations (Spacetime::Wasm) for writing server-side SpacetimeDB modules in Crystal.

Executive Summary & Key Topics

Topic Method / Anchor Description
Generating Typed Models & Reducers .topic_01_schema_codegen Compile SpacetimeDB v1 JSON schemas into Crystal structs and reducer helpers.
WASM Host Environment (wasm32-unknown-unknown) .topic_02_wasm_abi Low-level SpacetimeDB WebAssembly host imports and native simulation.
  • Spacetime::Codegen::CrystalGenerator
  • Spacetime::Schema::ModuleSchema
  • Spacetime::Wasm::Host

Class methods

topic_01_schema_codegen

Generating Typed Models & Reducers: Compile SpacetimeDB v1 JSON schemas into Crystal structs and reducer helpers.

Given a SpacetimeDB v1 schema JSON (fetched via spacetime-cr schema or exported from a module), Spacetime::Codegen::CrystalGenerator emits Spacetime::Serializable structs for all product types and tables, a register_tables(client) helper, and a Reducers module.

Working Examples

require "spacetime"

schema = Spacetime::Schema::ModuleSchema.from_json(File.read("schema.json")) code = Spacetime::Codegen::CrystalGenerator.generate(schema, module_name: "GameBindings") File.write("src/game_bindings.cr", code)

Source
topic_02_wasm_abi

WASM Host Environment (wasm32-unknown-unknown): Low-level SpacetimeDB WebAssembly host imports and native simulation.

Key Topics & Information

  • Spacetime::Wasm::LibSpacetime: Raw wasm32 host imports (_datastore_insert_bsatn, _datastore_delete_by_rel_bsatn, _datastore_iter_bsatn, _console_log).
  • Spacetime::Wasm::Host: Safe wrappers that work both on wasm32 and in native host simulation (Host.reset_simulated_store! for unit testing reducers natively).
  • Spacetime::Wasm::ReducerContext: Provides caller Identity, ConnectionId?, and Timestamp to reducer functions.
Source