Skip to content

Instantly share code, notes, and snippets.

@tuanquynet
Created March 13, 2018 11:32
Show Gist options
  • Save tuanquynet/c4690a444c968a62168131fdc79df68d to your computer and use it in GitHub Desktop.
Save tuanquynet/c4690a444c968a62168131fdc79df68d to your computer and use it in GitHub Desktop.
import Sequelize from 'sequelize';
function doTrick() {
console.log('doTrick');
const { UUID } = Sequelize.DataTypes;
const mysqlUUID = Sequelize.DataTypes.mysql.UUID;
UUID.types.mysql = ['BLOB'];
// TODO trick
UUID.prototype._sanitize = function(value) {
// Convert Buffer data to hex-string.
value = value instanceof Buffer ? value.toString('hex') : value;
return value;
};
UUID.parse = UUID.prototype._sanitize;
mysqlUUID.prototype.toSql = function toSql() {
// return 'CHAR(36) BINARY';
// Redefine expected mysql datatype
return 'BINARY(16)';
};
// TODO trick
mysqlUUID.prototype._stringify = function _stringify(value) {
// Convert hex-string to Buffer before saving to mysql. Buffer in turn will be sqlize to BLOB
value = value.replace(/-/g, '');
value = Buffer.from(value, 'hex');
return value;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment