PhoenixKit.Entities.EntityData (phoenix_kit v1.5.1)
View SourceEntity 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 blueprinttitle: Display title/name for the recordslug: URL-friendly identifier (optional)status: Record status ("draft", "published", "archived")data: JSONB map of all field values based on entity definitionmetadata: JSONB map for additional information (tags, categories, etc.)created_by: User ID who created the recorddate_created: When the record was createddate_updated: When the record was last modified
Core Functions
Data Management
list_all/0- Get all entity data recordslist_by_entity/1- Get all records for a specific entitylist_by_entity_and_status/2- Filter records by entity and statusget!/1- Get a record by ID (raises if not found)get_by_slug/2- Get a record by entity and slugcreate/1- Create a new recordupdate/2- Update an existing recorddelete/1- Delete a recordchange/2- Get changeset for forms
Query Helpers
search_by_title/2- Search records by titlefilter_by_status/1- Get records by statuscount_by_entity/1- Count records for an entitypublished_records/1- Get all published records for an entity
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
Returns an %Ecto.Changeset{} for tracking entity data changes.
Examples
iex> PhoenixKit.Entities.EntityData.change(record)
%Ecto.Changeset{data: %PhoenixKit.Entities.EntityData{}}
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.
Counts the total number of records for an entity.
Examples
iex> PhoenixKit.Entities.EntityData.count_by_entity(1)
42
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{}}
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{}}
Alias for delete/1 for consistency with LiveView naming.
Gets records filtered by status across all entities.
Examples
iex> PhoenixKit.Entities.EntityData.filter_by_status("draft")
[%PhoenixKit.Entities.EntityData{status: "draft"}, ...]
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)
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
Alias for get!/1 for consistency with LiveView naming.
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
}
Returns all entity data records ordered by creation date.
Examples
iex> PhoenixKit.Entities.EntityData.list_all()
[%PhoenixKit.Entities.EntityData{}, ...]
Alias for list_all/0 for consistency with LiveView naming.
Returns all entity data records for a specific entity.
Examples
iex> PhoenixKit.Entities.EntityData.list_by_entity(1)
[%PhoenixKit.Entities.EntityData{entity_id: 1}, ...]
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"}, ...]
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.
Examples
iex> PhoenixKit.Entities.EntityData.published_records(1)
[%PhoenixKit.Entities.EntityData{status: "published"}, ...]
Searches entity data records by title.
Examples
iex> PhoenixKit.Entities.EntityData.search_by_title("First Post", 1)
[%PhoenixKit.Entities.EntityData{}, ...]
Alias for search_by_title/1 for consistency with LiveView naming.
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{}}
Alias for update/2 for consistency with LiveView naming.