PhoenixKit.Modules.Entities.Mirror.Storage (phoenix_kit v1.7.71)

Copy Markdown View Source

Filesystem storage operations for entity mirror/export system.

Stores exported JSON files in the parent app's priv/entities/ directory. Each entity is stored as a single file containing both the definition and all data records.

Directory Structure

priv/entities/
  brand.json       # Contains definition + all data records
  product.json     # Contains definition + all data records

File Format

{
  "export_version": "1.0",
  "exported_at": "2025-12-11T10:30:00Z",
  "definition": { ... entity schema ... },
  "data": [ ... array of data records ... ]
}

Configuration

The export path can be configured via settings:

  • entities_mirror_path - Custom path (empty = use default priv/entities/)

Summary

Functions

Checks if entity data mirroring is enabled.

Returns the default storage path in the parent app's priv directory.

Checks if entity definitions mirroring is enabled.

Deletes an entity file.

Disables entity data mirroring.

Disables entity definitions mirroring.

Enables entity data mirroring.

Enables entity definitions mirroring.

Ensures the root directory exists.

Checks if a file exists for the given entity.

Returns the file path for a specific entity.

Returns statistics about exported files.

Lists all exported entity names.

Reads an entity file containing definition and data.

Returns the root path for entity mirror storage.

Writes an entity file containing definition and optionally data.

Functions

data_enabled?()

@spec data_enabled?() :: boolean()

Checks if entity data mirroring is enabled.

default_path()

@spec default_path() :: String.t()

Returns the default storage path in the parent app's priv directory.

definitions_enabled?()

@spec definitions_enabled?() :: boolean()

Checks if entity definitions mirroring is enabled.

delete_entity(entity_name)

@spec delete_entity(String.t()) :: :ok | {:error, term()}

Deletes an entity file.

disable_data()

@spec disable_data() :: {:ok, any()} | {:error, any()}

Disables entity data mirroring.

disable_definitions()

@spec disable_definitions() :: {:ok, any()} | {:error, any()}

Disables entity definitions mirroring.

enable_data()

@spec enable_data() :: {:ok, any()} | {:error, any()}

Enables entity data mirroring.

enable_definitions()

@spec enable_definitions() :: {:ok, any()} | {:error, any()}

Enables entity definitions mirroring.

ensure_directory()

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

Ensures the root directory exists.

entity_exists?(entity_name)

@spec entity_exists?(String.t()) :: boolean()

Checks if a file exists for the given entity.

entity_path(entity_name)

@spec entity_path(String.t()) :: String.t()

Returns the file path for a specific entity.

get_stats()

@spec get_stats() :: map()

Returns statistics about exported files.

Returns

Map with:

  • definitions_count - Number of exported entity files
  • data_count - Total number of data records across all entities
  • entities_with_data - List of entity names that have data records
  • last_export - Timestamp of most recent export (nil if no files)

list_entities()

@spec list_entities() :: [String.t()]

Lists all exported entity names.

Returns a list of entity names (without .json extension).

read_entity(entity_name)

@spec read_entity(String.t()) :: {:ok, map()} | {:error, term()}

Reads an entity file containing definition and data.

Parameters

  • entity_name - The entity name

Returns

  • {:ok, map} with decoded JSON on success
  • {:error, reason} on failure

root_path()

@spec root_path() :: String.t()

Returns the root path for entity mirror storage.

Uses custom path from settings if configured, otherwise defaults to the parent app's priv/entities/ directory.

write_entity(entity_name, content)

@spec write_entity(String.t(), map()) :: {:ok, String.t()} | {:error, term()}

Writes an entity file containing definition and optionally data.

Parameters

  • entity_name - The entity name (used as filename)
  • content - The full content map with definition and data

Returns

  • {:ok, file_path} on success
  • {:error, reason} on failure