# fastify-plugin-mongoose

> Fastify plugin to connect to mongoDB using mongoose

Latest version **0.1.6** (published 2021-02-22) · GPL-3.0 license · 0 weekly downloads

## Install

```sh
npm install fastify-plugin-mongoose
pnpm add fastify-plugin-mongoose
yarn add fastify-plugin-mongoose
bun add fastify-plugin-mongoose
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.6 |
| Published | 2021-02-22 |
| First published | 2021-02-08 |
| Weekly downloads | 0 |
| License | GPL-3.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 47 KB |
| Known vulnerabilities | 0 (+3 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Elliott Ross |
| Maintainers | eross |
| Keywords | fastify, mongoose, mongodb, plugin |

## Links

- npm: https://www.npmjs.com/package/fastify-plugin-mongoose
- Repository: https://github.com/erossdev/fastify-plugin-mongoose
- Homepage: https://github.com/erossdev/fastify-plugin-mongoose#readme
- Issues: https://github.com/erossdev/fastify-plugin-mongoose/issues
- npm.io page: https://npm.io/package/fastify-plugin-mongoose

## Dependencies (2)

- [mongoose](https://npm.io/package/mongoose.md) ^5.11.15
- [fastify-plugin](https://npm.io/package/fastify-plugin.md) ^3.0.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.1.6 (latest) — 2021-02-22
- 0.1.5 — 2021-02-09
- 0.1.4 — 2021-02-08

## README

# fastify-plugin-mongoose

[![NPM](https://nodei.co/npm/fastify-plugin-mongoose.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/fastify-plugin-mongoose/)

## Installation

```bash
npm install fastify-plugin-mongoose
```

## Usage

### Initialization

```javascript
const fastify = require("fastify")();
const fastifyPluginMongoose = require("fastify-plugin-mongoose");
const path = require("path");

fastify.register(fastifyPluginMongoose, {
	uri: "mongodb://localhost:27017/dbName",
	settings: {
		// https://mongoosejs.com/docs/connections.html#options
		// these are default
		useNewUrlParser: true,
		useUnifiedTopology: true,
	},
	modelDirectoryPath: path.resolve("PATH_TO_MODELS_DIRECTORY"),
	useModelAliases: true, // defaults to true
});
```

### Using in routes

This plugin decorates fastify with the connection and models

```javascript
const isDbConnected = fastify.mongoose.connection.readyState === 1;
```

By default, model aliases are enabled. We recommend using aliases.

```javascript
// models/seltzer-item.js
module.exports = {
	alias: "Seltzer",
	schema: {
		name: {
			type: "string",
		},
	},
};

// your routes
fastify.get("/seltzers", async (request, reply) => {
	const dbItems = await fastify.mongoose.models.Seltzer.find();
	reply.send(dbItems);
});

// if you disable model aliases by setting useModelAliases to false then you need to reference models by their file name
// NOTE: This means you cannot have duplicate file names even in different directories!
const itmes = await fastify.mongoose.models["seltzer-item"].find();
```

### Models

Define your models in separate files. If you pass in a modelDirectoryPath, the plugin will traverse all subdirectories for any models

```javascript
// models/seltzer.js
module.exports = {
	alias: "Seltzer",
	schema: {
		name: {
			type: "string",
		},
		slug: {
			type: String, // both 'string' and String work for type
		},
		company: {
			type: 'objectid', // ObjectId is special though so you HAVE to use a string, the plugin fixes the reference
			ref: 'Company',
		}
	},
	class: {
		// https://mongoosejs.com/docs/guide.html#es6-classes
	},
	// https://mongoosejs.com/docs/middleware.html#pre
	pre: {
		save: function (next) {
			if (this.name && this.name.length) {
				this.slug = this.name.toLowerCase());
			}

			next();
		},
	},
	// https://mongoosejs.com/docs/middleware.html#post
	post: {
		save: function (doc) {
			console.log(doc);
		},
	},
	options: {
		timestamps: true,
	}
};
```

## Contributing
Feel free to open a Pull Request or create an issue

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