@sfxcode/mongocamp-server-core
The MongoCamp REST API (openapi/docs.yaml) as a framework-neutral core: createMongocampServer() returns a web standard
fetch(Request): Promise<Response> handler. It replaces the Scala mongocamp-server and is hosted by apps/server (Nitro 3,
standalone) or embedded in a Nuxt application.
import { createMongocampServer } from '@sfxcode/mongocamp-server-core'
const server = await createMongocampServer({
connectionUri: 'mongodb://localhost:27017', // or the CONNECTION_* configurations
config: { CONNECTION_DATABASE: 'mongocamp' }, // any configuration key, the env variables win
})
const response = await server.fetch(new Request('http://localhost/version'))
await server.close()
Configuration
The keys are the env variables of the Scala server (CONNECTION_*, AUTH_*, CORS_*, ...). Persistent keys live in
<AUTH_PREFIX>configuration and can be changed with PATCH /system/configurations/{key}; env and programmatic values win.
| Plugin of the Scala server |
Keys |
| request logging |
REQUESTLOGGING_ENABLED — every request is a document of <AUTH_PREFIX>request_logging |
| micrometer |
METRICS_ENABLED — GET /system/monitoring/{jvm,system,mongodb,events,prometheus}; METRICS_MONGODB_* select the MongoDB metrics, LOGGING_METRICS_MONGODB_* write snapshots into monitoring_* every LOGGING_METRICS_MONGODB_STEP |
The category jvm holds the numbers of the Node runtime (nodejs_*).
Extensions
The dynamic JVM plugins of the Scala server are replaced by three things a host can use.
Events
A typed event bus on hookable, the hook system of Nitro and Nuxt. Listeners never change the answer
of a request; a failing listener is logged.
const server = await createMongocampServer({
on: { 'server:started': () => console.log('ready') }, // listeners that must see the start
})
const remove = server.events.on('document:update', (event) => {
console.log(event.user.userId, event.collectionName, event.updateResponse, event.oldValues)
})
Events: server:started, http:requestStart, http:requestCompleted, config:update, user:*, role:*, database:drop,
collection:*, document:create|update|delete, bucket:*, file:*, job:* (see MongocampEvents in src/events/bus.ts).
Expensive parts (the old values of documents, the user of a request) are only read when a listener is registered;
server.events.observe() sees every event without counting as listener.
Jobs
Jobs are documents in <AUTH_PREFIX>jobs, managed with /system/jobs. Their className names a handler; cron expressions use the
syntax of croner (*/5 * * * * *, @daily, 0 0 12 * * 5L).
await createMongocampServer({
jobs: {
handlers: { 'my.app.ReportJob': async run => sendReport(run.startedAt) },
enabled: true, // false: this instance does not run the jobs on their schedule (several instances of one application)
},
})
Routes
Routes of the host go in front of the core: answer them yourself and give every other request to server.fetch. In Nitro 3 a route
file (routes/api/report.get.ts) or a middleware does that; the core never needs to know about it.
export default {
async fetch(request: Request) {
if (new URL(request.url).pathname === '/report') {
return Response.json(await buildReport())
}
return (await mongocamp()).fetch(request)
},
}
Scripts
pnpm api-types — types of the OpenAPI spec (src/generated/openapi.ts)
pnpm operations — the table of operations for the request events (src/generated/operations.ts)
pnpm typecheck, pnpm test