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:
pnpm add @sfxcode/nuxt-mongocamp-server nitronpm install @sfxcode/nuxt-mongocamp-server nitroyarn add @sfxcode/nuxt-mongocamp-server nitroFor declared collections add a Standard Schema library and mongodb (for ObjectId in your schemas):
pnpm add zod mongodbNitro 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:
docker run -d -p 27017:27017 mongo:7// 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:
# .env
NUXT_MONGOCAMP_SERVER_CONNECTION_URI=mongodb://user:secret@localhost:27017/?authSource=adminOn 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:
// 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:
<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
- Tutorial: a small shop: schemas, validation, behaviours, hooks, indexes, relations, live updates and server code, step by step
- Declared collections: a schema, typed documents, validation and behaviours (timestamps, owner, soft delete, versions, audit)
- Coming from Mongoose: the same tasks side by side
- Module options: every option of
mongocampinnuxt.config.ts - Client composables: search, collections, files, jobs, admin
- Server composables: call the API from your server routes
- Configuration: all server settings
- REST API: every operation, generated from the OpenAPI spec
