Created
March 13, 2018 11:32
-
-
Save tuanquynet/c4690a444c968a62168131fdc79df68d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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