sequelize fixture loader
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 (3.20.0), should work on 2.x.
npm install sequelize-fixtures
const sequelize_fixtures = ;//a map of [model name] : model//see offical example on how to load models//const models = ;//from filesequelize_fixtures;//can use glob syntax to select multiple filessequelize_fixtures;//array of filessequelize_fixtures;//specify file encoding (default utf8)sequelize_fixtures;//load fixtures inside a transactionsequelize;//apply transform for each model being loadedsequelize_fixtures;//modify each model being loadedsequelize_fixtures;//from arrayvar fixtures =model: 'Foo'data:propA: 'bar'propB: 1model: 'Foo'data:propA: 'baz'propB: 3;sequelize_fixtures;
fixtures:- model: Foodata:propA: barpropB: 1- model: Foodata:propA: bazpropB: 3
moduleexports ="model": "Foo""data":"propA": "bar""propB": 1"model": "Foo""data":"propA": "baz""propB": 3;
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!
Associated entity can be mapped by providing either an id:
Or a property-value map (like a 'where' object) to find the entity with:
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
ActorsMovies = sequelize;Movie;Actor;
For each model you can provide build options that are passed to Model.build() and save options that are passed to instance.save(), example:
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 speficy these fields with a 'keys' property.
In this example only John will be loaded