Given the appropriate columnType
, MySQL and PostgreSQL will handle storing buffers for you.
module.exports = {
attributes: {
name: {
type: 'string',
},
avatar: {
type: 'ref',
columnType: 'mediumblob' // <-- for MySQL. Use `bytea` for PostgreSQL.
}
}
};
sails> var image = fs.readFileSync('/path/to/image.png');
sails> User.create({name: 'joe', avatar: image}).exec(console.log);
sails> User.findOne({name: 'joe'}).exec(function(err, user){ fs.writeFileSync('/new/file/path.png', user.avatar); });