Search documentation of phoenix_kit

# PhoenixKit.Entities.EntityData (phoenix_kit v1.6.19)

View Source

Entity data records for PhoenixKit entities system.

This module manages actual data records that follow entity blueprints. Each record is associated with an entity type and stores its field values in a JSONB column for flexibility.

## Schema Fields

- `entity_id`: Foreign key to the entity blueprint
- `title`: Display title/name for the record
- `slug`: URL-friendly identifier (optional)
- `status`: Record status ("draft", "published", "archived")
- `data`: JSONB map of all field values based on entity definition
- `metadata`: JSONB map for additional information (tags, categories, etc.)
- `created_by`: User ID who created the record
- `date_created`: When the record was created
- `date_updated`: When the record was last modified

## Core Functions

### Data Management

- `list_all/0` - Get all entity data records
- `list_by_entity/1` - Get all records for a specific entity
- `list_by_entity_and_status/2` - Filter records by entity and status
- `get!/1` - Get a record by ID (raises if not found)
- `get_by_slug/2` - Get a record by entity and slug
- `create/1` - Create a new record
- `update/2` - Update an existing record
- `delete/1` - Delete a record
- `change/2` - Get changeset for forms

### Query Helpers

- `search_by_title/2` - Search records by title
- `filter_by_status/1` - Get records by status
- `count_by_entity/1` - Count records for an entity
- `published_records/1` - Get all published records for an entity

## Usage Examples

    # Create a brand data record
    {:ok, data} = PhoenixKit.Entities.EntityData.create(%{
      entity_id: brand_entity.id,
      title: "Acme Corporation",
      slug: "acme-corporation",
      status: "published",
      created_by: user.id,
      data: %{
        "name" => "Acme Corporation",
        "tagline" => "Quality products since 1950",
        "description" => "<p>Leading manufacturer of innovative products</p>",
        "industry" => "Manufacturing",
        "founded_date" => "1950-03-15",
        "featured" => true
      },
      metadata: %{
        "tags" => ["manufacturing", "industrial"],
        "contact_email" => "info@acme.com"
      }
    })

    # Get all records for an entity
    records = PhoenixKit.Entities.EntityData.list_by_entity(brand_entity.id)

    # Search by title
    results = PhoenixKit.Entities.EntityData.search_by_title("Acme", brand_entity.id)

# Summary

## Functions

change(entity_data, attrs \\ %{})

Returns an `%Ecto.Changeset{}` for tracking entity data changes.

changeset(entity_data, attrs)

Creates a changeset for entity data creation and updates.

count_by_entity(entity_id)

Counts the total number of records for an entity.

create(attrs \\ %{})

Creates an entity data record.

delete(entity_data)

Deletes an entity data record.

delete_data(entity_data)

Alias for delete/1 for consistency with LiveView naming.

filter_by_status(status)

Gets records filtered by status across all entities.

get(id)

Gets a single entity data record by ID.

get!(id)

Gets a single entity data record by ID.

get_by_slug(entity_id, slug)

Gets a single entity data record by entity and slug.

get_data!(id)

Alias for get!/1 for consistency with LiveView naming.

get_data_stats(entity_id \\ nil)

Gets statistical data about entity data records.

list_all()

Returns all entity data records ordered by creation date.

list_all_data()

Alias for list_all/0 for consistency with LiveView naming.

list_by_entity(entity_id)

Returns all entity data records for a specific entity.

list_by_entity_and_status(entity_id, status)

Returns entity data records filtered by entity and status.

list_data_by_entity(entity_id)

Alias for list_by_entity/1 for consistency with LiveView naming.

list_data_by_status(status)

Alias for filter_by_status/1 for consistency with LiveView naming.

published_records(entity_id)

Gets all published records for a specific entity.

search_by_title(search_term, entity_id \\ nil)

Searches entity data records by title.

search_data(search_term)

Alias for search_by_title/1 for consistency with LiveView naming.

update(entity_data, attrs)

Updates an entity data record.

update_data(entity_data, attrs)

Alias for update/2 for consistency with LiveView naming.

# Functions

# change(entity_data, attrs \\ %{})

Returns an `%Ecto.Changeset{}` for tracking entity data changes.

## Examples

    iex> PhoenixKit.Entities.EntityData.change(record)
    %Ecto.Changeset{data: %PhoenixKit.Entities.EntityData{}}

# changeset(entity_data, attrs)

Creates a changeset for entity data creation and updates.

Validates that entity exists, title is present, and data validates against entity definition. Automatically sets date_created on new records.

# count_by_entity(entity_id)

Counts the total number of records for an entity.

## Examples

    iex> PhoenixKit.Entities.EntityData.count_by_entity(1)
    42

# create(attrs \\ %{})

Creates an entity data record.

## Examples

    iex> PhoenixKit.Entities.EntityData.create(%{entity_id: 1, title: "Test"})
    {:ok, %PhoenixKit.Entities.EntityData{}}

    iex> PhoenixKit.Entities.EntityData.create(%{title: ""})
    {:error, %Ecto.Changeset{}}

Note: `created_by` is auto-filled with the first admin or user ID if not provided, but only if at least one user exists in the system. If no users exist, the changeset will fail with a validation error on `created_by`.

# delete(entity_data)

Deletes an entity data record.

## Examples

    iex> PhoenixKit.Entities.EntityData.delete(record)
    {:ok, %PhoenixKit.Entities.EntityData{}}

    iex> PhoenixKit.Entities.EntityData.delete(record)
    {:error, %Ecto.Changeset{}}

# delete_data(entity_data)

Alias for delete/1 for consistency with LiveView naming.

# filter_by_status(status)

Gets records filtered by status across all entities.

## Examples

    iex> PhoenixKit.Entities.EntityData.filter_by_status("draft")
    [%PhoenixKit.Entities.EntityData{status: "draft"}, ...]

# get(id)

Gets a single entity data record by ID.

Returns the record if found, nil otherwise.

## Examples

    iex> PhoenixKit.Entities.EntityData.get(123)
    %PhoenixKit.Entities.EntityData{}

    iex> PhoenixKit.Entities.EntityData.get(456)
    nil

# get!(id)

Gets a single entity data record by ID.

Raises `Ecto.NoResultsError` if the record does not exist.

## Examples

    iex> PhoenixKit.Entities.EntityData.get!(123)
    %PhoenixKit.Entities.EntityData{}

    iex> PhoenixKit.Entities.EntityData.get!(456)
    ** (Ecto.NoResultsError)

# get_by_slug(entity_id, slug)

Gets a single entity data record by entity and slug.

Returns the record if found, nil otherwise.

## Examples

    iex> PhoenixKit.Entities.EntityData.get_by_slug(1, "acme-corporation")
    %PhoenixKit.Entities.EntityData{}

    iex> PhoenixKit.Entities.EntityData.get_by_slug(1, "invalid")
    nil

# get_data!(id)

Alias for get!/1 for consistency with LiveView naming.

# get_data_stats(entity_id \\ nil)

Gets statistical data about entity data records.

Returns statistics about total records, published, draft, and archived counts. Optionally filters by entity_id if provided.

## Examples

    iex> PhoenixKit.Entities.EntityData.get_data_stats()
    %{
      total_records: 150,
      published_records: 120,
      draft_records: 25,
      archived_records: 5
    }

    iex> PhoenixKit.Entities.EntityData.get_data_stats(1)
    %{
      total_records: 15,
      published_records: 12,
      draft_records: 2,
      archived_records: 1
    }

# list_all()

Returns all entity data records ordered by creation date.

## Examples

    iex> PhoenixKit.Entities.EntityData.list_all()
    [%PhoenixKit.Entities.EntityData{}, ...]

# list_all_data()

Alias for list_all/0 for consistency with LiveView naming.

# list_by_entity(entity_id)

Returns all entity data records for a specific entity.

## Examples

    iex> PhoenixKit.Entities.EntityData.list_by_entity(1)
    [%PhoenixKit.Entities.EntityData{entity_id: 1}, ...]

# list_by_entity_and_status(entity_id, status)

Returns entity data records filtered by entity and status.

## Examples

    iex> PhoenixKit.Entities.EntityData.list_by_entity_and_status(1, "published")
    [%PhoenixKit.Entities.EntityData{entity_id: 1, status: "published"}, ...]

# list_data_by_entity(entity_id)

Alias for list_by_entity/1 for consistency with LiveView naming.

# list_data_by_status(status)

Alias for filter_by_status/1 for consistency with LiveView naming.

# published_records(entity_id)

Gets all published records for a specific entity.

## Examples

    iex> PhoenixKit.Entities.EntityData.published_records(1)
    [%PhoenixKit.Entities.EntityData{status: "published"}, ...]

# search_by_title(search_term, entity_id \\ nil)

Searches entity data records by title.

## Examples

    iex> PhoenixKit.Entities.EntityData.search_by_title("Acme", 1)
    [%PhoenixKit.Entities.EntityData{}, ...]

# search_data(search_term)

Alias for search_by_title/1 for consistency with LiveView naming.

# update(entity_data, attrs)

Updates an entity data record.

## Examples

    iex> PhoenixKit.Entities.EntityData.update(record, %{title: "Updated"})
    {:ok, %PhoenixKit.Entities.EntityData{}}

    iex> PhoenixKit.Entities.EntityData.update(record, %{title: ""})
    {:error, %Ecto.Changeset{}}

# update_data(entity_data, attrs)

Alias for update/2 for consistency with LiveView naming.