Skip to content

Instantly share code, notes, and snippets.

@tuanquynet
Last active March 8, 2018 17:52
Show Gist options
  • Save tuanquynet/b747008f816d944f0a25d3f556a226fd to your computer and use it in GitHub Desktop.
Save tuanquynet/b747008f816d944f0a25d3f556a226fd to your computer and use it in GitHub Desktop.
patch-uuid-id-field-loopback-connector-mysql
# This is patch to trick uuid field to force it store as BINARY field in mysql
# In the model-definition
# Try to define uuid id field
`
"id": {
"type": "string",
"defaultFn": "uuid",
"id": true,
"mysql": {
"columnName": "user_id",
"dataType": "BINARY",
"dataLength": 16,
"nullable": "N"
}
}
`
Inside function toColumnValue, replace
`
if (prop.type === String) {
return String(val);
}
`
with
`
if (prop.type === String) {
const dataType = prop.mysql && prop.mysql.dataType || '';
if (dataType.toUpperCase() === 'BINARY') {
return Buffer.from(val.replace(/-/ig, ''), 'hex');
}
return String(val);
}
`
Inside function fromColumnValue, replace
`
case 'String':
val = String(val);
break;
`
with
`
case 'String':
const dataType = prop.mysql && prop.mysql.dataType || '';
if (dataType.toUpperCase() === 'BINARY') {
val = val instanceof Buffer ? val.toString('hex') : val;
} else {
val = String(val);
}
break;
`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment