You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

helpers.js 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. const { default: initModels } = require('./models/init-models')
  2. module.exports = {
  3. /**
  4. * Grabs the actual row information as JSON
  5. * and packs it up into an array
  6. *
  7. * @param {array} postList
  8. * @param {string} typeToFind
  9. * @returns array
  10. */
  11. getPostData: (postList, typeToFind) => {
  12. return postList.reduce((data, post) => {
  13. /**
  14. * Double-check our post matches the type
  15. */
  16. if(typeToFind && post.dataValues.post_type != typeToFind) {
  17. return data
  18. }
  19. /**
  20. * ONLY add matching postData to our data array
  21. * and omit all the extra sequelize meta data
  22. * by pushing the dataValues key
  23. */
  24. data.push(post.dataValues)
  25. return data
  26. }, [])
  27. },
  28. /**
  29. * Helper to find post descendants
  30. *
  31. * @param {array} parentList
  32. * @param {object} db - sequelize instance
  33. * @param {function} getParams - lambda returning param object { rule: 'example' }
  34. * @returns array
  35. */
  36. findDescendants: async (parentList, db, getParams) => {
  37. const matchList = []
  38. for(let post of parentList) {
  39. const match = await db.findAll({ where: getParams(post) })
  40. match.forEach(entity => matchList.push(entity))
  41. }
  42. return matchList
  43. },
  44. /**
  45. * The keys we want from the postmeta table
  46. */
  47. metakeys: [
  48. '_wp_attached_file',
  49. '_wp_attachment_metadata',
  50. '_thumbnail_id',
  51. ...process.env.METAKEYS
  52. ],
  53. /**
  54. * The post type names we're interested in
  55. *
  56. * 1. Status types we migrate
  57. */
  58. types: {
  59. ...process.env.POST_TYPES,
  60. /* 1 */
  61. status: ['publish']
  62. },
  63. /**
  64. * dB creation helper
  65. * !: Don't touch
  66. *
  67. * @param {object} models - sequelize instance
  68. * @returns object - all models associated with instance
  69. */
  70. db: models => initModels(models),
  71. }