1.2.0 • Published 6 years ago

ifnode-mongoose v1.2.0

Weekly downloads
39
License
MIT
Repository
github
Last release
6 years ago

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

NameTypeDescription
-stringMongo connection uri. Read more on mongoose site
configObject: { 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));
-functionAdds 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)

NameOptionalDescription
collectionfalseName of collection
nametrueName for attaching to ifnode's application models property (default is collection option)
columnstrueMongoose Schema columns. Rules for create check here
configtrueMongoose Schema options. List check here

db_options (optional)

NameDescription
dbWhich of database configuration need use (from app.config.db). Default: first in app.config.db
aliasModel`s alias (in app.models)

Model instance properties

Properties

NameDescription
.staticsLink to mongoose.statics
.methodsLink to mongoose.methods

Methods

NameDescription
.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('test@email.com').then(users => {
    /* do smt */
});
app.models.EventsModel.pushEvent({
    type: 'logsmt'
});
1.2.0

6 years ago

1.1.0

7 years ago

1.0.0

7 years ago

0.2.0

8 years ago

0.1.2

8 years ago

0.1.1

9 years ago