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-03-13 20:49:30 +00:00
|
|
|
addAllModels = function addAllModels() {
|
2016-03-29 14:14:44 +01:00
|
|
|
return Promise.mapSeries(fixtures.models, fixtureUtils.addFixturesForModel);
|
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-03-13 20:49:30 +00:00
|
|
|
addAllRelations = function addAllRelations() {
|
2016-03-29 14:14:44 +01:00
|
|
|
return Promise.mapSeries(fixtures.relations, fixtureUtils.addFixturesForRelation);
|
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-03-21 12:44:23 +00:00
|
|
|
createOwner = function createOwner(logger) {
|
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-03-29 14:14:44 +01:00
|
|
|
return models.User.add(user, fixtureUtils.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-03-21 12:44:23 +00:00
|
|
|
populate = function populate(logger) {
|
|
|
|
logger.info('Running fixture populations');
|
2016-02-25 08:10:36 +01:00
|
|
|
|
|
|
|
// ### Ensure all models are added
|
2016-03-29 14:14:44 +01:00
|
|
|
return addAllModels()
|
|
|
|
// ### Ensure all relations are added
|
|
|
|
.then(addAllRelations)
|
|
|
|
.then(function () {
|
|
|
|
return createOwner(logger);
|
|
|
|
});
|
2016-02-25 08:10:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = populate;
|