Skip to content

Feature/binary UUID - #10290

Closed
bradennapier wants to merge 13 commits into
sequelize:masterfrom
bradennapier:feature/binary-uuid
Closed

bradennapier wants to merge 13 commits into
sequelize:masterfrom
bradennapier:feature/binary-uuid

Conversation

@bradennapier

@bradennapier bradennapier commented Dec 21, 2018 •

Copy link
Copy Markdown

Pull Request check-list

  • Does npm run test or npm run test-DIALECT pass with this change (including linting)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Have you added new tests to prevent regressions?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Did you follow the commit message conventions explained in CONTRIBUTING.md?

Description of change

#10265 #9271 #4997

This starts to implement a binary uuid type. It is meant to open the general discussion around what may be missing. I have implemented a working package as a module here to give a better idea: https://github.com/odo-network/sequelize-binary-uuid

The benefit is fairly massive (@see: https://www.percona.com/blog/2014/12/19/store-uuid-optimized-way/)

This PR implements the UUIDV1 shuffling as recommended by above as well as mysql team directly to greatly improve performance while indexing UUIDs.

Example

const User = sequelize.define(
  'TableName',
  {
    id: {
      primaryKey: true,
      type: Sequelize.DataTypes.BINARYUUID,
      defaultValue: Sequelize.DataTypes.UUIDV1.BINARY
    }
  }
);

image


Open Questions

DataTypes.UUID.BINARY

I could not make it work when trying to use UUID.BINARY as it was breaking when going to dialects and not passing options on, among other things... probably makes the most sense there but way too many side effects so I changed to using BINARYUUID type.

Virtual / Transformed Field

In https://github.com/odo-network/sequelize-binary-uuid it also provides a virtual representation of the field in a fairly straight-forward way. I wasn't sure the best way to do something similar here.

https://github.com/odo-network/sequelize-binary-uuid#using-withbinaryuuid-preset-recommended
https://github.com/odo-network/sequelize-binary-uuid#creating-a-uuid-virtual-field

Specifically since the uuidv1 is transformed to binary - there should be a simple and easy way to view the field as its non-binary form.

v5 breaks X'${hex}' handling

It would appear that v5 breaks the handling of anything using X'... - I haven't tested but as far as I can tell this would break a few areas such as BLOB but I may be wrong. In the end it seems that stringify just doesn't make sense to use.

I was hoping it would work though as it makes the LOGGING much more helpful than just putting up question marks like it does now.

@codecov

codecov Bot commented Dec 21, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #10290 into master will decrease coverage by 12.67%.
The diff coverage is 91.3%.

Impacted file tree graph

@@             Coverage Diff             @@
##           master   #10290       +/-   ##
===========================================
- Coverage   96.27%    83.6%   -12.68%     
===========================================
  Files          68       63        -5     
  Lines        9772     9476      -296     
===========================================
- Hits         9408     7922     -1486     
- Misses        364     1554     +1190
Impacted Files Coverage Δ
lib/utils.js 98.29% <100%> (+0.02%) ⬆️
lib/utils/validator-extras.js 95.34% <50%> (-2.22%) ⬇️
lib/data-types.js 93.45% <93.75%> (-1.5%) ⬇️
lib/dialects/mssql/query-generator.js 3.57% <0%> (-92.27%) ⬇️
lib/dialects/mysql/query-generator.js 5.85% <0%> (-92.06%) ⬇️
lib/dialects/mssql/query-interface.js 8.69% <0%> (-91.31%) ⬇️
lib/dialects/mssql/query.js 5.02% <0%> (-87.94%) ⬇️
lib/dialects/mariadb/query-generator.js 10.41% <0%> (-87.5%) ⬇️
lib/dialects/mssql/resource-lock.js 20% <0%> (-80%) ⬇️
lib/dialects/mysql/query.js 20% <0%> (-78.47%) ⬇️
... and 19 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9c0f041...efc1087. Read the comment docs.

Comment thread lib/data-types.js
*
* @namespace DataTypes.BINARYUUID
*/
function BINARYUUID() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sushantdhiman I was wondering if you want to enforce these to be classes (and be called with new) in v5?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer it! Was just following what I saw previous!

Comment thread lib/data-types.js
*/
function UUIDV1() {
if (!(this instanceof UUIDV1)) return new UUIDV1();
function UUIDV1(binary) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest you make this parameter an object instead, avoid future breaking changes if we wish to add more optional parameters.

Comment thread lib/utils.js
const operators = require('./operators');
const operatorsArray = _.values(operators);

const toBinaryUUID = require('binary-uuid').toBinaryUUID;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it really worth it adding yet another dependency for a very niche data type like this?
Wouldn't it make more sense to split special datatypes and dialects into smaller packages?

@bradennapier bradennapier Jan 2, 2019 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dependency was written for this PR specifically - just left it as its own package as it was small and could be used in other places easily. It's only a few lines of code really! I implemented as its own package as well:

https://github.com/odo-network/sequelize-binary-uuid

However I do think having this native to sequelize is a good idea as using uuid's the wrong way are pretty damaging and based on other comments and stack overflow - a TON of people are implementing them in the worst way possible (using v4 as primary key and not shuffling their v1's when they use the right style).

@sushantdhiman

Copy link
Copy Markdown
Contributor

For now let's keep this as a separate package @bradennapier , if there is more demand we will add its support to Sequelize

@omidkrad

Copy link
Copy Markdown

@sushantdhiman Yes, there is demand. Thanks!

@NickCoad-NewsPerform

Copy link
Copy Markdown

I'm surprised this was called a niche use case. This package implements UUIDs in the way they're supposed to be implemented when used for primary keys, and UUIDs as primary keys are certainly not niche or uncommon.

Another +1 here, this would be invaluable for us.

@bradennapier

Copy link
Copy Markdown
Author

In the meantime, the package does work well :) - but agreed it should be built in.

@Seth10001

Copy link
Copy Markdown

+1 for me too, I would like to see this included!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants