uuidv7
    Preparing search index...

    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.

    Index

    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 (by default, 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 generateOrResetWithTs for the variant accepting a custom timestamp.

      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 (by default, more than ten seconds), this method aborts and returns undefined immediately.

      See generate for the other mode of generation and generateOrAbortWithTs for the variant accepting a custom timestamp.

      Returns UUID | undefined

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

      This method is a deprecated version of generateOrAbortWithTs that accepts the rollbackAllowance parameter as an argument, rather than using the generator-level parameter.

      Parameters

      • unixTsMs: number
      • rollbackAllowance: number

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

      Returns UUID | undefined

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

      Since v1.2.0. Use generateOrAbortWithTs instead.

    • 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.

      Parameters

      • unixTsMs: number

      Returns UUID | undefined

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

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

      This method is a deprecated version of generateOrResetWithTs that accepts the rollbackAllowance parameter as an argument, rather than using the generator-level parameter.

      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 unsigned integer.

      Since v1.2.0. Use generateOrResetWithTs instead.

    • 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.

      Parameters

      • unixTsMs: number

      Returns UUID

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

    • Sets the rollbackAllowance parameter of the generator.

      The rollbackAllowance parameter specifies the amount of unixTsMs rollback that is considered significant. The default value is 10_000 (milliseconds). See the generate or generateOrAbort documentation for the treatment of the significant rollback.

      Parameters

      • rollbackAllowance: number

      Returns void