Class V7Generator

Encapsulates the monotonic counter state.

This class provides APIs to utilize a separate counter state from that of the global generator used by uuidv7 and uuidv7obj. In addition to the default generate method, this class has generateOrAbort that is useful to absolutely guarantee the monotonically increasing order of generated UUIDs. See their respective documentation for details.

Constructors

  • Creates a generator object with the default random number generator, or with the specified one if passed as an argument. The specified random number generator should be cryptographically strong and securely seeded.

    Parameters

    • OptionalrandomNumberGenerator: {
          nextUint32(): number;
      }
      • nextUint32:function
        • Returns a 32-bit random unsigned integer.

          Returns number

    Returns V7Generator

Methods

  • Generates a new UUIDv7 object from the current timestamp, or resets the generator upon significant timestamp rollback.

    This method returns a monotonically increasing UUID by reusing the previous timestamp even if the up-to-date timestamp is smaller than the immediately preceding UUID's. However, when such a clock rollback is considered significant (i.e., by more than ten seconds), this method resets the generator and returns a new UUID based on the given timestamp, breaking the increasing order of UUIDs.

    See generateOrAbort for the other mode of generation and generateOrResetCore for the low-level primitive.

    Returns UUID

  • Generates a new UUIDv7 object from the current timestamp, or returns undefined upon significant timestamp rollback.

    This method returns a monotonically increasing UUID by reusing the previous timestamp even if the up-to-date timestamp is smaller than the immediately preceding UUID's. However, when such a clock rollback is considered significant (i.e., by more than ten seconds), this method aborts and returns undefined immediately.

    See generate for the other mode of generation and generateOrAbortCore for the low-level primitive.

    Returns undefined | UUID

  • Generates a new UUIDv7 object from the unixTsMs passed, or returns undefined upon significant timestamp rollback.

    This method is equivalent to generateOrAbort except that it takes a custom timestamp and clock rollback allowance.

    Parameters

    • unixTsMs: number
    • rollbackAllowance: number

      The amount of unixTsMs rollback that is considered significant. A suggested value is 10_000 (milliseconds).

    Returns undefined | UUID

    RangeError if unixTsMs is not a 48-bit positive integer.

  • Generates a new UUIDv7 object from the unixTsMs passed, or resets the generator upon significant timestamp rollback.

    This method is equivalent to generate except that it takes a custom timestamp and clock rollback allowance.

    Parameters

    • unixTsMs: number
    • rollbackAllowance: number

      The amount of unixTsMs rollback that is considered significant. A suggested value is 10_000 (milliseconds).

    Returns UUID

    RangeError if unixTsMs is not a 48-bit positive integer.