Skip to content

Commit 044ce21

Browse files
author
Carmine DiMascio
committed
initial version
0 parents  commit 044ce21

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.DS_Store

Diff for: README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# uuid-mongodb
2+
3+
Generates and parses [BSON UUIDs](https://docs.mongodb.com/manual/reference/method/UUID/) for use with MongoDB.
4+
5+
Inspired by [@srcagency's](https://github.com/srcagency) [mongo-uuid](https://github.com/srcagency/mongo-uuid)
6+
7+
## Usage
8+
9+
```
10+
# Create a v1 binary UUID
11+
const mUUID1 = MUUID.v1();
12+
13+
# Create a v4 binary UUID
14+
const mUUID4 = MUUID.v4();
15+
16+
# Print a string representation of a binary UUID
17+
mUUID1.toString()
18+
19+
# Create a binary UUID from a valid uuid string
20+
const mUUID2 = MUUID.from('393967e0-8de1-11e8-9eb6-529269fb1459')
21+
```
22+
23+
## Notes
24+
25+
Currently support UUID v1 and v4
26+
27+
## License
28+
29+
MIT

Diff for: lib/index.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const { Binary } = require('mongodb');
2+
import * as uuidv1 from 'uuid/v1';
3+
import * as uuidv4 from 'uuid/v4';
4+
5+
export const MUUID = {
6+
v1() {
7+
return generateUUID(null, 1);
8+
},
9+
10+
v4() {
11+
return generateUUID(null, 4);
12+
},
13+
14+
from(uuid) {
15+
return generateUUID(uuid);
16+
},
17+
};
18+
19+
function generateUUID(uuid = undefined, v = 1) {
20+
if (v <= 0 || v > 4 || v === 2 || v === 3)
21+
throw Error('unsupported uuid version');
22+
23+
let uuidv = uuidv1;
24+
if (v === 4) uuidv = uuidv4;
25+
26+
let muuid;
27+
if (uuid) {
28+
const normalized = normalize(uuid);
29+
if (normalized === false) throw new Error('Invalid hex string');
30+
muuid = new Binary(Buffer.from(normalized, 'hex'), Binary.SUBTYPE_UUID);
31+
} else {
32+
const uuid = uuidv(null, new Buffer(16));
33+
muuid = Binary(uuid, Binary.SUBTYPE_UUID);
34+
}
35+
36+
muuid.toString = function() {
37+
const buffer = this.buffer;
38+
39+
return [
40+
buffer.toString('hex', 0, 4),
41+
buffer.toString('hex', 4, 6),
42+
buffer.toString('hex', 6, 8),
43+
buffer.toString('hex', 8, 10),
44+
buffer.toString('hex', 10, 16),
45+
].join('-');
46+
}.bind(muuid);
47+
48+
return muuid;
49+
}
50+
51+
function normalize(string) {
52+
if (typeof string !== 'string') return false;
53+
54+
const stripped = string.replace(/-/g, '');
55+
56+
if (stripped.length !== 32 || !stripped.match(/^[a-fA-F0-9]+$/)) return false;
57+
58+
return stripped;
59+
}

Diff for: package-lock.json

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "uuid-mongodb",
3+
"version": "0.8.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/cdimascio/uuid-mongodb.git"
12+
},
13+
"keywords": [
14+
"uuid",
15+
"mongodb"
16+
],
17+
"author": "Carmine DiMascio <[email protected]>",
18+
"license": "MIT",
19+
"bugs": {
20+
"url": "https://github.com/cdimascio/uuid-mongodb/issues"
21+
},
22+
"homepage": "https://github.com/cdimascio/uuid-mongodb#readme",
23+
"dependencies": {
24+
"uuid": "^3.3.2"
25+
},
26+
"peerDependencies": {
27+
"mongodb": "^3.1.1"
28+
}
29+
}

0 commit comments

Comments
 (0)