0.0.6 • Published 10 months ago

nuxt-mongo v0.0.6

Weekly downloads
-
License
-
Repository
-
Last release
10 months ago

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.

0.0.6

10 months ago

0.0.5

10 months ago

0.0.4

10 months ago

0.0.3

11 months ago

0.0.2

11 months ago

0.0.1

11 months ago