@sfxcode/mongocamp-server-core 0.0.0-stage → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +86 -2
- package/dist/index.d.mts +1546 -0
- package/dist/index.mjs +14964 -0
- package/package.json +43 -3
package/README.md
CHANGED
|
@@ -1,3 +1,87 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @sfxcode/mongocamp-server-core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The MongoCamp REST API (`openapi/docs.yaml`) as a framework-neutral core: `createMongocampServer()` returns a web standard
|
|
4
|
+
`fetch(Request): Promise<Response>` handler. It replaces the Scala `mongocamp-server` and is hosted by `apps/server` (Nitro 3,
|
|
5
|
+
standalone) or embedded in a Nuxt application.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { createMongocampServer } from '@sfxcode/mongocamp-server-core'
|
|
9
|
+
|
|
10
|
+
const server = await createMongocampServer({
|
|
11
|
+
connectionUri: 'mongodb://localhost:27017', // or the CONNECTION_* configurations
|
|
12
|
+
config: { CONNECTION_DATABASE: 'mongocamp' }, // any configuration key, the env variables win
|
|
13
|
+
})
|
|
14
|
+
const response = await server.fetch(new Request('http://localhost/version'))
|
|
15
|
+
await server.close()
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Configuration
|
|
19
|
+
|
|
20
|
+
The keys are the env variables of the Scala server (`CONNECTION_*`, `AUTH_*`, `CORS_*`, ...). Persistent keys live in
|
|
21
|
+
`<AUTH_PREFIX>configuration` and can be changed with `PATCH /system/configurations/{key}`; env and programmatic values win.
|
|
22
|
+
|
|
23
|
+
| Plugin of the Scala server | Keys |
|
|
24
|
+
| --- | --- |
|
|
25
|
+
| request logging | `REQUESTLOGGING_ENABLED` — every request is a document of `<AUTH_PREFIX>request_logging` |
|
|
26
|
+
| 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` |
|
|
27
|
+
|
|
28
|
+
The category `jvm` holds the numbers of the Node runtime (`nodejs_*`).
|
|
29
|
+
|
|
30
|
+
## Extensions
|
|
31
|
+
|
|
32
|
+
The dynamic JVM plugins of the Scala server are replaced by three things a host can use.
|
|
33
|
+
|
|
34
|
+
### Events
|
|
35
|
+
|
|
36
|
+
A typed event bus on [hookable](https://github.com/unjs/hookable), the hook system of Nitro and Nuxt. Listeners never change the answer
|
|
37
|
+
of a request; a failing listener is logged.
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
const server = await createMongocampServer({
|
|
41
|
+
on: { 'server:started': () => console.log('ready') }, // listeners that must see the start
|
|
42
|
+
})
|
|
43
|
+
const remove = server.events.on('document:update', (event) => {
|
|
44
|
+
console.log(event.user.userId, event.collectionName, event.updateResponse, event.oldValues)
|
|
45
|
+
})
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Events: `server:started`, `http:requestStart`, `http:requestCompleted`, `config:update`, `user:*`, `role:*`, `database:drop`,
|
|
49
|
+
`collection:*`, `document:create|update|delete`, `bucket:*`, `file:*`, `job:*` (see `MongocampEvents` in `src/events/bus.ts`).
|
|
50
|
+
Expensive parts (the old values of documents, the user of a request) are only read when a listener is registered;
|
|
51
|
+
`server.events.observe()` sees every event without counting as listener.
|
|
52
|
+
|
|
53
|
+
### Jobs
|
|
54
|
+
|
|
55
|
+
Jobs are documents in `<AUTH_PREFIX>jobs`, managed with `/system/jobs`. Their `className` names a handler; cron expressions use the
|
|
56
|
+
syntax of [croner](https://croner.56k.guru) (`*/5 * * * * *`, `@daily`, `0 0 12 * * 5L`).
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
await createMongocampServer({
|
|
60
|
+
jobs: {
|
|
61
|
+
handlers: { 'my.app.ReportJob': async run => sendReport(run.startedAt) },
|
|
62
|
+
enabled: true, // false: this instance does not run the jobs on their schedule (several instances of one application)
|
|
63
|
+
},
|
|
64
|
+
})
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Routes
|
|
68
|
+
|
|
69
|
+
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
|
|
70
|
+
file (`routes/api/report.get.ts`) or a middleware does that; the core never needs to know about it.
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
export default {
|
|
74
|
+
async fetch(request: Request) {
|
|
75
|
+
if (new URL(request.url).pathname === '/report') {
|
|
76
|
+
return Response.json(await buildReport())
|
|
77
|
+
}
|
|
78
|
+
return (await mongocamp()).fetch(request)
|
|
79
|
+
},
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Scripts
|
|
84
|
+
|
|
85
|
+
- `pnpm api-types` — types of the OpenAPI spec (`src/generated/openapi.ts`)
|
|
86
|
+
- `pnpm operations` — the table of operations for the request events (`src/generated/operations.ts`)
|
|
87
|
+
- `pnpm typecheck`, `pnpm test`
|