Skip to content

Getting Started ​

Install ​

Add the module and nitro to your Nuxt 4.7 application. Declaring nitro makes Nuxt build with Nitro 3 (@nuxt/nitro-v3-server), and the module needs that:

bash
pnpm add @sfxcode/nuxt-mongocamp-server nitro
bash
npm install @sfxcode/nuxt-mongocamp-server nitro
bash
yarn add @sfxcode/nuxt-mongocamp-server nitro

For declared collections add a Standard Schema library and mongodb (for ObjectId in your schemas):

bash
pnpm add zod mongodb

Nitro 3 is required

Without nitro in the dependencies the module stops the build with the module needs Nitro 3: add "nitro" to the dependencies of the application.

Option A: embedded server ​

The API runs inside your application. All you need is a MongoDB:

bash
docker run -d -p 27017:27017 mongo:7
ts
// nuxt.config.ts
export default defineNuxtConfig({
  modules: ['@sfxcode/nuxt-mongocamp-server'],
  mongocamp: {
    server: {
      enabled: true,
      // prefix: '/api/mongocamp-server', the clients use it as their url
      config: { CONNECTION_DATABASE: 'mongocamp' },
    },
    refreshToken: true,
  },
})

The connection uses the CONNECTION_* environment variables (default localhost:27017). You can also pass a full connection string with NUXT_MONGOCAMP_SERVER_CONNECTION_URI:

bash
# .env
NUXT_MONGOCAMP_SERVER_CONNECTION_URI=mongodb://user:secret@localhost:27017/?authSource=admin

On the first start the server creates the user admin with a random password and prints the password to the log. Log in with it and change it, or create your own users.

TIP

The server is created with the first request, so the log line appears after you open the app for the first time.

Option B: external server ​

Start the standalone server (see Standalone & Docker) and point the module at it:

ts
// nuxt.config.ts
export default defineNuxtConfig({
  modules: ['@sfxcode/nuxt-mongocamp-server'],
  mongocamp: {
    url: process.env.MONGOCAMP_URL, // e.g. http://localhost:8080
    apiKey: process.env.MONGOCAMP_API_KEY, // optional, server side only
    refreshToken: true,
  },
})

First page ​

Log in, then list the documents of a collection:

vue
<script setup lang="ts">
const { login, logout, isLoggedIn } = useMongocampAuth()
const { documents, page, pending, load, nextPage, previousPage } = usePaginatedFind('books', {
  filter: 'year:[2000 TO *]',
  sort: ['-year'],
  rowsPerPage: 20,
})

async function signIn() {
  await login('admin', 'the-password-from-the-log')
  await load()
}
</script>

<template>
  <button v-if="!isLoggedIn" @click="signIn">Login</button>
  <template v-else>
    <button @click="logout">Logout</button>
    <ul v-if="!pending">
      <li v-for="book in documents" :key="documentId(book)">{{ book.title }}</li>
    </ul>
    <button @click="previousPage">Previous</button> {{ page }} <button @click="nextPage">Next</button>
  </template>
</template>

useMongocampAuth, usePaginatedFind and documentId are auto-imported. Documents arrive as Extended JSON, so read the id with documentId(doc).

Next steps ​

Released under the MIT License.