Skip to content

Testing, CLI & DevTools ​

Version 2.10 adds tools around the server. None of them changes how an application runs.

Test helpers ​

@sfxcode/mongocamp-server-core/testing starts a server for the tests of your application:

  • It uses a fresh database on a running MongoDB.
  • Users and roles are static (AUTH_USERS, AUTH_ROLES), not stored in mc_users.
  • Your declarations are registered and the seed documents inserted.
ts
import { createMongocampTestServer } from '@sfxcode/mongocamp-server-core/testing'
import orders from '../server/mongocamp/orders'

const server = await createMongocampTestServer({
  collections: [orders],
  users: [{ id: 'ann', roles: ['editor'] }],
  roles: [{ name: 'editor', isAdmin: false, collectionGrants: [{ name: '*', read: true, write: true, administrate: false, grantType: 'COLLECTION' }] }],
  seed: { orders: [{ item: 'book', qty: 1 }] },
})
const ann = await server.as('ann')
const response = await ann.fetch('/mongodb/collections/orders/documents')
const mine = await ann.collection('orders').find()
await server.close() // drops the database
  • Requests: they go to the core in process, so no port is opened.
  • MongoDB: uri sets the server; the default is MONGO_URL, then mongodb://localhost:27017.
  • Administrator: server.admin is a client for the built-in administrator.

Command line: mongocamp ​

The core package has a mongocamp command. It reads the same env variables as the server (CONNECTION_*, NUXT_MONGOCAMP_SERVER_CONNECTION_URI, AUTH_*) and starts the core without HTTP and jobs.

bash
npx mongocamp admin create ops --password secret      # a user with the administrator role
npx mongocamp admin reset-password admin              # a random password, printed once
npx mongocamp introspect orders --out server/mongocamp/orders.ts
npx mongocamp config diff                             # values that are not the default, with their source
npx mongocamp status                                  # database, versions, collections

introspect turns the documents of an existing collection into a declaration with a Zod schema:

  • Objects are loose, so fields the sample did not see are still allowed.
  • Fields that were not in every document are optional.
  • The validation mode is warn.

Check the types before you switch validation to error.

Nuxt DevTools ​

In nuxt dev with the embedded server, the Nuxt DevTools get a MongoCamp tab with four views:

  • Collections: the declared collections with their behaviours, access rules, field rules, references, views, and the result of the validator and index sync.
  • Configuration: every value, where it comes from, and whether it was changed.
  • Jobs: the jobs with their schedule, last run and next run.
  • Events: the events of the server as they happen.

The tab answers requests to localhost only, and production builds do not have it.

A client without Nuxt ​

@sfxcode/mongocamp-client gives apps without Nuxt (Node scripts, Vue or React apps, other frameworks) the generated API of the module plus a small typed layer:

ts
import { createMongocampClient } from '@sfxcode/mongocamp-client'

interface Collections { orders: Order }
const client = createMongocampClient<Collections>({ url: 'https://example.com/api/mongocamp-server' })
await client.login('ann', 'secret')
const open = await client.collection('orders').find({ filter: 'status:open', expand: ['customerId'] })
await client.batch([{ op: 'insert', collection: 'orders', document: { item: 'pen', qty: 1 } }], { atomic: false })
const stop = client.collection('orders').watch({ onEvent: event => console.log(event.event, event.document) })

// 3.0: the auth flows of the server, as in useMongocampAuth()
await client.auth.register({ email: 'ann@example.org', password: 'secret-1' })
await client.auth.loginWithLink({ email: 'ann@example.org', code: '123456' }) // logs the client in
const sessions = await client.auth.sessions()

Since 3.0 its errors are MongocampRequestError (also ResponseError), the same type as in a Nuxt app; isMongocampError(e) narrows.

Documents come decoded with bson, so values are ObjectId and Date. Errors carry status, code and issues. client.apis holds the generated API classes for everything else.

Released under the MIT License.