# nuxt-mongo

> The module will expose **dbClient** variable which contains your mongo client connection. It will be available globally without import. Based on [Nuxt3 Layers](https://nuxt.com/docs/getting-started/layers)

Latest version **0.0.6** (published 2023-07-09) · 0 weekly downloads

## Install

```sh
npm install nuxt-mongo
pnpm add nuxt-mongo
yarn add nuxt-mongo
bun add nuxt-mongo
```

## Health

**Score 10/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; low quality score; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.6 |
| Published | 2023-07-09 |
| First published | 2023-06-28 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 61.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | yes |
| Maintainers | yurii_poliakov |
| Keywords | nuxt, mongoDB, mongo, nuxt layer |

## Links

- npm: https://www.npmjs.com/package/nuxt-mongo
- npm.io page: https://npm.io/package/nuxt-mongo

## Dependencies (2)

- [dotenv](https://npm.io/package/dotenv.md) ^16.1.4
- [mongodb](https://npm.io/package/mongodb.md) ^5.6.0

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 0.0.6 (latest) — 2023-07-09
- 0.0.5 — 2023-07-09
- 0.0.4 — 2023-07-02
- 0.0.3 — 2023-07-01
- 0.0.2 — 2023-07-01
- 0.0.1 — 2023-06-28

## README

# 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](https://nuxt.com/docs/getting-started/layers)

## Usage

Add these variables to your `.env` file
```dosini
MONGO_PASS
MONGO_USER
MONGO_HOST
MONGO_PORT
MONGO_DB
```

Add env variable to nuxt `runtimeConfig` in `nuxt.config.ts`
```javascript
runtimeConfig: {
  env: 'dev' 
}
```

This `env` variable need to establish connection for mongo client
```javascript
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
```javascript
nuxt.config.ts

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

Create js file for DB CRUD operations
```javascript
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.

---
_Source: https://npm.io/package/nuxt-mongo · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
