RoomDefinition: migrations property
@irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room hostThe migrations property of a room definition holds the up migrations of the room’s room state, keyed by the version each one produces.
Value
An object whose keys are versions and whose values are Migration functions. A migration takes room state as plain RoomData at the version before its key and returns it at its key.
type Migration = (data: RoomData) => RoomData;
interface RoomData {
version: number;
tick: number;
savedAt: number; // wall-clock ms when it was written
nextId: number;
state: Record<string, unknown>;
private: Record<string, Record<string, unknown>>; // by player ID
privateSeen: Record<string, number>; // when each player was last connected
entities: { id: number; kind: string; values: Record<string, unknown>; owner?: string }[]; // owner absent when the room owns it
stepper: unknown; // what the stepper's save returned
bans?: RoomBan[]; // from room.ban
mutes?: RoomMute[]; // from room.muteVoice
bots?: { player: string; behaviour: string; name: string; status: unknown; data: Record<string, any> }[]; // from room.bots
fill?: { players: number; behaviour: string }; // from room.bots.fill
alarm?: number; // from room.setAlarm
}
Description
On wake, room state written at an older version runs through each migration in turn, up to the definition’s version. The room sets the version itself after each step, so a migration needn’t.
A migration can’t drop or change the room’s bans, voice mutes or alarm: the room keeps them whatever the migration returns. It can change the room’s bots and their fill level.
A missing migration, a kind or field the build doesn’t declare, or a value of the wrong type stops the room with a reason that says which. An error thrown in a migration stops the room too. Fields the build declares and the data lacks get their defaults.
Examples
Renaming a field
// timer.ts
import { defineRoom, defineSchema, entity } from "@irtio/room";
// Version 1 had { roundSeconds: "u32" }; version 2 keeps milliseconds.
const Round = entity("round", { roundMs: "u32" });
export default defineRoom({
schema: defineSchema({ state: Round }),
version: 2,
migrations: {
2: (data) => {
const { roundSeconds, ...state } = data.state;
return { ...data, state: { ...state, roundMs: Number(roundSeconds ?? 0) * 1000 } };
},
},
});
See also
Runtime availability
| Hosted rooms | irt dev | @irtio/testing | Self-hosted room host | |
|---|---|---|---|---|
migrations | 0.1 | 0.1 | 0.1 | 0.1 |