feat(member): resolve custom-field filters and sorting in PostgreSQL
Push the boolean and date custom-field predicates and custom-field sorting down to JSONB expressions so the overview stops classifying custom-field values in memory. A GIN index on custom_field_values.value keeps the boolean membership predicates index-served.
This commit is contained in:
parent
e64f55c36a
commit
b09cdf7f3a
9 changed files with 883 additions and 5 deletions
|
|
@ -48,6 +48,13 @@ defmodule Mv.Membership.CustomFieldValue do
|
|||
table "custom_field_values"
|
||||
repo Mv.Repo
|
||||
|
||||
custom_indexes do
|
||||
# GIN index on the JSONB value to serve boolean custom-field containment
|
||||
# predicates (`value @> '{"value": <bool>}'`) used by the member overview.
|
||||
# Default jsonb_ops supports the `@>` operator we rely on.
|
||||
index ["value"], name: "custom_field_values_value_gin_index", using: "gin"
|
||||
end
|
||||
|
||||
references do
|
||||
reference :member, on_delete: :delete
|
||||
reference :custom_field, on_delete: :delete
|
||||
|
|
@ -133,6 +140,16 @@ defmodule Mv.Membership.CustomFieldValue do
|
|||
|
||||
calculations do
|
||||
calculate :value_to_string, :string, expr(value[:value] <> "")
|
||||
|
||||
# Typed sort keys for DB-side ordering of the member overview by a custom
|
||||
# field. The stored JSONB value (`{"type": ..., "value": ...}`) is cast per
|
||||
# the field's value type so integers sort numerically and dates
|
||||
# chronologically. Empty strings collapse to NULL so they sort last, matching
|
||||
# the previous in-memory `CustomFieldSort` behaviour.
|
||||
calculate :sort_text, :string, expr(fragment("nullif(?->>'value','')", value))
|
||||
calculate :sort_numeric, :decimal, expr(fragment("nullif(?->>'value','')::numeric", value))
|
||||
calculate :sort_date, :date, expr(fragment("nullif(?->>'value','')::date", value))
|
||||
calculate :sort_boolean, :boolean, expr(fragment("nullif(?->>'value','')::boolean", value))
|
||||
end
|
||||
|
||||
# Ensure a member can only have one custom field value per custom field
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue