# defineSchema() function

> The defineSchema() function declares a room's schema, its state, entity kinds and calls, once for the room and its clients.

The **`defineSchema()`** function declares a room's [`Schema`](/docs/room/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

```ts
defineSchema(declaration)
```

### Parameters

`declaration`
: An object with any of these properties:

  `state`
  : The kind of the shared state every connection sees, from [`entity()`](/docs/room/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()`](/docs/room/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`](/docs/room/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

```ts
// 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 }>() }),
});
```

```ts
// 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

- [`Schema`](/docs/room/schema)
- [State sync and interest](/docs/room/guides/state-sync)
