PhoenixKit.Modules.Sync.DataExporter (phoenix_kit v1.7.71)

Copy Markdown View Source

Exports data from database tables for DB Sync module.

Provides functions to fetch records from tables with pagination, handle data serialization, and stream large datasets.

Security Considerations

  • Uses parameterized queries to prevent SQL injection
  • Validates table names against actual database tables
  • Respects configured table whitelist/blacklist (future feature)

Example

iex> DataExporter.get_count("users")
{:ok, 150}

iex> DataExporter.fetch_records("users", offset: 0, limit: 100)
{:ok, [
  %{"id" => 1, "email" => "user@example.com", ...},
  ...
]}

Summary

Functions

Fetches records from a table with pagination.

Gets the exact count of records in a table.

Exports all records from a table as a stream.

Functions

fetch_records(table_name, opts \\ [])

@spec fetch_records(
  String.t(),
  keyword()
) :: {:ok, [map()]} | {:error, any()}

Fetches records from a table with pagination.

Records are returned as maps with string keys matching column names. All values are JSON-serializable.

Options

  • :offset - Number of records to skip (default: 0)
  • :limit - Maximum records to return (default: 100, max: 1000)
  • :schema - Database schema (default: "public")
  • :order_by - Column(s) to order by (default: primary key)

get_count(table_name, opts \\ [])

@spec get_count(
  String.t(),
  keyword()
) :: {:ok, non_neg_integer()} | {:error, any()}

Gets the exact count of records in a table.

Options

  • :schema - Database schema (default: "public")

stream_records(table_name, opts \\ [])

@spec stream_records(
  String.t(),
  keyword()
) :: {:ok, Enumerable.t()} | {:error, any()}

Exports all records from a table as a stream.

Useful for large tables where loading all records into memory is not practical. Returns a stream that yields batches of records.

Options

  • :batch_size - Records per batch (default: 500)
  • :schema - Database schema (default: "public")