SimpleSchemaContext.Schema.Behaviour behaviour (simple_schema_context v0.1.0)
View SourceDefines the behaviour that all schemas using SimpleSchemaContext must implement.
This module specifies the contract for common database operations like listing, fetching, creating, updating, and deleting entities. It ensures that all schemas using SimpleSchemaContext provide a consistent API.
Summary
Types
Represents an entity or schema record, which can be a struct or a tuple of structs.
The ID of an entity, which can be an integer or a binary (UUID).
Options that can be passed to the various callback functions.
Callbacks
Creates a changeset for the given entity and attributes.
Creates a changeset for validating the given entity with the provided attributes.
Creates a new entity with the given attributes.
Deletes the given entity.
Fetches a single entity by the given criteria.
Returns a keyword list of all associations to preload for this entity.
Gets a single entity by ID. Raises an error if not found.
Returns a list of all entities.
Returns a filtered list of entities based on provided filters.
Returns a list of entities with the given IDs.
Updates an existing entity with the given attributes.
Types
Represents an entity or schema record, which can be a struct or a tuple of structs.
@type entity_id() :: pos_integer() | binary()
The ID of an entity, which can be an integer or a binary (UUID).
@type options() :: keyword()
Options that can be passed to the various callback functions.
Callbacks
@callback change(entity(), map(), options()) :: Ecto.Changeset.t()
Creates a changeset for the given entity and attributes.
@callback changeset(entity(), map()) :: Ecto.Changeset.t()
Creates a changeset for validating the given entity with the provided attributes.
@callback create(map(), options()) :: {:ok, entity()} | {:error, Ecto.Changeset.t()}
Creates a new entity with the given attributes.
Returns {:ok, entity}
if successful, or {:error, changeset}
if validation fails.
@callback delete(entity(), options()) :: {:ok, entity()} | {:error, Ecto.Changeset.t()}
Deletes the given entity.
Returns {:ok, entity}
if successful, or {:error, changeset}
if deletion fails.
Fetches a single entity by the given criteria.
Returns {:ok, entity}
if found, or {:error, :not_found}
if not found.
@callback full_preload() :: keyword()
Returns a keyword list of all associations to preload for this entity.
Used for tests and similar places where we don't care about database performance.
Gets a single entity by ID. Raises an error if not found.
@callback list() :: [entity()]
Returns a list of all entities.
Returns a filtered list of entities based on provided filters.
Filters can include:
- Simple equality:
%{"name" => "John"}
- Association filters:
%{"author.name" => "Jane"}
- Complex operators:
%{"age" => ["gt", 30]}
Options can include:
:preload
- associations to preload:limit
- maximum number of records to return:order_by
- sorting options:transformer
- function to transform results
Returns a list of entities with the given IDs.
Options are the same as for list/2
.
@callback update(entity(), map(), options()) :: {:ok, entity()} | {:error, Ecto.Changeset.t()}
Updates an existing entity with the given attributes.
Returns {:ok, entity}
if successful, or {:error, changeset}
if validation fails.