TinyBase logoTinyBase

getUniqueId

The getUniqueId function returns a unique string of a given length.

getUniqueId(length?: number): Id
TypeDescription
length?number

The desired length of the unique Id, defaulting to 16.

returnsId

A unique Id of the required length.

This is used internally by TinyBase for the synchronizer protocol and for unique MergeableStore identifiers. But it is useful enough for it to be publicly exposed for purposes such as identifying shared collaboration rooms, or creating other Ids that need to be unique.

The string may contain numbers, lower or upper case letters, or the '-' or '_' characters. This makes them URL-safe, and means they can be identified with a regex like /[-_0-9A-Za-z]+/.

This function prefers to use the crypto module to generate random numbers, but where that is not available (such as in React Native), a Math.random implementation is used. Whilst that may not be cryptographically sound, it should suffice for most TinyBase-related purposes.

Example

This example creates two 8 character long Ids and compares them.

import {getUniqueId} from 'tinybase';

const id1 = getUniqueId(8);
const id2 = getUniqueId(8);

console.log(id1.length);
// -> 8
console.log(id2.length);
// -> 8
console.log(id1 == id2);
// -> false

Since

v5.0.0