PhoenixKit.Modules.Sync.DataImporter (phoenix_kit v1.7.63)

Copy Markdown View Source

Handles import of records from remote sender with conflict resolution.

This module is responsible for importing records received from a sender into the local database, handling primary key conflicts according to the configured strategy.

Conflict Strategies

  • :skip - Skip import if record with same primary key exists (default)
  • :overwrite - Replace existing record with imported data
  • :merge - Merge imported data with existing record (keeps existing values where new is nil)
  • :append - Always insert as new record with auto-generated ID (ignores source primary key)

Usage

# Import a batch of records with a strategy
{:ok, result} = DataImporter.import_records("users", records, :skip)

# Result structure:
%{
  created: 5,
  updated: 2,
  skipped: 3,
  errors: []
}

Summary

Functions

Imports records for multiple tables in a single operation.

Imports a batch of records into the specified table.

Types

conflict_strategy()

@type conflict_strategy() :: :skip | :overwrite | :merge | :append

import_result()

@type import_result() :: %{
  created: non_neg_integer(),
  updated: non_neg_integer(),
  skipped: non_neg_integer(),
  errors: list()
}

Functions

import_multiple(table_records, strategies \\ %{})

@spec import_multiple(map(), map()) :: {:ok, map()} | {:error, term()}

Imports records for multiple tables in a single operation.

Parameters

  • table_records - Map of table name to records list
  • strategies - Map of table name to conflict strategy

Returns

  • {:ok, %{table_name => result}}

import_records(table, records, strategy \\ :skip)

@spec import_records(String.t(), [map()], conflict_strategy()) ::
  {:ok, import_result()} | {:error, term()}

Imports a batch of records into the specified table.

Parameters

  • table - The table name to import into
  • records - List of record maps from the sender
  • strategy - Conflict resolution strategy (:skip, :overwrite, :merge, :append)

Returns

  • {:ok, result} with counts and any errors
  • {:error, reason} if import fails completely