2016-02-25 08:10:36 +01:00
|
|
|
// # Populate Fixtures
|
|
|
|
// This module handles populating fixtures on a fresh install.
|
|
|
|
// This is done automatically, by reading the fixtures.json file
|
|
|
|
// All models, and relationships inside the file are then setup.
|
2016-03-29 14:14:44 +01:00
|
|
|
var Promise = require('bluebird'),
|
|
|
|
models = require('../../../models'),
|
|
|
|
coreUtils = require('../../../utils'),
|
|
|
|
fixtureUtils = require('./utils'),
|
|
|
|
fixtures = require('./fixtures'),
|
2016-02-25 08:10:36 +01:00
|
|
|
|
|
|
|
// private
|
|
|
|
addAllModels,
|
|
|
|
addAllRelations,
|
|
|
|
createOwner,
|
|
|
|
|
|
|
|
// public
|
|
|
|
populate;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Add All Models
|
|
|
|
* Sequentially calls add on all the models specified in fixtures.json
|
|
|
|
*
|
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
2016-07-14 12:59:42 +02:00
|
|
|
addAllModels = function addAllModels(modelOptions) {
|
|
|
|
return Promise.mapSeries(fixtures.models, function (model) {
|
|
|
|
return fixtureUtils.addFixturesForModel(model, modelOptions);
|
|
|
|
});
|
2016-02-25 08:10:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Add All Relations
|
|
|
|
* Sequentially calls add on all the relations specified in fixtures.json
|
|
|
|
*
|
|
|
|
* @returns {Promise|Array}
|
|
|
|
*/
|
2016-07-14 12:59:42 +02:00
|
|
|
addAllRelations = function addAllRelations(modelOptions) {
|
|
|
|
return Promise.mapSeries(fixtures.relations, function (model) {
|
|
|
|
return fixtureUtils.addFixturesForRelation(model, modelOptions);
|
|
|
|
});
|
2016-02-25 08:10:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Create Owner
|
|
|
|
* Creates the user fixture and gives it the owner role.
|
|
|
|
* By default, users are given the Author role, making it hard to do this using the fixture system
|
|
|
|
*
|
2016-03-21 12:44:23 +00:00
|
|
|
* @param {{info: logger.info, warn: logger.warn}} logger
|
2016-02-25 08:10:36 +01:00
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
2016-07-14 12:59:42 +02:00
|
|
|
createOwner = function createOwner(logger, modelOptions) {
|
2016-02-25 08:10:36 +01:00
|
|
|
var user = {
|
|
|
|
name: 'Ghost Owner',
|
|
|
|
email: 'ghost@ghost.org',
|
|
|
|
status: 'inactive',
|
2016-03-29 14:14:44 +01:00
|
|
|
password: coreUtils.uid(50)
|
2016-02-25 08:10:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return models.Role.findOne({name: 'Owner'}).then(function (ownerRole) {
|
|
|
|
if (ownerRole) {
|
|
|
|
user.roles = [ownerRole.id];
|
|
|
|
|
2016-03-21 12:44:23 +00:00
|
|
|
logger.info('Creating owner');
|
2016-07-14 12:59:42 +02:00
|
|
|
return models.User.add(user, modelOptions);
|
2016-02-25 08:10:36 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ## Populate
|
|
|
|
* Sequentially creates all models, in the order they are specified, and then
|
|
|
|
* creates all the relationships, also maintaining order.
|
|
|
|
*
|
2016-03-21 12:44:23 +00:00
|
|
|
* @param {{info: logger.info, warn: logger.warn}} logger
|
2016-02-25 08:10:36 +01:00
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
2016-07-14 12:59:42 +02:00
|
|
|
populate = function populate(logger, modelOptions) {
|
2016-03-21 12:44:23 +00:00
|
|
|
logger.info('Running fixture populations');
|
2016-02-25 08:10:36 +01:00
|
|
|
|
2016-07-14 12:59:42 +02:00
|
|
|
return addAllModels(modelOptions)
|
|
|
|
.then(function () {
|
|
|
|
return addAllRelations(modelOptions);
|
|
|
|
})
|
2016-03-29 14:14:44 +01:00
|
|
|
.then(function () {
|
2016-07-14 12:59:42 +02:00
|
|
|
return createOwner(logger, modelOptions);
|
2016-03-29 14:14:44 +01:00
|
|
|
});
|
2016-02-25 08:10:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = populate;
|