PhoenixKit.Entities.EntityData (phoenix_kit v1.5.1)

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

Query Helpers

Usage Examples

# Create a blog post data record
{:ok, data} = PhoenixKit.Entities.EntityData.create(%{
  entity_id: blog_entity.id,
  title: "My First Post",
  slug: "my-first-post",
  status: "published",
  created_by: user.id,
  data: %{
    "title" => "My First Post",
    "excerpt" => "This is my first blog post",
    "content" => "<p>Blog content here</p>",
    "category" => "Tech",
    "publish_date" => "2025-09-30",
    "featured" => true
  },
  metadata: %{
    "tags" => ["elixir", "phoenix"],
    "author_name" => "John Doe"
  }
})

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

# Search by title
results = PhoenixKit.Entities.EntityData.search_by_title("First Post", blog_entity.id)

Summary

Functions

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

Creates a changeset for entity data creation and updates.

Counts the total number of records for an entity.

Creates an entity data record.

Deletes an entity data record.

Alias for delete/1 for consistency with LiveView naming.

Gets records filtered by status across all entities.

Gets a single entity data record by ID.

Gets a single entity data record by entity and slug.

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

Gets statistical data about entity data records.

Returns all entity data records ordered by creation date.

Alias for list_all/0 for consistency with LiveView naming.

Returns all entity data records for a specific entity.

Returns entity data records filtered by entity and status.

Alias for list_by_entity/1 for consistency with LiveView naming.

Alias for filter_by_status/1 for consistency with LiveView naming.

Gets all published records for a specific entity.

Searches entity data records by title.

Alias for search_by_title/1 for consistency with LiveView naming.

Updates an entity data record.

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{}}

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.

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, "my-first-post")
%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("First Post", 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.