2013-11-03 12:13:19 -05:00
|
|
|
var knex = require('../../../server/models/base').knex,
|
2013-06-25 06:43:15 -05:00
|
|
|
when = require('when'),
|
2013-11-03 12:13:19 -05:00
|
|
|
migration = require("../../../server/data/migration/"),
|
|
|
|
Settings = require('../../../server/models/settings').Settings,
|
|
|
|
DataGenerator = require('../fixtures/data-generator'),
|
|
|
|
API = require('./api');
|
2013-05-23 23:02:41 -05:00
|
|
|
|
2013-10-07 20:39:33 -05:00
|
|
|
function initData() {
|
|
|
|
return migration.init();
|
|
|
|
}
|
2013-06-14 17:12:04 -05:00
|
|
|
|
2013-10-07 20:39:33 -05:00
|
|
|
function clearData() {
|
|
|
|
// we must always try to delete all tables
|
|
|
|
return migration.reset();
|
|
|
|
}
|
2013-06-14 17:12:04 -05:00
|
|
|
|
2013-10-07 20:39:33 -05:00
|
|
|
function insertDefaultFixtures() {
|
2013-11-03 09:12:09 -05:00
|
|
|
return when(insertDefaultUser()
|
|
|
|
.then(insertPosts()));
|
2013-10-07 20:39:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function insertPosts() {
|
2013-11-03 09:12:09 -05:00
|
|
|
return when(knex('posts').insert(DataGenerator.forKnex.posts)
|
|
|
|
.then(knex('tags').insert(DataGenerator.forKnex.tags)
|
|
|
|
.then(knex('posts_tags').insert(DataGenerator.forKnex.posts_tags))));
|
2013-10-07 20:39:33 -05:00
|
|
|
}
|
2013-06-14 17:12:04 -05:00
|
|
|
|
2013-10-07 20:39:33 -05:00
|
|
|
function insertMorePosts() {
|
|
|
|
var lang,
|
|
|
|
status,
|
|
|
|
posts,
|
|
|
|
promises = [],
|
|
|
|
i, j, k = 0;
|
2013-06-14 17:12:04 -05:00
|
|
|
|
2013-10-07 20:39:33 -05:00
|
|
|
for (i = 0; i < 2; i += 1) {
|
|
|
|
posts = [];
|
|
|
|
lang = i % 2 ? 'en' : 'fr';
|
|
|
|
posts.push(DataGenerator.forKnex.createGenericPost(k++, null, lang));
|
2013-06-14 17:12:04 -05:00
|
|
|
|
2013-10-07 20:39:33 -05:00
|
|
|
for (j = 0; j < 50; j += 1) {
|
|
|
|
status = j % 2 ? 'published' : 'draft';
|
|
|
|
posts.push(DataGenerator.forKnex.createGenericPost(k++, status, lang));
|
|
|
|
}
|
|
|
|
|
|
|
|
promises.push(knex('posts').insert(posts));
|
2013-06-25 06:43:15 -05:00
|
|
|
}
|
2013-08-24 15:51:58 -05:00
|
|
|
|
2013-10-07 20:39:33 -05:00
|
|
|
return when.all(promises);
|
|
|
|
}
|
|
|
|
|
|
|
|
function insertDefaultUser() {
|
|
|
|
var users = [],
|
2013-11-03 09:12:09 -05:00
|
|
|
userRoles = [];
|
2013-05-23 23:02:41 -05:00
|
|
|
|
2013-10-07 20:39:33 -05:00
|
|
|
users.push(DataGenerator.forKnex.createUser(DataGenerator.Content.users[0]));
|
|
|
|
userRoles.push(DataGenerator.forKnex.createUserRole(1, 1));
|
2013-11-03 09:12:09 -05:00
|
|
|
return when(knex('users').insert(users))
|
|
|
|
.then(knex('roles_users').insert(userRoles));
|
2013-10-07 20:39:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
initData: initData,
|
|
|
|
clearData: clearData,
|
|
|
|
insertDefaultFixtures: insertDefaultFixtures,
|
|
|
|
insertPosts: insertPosts,
|
|
|
|
insertMorePosts: insertMorePosts,
|
|
|
|
insertDefaultUser: insertDefaultUser,
|
|
|
|
|
|
|
|
DataGenerator: DataGenerator,
|
|
|
|
API: API
|
|
|
|
};
|