spacetime-cr v0.1.2
spacetime-cr
A comprehensive, zero-dependency Crystal SDK, BSATN & SATS-JSON codec, real-time WebSocket & HTTP client, reactive client-side table cache, code generator, and WASM module bindings for SpacetimeDB.
Built according to the Sol-Vin Two-Tier Engine Architecture, separating low-level binary/algebraic primitives (Spacetime::BSATN, Spacetime::SATS, Spacetime::Protocol) from expressive compile-time AST macros (Spacetime.table, Spacetime.type, Spacetime::Serializable, Spacetime::SQL).
Features
- Complete Spacetime Algebraic Type System (
Spacetime::SATS):- Full
AlgebraicTypeAST (Ref,Sum,Product,Array,String,Bool,I8..I256,U8..U256,F32,F64) andTypespaceresolution. - Native 256-bit integers (
Spacetime::U256,Spacetime::I256) and first-class special SATS types:Spacetime::Identity,Spacetime::ConnectionId,Spacetime::Timestamp,Spacetime::TimeDuration,Spacetime::UUID,Spacetime::ScheduleAt, andSpacetime::EnergyQuanta. - Dynamic
AlgebraicValue,ProductValue, andSumValuewith schema-guided BSATN encoding/decoding, SATS-JSON serialization, SATN human-readable formatting (to_satn), and structural type checking (type_check?).
- Full
- Zero-Allocation Binary Codec (
Spacetime::BSATN):- Little-endian binary
BSATN::EncoderandBSATN::Decoderwith strictBool(0or1) and UTF-8 validation. - Packed
BsatnRowListandRowSizeHint(FixedSizeandRowOffsets) for high-throughput table row streaming.
- Little-endian binary
- Full WebSocket Wire Protocol (
v1.bsatn.spacetimedb&v1.json.spacetimedb):- All 8
ClientMessagevariants (CallReducer,Subscribe,OneOffQuery,SubscribeSingle,SubscribeMulti,Unsubscribe,UnsubscribeMulti,CallProcedure). - All 11
ServerMessagevariants (InitialSubscription,TransactionUpdate,TransactionUpdateLight,IdentityToken,OneOffQueryResponse,SubscribeApplied,UnsubscribeApplied,SubscriptionError,SubscribeMultiApplied,UnsubscribeMultiApplied,ProcedureResult). - Transparent WebSocket frame and
CompressableQueryUpdateGzip compression (0x00None,0x02Gzip).
- All 8
- Reactive Client & Local Table Cache (
Spacetime::Client&Spacetime::TableCache(T)):- Automatic primary-key deduplication and row-level reconciliation: when a transaction deletes an old row and inserts a new row with the same
@[Spacetime::PrimaryKey],TableCache(T)automatically emitson_update(old_row, new_row)alongsideon_insert(row)andon_delete(row). - Pluggable
Spacetime::Transport(WebSocketTransportfor live connections andMockTransportfor deterministic unit testing).
- Automatic primary-key deduplication and row-level reconciliation: when a transaction deletes an old row and inserts a new row with the same
- Complete HTTP REST API Client (
Spacetime::HTTPClient):- Covers
/v1/identity(create_identity,create_websocket_token,public_key,list_databases,verify_identity) and/v1/database(publish_database,get_database,delete_database,get_database_identity,get_database_names,add_database_name,schema,sql,call_reducer,logs).
- Covers
- Compile-Time Two-Tier DSL & Schema Codegen (
spacetime-cr generate):Spacetime.tableandSpacetime.typemacros (andSpacetime::Serializablewith@[Spacetime::PrimaryKey],@[Spacetime::Unique],@[Spacetime::AutoInc],@[Spacetime::Index]) generate BSATN codecs, SATS-JSON codecs, andRawTableDefV9schemas at compile time.Spacetime::Codegen::CrystalGeneratorandspacetime-cr generateinspect a V9 schema JSON file or live database and generate typed Crystal structs, sum enums, and reducer methods.
- WASM Guest Module ABI & Transactional Datastore (
Spacetime::Wasm):spacetime_10.0WASM host import bindings (LibSpacetimeWasm) plus an in-memory transactionalSpacetime::Wasm::Datastorewith atomic rollback for testing server module logic natively incrystal spec.
Installation
Add spacetime to your shard.yml:
dependencies:
spacetime:
github: sol-vin/spacetime-cr
branch: main
Then run:
shards install
Quick Start
1. Define Tables & Types with the Declarative DSL
require "spacetime"
Spacetime.type Vector3 do
field x : Float32
field y : Float32
field z : Float32
end
Spacetime.table Player, name: "players", public: true do
primary_key identity : Spacetime::Identity
unique username : String
field position : Vector3
field health : UInt32 = 100_u32
field online : Bool = true
end
2. Connect, Subscribe & React to Live Table Updates
client = Spacetime.connect("http://localhost:3000", "mmo_world") do |opts|
opts.use_bsatn
opts.compression(Spacetime::Protocol::Compression::None)
end
players = client.register_table(Player)
players.on_insert do |player|
puts "[SPAWN] #{player.username} at (#{player.position.x}, #{player.position.y}, #{player.position.z})"
end
players.on_update do |old_player, new_player|
puts "[MOVE] #{new_player.username}: #{old_player.position} -> #{new_player.position}"
end
players.on_delete do |player|
puts "[LEAVE] #{player.username}"
end
# Subscribe using the fluent SQL builder
query = Spacetime::SQL.select_from("players").where_eq("online", true)
client.subscribe_single(query)
# Invoke a server-side reducer with BSATN arguments
client.call_reducer("move_player") do |enc|
enc.write_value(Vector3.new(x: 42.0_f32, y: 0.0_f32, z: 15.0_f32))
end
3. Query via HTTP REST API (/v1/database/:name/sql)
http = Spacetime.http("http://localhost:3000")
creds = http.create_identity
results = http.sql("mmo_world", Spacetime::SQL.select_from("players").where_eq("online", true))
online_players = results.first.decode_rows(Player)
4. Generate Crystal Bindings from a Module Schema (spacetime-cr)
# Generate bindings from a live SpacetimeDB instance:
bin/spacetime-cr generate --host http://localhost:3000 --database mmo_world --module MmoBindings -o src/mmo_bindings.cr
# Or from a local V9 schema JSON file:
bin/spacetime-cr generate --schema schema.json --module MmoBindings -o src/mmo_bindings.cr
# Run SDK & codec self-diagnostics:
bin/spacetime-cr doctor
License
MIT License — Copyright (c) 2026 Ian Rash (@sol-vin).
Repository
spacetime-cr
Owner
Statistic
- 0
- 0
- 0
- 0
- 2
- about 1 hour ago
- October 23, 2026
License
MIT License
Links
Synced at
Fri, 23 Oct 2026 04:36:34 GMT
Languages