项目作者: iniva

项目描述 :
Mongoose plugin for HapiJS (v17+)
高级语言: JavaScript
项目地址: git://github.com/iniva/hapi-nosql-mongoose.git
创建时间: 2018-05-05T09:58:34Z
项目社区:https://github.com/iniva/hapi-nosql-mongoose

开源协议:BSD 3-Clause "New" or "Revised" License

下载


:warning: This won’t be updated anymore

hapi-nosql-mongoose

Mongoose plugin for HapiJS (v17+)

Installation

  1. # npm
  2. npm install hapi-nosql-mongoose mongoose
  3. # yarn
  4. yarn add hapi-nosql-mongoose mongoose

Register as Hapi Plugin

  1. const Mongoose = require('hapi-nosql-mongoose');
  2. const schemas = require('./my/mongoose/schemas');
  3. await server.register({
  4. plugin: Mongoose,
  5. options: {
  6. uri: 'mongodb://localhost:27017/database',
  7. config: {...},
  8. schemas: {...}
  9. }
  10. });

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:

  1. -- /my/mongoose/schemas/
  2. |-- index.js
  3. |-- post.js
  4. |-- user.js
  1. // Post schema (post.js)
  2. 'use strict';
  3. const Schema = require('mongoose').Schema;
  4. const Post = new Schema({
  5. title: {
  6. type: String,
  7. trim: true
  8. },
  9. content: String,
  10. authorId: {
  11. type: String // referencing the User as you see fit
  12. },
  13. createdAt: {
  14. type: Date,
  15. 'default': Date.now
  16. }
  17. });
  18. module.exports = Post;
  1. // User schema (user.js)
  2. 'use strict';
  3. const Schema = require('mongoose').Schema;
  4. const User = new Schema({
  5. uuid: {
  6. type: String,
  7. 'default': uuid.v4 // using an uuid library
  8. },
  9. name: {
  10. type: String,
  11. trim: true
  12. },
  13. lastName: {
  14. type: String,
  15. trim: true
  16. },
  17. createdAt: {
  18. type: Date,
  19. 'default': Date.now
  20. }
  21. });
  22. module.exports = User;
  1. // Exporter (index.js)
  2. 'use strict';
  3. const Post = require('./post');
  4. const User = require('./user');
  5. const schemas = {
  6. Post,
  7. User
  8. };
  9. module.exports = 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:

  1. server.route({
  2. method: 'GET',
  3. path: '/posts',
  4. handler: async (request, h) => {
  5. const Post = request.server['mongoose:connector'].getModel('Post');
  6. // More code below
  7. }
  8. });

Plugin Methods Exposed

  • connection: This gives you access to the Mongoose Connection Object.

    1. server.route({
    2. method: 'GET',
    3. path: '/posts',
    4. handler: async (request, h) => {
    5. const MongooseConnection = request.server.plugins['hapi-nosql-mongoose'].connection;
    6. // More code below
    7. }
    8. });
  • mongoose: This gives you access to the Mongoose Object.

    1. server.route({
    2. method: 'GET',
    3. path: '/posts',
    4. handler: async (request, h) => {
    5. const Mongoose = request.server.plugins['hapi-nosql-mongoose'].mongoose;
    6. // More code below
    7. }
    8. });