Search documentation of phoenix_kit

# PhoenixKit.Settings.Queries (phoenix_kit v1.7.87)

Copy Markdown View Source

Ecto queries for Settings context.

This module encapsulates all database queries for settings management, providing a centralized location for query logic.

## Summary

### Functions

get_setting_by_key(key)

Gets a setting record by key.

insert_setting(changeset)

Inserts a new setting.

list_settings()

Lists all settings ordered by key.

list_settings_by_keys(keys)

Lists setting records for specific keys.

list_settings_key_values()

Gets all settings as a list of {key, value} tuples.

list_settings_key_values_by_keys(keys)

Lists settings for specific keys as a list of {key, value} tuples.

list_settings_with_json_priority_by_keys(keys)

Lists settings by keys with JSON priority as a list of {key, value} tuples.

transaction(multi)

Executes a transaction with multiple operations.

update_setting(changeset)

Updates an existing setting.

## Functions

# get_setting_by_key(key)

Gets a setting record by key.

## Examples

    iex> PhoenixKit.Settings.Queries.get_setting_by_key("time_zone")
    %Setting{key: "time_zone", value: "0"}

    iex> PhoenixKit.Settings.Queries.get_setting_by_key("non_existent")
    nil

# insert_setting(changeset)

Inserts a new setting.

## Examples

    iex> %Setting{} |> Setting.changeset(%{key: "theme", value: "dark"})
    ...> |> PhoenixKit.Settings.Queries.insert_setting()
    {:ok, %Setting{}}

# list_settings()

Lists all settings ordered by key.

## Examples

    iex> PhoenixKit.Settings.Queries.list_settings()
    [%Setting{key: "date_format", value: "Y-m-d"}, %Setting{key: "time_zone", value: "0"}, ...]

# list_settings_by_keys(keys)

Lists setting records for specific keys.

## Examples

    iex> PhoenixKit.Settings.Queries.list_settings_by_keys(["time_zone"])
    [%Setting{key: "time_zone", value: "0"}]

# list_settings_key_values()

Gets all settings as a list of {key, value} tuples.

## Examples

    iex> PhoenixKit.Settings.Queries.list_settings_key_values()
    [{"time_zone", "0"}, {"date_format", "Y-m-d"}]

# list_settings_key_values_by_keys(keys)

Lists settings for specific keys as a list of {key, value} tuples.

## Examples

    iex> PhoenixKit.Settings.Queries.list_settings_key_values_by_keys(["time_zone", "date_format"])
    [{"time_zone", "0"}, {"date_format", "Y-m-d"}]

# list_settings_with_json_priority_by_keys(keys)

Lists settings by keys with JSON priority as a list of {key, value} tuples.

Returns a list where value_json is used if present, otherwise falls back to the string value.

## Examples

    iex> PhoenixKit.Settings.Queries.list_settings_with_json_priority_by_keys(["theme"])
    [{"theme", %{"primary" => "#3b82f6"}}]

# transaction(multi)

Executes a transaction with multiple operations.

## Examples

    iex> Ecto.Multi.new()
    ...> |> multi_operation()
    ...> |> PhoenixKit.Settings.Queries.transaction()
    {:ok, result}

# update_setting(changeset)

Updates an existing setting.

## Examples

    iex> setting |> Setting.update_changeset(%{value: "light"})
    ...> |> PhoenixKit.Settings.Queries.update_setting()
    {:ok, %Setting{}}