Skip to content

useMongocampDeclaration ​

The declaration of a collection (server 2.3, GET …/declaration): validation mode, behaviours, system and protected fields. See Declared collections.

ts
const { declaration, status, load, invalidate } = useMongocampDeclaration(() => props.collectionName)
await load()
if (declaration.value?.softDelete) { /* … */ }
ReturnDescription
declarationComputedRef<Declaration | null> — null for an undeclared collection (the server's 404), while loading and after a failure: treat "unknown" like "not declared"
status'idle' | 'loading' | 'ready' | 'failed' (a 404 is ready)
load(force?)Asks once per collection and signed-in user; callers asking together share one request. Never throws; 15 s timeout
invalidate(collection?)Forgets one collection so the next load() asks again

The cache is a shared useState per signed-in user: another user starts empty, and an answer that arrives after the user changed is dropped. Works in proxy mode.

Declaration (utils/declaration.ts, normalizeDeclaration) holds name, database, validation, systemFields, protectedFields and, when on, timestamps (createdAt, updatedAt, createdBy?, updatedBy?), owner (field, read: own / all / custom), softDelete (field, purgeAfterDays?), versioned (mode, field, required) and audit (retentionDays). The server sends them resolved; anything garbled turns that behaviour off. Helpers in the same file: behavioursOf, isSystemField, isProtectedField, isServerOwnedField, versionOf, deletedMarkOf, preservedOnInsert (server 2.7: the fields an administrator's insert keeps; timestamps.preserveOnInsert/preserveOnUpdate and owner.preserveOnInsert are kept on the declaration only when true).

Server 2.4 adds indexes (DeclaredIndex: name, keys in index order with direction 1 / -1 / text, unique) and refs (DeclaredRef: field, collection as the routes name it, database for another database). Helpers: isManagedIndexName (mongocamp_idx_), declaredIndexNamed, refOf, MAX_EXPAND (5).

Server 2.6 adds to each index sparse, origin ('declaration', or the behaviour that asked for it, e.g. 'owner'), partialFilterExpression / partialFilterFrom ('softDelete' for the unique index of a soft-delete collection) and expireAfterSeconds; to each reference many (false unless the declaration says "array"); and referencedBy (ReferencingField: collection, database?, field, many) — the declared references of other collections that point here.

Server 2.10/2.11 add (each absent when the declaration does not have it; read defensively like the rest):

  • search (SearchSpec: fields field → weight, language) — full-text search, reads take q; its managed text index has origin: 'search'.
  • tenant (TenantSpec: field, adminBypass) — the field is a system field; its index has origin: 'tenant'.
  • access (AccessSpec: rules.read|create|update|delete as AccessRuleSummary { kind: 'all'|'none'|'roles'|'custom', roles? }, adminBypass) — the server sends delete (the generated model says _delete, also accepted); a missing rule is all, an unreadable one custom. Field rules are not answered.
  • views (DeclaredView[]: name, hasParams, description?) — always an array.
  • drafts (DraftsSpec: readers) — the five draft fields (DRAFT_FIELDS) come as system fields.
  • fileRefs (DeclaredFileRef[]: field, bucket, collection = <bucket>.files, database?, many) — file references, which the server lists among refs with a bucket key; they are left out of refs, so the reference picker, the expand menu and the lookups never treat a file id as a document id. Helper fileRefOf(declaration, field).

behavioursOf also flags search, tenant, drafts and access. Drafts helpers: draftStateOf(row, declaration) → 'draft' | 'published' | 'changed' | undefined (changed only when the row carries _draft) and withoutDraftInternals(row).

Released under the MIT License.