使用 Sequelize 保存图像 [英] Saving images with Sequelize

查看:56
本文介绍了使用 Sequelize 保存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Sequelize 作为我在 Postgres 上的 ORM 创建一个节点应用程序.但是 Sequelize 文档没有提到如何进行数据块,特别是图像,保存.有谁知道:

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. 如何使用 sequelize 将图像保存到数据库,例如从表单中保存.我似乎需要以某种方式将图像转换为二进制数据.
  2. 如何使用图像创建种子,以便我可以在我的应用上线之前进行测试.

这是我的示例模型的迁移文件

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');
  },
};

还有一个小的种子文件

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, {});
  },
};

推荐答案

你可以试试 Sequelize-file ,让它处理文件存储、与模型的关联和从 blob 读回.除此之外,您可以在您使用的框架的请求对象中使用缓冲区、流,通常模块会公开缓冲区或流或其他任何内容.

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.

流程通常如下.

提交时

  • 提交表单 -> http 请求对象 -> 读取缓冲区 -> 存储到 blob

回复时

  • 从 blob 读取 -> 到缓冲区 -> 构建 http 响应.

关于测试,您可以手动创建一些文件以及正确值宽度/高度/图像类型/等的 json 文件,并使用这些文件以某种方式读取和提交或模拟文件.

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.

这篇关于使用 Sequelize 保存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆