uuid-mongodb
Advanced tools
Comparing version 0.9.1 to 1.0.0
@@ -5,2 +5,10 @@ const { Binary } = require('mongodb'); | ||
const Err = { | ||
InvalidUUID: new Error(`Invalid UUID.`), | ||
UnexpectedUUIDType: new Error( | ||
'Unexpected UUID type. UUID must be a string or a MongoDB Binary (SUBTYPE_UUID).' | ||
), | ||
UnsupportedUUIDVersion: new Error('Unsupported uuid version.'), | ||
InvalidHexString: new Error('Invalid hex string.'), | ||
}; | ||
module.exports = { | ||
@@ -16,8 +24,10 @@ v1() { | ||
from(uuid) { | ||
if (typeof uuid === 'string') return generateUUID(uuid); | ||
else if (uuid instanceof Binary) | ||
if (typeof uuid === 'string') { | ||
if (!validateUuid(uuid)) throw Err.InvalidUUID; | ||
return generateUUID(uuid); | ||
} else if (uuid instanceof Binary) { | ||
if (uuid.length() !== 36) throw Err.InvalidUUID; | ||
return stringify(uuid.read(0, uuid.length())); | ||
throw new Error( | ||
'Unexpected UUID type. UUID must be a string or a MongoDB Binary (SUBTYPE_UUID)' | ||
); | ||
throw Err.UnexpectedUUIDType; | ||
} | ||
}, | ||
@@ -27,4 +37,3 @@ }; | ||
function generateUUID(uuid = undefined, v = 1) { | ||
if (v <= 0 || v > 4 || v === 2 || v === 3) | ||
throw Error('unsupported uuid version'); | ||
if (v <= 0 || v > 4 || v === 2 || v === 3) throw Err.UnsupportedUUIDVersion; | ||
@@ -34,17 +43,27 @@ let uuidv = uuidv1; | ||
let muuid; | ||
const apply = mu => { | ||
// add to string method | ||
mu.toString = function() { | ||
return stringify(this.buffer); | ||
}.bind(mu); | ||
return mu; | ||
}; | ||
if (uuid) { | ||
if (!validateUuid(uuid)) throw Err.InvalidUUID; | ||
const normalized = normalize(uuid); | ||
if (normalized === false) throw new Error('Invalid hex string'); | ||
muuid = new Binary(Buffer.from(normalized, 'hex'), Binary.SUBTYPE_UUID); | ||
if (!normalized) throw Err.InvalidHexString; | ||
return apply( | ||
new Binary(Buffer.from(normalized, 'hex'), Binary.SUBTYPE_UUID) | ||
); | ||
} else { | ||
const uuid = uuidv(null, new Buffer(16)); | ||
muuid = Binary(uuid, Binary.SUBTYPE_UUID); | ||
return apply(Binary(uuid, Binary.SUBTYPE_UUID)); | ||
} | ||
} | ||
muuid.toString = function() { | ||
return stringify(this.buffer); | ||
}.bind(muuid); | ||
return muuid; | ||
function validateUuid(string) { | ||
return !!/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.exec( | ||
string | ||
); | ||
} | ||
@@ -55,5 +74,5 @@ | ||
const stripped = string.replace(/-/g, ''); | ||
const stripped = string.replace(/-/g, '').toLowerCase(); | ||
if (stripped.length !== 32 || !stripped.match(/^[a-fA-F0-9]+$/)) return false; | ||
if (!stripped.match(/^[a-f0-9]{32}$/)) return false; | ||
@@ -60,0 +79,0 @@ return stripped; |
{ | ||
"name": "uuid-mongodb", | ||
"version": "0.9.1", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "mocha test/**/*.js " | ||
}, | ||
@@ -28,3 +28,7 @@ "repository": { | ||
"mongodb": "^3.1.1" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^5.2.0", | ||
"mongodb": "^3.1.1" | ||
} | ||
} |
# uuid-mongodb | ||
Generates and parses [BSON UUIDs](https://docs.mongodb.com/manual/reference/method/UUID/) for use with MongoDB. | ||
Generates and parses [BSON UUIDs](https://docs.mongodb.com/manual/reference/method/UUID/) for use with MongoDB. BSON UUIDs provide better performance than their string counterparts. | ||
<p align="center"> | ||
<img src="https://raw.githubusercontent.com/cdimascio/uuid-mongodb/master/assets/uuid-mongodb.png?raw=truef"/> | ||
</p> | ||
Inspired by [@srcagency's](https://github.com/srcagency) [mongo-uuid](https://github.com/srcagency/mongo-uuid) | ||
@@ -27,11 +31,28 @@ | ||
const mUUID2 = MUUID.from('393967e0-8de1-11e8-9eb6-529269fb1459') | ||
const mUUID2 = MUUID.from(/** MongoDb Binary of SUBTYPE_UUID */) | ||
# Create a binary UUID from a MongoDb Binary | ||
# This is useful to get MUUIDs helpful toString() method | ||
const mUUID3 = MUUID.from(/** MongoDb Binary of SUBTYPE_UUID */) | ||
``` | ||
## Example | ||
### Query | ||
```javascript | ||
const uuid = MUUID.from('393967e0-8de1-11e8-9eb6-529269fb1459'); | ||
return collection | ||
.then(c => | ||
c.findOne({ | ||
_id: uuid, | ||
}) | ||
) | ||
``` | ||
## Notes | ||
Currently support UUID v1 and v4 | ||
Currently supports UUID v1 and v4 | ||
## License | ||
MIT | ||
[MIT](./LICENSE) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
25118
6
130
0
58
2