ObjectId are generated identifier (known as surrogate) with the intent to be unique for a Json.
ObjectId are custom UUID that are created from:
The risk of collisions is almost the same than for uuid.
The id is called ObjectId because Json is the string version of a Javascript Object
They have been popularized by the MongoDb implementation.
They are:
consisting of:
let mongoObjectId = function () {
let timestamp = (new Date().getTime() / 1000 | 0).toString(16);
return timestamp + 'xxxxxxxxxxxxxxxx'.replace(/[x]/g, function() {
return (Math.random() * 16 | 0).toString(16);
}).toLowerCase();
};
console.log("New Object Id: "+mongoObjectId());
This example has been adapted from the Parse Server Object Id implementation.
let randomBytes = function(n) {
let a = new Uint8Array(n), QUOTA = 65536;
for (let i = 0; i < n; i += QUOTA) {
crypto.getRandomValues(a.subarray(i, i + Math.min(n - i, QUOTA)));
}
return a;
};
let parseObjectId = function (size = 10) {
if (size === 0) {
throw new Error('Zero-length randomString is useless.');
}
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + 'abcdefghijklmnopqrstuvwxyz' + '0123456789';
let objectId = '';
const bytes = randomBytes(size);
for (let i = 0; i < bytes.length; ++i) {
let number = bytes[i]
objectId += chars[number % chars.length];
}
return objectId;
}
console.log("New Parse Object Id: "+parseObjectId());
Each of their IDs consists 1) of: