Collection: get() method

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

The get() method of the Collection interface resolves with an entry and its value, or undefined when the collection has no entry with that ID.

Syntax

get(id)
get<T>(id)

Parameters

id
The entry’s ID.

Return value

A Promise that resolves with the entry, or undefined. The entry is an object with these properties:

id
The entry’s ID.
value
Its value, JSON or a Uint8Array. The type parameter T types it, without checking it.
meta
Its flat meta, {} when it has none.
score
Its score, or null.
owner
The player who owns it, or null.
version
Its version, which goes up on every write.
created and updated
When it was created and last written, in wall-clock milliseconds.

Exceptions

The promise rejects with a DataError, such as E_UNKNOWN when the collection isn’t declared.

Description

A get() within 5 seconds of the room’s last read or write of the same entry is answered from memory, except that the first get() after the room’s own put() of it reads it again, since the data service sets its times.

Examples

Loading the level a room was created for

// play-level.ts
import { defineRoom, defineSchema, entity, json } from "@irtio/room";

const Level = entity("level", { tiles: json<number[]>([]) });

export default defineRoom({
  schema: defineSchema({ state: Level }),
  async onCreate(room) {
    const id = (room.meta as { level?: string } | null)?.level;
    const level = id ? await room.collection("levels").get<{ tiles: number[] }>(id) : undefined;
    room.state.tiles = level?.value.tiles ?? [];
  },
});

See also

Runtime availability

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