Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set index for model attributes in Sails js?

Tags:

sails.js

Here's my model class:

module.exports =
{
  attributes:
  {
    id          : {type: 'integer', unique: true, primaryKey: true},
    title       : {type:'string', required:true},
    firstName   : {type:'string', required:true},
    lastName    : {type:'string', required:true},
    phone       : {type:'string', required:true},
    email       : {type:'string', required:true, unique: true},
    password    : {type:'string', required:false},
    roles       : {type:'array'}
  }
}

How to set index for the email and phone attributes? Also, is it possible to create compule index like index(firstName,lastName)?

I tried hard to search on Google but did not find any result about creating an index in model class.

like image 326
Chanatip Yim Avatar asked Jan 08 '16 04:01

Chanatip Yim


1 Answers

You can add an index property to any attribute to create an index if your adapter supports it. You specify an index attribute.

Currently Waterline doesn't support multi-column indexes in the attributes definition.

Also, when adding a unique property to an attribute, an index will automatically be created for that attribute.

In your example, we just need to add index: true, to the phone attribute.

module.exports =

{
  attributes:
  {
    id          : {type: 'integer', unique: true, primaryKey: true},
    title       : {type:'string', required:true},
    firstName   : {type:'string', required:true},
    lastName    : {type:'string', required:true},
    phone       : {type:'string', required:true, index: true},
    email       : {type:'string', required:true, unique: true},
    password    : {type:'string', required:false},
    roles       : {type:'array'}
  }
}

Although you are using Sailsjs, this is actually in the documentation for the Sails ORM which is Waterline - see here.

like image 179
arcseldon Avatar answered Oct 19 '22 08:10

arcseldon