npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hicsail-hapi-mongo-models

v6.0.0

Published

A hapi plugin for mongo-models

Downloads

1

Readme

hapi-mongo-models

A hapi plugin for mongo-models.

Build Status Dependency Status devDependency Status peerDependency Status

Install

$ npm install hapi-mongo-models

Server plugin

During plugin registration we connect to MongoDB using the supplied options.

Register manually

const HapiMongoModels = require('hapi-mongo-models');

const plugin = {
    register: HapiMongoModels,
    options: {
        mongodb: {
          uri: 'mongodb://localhost:27017/hapi-mongo-models-test',
          options: {}
        },
        autoIndex: false,
        models: {
            Customer: './path/to/customer',
            Order: './path/to/order'
        }
    }
};

server.register(plugin, (err) => {

     if (err) {
         console.log('Failed loading plugin');
     }
 });

Register via manifest

{
    "connections": [{
        "port": 8080
    }],
    "registrations": [{
        "plugin": {
            "register": "hapi-mongo-models",
            "options": {
                "mongodb": {
                    "uri": "mongodb://localhost:27017/hapi-mongo-models-test",
                    "options": {},
                },
                "autoIndex": false,
                "models": {
                    "Customer": "./path/to/customer",
                    "Order": "./path/to/order"
                }
            }
        }
    }]
}

Plugin options

The options passed to the plugin is an object where:

  • mongodb - is an object where:
    • uri - a string representing the connection uri for MongoDB.
    • options - an optional object passed to MongoDB's native connect function.
  • autoIndex - a boolean specifying if the plugin should call createIndexes for each model that has a static indexes property. Defaults to true. Typically set to false in production environments.
  • models - an object where each key is the exposed model name and each value is the path (relative to the current working directory or absolute) of where to find the model on disk.

Usage in other plugins

You can depend on hapi-mongo-models inside other plugins. This allows you to access models that were defined in the plugin config and add models dynamically.

For example, in a plugin you author:

const DynamoKitty = require('./models/dynamo-kitty');

exports.register = function (server, options, next) {

    const addModel = server.plugins['hapi-mongo-models'].addModel;
    addModel('DynamoKitty', DynamoKitty);
    next();
};

exports.register.attributes = {
    name: 'dynamo',
    version: '1.0.0',
    dependencies: ['hapi-mongo-models']
};

The addModel method is a function with the signature function (key, model) where:

  • key - is a string representing the name that will be exported.
  • model - is a model class created by using BaseModel.extend(...).

Example

Example usage in a route handler:

// customer plugin

exports.register = function (server, options, next) {

    server.route({
        method: 'GET',
        path: '/customers',
        config: {
            validate: {
                query: {
                    name: Joi.string().allow('')
                }
            }
        },
        handler: function (request, reply) {

            const Customer = request.server.plugins['hapi-mongo-models'].Customer;
            const filter = {};

            if (request.query.name) {
                filter.name = request.query.name;
            }

            Customer.find(filter, (err, results) => {

                if (err) {
                    return reply(err);
                }

                reply(results);
            });
        }
    });

    next();
};

exports.register.attributes = {
    name: 'customers'
};

In the wild

To see hapi-mongo-models in action, checkout the Frame project's models.

Have a question?

Any issues or questions (no matter how basic), open an issue. Please take the initiative to read relevant documentation and be pro-active with debugging.

Want to contribute?

Contributions are welcome. If you're changing something non-trivial, you may want to submit an issue before creating a large pull request.

License

MIT

Don't forget

What you create with hapi-mongo-models is more important than hapi-mongo-models.