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.
npm install ifnode-mongoose --save| Name | Type | Description |
|---|
- |
string| Mongo connection uri. Read more on mongoose siteconfig|Object: { uri, options }| Mongoose connect params. Read more on mongoose site (uri- first argument ofmongoose.connect(uri, options),options- second argument ofmongoose.connect(uri, options)); - |
function| Adds possibility to create own connection. Useful for multiplied mongo connections
module.exports = {
db: {
my_mongo_database: {
schema: 'mongoose',
config: 'mongodb://root:123@localhost:27017/db'
}
}
};module.exports = {
db: {
my_mongo_database: {
schema: 'mongoose',
config: {
uri: 'mongodb://localhost:27017/db',
options: {
user: 'root',
pass: '123'
}
}
}
}
};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'
});
}
}
}
};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
});
}
}
}
};const app = require('ifnode')();
const UsersModel = app.Model(
model_options,
db_options
);| 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 |
| 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) |
| Name | Description |
|---|---|
.statics |
Link to mongoose.statics |
.methods |
Link to mongoose.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) |
// config/dev.js
module.exports = {
db: {
customers_db: {
schema: 'mongoose',
config: 'mongodb://localhost/customers'
},
events_db: {
schema: 'mongoose',
config: 'mongodb://localhost/events'
}
}
}// 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'
});