# Collection

> The Collection interface of the Room API is a named set of entries, declared in irt.config.ts and kept per project.

The **`Collection`** interface of the [Room API](/docs/room) is a named set of entries that outlives every room, such as the levels players build. Collections are declared under `settings.data.collections` in `irt.config.ts`, and `room.collection(name)` returns one.

An entry has an `id`, a value, flat `meta` to filter on, an optional `score` to sort on, an optional `owner`, a `version`, and `created` and `updated` times in wall-clock milliseconds. Room code may do anything with any declared collection; the collection's client settings limit only clients.

## Instance properties

`Collection.name`
: The collection's name. Read only.

## Instance methods

[`Collection.get()`](/docs/room/collection/get)
: Resolves with an entry and its value, or `undefined`.

[`Collection.put()`](/docs/room/collection/put)
: Creates or replaces an entry.

[`Collection.list()`](/docs/room/collection/list)
: Resolves with a page of entries, without their values.

[`Collection.delete()`](/docs/room/collection/delete)
: Deletes an entry.

[`Collection.refresh()`](/docs/room/collection/refresh)
: Drops what the room holds of the collection's entries.

## Description

- **Values** are JSON or a `Uint8Array`, at most 64 KB. `meta` is flat strings, numbers and booleans, at most 1 KB. Entry IDs are 1 to 64 of `A-Z a-z 0-9 _ . : -`. A collection holds at most 1,000,000 entries.
- **The cache.** A `get()` within 5 seconds of the room's last read or write of the same entry comes from memory. `list()` is never cached.
- **Errors.** Calls reject with a [`DataError`](/docs/room/data-error); `E_UNKNOWN` means the collection isn't declared in the live build, or is the wrong kind for the call.
- **Limits.** A room makes at most 200 data calls a second and 64 at once.

## Examples

### Publishing a level a player built

```ts
// workshop.ts
import { defineRoom } from "@irtio/room";

export default defineRoom({
  async onMessage(room, connection, message) {
    const m = message as { type?: string; tiles?: number[] };
    if (m.type !== "publish" || !Array.isArray(m.tiles)) return;
    const { id } = await room.collection("levels").put({ value: { tiles: m.tiles }, meta: { size: m.tiles.length }, owner: connection.player });
    room.send(connection, { type: "published", id });
  },
});
```

## See also

- [Player data, collections and leaderboards](/docs/room/guides/player-data), the guide
- [`Leaderboard`](/docs/room/leaderboard)
