Skip to content

useMongocampAdmin ​

Wraps adminApi and collectionApi (from useMongocampApi()) for user and role management.

ts
const {
  listUsers,          // (filter?: string) => Promise<UserProfile[]>
  addUser,            // (userId, password, apiKey?, roles?, { email?, displayName? }?) => Promise<UserProfile> — an empty apiKey / field is left out
  updateUser,         // (userId, { email?, emailVerified?, displayName?, disabled? }) => Promise<UserProfile>  — server 3.0 PATCH, null removes a field
  setUserDisabled,    // (userId, disabled) => Promise<UserProfile>  — server 3.0
  inviteUser,         // (email, roles?) => Promise<void>  — server 3.0 POST /auth/invite (AUTH_REGISTRATION=invite)
  deleteUser,         // (userId) => Promise<JsonValueBoolean>  — a user that is already gone (404) counts as deleted
  updateUserRoles,    // (userId, roles) => Promise<UserProfile>
  updateUserPassword, // (userId, password) => Promise<void>
  listRoles,          // (filter?: string) => Promise<Role[]>
  addRole,            // (name, isAdmin?, collectionGrants?) => Promise<Role>
  updateRole,         // (roleName, isAdmin, collectionGrants?) => Promise<Role>
  deleteRole,         // (roleName) => Promise<JsonValueBoolean>  — likewise for a role
  listCollections,    // () => Promise<string[]>
  clearCollection,    // (collectionName) => Promise<JsonValueBoolean>  — deletes every document
  deleteCollection,   // (collectionName) => Promise<JsonValueBoolean>  — drops the collection
  getJsonSchema,      // (collectionName) => Promise<JsonSchema>
  getSchemaAnalysis,  // (collectionName, documents, depth = 3) => Promise<SchemaAnalysis>  — the server's field analysis
  getSuggestedValidator, // (collectionName, sampleSize?) => Promise<SuggestedValidator>  — server 2.3, { validator: { $jsonSchema }, count, sample, warnings }
  getUser,            // (userId) => Promise<UserProfile>
  getRole,            // (roleName) => Promise<Role>
  regenerateUserApiKey, // (userId) => Promise<string>  — the new key, plain
} = useMongocampAdmin()

Clearing and dropping collections ​

A collection that no longer exists (404) has nothing to clear and is already dropped, so clearCollection and deleteCollection resolve ({ value: true }) for it.

clearCollection and deleteCollection are irreversible and have no guard of their own. Put them behind a type-the-name confirmation (MongocampConfirmByName) and keep MongoCamp's mc_* collections and GridFS .files/.chunks members out of reach (they belong to the bucket actions — see useMongocampFiles); MongocampCollections does both. The three collection methods are bounded by a 15 s timeout, so a hung server rejects instead of hanging, and a refused call (403, ...) is rethrown unchanged.

getSchemaAnalysis ​

The server's own field analysis: for every field the number of documents that have it and the split by value type, nested to depth levels (default 3). documents is how many documents to analyse — pass the collection's count to analyse every one; the value sent is always at least 1 and at most 1 000 000, and asking for more than the collection holds simply analyses everything (a negative size would be a 400). The server reads about 86 000 documents per second (100 000 in 1.2 s on a local server), so the request is not capped at 15 s like the others: the timeout is 15 s plus 0.05 ms per document, at most 2 minutes (a request that outlasts it rejects with a TimeoutError). Counts are exact for nested objects, arrays of objects, mixed types, nulls and sparse fields (checked against generated data). statisticsFromSchemaAnalysis (in utils/schemaAnalysis.ts) turns the answer into the rows of the field-statistics table. Used by the Analyze all documents button of MongocampCollectionInfos.

getUser, getRole ​

Single-entry reads (15 s timeout). They return the same object the list call already carries for that entry, so MongocampUsers and MongocampRoles render their detail drawers from the list row instead of calling them; they are here for consumers that only know an id.

updateUser, setUserDisabled (server 3.0) ​

PATCH /admin/users/{id} with only the fields to change (15 s timeout): displayName, email, emailVerified, disabled; null removes a text field. An address another user already has is refused with 409, code 4093 (describeServerError says so). A disabled user's login is refused with 401, code 4013 ("account disabled"), and disabling revokes the user's tokens and sessions at once — a request with an earlier token answers 401. setUserDisabled(userId, disabled) is the shortcut for the Disable / Enable action of MongocampUsers. The server has no route for a user to change their own fields.

regenerateUserApiKey ​

Generates a new API key for a user and returns it as a plain string. The old key stops working immediately, and the new one is only available in this return value — show it once and discard it; don't store or log it. It calls adminApi.generateNewApiKeyForUser (server 2.6). Regenerating the signed-in user's key through this path is not what MongocampUsers offers — that is useMongocampAccount().regenerateApiKey, because the session profile carries the key.

updateRole replaces the whole grants array ​

There is no PATCH endpoint for roles — updateRole(roleName, isAdmin, collectionGrants) replaces isAdmin and the entire collectionGrants array. When building an edit form:

  • Always shallow-clone grants before binding them to the form: role.collectionGrants.map(g => ({ ...g })).
  • If your edit form only exposes isAdmin (no grant UI), still populate the grants from the role and pass them back on save — otherwise grants are silently wiped.

See MongocampRoleGrants for the full pattern.

Released under the MIT License.