randomUuid
此内容尚不支持你的语言。
randomUuid(
exists?):string
Defined in: src/util/RandomString.ts:181
A random version-4 UUID — 'f81d4fae-7dec-41d0-a765-00a0c91e6bf6'.
The counterpart to randomId. That one names something inside one
process and only has to be unguessable among the names that process holds live
at once, which is why ~48 bits are plenty. This one has to stay distinct from
identifiers minted by other processes, on other machines, years apart, with
nothing coordinating them: a PersistenceId for a new aggregate, a
correlation id carried across a broker, a key some other system will read
later. 122 random bits are what makes that hold without a coordinator.
Delegates to globalThis.crypto.randomUUID() instead of assembling one from
randomHex, because six of the 128 bits are not entropy: RFC 9562 fixes
a version and a variant field, and a hex string with dashes inserted in the
right places merely looks like a UUID — anything that parses a version out
of it reads garbage. Delegating gets that right by construction, and keeps
the choice of primitive in the one file that owns where identifiers come from
rather than re-made per call site.
No length parameter, deliberately: a UUID is 36 characters, and cutting one
down is the exact mistake this module exists to make unnecessary — reach for
randomHex or randomId when you want n characters.
exists is the same optional collision check as on the other three, and is
the only argument this one has — there is no length to precede it. Needing it
on 122 random bits is rare, and that is fine: whether an identifier is unique
in the world and whether it is free in your table are different questions,
and a PersistenceId has to answer both.
Parameters
Section titled “Parameters”exists?
Section titled “exists?”Returns
Section titled “Returns”string
