Skip to content

Commit

Permalink
Add Duration to docs (#698)
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-rychlewski authored Jul 27, 2024
1 parent 6756640 commit e5c3a34
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ iex> Postgrex.query!(pid, "INSERT INTO comments (user_id, text) VALUES (10, 'hey
| `timestamp` | `%NaiveDateTime{year: 2013, month: 10, day: 12, hour: 0, minute: 37, second: 14}` |
| `timestamptz` | `%DateTime{year: 2013, month: 10, day: 12, hour: 0, minute: 37, second: 14, time_zone: "Etc/UTC"}` (2) |
| `interval` | `%Postgrex.Interval{months: 14, days: 40, secs: 10920, microsecs: 315}` |
| `interval` | `%Duration{year: 1, month: 2, week: 5, day: 5, hour: 3, minute: 2, second: 0, microsecond: {315, 6}}` (3) |
| `array` | `[1, 2, 3]` |
| `composite type` | `{42, "title", "content"}` |
| `range` | `%Postgrex.Range{lower: 1, upper: 5}` |
| `multirange` | `%Postgrex.Multirange{ranges: [%Postgrex.Range{lower: 1, upper: 5}, %Postgrex.Range{lower: 20, upper: 23}]}` |
| `uuid` | `<<160,238,188,153,156,11,78,248,187,109,107,185,189,56,10,17>>` |
| `hstore` | `%{"foo" => "bar"}` |
| `oid types` | `42` |
| `enum` | `"ok"` (3) |
| `enum` | `"ok"` (4) |
| `bit` | `<< 1::1, 0::1 >>` |
| `varbit` | `<< 1::1, 0::1 >>` |
| `tsvector` | `[%Postgrex.Lexeme{positions: [{1, :A}], word: "a"}]` |
Expand All @@ -59,9 +60,11 @@ iex> Postgrex.query!(pid, "INSERT INTO comments (user_id, text) VALUES (10, 'hey

(2) Timezones will always be normalized to UTC or assumed to be UTC when no information is available, either by PostgreSQL or Postgrex

(3) Enumerated types (enum) are custom named database types with strings as values.
(3) `%Duration{}` may only be used with Elixir 1.17.0+. Intervals will only be decoded into a `%Duration{}` struct if the option `interval_decode_type: Duration` is passed to `Postgrex.Types.define/3`.

(4) Anonymous composite types are decoded (read) as tuples but they cannot be encoded (written) to the database
(4) Enumerated types (enum) are custom named database types with strings as values.

(5) Anonymous composite types are decoded (read) as tuples but they cannot be encoded (written) to the database

Postgrex does not automatically cast between types. For example, you can't pass a string where a date is expected. To add type casting, support new types, or change how any of the types above are encoded/decoded, you can use extensions.

Expand Down
8 changes: 8 additions & 0 deletions lib/postgrex/types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,14 @@ defmodule Postgrex.Types do
* `:moduledoc` - The moduledoc to be used for the generated module.
* `:allow_infinite_timestamps` - A boolean controlling whether or not
the built-in extensions `timestamp` and `timestamptz` will allow
a value of infinity to be decoded. Defaults to `false`.
* `:interval_decode_type` - The struct that intervals will be decoded
into. Either `Postgrex.Interval` or `Duration` (Elixir 1.17.0+ only).
Defaults to `Postgrex.Interval`.
"""
def define(module, extensions, opts \\ []) do
Postgrex.TypeModule.define(module, extensions, opts)
Expand Down

0 comments on commit e5c3a34

Please sign in to comment.