20181209110718-add-plaintext-column.js 798 B

1234567891011121314151617181920212223242526272829303132
  1. let cheerio = require('cheerio');
  2. module.exports = {
  3. up: async (queryInterface, Sequelize) => {
  4. await queryInterface.addColumn('posts', 'plainText', {
  5. type: Sequelize.TEXT
  6. });
  7. let posts = await queryInterface.sequelize.query(
  8. 'SELECT id, content FROM posts',
  9. { type: queryInterface.sequelize.QueryTypes.SELECT }
  10. );
  11. let promises = posts.map(post => {
  12. let replacements = {
  13. id: post.id,
  14. text: cheerio(post.content).text()
  15. }
  16. return queryInterface.sequelize.query(
  17. 'UPDATE posts SET plainText = :text WHERE id = :id',
  18. { replacements }
  19. )
  20. });
  21. return Promise.all(promises);
  22. },
  23. down: (queryInterface, Sequelize) => {
  24. return queryInterface.removeColumn('posts', 'plainText');
  25. }
  26. };