Tooling

Database

Give the game server and its plugins a database with the @mavonengine/sqlite plugin - Kysely queries, plugin migrations and a Database tab in the dashboard.

A game server has no database until the project gets a database plugin. With one, the server and every plugin share a Kysely instance: server.db in the game's code, ctx.db in a plugin. @mavonengine/sqlite is the official one. It uses Node's built-in node:sqlite, so there is nothing native to compile.

npx @mavonengine/cli install @mavonengine/sqlite

The database is created at .mavonengine/data/mavonengine.sqlite in the project when the server starts. Projects made with mavonengine create ignore .mavonengine/ in git; add it to your .gitignore if your project predates that.

Without a database plugin, ctx.db and server.db are undefined and plugins run without one. Nothing database-related is installed until you add a database plugin: kysely is an optional peer dependency of @mavonengine/core.

Options

Set in mavonengine.json under plugins["@mavonengine/sqlite"].options:

OptionDefault
path.mavonengine/data/mavonengine.sqliteThe database file, relative to the project root. :memory: keeps it in memory, gone when the server stops
busyTimeoutMs5000How long a query waits for another connection's lock before it fails

MAVONENGINE_SQLITE_PATH overrides path, for example to keep the database on a volume in production.

The database runs in WAL mode with foreign keys on.

In a plugin

Every server plugin gets the database as ctx.db. Name your tables with ctx.table(), which prefixes them with the package name so plugins' tables don't clash: @mavonengine/chat → chat_mutes, @acme/shop → acme_shop_items. Create them with migrations, which the engine runs before setup():

import { defineServerPlugin } from '@mavonengine/core/Plugins/types'

export default defineServerPlugin({
  migrations: {
    '001_init': {
      async up(db, { table }) {
        await db.schema.createTable(table('scores'))
          .addColumn('player', 'text', c => c.primaryKey())
          .addColumn('score', 'integer', c => c.notNull().defaultTo(0))
          .execute()
      },
    },
  },

  http(app, ctx) {
    app.get('/api/leaderboard', async (_req, res) => {
      if (!ctx.db)
        return res.status(503).json({ error: 'No database' })

      res.json(await ctx.db.selectFrom(ctx.table('scores')).selectAll().orderBy('score', 'desc').limit(10).execute())
    })
  },
})

Migrations

  • They run in the order of their names, once per database. Name them so they sort, like 001_init and 002_add_reason.
  • Never change or rename a migration that has shipped; add a new one instead.
  • Each migration runs in a transaction. One that fails is rolled back and turns the plugin off until the next start.
  • Which ones ran is recorded per plugin in <prefix>__migrations.
  • Without a database plugin they are skipped, and the server warns that the plugin runs without a database.

Good to know

  • No database: ctx.db is undefined when the project has no database plugin. Check for it, and keep working without the database if your plugin can.
  • Shared instance: don't destroy ctx.db, since all plugins share it. The engine closes it when the server stops, after every plugin's destroy() has finished. destroy() may return a promise, so a plugin can still save what it has.
  • Portable SQL: stick to SQL that works across databases, so your plugin also runs on database plugins other than SQLite. SQLite stores booleans as 0/1 and dates as ISO strings, and reads them back as those values.
  • Kysely: add kysely as a dev dependency for its types and for sql.

In the game's server code

The game gets the same database as server.db, once start() has resolved. Name its tables so they don't start with a plugin's prefix, e.g. game_scores:

await server.start()

const top = await server.db?.selectFrom('game_scores').selectAll().orderBy('score', 'desc').limit(10).execute()

server.stop() returns a promise that settles once the database is closed. The server stops itself that way on SIGINT and SIGTERM, see Stopping.

Looking into it

The database holds what players did, so the server only shows it with its admin token; without one it only answers on the server's own machine. Everything below only reads.

  • The Database tab of the dashboard lists the tables with the plugin each belongs to, their rows and the migrations that ran.
  • GET /api/game/database and GET /api/game/database/tables/:name are what it reads, see Built-in endpoints.
  • The database_overview and database_rows tools of the MCP server let an assistant read it, with secret-looking columns redacted.

More than one database plugin

When more than one database plugin is enabled, name the one to use in mavonengine.json (or as database in the server's options):

{
  "database": "@mavonengine/sqlite",
  "plugins": { "@mavonengine/sqlite": {} }
}

Outside a game server

For scripts and tests, openSqlite(path) opens a database the same way:

import { openSqlite } from '@mavonengine/sqlite'

const db = openSqlite('.mavonengine/data/mavonengine.sqlite')
const mutes = await db.selectFrom('chat_mutes').selectAll().execute()
await db.destroy()

Writing a database plugin

A server plugin with a database() hook is a database plugin. It returns the Kysely instance the server and its plugins use:

export default defineServerPlugin({
  database(ctx) {
    return new Kysely({ dialect: myDialect(ctx.options) })
  },
})

It is called once, before any plugin's migrations and setup(), and only for the plugin the project picked. The engine destroys the instance when the server stops.