Selecto.Jsonb (Selecto v0.4.3)
JSONB column support with dot notation for filtering and selection.
Provides structured JSONB column definitions with schemas that enable:
- Dot notation access:
"attributes.color"→attributes->>'color' - Type-aware comparisons with automatic casting
- Contains/exists operations for JSONB-specific queries
- Nested object and array support
domain-configuration
Domain Configuration
columns: %{
"attributes" => %{
type: :jsonb,
schema: %{
"color" => %{type: :string, required: true},
"size" => %{type: :string, enum: ["small", "medium", "large"]},
"weight" => %{type: :decimal},
"dimensions" => %{
type: :object,
schema: %{
"length" => %{type: :decimal},
"width" => %{type: :decimal}
}
},
"tags" => %{type: :array, items: %{type: :string}}
}
}
}
filtering-examples
Filtering Examples
# Equality on JSONB path
Selecto.filter(selecto, {"attributes.color", "red"})
# Comparison operators
Selecto.filter(selecto, {"attributes.weight", {:gt, 10.0}})
# JSONB contains
Selecto.filter(selecto, {"attributes", {:jsonb_contains, %{"color" => "red"}}})
# Array contains
Selecto.filter(selecto, {"attributes.tags", {:contains, "featured"}})
# Key exists
Selecto.filter(selecto, {"attributes.warranty", :exists})
Link to this section Summary
Functions
Build a JSONB array contains check expression.
Build a JSONB array contains all check expression.
Build a JSONB containment check expression.
Build a PostgreSQL JSONB extraction expression.
Build a JSONB key exists check expression.
Get the JSONB schema for a path within a column.
Check if a column is a JSONB type in the domain.
Parse a field reference that may contain JSONB dot notation.
Determine the PostgreSQL cast type for a JSONB schema type.
Link to this section Functions
build_array_contains(column, path, value, opts \\ [])
Build a JSONB array contains check expression.
examples
Examples
iex> build_array_contains("attributes", ["tags"], "featured")
~s("attributes"->'tags' ? 'featured')
iex> build_array_contains("attributes", ["tags"], ["featured", "new"])
~s("attributes"->'tags' ?| array['featured','new'])
build_array_contains_all(column, path, values, opts \\ [])
Build a JSONB array contains all check expression.
examples
Examples
iex> build_array_contains_all("attributes", ["tags"], ["featured", "new"])
~s("attributes"->'tags' ?& array['featured','new'])
build_contains(column, value, opts \\ [])
Build a JSONB containment check expression.
examples
Examples
iex> build_contains("attributes", %{"color" => "red"})
~s("attributes" @> '{"color":"red"}'::jsonb)
build_extraction(column, path, opts \\ [])
Build a PostgreSQL JSONB extraction expression.
options
Options
:as_text- Use->>for text extraction (default: true for leaf values):cast- Cast to specific type after extraction
examples
Examples
iex> build_extraction("attributes", ["color"], as_text: true)
~s("attributes"->>'color')
iex> build_extraction("attributes", ["dimensions", "length"], cast: :decimal)
~s(("attributes"#>>'{dimensions,length}')::numeric)
build_key_exists(column, key_or_path, opts \\ [])
Build a JSONB key exists check expression.
examples
Examples
iex> build_key_exists("attributes", "color")
~s("attributes" ? 'color')
iex> build_key_exists("attributes", ["dimensions", "length"])
~s("attributes"->'dimensions' ? 'length')
get_path_schema(domain, column, path)
Get the JSONB schema for a path within a column.
Returns the schema definition for the nested field, or nil if not defined.
jsonb_column?(domain, column)
Check if a column is a JSONB type in the domain.
parse_field_reference(field, domain)
Parse a field reference that may contain JSONB dot notation.
Returns {:jsonb, column, path} if it's a JSONB path, or {:regular, field} otherwise.
examples
Examples
iex> parse_field_reference("attributes.color", domain)
{:jsonb, "attributes", ["color"]}
iex> parse_field_reference("attributes.dimensions.length", domain)
{:jsonb, "attributes", ["dimensions", "length"]}
iex> parse_field_reference("name", domain)
{:regular, "name"}
pg_cast_for_type(arg1)
Determine the PostgreSQL cast type for a JSONB schema type.