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

ifnode-mongoose

v1.2.0

Published

Mongoose plugin for ifnode

Downloads

2

Readme

ifnode-mongoose

Description

ifnode-mongoose is a schema plugin which is specified for ifnode and provide possibility to using mongoose in ifnode eco-system. Plugin does not around developer under some special features of mongoose and it more like proxy.

Each ifnode model (returned by app.Model) is a abstraction under Mongoose.Schema and Mongoose.Model. ifnode-mongoose get possibility to reuse any mongoose plugins, validation and all other features.

Usage

Install module:

npm install ifnode-mongoose --save

API

ifnode database connection config options

Name | Type | Description ---- | ---- | -----------

  • | string | Mongo connection uri. Read more on mongoose site config | Object: { uri, options } | Mongoose connect params. Read more on mongoose site (uri - first argument of mongoose.connect(uri, options), options - second argument of mongoose.connect(uri, options));
  • | function | Adds possibility to create own connection. Useful for multiplied mongo connections

Database connection examples

By string
module.exports = {
    db: {
        my_mongo_database: {
            schema: 'mongoose',
            config: 'mongodb://root:123@localhost:27017/db'
        }
    }
};
By Object
module.exports = {
    db: {
        my_mongo_database: {
            schema: 'mongoose',
            config: {
                uri: 'mongodb://localhost:27017/db',
                options: {
                    user: 'root',
                    pass: '123'
                }
            }
        }
    }
};
By function
module.exports = {
    db: {
        my_mongo_database: {
            schema: 'mongoose',
            config(Mongoose) {
                Mongoose.set('debug', true);
                Mongoose.Promise = Promise;
                
                return Mongoose.createConnection('mongodb://localhost:27017/db', {
                    user: 'root',
                    pass: '123'
                });
            }
        }
    }
};
Combined connections
module.exports = {
    db: {
        connection1: {
            schema: 'mongoose',
            config: 'mongodb://root:123@localhost:27017/somedb1'
        },
        connection2: {
            schema: 'mongoose',
            config: {
                uri: 'mongodb://localhost:27017/somedb2',
                options: {
                    user: 'root',
                    pass: '123'
                }
            }
        },
        connection3: {
            schema: 'mongoose',
            config(Mongoose) {
                return Mongoose.createConnection('mongodb://localhost:27018/db', {
                    mongos: true
                });
            }
        }
    }
};

Creating ifnode models options

const app = require('ifnode')();
const UsersModel = app.Model(
    model_options,
    db_options
);

model_options (required)

Name | Optional | Description ---- | -------- | ----------- collection | false | Name of collection name | true | Name for attaching to ifnode's application models property (default is collection option) columns | true | Mongoose Schema columns. Rules for create check here config | true | Mongoose Schema options. List check here

db_options (optional)

Name | Description ---- | ----------- db | Which of database configuration need use (from app.config.db). Default: first in app.config.db alias | Model`s alias (in app.models)

Model instance properties

Properties

Name | Description ---- | ----------- .statics | Link to mongoose.statics .methods | Link to mongoose.methods

Methods

Name | Description ---- | ----------- .schema() | Return mongoose Schema (new Mongoose.Schema) instance (result of new Mongoose.Schema) .model() | Return mongoose Nodel (result of Mongoose.createConnection().Model)

Example of usage in project

config

// config/dev.js
module.exports = {
    db: {
        customers_db: {
            schema: 'mongoose',
            config: 'mongodb://localhost/customers'
        },
        events_db: {
            schema: 'mongoose',
            config: 'mongodb://localhost/events'
        }
    }
}

models

// customers.js

const app = require('ifnode')();
const CustomersModel = app.Model({
    collection: 'customers',
    config: {
        strict: false
    }
}, {
    db: 'customers_db',
    alias: 'UsersModel'
});
const originalCustomersModel = CustomersModel.model();

originalCustomersModel.schema.path('name').validate(
    name => /[0-9]/i.test(name),
    'Invalid name'
);

CustomersModel.statics.findByEmail = function(email) {
    return this.find({ email });
};
// customers.js

const app = require('ifnode')();
const EventsModel = app.Model({
    name: 'EventsModel',
    collection: 'events',
    columns: {
        id: {
            type: String,
            index: true,
            unique: true
        },
        type: String
    }
}, {
    db: 'events_db'
});

EventsModel.statics.pushEvent = function(event) {
    return this.create(event);
};
// app.js
const IFNode = require('ifnode');
const app = IFNode({
    environment: 'dev'
});

app.load();
app.models.UsersModel.findByEmail('[email protected]').then(users => {
    /* do smt */
});
app.models.EventsModel.pushEvent({
    type: 'logsmt'
});