Migrations & Seeds
Since 2.11 the server runs migrations — data changes that run once per database: rename a field, backfill a default, split a collection — and upserts seeds: documents a database should have for development, tests and demos. Declarations already sync validators and indexes; migrations cover what they cannot.
Migrations
A Nuxt application keeps them in server/mongocamp/migrations/; the file name is the id, and the ids decide the order:
// server/mongocamp/migrations/2026-12-10-split-names.ts
export default defineMongocampMigration({
description: 'split name into firstName and lastName',
async up({ db }) {
for await (const person of db.collection('people').find({ name: { $exists: true } })) {
const [firstName, ...rest] = String(person.name).split(' ')
await db.collection('people').updateOne({ _id: person._id }, { $set: { firstName, lastName: rest.join(' ') }, $unset: { name: '' } })
}
},
async down({ db }) {
await db.collection('people').updateMany({}, [{ $set: { name: { $concat: ['$firstName', ' ', '$lastName'] } } }, { $unset: ['firstName', 'lastName'] }])
},
})The function gets:
| Field | Meaning |
|---|---|
db | The default database (driver: no grants, no behaviours, no events) |
collection(name) | A server collection as the user system: validation, behaviours, audit and events apply |
session | The session of the transaction with transaction: true (replica set): pass it to driver calls, a throw rolls everything back |
context, log | The core and its log |
A standalone host registers them with server.use(migrations([...])) (each with an id).
When they run
At the start, after the declarations are synced and before the first request: the Nitro plugin of the module calls them on mongocamp:ready; the scheduled jobs of the instance wait. MIGRATIONS_ON_START decides:
| Value | Start |
|---|---|
run (default) | applies the pending migrations; a failed one is recorded and stops the start |
check | refuses to start while migrations are pending (run them with the CLI first) |
off | only registers them (status route, DevTools) |
Several instances: the instance that takes the lock migrations (a lease in mc_locks, renewed by a heartbeat) applies them, the others wait and find nothing left. A lock of an instance that died runs out after its lease.
Each run is recorded in mc_migrations (_id = id, checksum, start, end, duration, error). A migration whose source changed after it was applied is reported as changed and not run again — write a new one. down runs only by an explicit command.
Status and commands
mongocamp migrate status # applied, pending, failed, changed
mongocamp migrate up [--to <id>] # waits for an instance that runs them
mongocamp migrate down <id> # with down()The commands load server/mongocamp/migrations (--dir; .mjs, .js, or .ts with the type stripping of Node) and register the declarations of server/mongocamp (--collections) first. Administrators also have GET /system/migrations and POST /system/migrations/run (useMongocampApiAdmin().listMigrations()), and the MongoCamp tab of the DevTools lists them.
Seeds
// server/mongocamp/seeds/customers.ts
import customers from '../customers'
export default defineMongocampSeed({
collection: customers, // typed documents (or a name)
key: ['email'],
documents: [{ email: 'ada@example.com', name: 'Ada' }],
})A seed upserts its documents by the key fields through the API rules as system: a new document is inserted (timestamps, versions, audit apply, validation too — an invalid seed fails with its issues), a stored one gets the fields that differ, an unchanged one is left alone, so a second run changes nothing. documents may be a function (async).
They run at the start when SEED_ON_START is true — the module sets it in development (nuxt dev) and leaves it off in production builds — with mongocamp seed, and in tests with createMongocampTestServer({ seeds }).
