Skip to content

Instantly share code, notes, and snippets.

@sgress454
Last active February 23, 2017 22:05
Show Gist options
  • Save sgress454/8cd563bca6365c1a16366e7019aa64f1 to your computer and use it in GitHub Desktop.
Save sgress454/8cd563bca6365c1a16366e7019aa64f1 to your computer and use it in GitHub Desktop.
Saving binary data in a model using Waterline

Given the appropriate columnType, MySQL and PostgreSQL will handle storing buffers for you.

Model

module.exports = {
  attributes: {
    name: {
      type: 'string',
    },
    avatar: {
      type: 'ref',
      columnType: 'mediumblob' // <-- for MySQL.  Use `bytea` for PostgreSQL.
    }
  }
};

In sails console...

sails> var image = fs.readFileSync('/path/to/image.png');
sails> User.create({name: 'joe', avatar: image}).exec(console.log);

To retrieve

sails> User.findOne({name: 'joe'}).exec(function(err, user){ fs.writeFileSync('/new/file/path.png', user.avatar); });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment