Taylor Johnson
Taylor Johnson

Reputation: 1953

sequelize return blob as text

I am using Sequelize to retrieve data from a legacy mysql database. One of the columns in the table is a blob, so sequelize returns a buffer.

Is it possible to return the blob as text or as a string using Sequelize? Or will i need to loop through the array of objects and convert them?

Thanks for the help!

Similar code:

await findAll({
  where: {
    date: { $gte: sevenDaysAgo },
    newsSource: sourceList,
  },
  order: ['date'],
  raw: true,
});

Upvotes: 3

Views: 7026

Answers (1)

Zeke Nierenberg
Zeke Nierenberg

Reputation: 2196

I believe you can use a sequelize getter for this. Depending on the size of the buffer, this may be a very bad idea.

  const Employee = sequelize.define('employee', {
    picture: {
      type: Sequelize.BLOB,
      allowNull: false,
      get() {
        return this.getDataValue('picture').toString('utf8'); // or whatever encoding is right
      },
    },

  });

modified from here.

Upvotes: 6

Related Questions