feat: enhance validation rules and improve content structure across collections

This commit is contained in:
2026-05-17 12:25:28 +00:00
parent 4020ad62c5
commit 819147f518
8 changed files with 184 additions and 26 deletions
@@ -141,6 +141,52 @@ For `GET /:collection/:id`, the Go server sets `_id` automatically from the URL
GET read hooks should **not** set their own `_id` filter for `req.param("id")`. Only add authorization filters (e.g. `{ userId: userId }`).
## Interne DB-Lookups in Hooks (Read & Write)
Innerhalb von goja-Hooks hast du über die `context.db`-API Vollzugriff auf die lokale MongoDB. Dies ist essenziell für komplexe Prüfungen (z. B. "Gehört der angemeldete User wirklich zur ID im Foreign-Key des Objekts?").
**Wichtige Konzepte für DB-Calls in Hooks:**
1. **Keine Automatik-Lookups (`_lookup`) in Hook-Queries:** Der Go-Befehl `context.db.find` liefert nur die flachen Datenbank-Dokumente als Array. Die in der REST-API verfügbare `lookup`-Automatik für Foreign-Keys wird in den internen Backend-Hooks *nicht* angewendet. Du musst die verknüpften Collections ggf. manuell nachladen.
2. **Immer Arrays:** `context.db.find` gibt **immer** ein Array zurück, auch wenn du `limit: 1` setzt.
3. **Rechte ignorierend:** Die `context.db.*`-Methoden umgehen alle `permissions` der YAML-Rollen. Du lädst als System-Benutzer!
**Beispiel: Datensatz validieren / verknüpftes Element prüfen**
```javascript
// hooks/my_action/post.before
(function() {
var userId = context.auth().id;
var submittedRefId = context.data.refId;
// 1. Manuell nachladen
var targetList = context.db.find("target_collection", {
filter: { id: submittedRefId },
limit: 1 // Begrenzen für Performance
});
if (targetList.length === 0) {
throw { status: 404, json: { error: "Ziel nicht gefunden" } };
}
var target = targetList[0];
// 2. Custom Security Check
if (target.ownerId !== userId) {
throw { status: 403, json: { error: "Keine Berechtigung für dieses Ziel" } };
}
// ... Hook fortsetzen
})();
```
**Verfügbare DB-Methoden in `context.db`:**
* `context.db.find(collection, { filter: {}, selector: {}, sort: [], limit: 10 })`
* `context.db.count(collection, { filter: {} })`
* `context.db.create(collection, { field: "value" })`
* `context.db.update(collection, "id_string", { field: "new_value" })` (bzw. mit Mongo-Operatoren `$set`, `$inc`, etc.)
* `context.db.delete(collection, "id_string")`
## Current hook surfaces that matter for website projects
- Collection CRUD hooks under `get`, `post`, `put`, `delete`