Using the BLOB column type
Sometimes, our application will require us to store buffer or binary data in our system. The following is a quick example of how to create and read binary data with Sequelize:
- We will start with our definition:
class Users extends Model {} Users.init({ avatar: DataTypes.BLOB, keycode: DataTypes.BLOB });
- Next, we can insert our record, like so:
await Users.create({ avatar: require("fs").readFileSync ("/some/path/avatar.jpg"), keycode: Buffer.from("secretpassword") });
- To retrieve and use the buffered data, we can simply use a finder method and write directly using Node.js’
fs
module:const user = await Users.findOne({}); require("fs").writeFileSync( "/some/path/to/write/avatar.jpg", ...