我正在使用Sequelize作为我的ORM,通过Postgres创建一个Node应用程序。但是Sequelize文档中缺少如何进行数据块(特别是图像)保存的任何说明。有没有人知道:
- 如何使用sequelize将图像保存到数据库中,例如来自表单。从我所了解的情况来看,似乎我需要将图像转换为二进制数据。
- 如何创建带有图像的种子,以便在我的应用程序上线之前进行测试。
这是一个示例模型的迁移文件
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, {});
},
};