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

hapi-mongoose2

v4.1.0

Published

mongoose plugin for hapi-based servers

Downloads

11

Readme

hapi-mongoose2

npm version Greenkeeper badge CircleCI

Mongoose plugin for hapi-based servers. Supports connecting to one or multiple databases and look up and register models by connection. The plugin options are:

  • options - a connection or an array of connections where:
    • connection - an object containing:
      • uri - a mongo uri string
      • alias - (optional) a database name alias used to namespace connections when multiple are created. otherwise ignored.
      • loadSchemasFrom - (optional) one of:
        • An array of globs from where schemas will be loaded. matching files must export a mongoose.Schema object or a function with the signature async function(server) returning a schema.
        • An object containing Mongoose.Schema elements.
      • options - (optional) options passed to mongoose createConnection method. unknown properties are allowed:
        • auth - an object with auth credentials
          • user
          • password
        • autoIndex
        • bufferCommands
    • connections - an array of connection objects as described above.
    • decorations - (optional) an array of interfaces to be decorated using server.decorate method. allowed values are server, request.

Connection and models are accessible under the server.app.mongo property. When multiple connections are created the database name or alias is used as namespace for accessing each database properties. Same applies for decorated interfaces.

Models are named as the filename matching the schema pattern. Model name first letter is capitalized by default. e.g. Animal.

Example

const plugin = {
    plugin: require('hapi-mongoose2'),
    options: {
        connections: [
            {
                uri: 'mongodb://localhost:27017/myapp'
            },
            {
                alias: 'safebox',
                uri: 'mongodb://localhost:27017/secrets',
                loadSchemasFrom: [
                    'src/schemas',
                    '!.{md,json}'
                ],
                options: {
                    auth: {
                        user: 'admin',
                        password: 'pa55w0rd'
                    },
                    autoIndex: false,
                    bufferCommands: true
                }
            }
        ],
        decorations: ['request', 'server']
    }
};

const server = new Hapi.server();
await server.register(plugin);

// Using database `secrets` from:
// 1 - `server.app` object
// 2 - `request` decorated object
// 3 - `server` decorated object

const { Admin } = server.app.mongo.safebox.models;
await Admin.create({ name: 'Quentin', last: 'Tarantino' });

server.route({
    method: 'GET',
    path: '/',
    handler: function (request) {
        const { Admin } = request.mongo.safebox.models;
        return Admin.findOne({ name: 'Quentin' }).exec();
    }
});

await server.mongo.safebox.connection.close();

Dependencies

mongoose needs to be installed alongside this plugin in order to work.