MongoDB Nuxt (3) Module to connect Nuxt Front-end to a MongoDB Database using Nitro server engine and global defined Nuxt plugins for a fast, easy and extensive integration.
- Easy integration by adding full Connection String or add credentials / params
- Auto-generating URI / connection string
- Default server route implemented for communication to Nitro server where requests get handled towards MongoDB
- Request handling by Nitro server to keep credentials SSR and safe
- Out of the box Nuxt plugins added for easy implementation within clientside and execute CRUD operation
- Nuxt 3 support
In order to use this module, please provide the following steps.
Using yarn:
$ yarn add nuxt-mongodb
Using npm:
$ npm i nuxt-mongodb
add nuxt-mongodb to your Nuxt Options:
// nuxt.config.ts
export default defineNuxtConfig({
...
modules: ['nuxt-mongodb'],
...
})Either add the credentials either by adding values to process.env or nuxt options. Choose between adding uri or params.
# URI
MONGODB_URI=""- or -
# PARAMS
MONGODB_USERNAME=""
MONGODB_PASSWORD=""
MONGODB_HOST=""
MONGODB_DATABASE=""
MONGODB_OPTIONS=""
MONGODB_CLUSTER_URL=""// nuxt.config.ts
export default defineNuxtConfig({
...
mongoDb: {
uri: '',
}
...
})// nuxt.config.ts
export default defineNuxtConfig({
...
mongoDb: {
username: '',
password: '',
host: '',
database: '',
options: '',
clusterUrl: '',
}
...
})Please visit the official MongoDB docs for a complete overview of options
In order to use plugin, the global $mongo variable is exposed. This variable hooks into your database and can operate on it's given collection by making direct usage of Mongo DB's collection operations.
<script setup>
const { $mongo } = useNuxtApp()
</script>$mongo.[operation](arg1: Payload, arg2: Collection, arg3: Database)<script setup>
const { $mongo } = useNuxtApp()
const { data } = await $mongo.find({ name: 'Riviera Caterer' }, 'restaurants')
</script>Please visit the official MongoDB docs for a complete overview of operations.
- Run
npm run dev:prepareto generate type stubs. - Use
npm run devto start playground in development mode.