Skip to content

fpm-git/sails-hook-mongoat

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sails-hook-mongoat

License Current release Build status Dependency status Development dependency status

Provides advanced MongoDB indexing options for Sails.js models that use the sails-mongo adapter.

Usage

npm i git+ssh://git@github.com/fpm-git/sails-hook-mongoat.git

Then simply add an 'indexes' array property to your sails model(s) that you want to add custom indexers on. This contains all your indexes.

Index properties:

  • attributes - an object with the attributes to index (can also be text indexes)
  • options (optional) - index options (see Mongo Index Options)

Examples

Creating a TTL index:

// MY MODEL WITH A DATE FIELD
module.exports = {
  attributes: {
    myDate: {
      type: 'date',
      required: true,
    },
  },
  indexes: [{
    attributes: {
      myDate: 1,
    },
    options: {
      expireAfterSeconds: 60,  // expire 60s after myDate
    },
  }],
};

Creating a composite unique index:

// MY EVENTS MODEL
module.exports = {
  attributes: {
    event_id: {
      type: 'integer',
      required: true,
    },
    match_id: {
      type: 'integer',
      required: true,
    },
  },
  indexes: [{
    // Index on both `event_id` and `match`.
    attributes: {
      event_id: -1,    // desc
      match: 1,        // asc
    },
    options: {
      unique: true,
    },
  }],
};

Forcing updates to run in safe migration mode:

Sometimes it might be desireable to run migrations in safe mode, something which is automatically prevented by default. In such scenarios, switching to drop or alter may not entirely be an option.

For these cases, the mongoat.forceUpdate config value may be set to a truthy value, forcing the hook to attempt updates regardless of model migration preference.

This flag may be set conveniently through the commandline when launching your Sails app:

# With Sails running in console mode:
$ sails console --mongoat.forceUpdate

# Or run Sails without the REPL:
$ sails lift --mongoat.forceUpdate

# Or run Sails directly:
$ node ./node_modules/.bin/sails lift --mongoat.forceUpdate

About

Provides advanced mongo indexing options for sails.js models that use the sails-mongo adapter.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%