module

Spacetime::Docs::A_GETTING_STARTED::Quickstart

Quickstart Guide

Getting started with spacetime-cr takes only a few lines of Crystal. Add the shard to your shard.yml, define your table structs with include Spacetime::Serializable, and connect to your SpacetimeDB instance.

Executive Summary & Key Topics

Topic Method / Anchor Description
1. Installation .topic_01_installation Add spacetime-cr to your project's shard.yml and install dependencies.
2. Define a Model, Connect & Subscribe .topic_02_connect_and_subscribe Register a typed TableCache, subscribe with SQL builder, and invoke a reducer.
  • Spacetime::Client
  • Spacetime::Query
  • Spacetime::TableCache

Class methods

topic_01_installation

1 Installation: Add spacetime-cr to your project's shard.yml and install dependencies.

Add spacetime to your shard.yml dependencies and run shards install:

Working Examples

dependencies: spacetime: github: sol-vin/spacetime-cr version: ~> 0.1.0

Source
topic_02_connect_and_subscribe

2 Define a Model, Connect & Subscribe: Register a typed TableCache, subscribe with SQL builder, and invoke a reducer.

Include Spacetime::Serializable in your row structs, annotate the primary key with @[Spacetime::Field(primary_key: true)], and register the table with your Spacetime::Client:

Working Examples

require "spacetime"

struct Player include Spacetime::Serializable

@[Spacetime::Field(primary_key: true)] getter id : UInt64 getter username : String getter score : UInt32

def initialize(@id : UInt64, @username : String, @score : UInt32) end end

client = Spacetime.connect("ws://localhost:3000", "game_world") do |opts| opts.protocol = Spacetime::Subprotocol::Bsatn end

players = client.register_table("player", Player) players.on_insert do |player| puts "Joined: #{player.username} (score: #{player.score})" end

client.subscribe( Spacetime::Query.from("player").where_gte("score", 100) )

client.call_reducer("spawn_player", "Alice", 250_u32)

Source