bookshelf-prefixed-ordered-uuid

Increase database performance by supporting ordered UUID's for your Bookshelf models. The prefix helps you identify the type of resource associated with its ID.
Installation
After installing bookshelf-prefixed-ordered-uuid
with npm i --save bookshelf-prefixed-ordered-uuid
,
add it as a bookshelf plugin and enable it on your models.
let knex = require('knex')(require('./knexfile.js').development);
let bookshelf = require('bookshelf')(knex);
bookshelf.plugin(require('bookshelf-prefixed-ordered-uuid'));
let User = bookshelf.Model.extend({
tableName: 'users',
orderedUuids: {
id: 'UR',
},
});
Usage
You can call every method as usual and bookshelf-prefixed-ordered-uuid
will handle the conversion of ID's from/to UUID strings to/from binary format for the database.
Note that when creating your database tables your primary keys should be of type BINARY(16) for no prefix, adding to the length depending on the length of prefix
you intend on using. BINARY(18) works for two letter prefixes.
new User({ name: 'Sally', email: 'sally@example.com' })
.save()
.then(function(user) {
});
new User({ id: "UR470300d5a23108cbba1a410d65dd05ff" })
.fetch()
.then(function(user) {
});
Useful Methods
let uuid = bookshelf.Model.generateUuid('BO');
let regex = bookshelf.Model.prefixedUuidRegex('UR');
let uuidBinary = bookshelf.Model.prefixedUuidToBinary(uuid, 2);
let uuidBinary = bookshelf.Model.binaryToPrefixedUuid(uuidBinary, 2);
MySQL Functions
Here are some custom MySQL functions for generating and converting Prefixed Ordered UUID's (these are built for prefix lengths of 2):
DELIMITER //
CREATE DEFINER=`user`@`localhost` FUNCTION `POUUID`(prefix CHAR(2), uuid BINARY(36))
RETURNS BINARY(18) DETERMINISTIC
RETURN CONCAT(CONVERT(prefix, BINARY), UNHEX(CONCAT(SUBSTR(uuid, 15, 4),SUBSTR(uuid, 10, 4),SUBSTR(uuid, 1, 8),SUBSTR(uuid, 20, 4),SUBSTR(uuid, 25))));
//
DELIMITER ;
DELIMITER //
CREATE DEFINER=`user`@`localhost` FUNCTION `FROM_POUUID`(pouuid BINARY(18))
RETURNS CHAR(38) DETERMINISTIC
RETURN CONCAT(SUBSTR(pouuid, 1, 2), LOWER(HEX(SUBSTR(pouuid, 3))));
//
DELIMITER ;
DELIMITER //
CREATE DEFINER=`user`@`localhost` FUNCTION `TO_POUUID`(pouuid CHAR(38))
RETURNS BINARY(18) DETERMINISTIC
RETURN CONCAT(SUBSTR(pouuid, 1, 2), UNHEX(SUBSTR(pouuid, 3)));
//
DELIMITER ;
MySQL Function Usage
Generate new Prefixed Ordered UUID binary:
INSERT INTO users (id, name) VALUES (POUUID('UR', uuid()), 'Bim Jimbo');
Convert Prefixed Ordered UUID binary to string:
SELECT FROM_POUUID(id) FROM users;
Convert Prefixed Ordered UUID string to binary:
SELECT * FROM users WHERE id = TO_POUUID("UR407cbd87e831746980ac705c6e7e176c");