Skip to content

Commit 4bc8da8

Browse files
Update Ecto.UUID.generate/1 docs
1 parent f27cf18 commit 4bc8da8

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

lib/ecto/uuid.ex

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,48 @@ defmodule Ecto.UUID do
215215

216216
@default_version 4
217217
@doc """
218-
Generates a uuid with the given options.
218+
Generates a UUID string.
219+
220+
## Options
221+
222+
* `:version` - The UUID version to generate. Supported values are `4` (random)
223+
and `7` (time-ordered). Defaults to `4`.
224+
225+
## Options (version 7)
226+
227+
* `:precision` - The timestamp precision for version 7 UUIDs. Supported values
228+
are `:millisecond` and `:nanosecond`. Defaults to `:millisecond`. When using
229+
`:nanosecond`, the sub-millisecond precision is encoded in the `rand_a` field.
230+
Note that due to the 12-bit space available, nanosecond precision is limited
231+
to 4096 (2^12) distinct values per millisecond.
232+
233+
* `:monotonic` - When `true`, ensures that generated version 7 UUIDs are
234+
strictly monotonically increasing, even when multiple UUIDs are generated
235+
within the same timestamp. This is useful for maintaining insertion order
236+
in databases. Defaults to `false`.
237+
238+
## Examples
239+
240+
iex> Ecto.UUID.generate()
241+
"a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11"
242+
243+
iex> Ecto.UUID.generate(version: 7)
244+
"018ec4c1-ae46-7f5a-8f5a-6f5a8f5a6f5a"
245+
246+
iex> Ecto.UUID.generate(version: 7, precision: :nanosecond)
247+
"018ec4c1-ae46-7f5a-8f5a-6f5a8f5a6f5a"
248+
249+
iex> Ecto.UUID.generate(version: 7, monotonic: true)
250+
"018ec4c1-ae46-7f5a-8f5a-6f5a8f5a6f5a"
251+
219252
"""
220253
@spec generate() :: t
221254
@spec generate(options) :: t
222255
def generate(opts \\ []), do: encode(bingenerate(opts))
223256

224257
@doc """
225258
Generates a uuid with the given options in binary format.
259+
See `generate/1` for options.
226260
"""
227261
@spec bingenerate(options) :: raw
228262
def bingenerate(opts \\ []) do

0 commit comments

Comments
 (0)