tm-apps-db
Advanced tools
Comparing version 3.0.0 to 3.0.1
@@ -51,4 +51,4 @@ "use strict"; | ||
return queryInterface.removeColumn('tags', 'standardized_name:', sequelize.STRING); | ||
}) | ||
}); | ||
} | ||
}; |
@@ -5,3 +5,17 @@ "use strict"; | ||
module.exports = function(sequelize, DataTypes) { | ||
module.exports = function (sequelize, DataTypes) { | ||
function purge(model) { | ||
return function (options) { | ||
return sequelize.models[model].destroy(R.merge({where: {article_id: this.getDataValue('id')}}, options)); | ||
}; | ||
} | ||
function create(model) { | ||
return function (data, options) { | ||
const content = R.map(R.assoc('article_id', this.getDataValue('id')), data); | ||
return sequelize.models[model].bulkCreate(content, options); | ||
}; | ||
} | ||
const article = sequelize.define('article', { | ||
@@ -88,3 +102,3 @@ id: { | ||
classMethods: { | ||
associate: function(models) { | ||
associate: function (models) { | ||
article.belongsTo(models.section, {as: 'home_section', constraints: false}); | ||
@@ -99,41 +113,21 @@ article.hasMany(models.article_tag, {constraints: false}); | ||
instanceMethods: { | ||
purgeContent: function (options) { | ||
return sequelize.models.article_content.destroy(R.merge({ where: {article_id: this.getDataValue('id')} }, options)); | ||
}, | ||
setContent: function (data, options) { | ||
const content = R.map(R.assoc('article_id', this.getDataValue('id')), data); | ||
return sequelize.models.article_content.bulkCreate(content, options); | ||
}, | ||
purgeAuthors: function (options) { | ||
return sequelize.models.article_author.destroy(R.merge({ where: {article_id: this.getDataValue('id')} }, options)); | ||
}, | ||
setAuthors: function (data, options) { | ||
const content = R.map(R.assoc('article_id', this.getDataValue('id')), data); | ||
return sequelize.models.article_author.bulkCreate(content, options); | ||
}, | ||
purgeRelations: function (options) { | ||
return sequelize.models.article_relation.destroy(R.merge({ where: {article_id: this.getDataValue('id')} }, options)); | ||
}, | ||
setRelations: function (data, options) { | ||
const relations = R.map(R.assoc('article_id', this.getDataValue('id')), data); | ||
return sequelize.models.article_relation.bulkCreate(relations, options); | ||
}, | ||
purgeSections: function (options) { | ||
return sequelize.models.article_section.destroy(R.merge({ where: {article_id: this.getDataValue('id')} }, options)); | ||
}, | ||
setSections: function (data, options) { | ||
const relations = R.map(R.assoc('article_id', this.getDataValue('id')), data); | ||
return sequelize.models.article_section.bulkCreate(relations, options); | ||
}, | ||
purgeTags: function (options) { | ||
return sequelize.models.article_tag.destroy(R.merge({ where: {article_id: this.getDataValue('id')} }, options)); | ||
}, | ||
setTags: function (data, options) { | ||
const relations = R.map(R.assoc('article_id', this.getDataValue('id')), data); | ||
return sequelize.models.article_tag.bulkCreate(relations, options); | ||
}, | ||
findOrCreateThenSetTags: function (data, options) { | ||
const promises = R.map(tag => sequelize.models.tag.findOrCreate(R.merge(options, {where: { id: tag.id }, defaults: R.omit(['id'], tag)})).spread((a, b) => a), data); | ||
return sequelize.Sequelize.Promise.all(promises).then(tags => this.setTags(tags, options)); | ||
} | ||
purgeContent: purge("article_content"), | ||
setContent: create("article_content"), | ||
purgeAuthors: purge("article_author"), | ||
setAuthors: create("article_author"), | ||
purgeRelations: purge("article_relation"), | ||
setRelations: create("article_relation"), | ||
purgeSections: purge("article_section"), | ||
setSections: create("article_section"), | ||
purgeTags: purge("article_tag"), | ||
setTags: create("article_tag") | ||
} | ||
@@ -140,0 +134,0 @@ }); |
{ | ||
"name": "tm-apps-db", | ||
"version": "3.0.0", | ||
"version": "3.0.1", | ||
"description": "Apps consumer database initialiser", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
38239
1052