-
-
Save srjefers/6434dc62f53443d01ddf09d9ed885e5e to your computer and use it in GitHub Desktop.
Generates a MongoDB-style ObjectId in Node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Generates a MongoDB-style ObjectId in Node.js. Uses nanosecond timestamp in place of counter; | |
| * should be impossible for same process to generate multiple objectId in same nanosecond? (clock | |
| * drift can result in an *extremely* remote possibility of id conflicts). | |
| * | |
| * @returns {string} Id in same format as MongoDB ObjectId. | |
| */ | |
| function objectId() { | |
| const os = require('os'); | |
| const crypto = require('crypto'); | |
| const seconds = Math.floor(new Date()/1000).toString(16); | |
| const machineId = crypto.createHash('md5').update(os.hostname()).digest('hex').slice(0, 6); | |
| const processId = process.pid.toString(16).slice(0, 4).padStart(4, '0'); | |
| const counter = process.hrtime()[1].toString(16).slice(0, 6).padStart(6, '0'); | |
| return seconds + machineId + processId + counter; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment