balderdashy / sails

Realtime MVC Framework for Node.js
https://sailsjs.com
MIT License
22.85k stars 1.95k forks source link

Convert UUID model from `binary(16)` to string #6894

Open intervalia opened 4 years ago

intervalia commented 4 years ago

Node version: 10.15.0 sails: ^1.2.3, sails-hook-orm: ^2.1.1, sails-hook-sockets: ^2.0.0, sails-mysql: ^1.0.1


I have an existing mySQL database with a table that stores a UUID as binary(16) This database also has a function (UUID_TO_BIN) to convert a UUID from a string to binary(16) and another function (BIN_TO_UUID) to convert from binary(16) to a string.

But I can not find anywhere that explains how I can call these functions using a sails model.

Here is my model:

module.exports = {
  primaryKey: 'id',
  tableName: 'name_refs',
  attributes: {
    id: { type: 'number', columnType: 'int(10) unsigned', required: true, unique: true, autoIncrement: true },
    name: { type: 'string', required: true },
    type: { type: 'string', isIn: ['USER', 'GROUP'] },
    uuid: { type: 'ref', columnType: 'binary(16)' },
    createdAt: { type: 'ref', columnType: 'timestamp(6)', columnName: 'created_at'},
    updatedAt: false
  }
};

What can I do to get the uuid value to be processed by my internal function BIN_TO_UUID?

sailsbot commented 4 years ago

@intervalia Thanks for posting! We'll take a look as soon as possible.

In the mean time, there are a few ways you can help speed things along:

Please remember: never post in a public forum if you believe you've found a genuine security vulnerability. Instead, disclose it responsibly.

For help with questions about Sails, click here.

eltonk commented 4 years ago

Same problem. If you figure out the solution, please share it!

whichking commented 4 years ago

Hmm, @intervalia, I don't think that Waterline supports calling those functions. If this functionality is desirable to many users, though, we'll consider it for the future!

intervalia commented 4 years ago

I think it is important to be able to call specific SQL functions through the ORM. It would be nice to specify a SET function and a GET function for a specific field. In this case we are storing a GUID as a 16-byte binary value and not as a 36-byte string. This is a huge savings when we have millions of entries that all have a GUID.

whichking commented 4 years ago

@intervalia, that makes a lot of sense to me. We'll be sure to keep this in mind when considering new features.

intervalia commented 4 years ago

I guess the other way to do this is to provide a BIN_GUID format/type that would convert to and from the binary(16) and char(36)

intervalia commented 1 year ago

Just an FYI: I am no on the project that had this problem and have no way to validate if it is working.

mikermcneil commented 1 year ago

I think you can accomplish this using sendNativeQuery().