defineSchema() function

Available since @irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room host

The defineSchema() function declares a room’s Schema: its shared and private state, its entity kinds and its calls. Declare it once, in a module the room and its clients both import.

Syntax

defineSchema(declaration)

Parameters

declaration

An object with any of these properties:

state
The kind of the shared state every connection sees, from entity(). Without one, the room has an empty one.
private
The kind of each player’s private state, which only that player sees. Without one, the room has an empty one.
entities
An array of the entity kinds the room creates.
calls
The room’s calls, from defineCalls(). Without them, the room has none.
input
A kind declaring a stepper’s input fields, so inputs go as binary in that layout rather than as JSON. It’s never created as an entity.

Return value

A Schema.

Exceptions

Error
Thrown for an entity kind declared twice; a state kind with writable or owner-only fields; a private kind with owner-only fields; a hidden state or private kind; an input kind with writable or owner-only fields or a json field with an initial value; and a key on the state, private or input kind.

Examples

A schema shared with the client

// schema.ts
import { call, defineCalls, defineSchema, entity } from "@irtio/room";

export const Table = entity("table", { pot: "u32", dealer: "string" });
export const Hand = entity("hand", { cards: "bytes" });
export const Chip = entity("chip", { owner: "string", x: "f32", y: "f32" }, { writable: ["x", "y"] });

export const schema = defineSchema({
  state: Table,
  private: Hand,
  entities: [Chip],
  calls: defineCalls({ bet: call({ amount: "u32" }).returns<{ pot: number }>() }),
});
// poker.ts
import { defineRoom } from "@irtio/room";
import { schema } from "./schema.js";

export default defineRoom({
  schema,
  calls: {
    bet({ amount }, { room }) {
      room.state.pot += amount;
      return { pot: room.state.pot };
    },
  },
});

See also

Runtime availability

Hosted roomsirt dev@irtio/testingSelf-hosted room host
defineSchema()0.10.10.10.1