hapi-nosql-mongoose
Mongoose plugin for HapiJS (v17+)
Installation
# npm npm install hapi-nosql-mongoose mongoose # yarn yarn add hapi-nosql-mongoose mongoose
Register as Hapi Plugin
const Mongoose = ;const schemas = ; await server;
Options
- uri: a mongodb valid uri
- config: a javascript object with mongoose options
- schemas: a javascript object with mongoose schema definitions
Schema Definitions
For ease of use you can have a folder with all your schema definitions along an index.js
file that exports all the schemas inside the folder. e.g:
-- /my/mongoose/schemas/ |-- index.js |-- post.js |-- user.js
// Post schema (post.js)'use strict'; const Schema = Schema; const Post = title: type: String trim: true content: String authorId: type: String // referencing the User as you see fit createdAt: type: Date 'default': Datenow ; moduleexports = Post;
// User schema (user.js)'use strict'; const Schema = Schema; const User = uuid: type: String 'default': uuidv4 // using an uuid library name: type: String trim: true lastName: type: String trim: true createdAt: type: Date 'default': Datenow ; moduleexports = User;
// Exporter (index.js)'use strict'; const Post = ;const User = ; const schemas = Post User; moduleexports = schemas
Server Decorations
This plugin decorates the server object, adding a method called mongoose:connector
that returns the full Connector object.
Use the Connector object to get your models in your controllers like this:
server;
Plugin Methods Exposed
- connection: This gives you access to the Mongoose Connection Object.server;
- mongoose: This gives you access to the Mongoose Object.server;