sails-hook-mongoindexes

1.2.1 • Public • Published

sails-hook-mongoindexes

NPM Version NPM Downloads Build Status Code Climate Issue Count bitHound Overall Score bitHound Dependencies bitHound Dev Dependencies bitHound Code

Hook for sails and mongoDB

About

This hook allows us to write indexes for mongoDB

NPM

Use

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!

Package Sidebar

Install

npm i sails-hook-mongoindexes

Weekly Downloads

0

Version

1.2.1

License

MIT

Last publish

Collaborators

  • sanjorgek