Sequelize fixtures
This is a simple lib to load data to database using sequelize.
It is intended for easily setting up test data.
Yaml and json formats are supported. Includes a grunt task.
Duplicate records are not inserted.
API returns bluebird promises, but callbacks can also be used as the last argument.
Tested with latest Sequelize (5.0)
Install
npm install sequelize-fixtures
Test
npm test
Usage
const sequelize_fixtures = ; //a map of [model name] : model //see offical example on how to load models //https://github.com/sequelize/express-example/blob/master/models/index.js const models = ; //from file sequelize_fixtures; //can use glob syntax to select multiple files sequelize_fixtures; //array of files sequelize_fixtures; //specify file encoding (default utf8) sequelize_fixtures; //specify logging function (default console.log) { console } sequelize_fixtures; //specify separate stdout and stderr streams //(e.g. a winston-compatible logger) { console; } sequelize_fixtures; //load fixtures inside a transaction sequelize; //apply transform for each model being loaded sequelize_fixtures; //modify each model being loaded sequelize_fixtures; //from array var fixtures = model: 'Foo' data: propA: 'bar' propB: 1 model: 'Foo' data: propA: 'baz' propB: 3 ; sequelize_fixtures;
File formats
json
yaml
fixtures: - model: Foo data: propA: bar propB: 1 - model: Foo data: propA: baz propB: 3
javascript
moduleexports = "model": "Foo" "data": "propA": "bar" "propB": 1 "model": "Foo" "data": "propA": "baz" "propB": 3 ;
Associations
You can specify associations by providing related object id or a where clause to select associated object with. Make sure associated objects are described before associations!
One to many
Assuming
Car;Owner;
Associated entity can be mapped by providing either an id:
Or a property-value map (like a 'where' object) to find the entity with:
Many to many
Assuming
Project;Person;
Associated entities can be mapped using the association 'as' name ('people' for Project and 'projects' for People) or 'through' table name, and providing an array of ids:
Or an array of property-value mappings to find the entities with:
If you have a custom through model for a m2m relationship, you can set additional attributes using _through
:
ActorsMovies = sequelize;Movie;Actor;
Build options, save options
For each model you can provide build options that are passed to Model.build() and save options that are passed to instance.save(), example:
Detect duplicates based on select fields
In case you want to detect duplicates based on specific field or fields rather than all fields (for example, don't include entities with the same id, even if other fields don't match), you can specify these fields with a 'keys' property.
,
In this example only John will be loaded
ignoreSet
)
Ignore setters (By default, this library attempts to run values through any defined property setters to coerce the value correctly.
If you use instance methods (other than setDataValue
, which a mock is created for), then this will raise an error.
For example:
const User = sequelize;
You can turn off this behavior by setting ignoreSet
to true.
This ignores any defined setters for this model and instead just set the value as the same data literal specified in the fixture.
grunt task
Gruntfile.js:
grunt; grunt;