Skip to content

Simple data seeding for sails.js projects.

Notifications You must be signed in to change notification settings

RJiraya/sails-seed

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SAILS-SEED Build StatusCoverage Status


Installation

npm install sails-seed

Depending on you version of sails, sails-seed is treated differently On install, a config/seeds.js is created. Here you will put your seeding data.

Sails 0.11

For Sails 0.11 and greater there is nothing further to do. Sails-seed runs as an installable hook.

Sails 0.10

For Sails 0.10 and previous, a file of api/hook/seed/index.js is created on installation. No further configuration is required, but this file is necessary for the data to seed.

Usage

Place your seeding data in the config/seeds.js file. For exampe, for a Person model, your config/seeds.js file might look like

module.exports.seeds = {
  person: [
    {
      firstName: 'Luke',
      lastName:  'Skywalker'
    },
    {
      firstName: 'Darth',
      lastName:  'Vader'
    }
  ]
}

Configuration

Overwrite

By default, sails-seed will not overwrite the data which is present. If you would not like your seed to overwrite, your new config/seeds.js file might look like

module.exports.seeds = {
  person: {
    data: [
      {
        firstName: 'Luke',
        lastName:  'Skywalker'
      },
      {
        firstName: 'Darth',
        lastName:  'Vader'
      }
    ],
    overwrite: false
  }
}

Unique

If you would like to seed only data that is not presetnt, you can use a unique field(s), like so

module.exports.seeds = {
  person: {
    data: [
      {
        firstName: 'Luke',
        lastName:  'Skywalker'
      },
      {
        firstName: 'Darth',
        lastName:  'Vader'
      }
    ],
    unique: ['lastName', 'firstName']
  }
}

This will only create objects that do not have the unique firstName and lastName combinations Given an overwrite true option, sails-seed will only overwrite items which match the unique requirements.

Disable

If you would like disable the seed hook (say for testing purposes) simply add the following config option

module.exports.seeds = {
  disable: true,
  person: {
    data: [
      {
        firstName: 'Luke',
        lastName:  'Skywalker'
      },
      {
        firstName: 'Darth',
        lastName:  'Vader'
      }
    ],
    unique: ['lastName', 'firstName']
  }
}

Active

If you would like to disable per model, you can add an active option. Like so will disable the person seeding

module.exports.seeds = {
  person: {
    active: false,
    data: [
      {
        firstName: 'Luke',
        lastName:  'Skywalker'
      },
      {
        firstName: 'Darth',
        lastName:  'Vader'
      }
    ],
    unique: ['lastName', 'firstName']
  }
}

Configuration

You may also set configurations (say for testing) via the sails pipeline. Just make sure that you have set the configurations you wish you have before the hook:moduleloader:loaded event. This way you wouldn't have to change your configuration for testing in the code, and could have the test configuration.

Author

M. Elliot Frost, CEO of Frostware

About

Simple data seeding for sails.js projects.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%