Database

Database & Persistence

Colyseus is agnostic to the database you use. You can use your preferred database and Node.js module for working with databases.

⚠️

Having database-first features such as player identity, leaderboards, configurations, etc. is in our Roadmap. Currently, we don’t have an official database recommendation. Join the discussion.


ORMs (Object-Relational Mappers)

ORMs are known for abstracting the database layer and providing a more object-oriented approach.

Query builders

Query builders are known for their simplicity and flexibility.


Usage patterns with Colyseus

Query the database in onAuth()

You may fetch user data based on the options passed to the client on connection, or based on the authentication token.

src/rooms/MyRoom.ts
// ...
async onAuth(client, options) {
    const user = await db.query('SELECT * FROM users WHERE id = ?', [options.userId]);
    return user;
}
 
onJoin (client, options) {
    // ...
    await db.query('UPDATE users SET online = true WHERE id = ?', [client.auth.id]);
}

Update the database in onLeave()

You may update the database when the client leaves the room.

src/rooms/MyRoom.ts
// ...
async onLeave(client, consented) {
    // ...
    await db.query('UPDATE users SET online = false WHERE id = ?', [client.auth.id]);
}