nuxt-mongo

0.0.6 • Public • Published

MongoDB injection to Nuxt3 application

The module will expose dbClient variable which contains your mongo client connection. It will be available globally without import. Based on Nuxt3 Layers

Usage

Add these variables to your .env file

MONGO_PASS
MONGO_USER
MONGO_HOST
MONGO_PORT
MONGO_DB

Add env variable to nuxt runtimeConfig in nuxt.config.ts

runtimeConfig: {
  env: 'dev' 
}

This env variable need to establish connection for mongo client

prod: `mongodb+srv://${user}:${pass}@${host}/${db}?retryWrites=true&w=majority`,
dev:`mongodb://${host}:${port}/${db}`
Example

In nuxt.config.ts add nuxt-mongo layer

nuxt.config.ts

export default defineNuxtConfig({
  extends: [
    'nuxt-mongo'
  ]
    ...

Create js file for DB CRUD operations

db.js

const createOrUpdate  = async (email, reminders) => {
  const client = await dbClient(); //here we're using our injected client
  return client.collection('users').updateOne(
      { email: email.toLowerCase() },
      { upsert: true }
  );
}

...

export default {
  createOrUpdate
}

Then import this module into your app and use it.

Package Sidebar

Install

npm i nuxt-mongo

Weekly Downloads

2

Version

0.0.6

License

none

Unpacked Size

62.8 kB

Total Files

28

Last publish

Collaborators

  • yurii_poliakov