Saving images with Sequelize

≯℡__Kan透↙ 提交于 2021-02-08 01:48:19

问题


I'm creating a node app using Sequelize as my ORM over Postgres. But the Sequelize docs are missing any mention of how to do datablob, specifically image, saving. Does anyone know:

  1. How to save an image to the database using sequelize, for example from a form. I seems like I need to convert the image to binary data somehow from what I can tell.
  2. How to create seeds with images so that I can test before my app goes live.

Here is my migration file for an example model

export default {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('Users', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER,
      },
      firstName: {
        allowNull: false,
        type: Sequelize.STRING,
      },
      lastName: {
        type: Sequelize.STRING,
      },
      photo: { <--------------------------- Image
        type: Sequelize.BLOB,
      },
      email: {
        type: Sequelize.STRING,
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE,
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE,
      },
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('Users');
  },
};

As well as a small seed file

export default {
  up: (queryInterface, Sequelize) => {
    /*
      Add altering commands here.
      Return a promise to correctly handle asynchronicity.

      Example:
      return queryInterface.bulkInsert('Persons', [{
        name: 'John Doe',
        isBetaMember: false
      }], {});
    */
    return queryInterface.bulkInsert('Users', [
      {
        firstName: 'Ricky',
        lastName: 'Sparks',
        // photo: <---------- what should I fill in here to seed with a photo file?
        email: 'email@gmail.com',
        createdAt: Sequelize.fn('NOW'),
        updatedAt: Sequelize.fn('NOW'),
      },
    ]);
  },

  down: (queryInterface, Sequelize) => {
    /*
      Add reverting commands here.
      Return a promise to correctly handle asynchronicity.

      Example:
      return queryInterface.bulkDelete('Persons', null, {});
    */

    return queryInterface.bulkDelete('Users', null, {});
  },
};

回答1:


You can try Sequelize-file , let it handle the file storage,association with models and reading back from blob. Other than that you can use the buffer,stream in the request object of the framework you are using usually the modules expose an buffer or stream or whatever.

the flow usually goes as below.

when submitting

  • submit form -> http request object -> read buffer -> store to blob

when responding

  • read from blob -> to buffer -> build the http response.

About testing you can either create some files manually along with an json file of what are the correct values width/height/image type/etc and use those to read and submit or mock files somehow.

I would personally try to avoid storing files into database as you see its even in theory difficult and not easy to test etc. Instead you can store the files in the filesystem with an encoded filename, store information in database, test existence etc. way easier than doing all that unless its necessary.



来源:https://stackoverflow.com/questions/47701640/saving-images-with-sequelize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!