Hook for sails and mongoDB
This hook allows us to write indexes for mongoDB
Install
npm install sails-hook-mongoindexes
In config/connections.js
module.exports.connections = {
somethingMongodbServer:{
//You need sails-mongo adapter to work
adapter: 'sails-mongo',
host: 'localhost',
port: 27017,
// user: 'username',
// password: 'password',
database: 'test'
},
//You can configure multiple databases with the same format
othermongoDBServer:{}
}
In config/models.js
module.exports.models = {
connection: 'somethingMongodbServer',
migrate: 'safe'
};
In models, example User.js
module.exports = {
//Put names on your fields
atributes:{
name: 'string',
lastName: 'string',
userName: 'string',
password: 'string',
},
//And here your indexes for that fields
index: [
{
//Now you can index unique fields
ind:{userName:1},
ops: {
unique: true,
required: true,
}
},
{
//Agruop fields
ind:{name:1, lastName:1},
ops: {
unique: true,
w:1
}
},
{
//Or simply required
ind:{password:1},
ops: {
required: true,
w:1
}
}
]
};
Thats all!