// virtual getter for custom _id kittySchema .virtual('id') .get(function() { return MUUID.from(this._id).toString(); }) .set(function(val) { this._id = MUUID.from(val); });
// 3. Save the new kitten to the database silence .save() // 4. Fetch the new kitten from the database .then(kitten => { console.log('inserted kitten with id', MUUID.from(kitten._id).toString()); return Kitten.findOne({ _id: kitten._id }); }) .then(kitten => console.log('found kitten with id', MUUID.from(kitten._id).toString()) ) .catch(e => console.error(e)) .finally(() => db.close());
describe('v4()', () => { it('should generate a validate v4 uuid', function () { const mUUID = MUUID.v4(); assert.equal(validate(mUUID.toString()), true); // ensure generated uuids are always lowercase // as per spec - (see https://www.itu.int/rec/T-REC-X.667-201210-I/en) assert.equal(hasHexUpperCase(mUUID.toString()), false); }); });
from(uuid) { if (typeof uuid === 'string') { return generateUUID(uuid); } else if ( (uuid instanceof Binary || uuid._bsontype === 'Binary') && uuid.sub_type === Binary.SUBTYPE_UUID ) { if (uuid.length() !== 16) throw Err.InvalidUUID; return apply( new Binary(uuid.read(0, uuid.length()), Binary.SUBTYPE_UUID) ); } else { throw Err.UnexpectedUUIDType; } }
function generateUUID(uuid, v = 1) { if (v <= 0 || v > 4 || v === 2 || v === 3) throw Err.UnsupportedUUIDVersion; let uuidv = uuidv1; if (v === 4) uuidv = uuidv4; if (uuid) { if (!validateUuid(uuid)) throw Err.InvalidUUID; const normalized = normalize(uuid); if (!normalized) throw Err.InvalidHexString; return apply( new Binary(Buffer.from(normalized, 'hex'), Binary.SUBTYPE_UUID) ); } else { const uuid = uuidv(null, Buffer.alloc(16)); return apply(Binary(uuid, Binary.SUBTYPE_UUID)); } }
describe('from(uuid)', () => { it('should convert uuid from string', () => { const mUUID = MUUID.from(uuidv4()); assert.equal(validate(mUUID.toString()), true); }); assert.equal(hasHexUpperCase(mUUID.toString()), false); assert.equal(validate(mUUID.toString()), true); }); assert.equal(validate(mUUID.toString()), true); }); Binary(uuidv1(null, Buffer.alloc(16)), Binary.SUBTYPE_UUID) ); assert.equal(mUUID instanceof Binary, true); assert.equal(validate(mUUID.toString()), true); }); const binary = new Binary(Buffer.from(uuidv1()), Binary.SUBTYPE_UUID); assert.throws(() => MUUID.from(binary), /Invalid UUID\.$/); }); const binary = new Binary( uuidv1(null, Buffer.alloc(16)), Binary.SUBTYPE_USER_DEFINED ); assert.throws(
describe('formatting', () => { it('should format with default (dashes)', function () { const mUUID = MUUID.v1(); assert.equal(validate(mUUID.toString()), true); assert.equal(hasHexUpperCase(mUUID.toString()), false); }); it("should format with format 'D' (dashes)", function () { const mUUID = MUUID.v1(); const uuid = mUUID.toString('D'); assert.equal(validate(uuid, 'D'), true); assert.equal(hasHexUpperCase(mUUID.toString('D')), false); }); it("should format with format 'N' no delimiter", function () { const mUUID = MUUID.v1(); const uuid = mUUID.toString('N'); assert.equal(validate(uuid, 'N'), true); assert.equal(hasHexUpperCase(uuid), false); it("should format with format 'B' (curly braces)", function () { const mUUID = MUUID.v1(); const uuid = mUUID.toString('B'); assert.equal(validate(uuid, 'B'), true); assert.equal(hasHexUpperCase(uuid), false); it("should format with format 'P' (parens)", function () { const mUUID = MUUID.v1(); const uuid = mUUID.toString('P'); assert.equal(validate(uuid, 'P'), true); assert.equal(hasHexUpperCase(uuid), false);
describe('v1()', () => { it('should generate a valid v1 uuid', function () { const mUUID = MUUID.v1(); assert.equal(validate(mUUID.toString()), true); // ensure generated uuids are always lowercase // as per spec - (see https://www.itu.int/rec/T-REC-X.667-201210-I/en) assert.equal(hasHexUpperCase(mUUID.toString()), false); }); });