Search documentation of phoenix_kit

# PhoenixKit.Modules.DB (phoenix_kit v1.7.87)

Copy Markdown View Source

Database helpers for PhoenixKit.

Provides metadata, stats, and paginated previews for Postgres tables so the admin UI can browse data without exposing full SQL access.

## Live Updates

The DB module supports real-time updates via PostgreSQL LISTEN/NOTIFY. When a table is being viewed, changes to that table trigger automatic refreshes.

This requires:

1. The `DB.Listener` GenServer running (started via Application supervisor)
2. A notification trigger on the table being viewed

Use `ensure_trigger/2` to set up the trigger for a table.

## Summary

### Functions

database_stats()

Aggregated Postgres stats for all user tables.

disable_system()

Disable the DB explorer module.

enable_system()

Enable the DB explorer module.

enabled?()

Whether the DB explorer module is enabled.

ensure_trigger(schema, table)

Ensures the notification function exists and creates a trigger on the table.

fetch_row(schema, table, row_id)

Fetches a single row by ID from a table. Returns {:ok, row_map} or {:error, :not_found}.

get_config()

Returns configuration metadata for the admin dashboard card.

has_trigger?(schema, table)

Checks if a table has a notification trigger installed.

list_tables(opts \\ %{})

Lists tables + stats with pagination and search.

list_triggered_tables()

Lists all tables that have notification triggers installed.

remove_all_triggers()

Removes all notification triggers from all tables.

remove_trigger(schema, table)

Removes the notification trigger from a table.

table_preview(schema, table, opts \\ %{})

Returns table metadata and row preview.

## Functions

# database_stats()

Aggregated Postgres stats for all user tables.

# disable_system()

Disable the DB explorer module.

# enable_system()

Enable the DB explorer module.

# enabled?()

Whether the DB explorer module is enabled.

# ensure_trigger(schema, table)

Ensures the notification function exists and creates a trigger on the table.

This sets up PostgreSQL LISTEN/NOTIFY for live updates. The trigger fires on INSERT, UPDATE, or DELETE and sends a notification to the `phoenix_kit_db_changes` channel.

Returns `:ok` on success or `{:error, reason}` on failure.

# fetch_row(schema, table, row_id)

Fetches a single row by ID from a table. Returns {:ok, row_map} or {:error, :not_found}.

# get_config()

Returns configuration metadata for the admin dashboard card.

# has_trigger?(schema, table)

Checks if a table has a notification trigger installed.

# list_tables(opts \\ %{})

Lists tables + stats with pagination and search.

# list_triggered_tables()

Lists all tables that have notification triggers installed.

# remove_all_triggers()

Removes all notification triggers from all tables.

# remove_trigger(schema, table)

Removes the notification trigger from a table.

# table_preview(schema, table, opts \\ %{})

Returns table metadata and row preview.