CommBus.Storage.EctoAdapter (CommBus v0.2.0)

Copy Markdown View Source

Generic helper that persists CommBus entries and conversations using any Ecto repo.

The adapter expects a configuration map with the following keys:

  • :repo - Ecto repo module implementing insert_or_update/1, all/1, get/2, delete/1
  • :entry_schema - schema module with changeset/2 (or rely on Ecto.Changeset.change/2)
  • :conversation_schema - schema module for conversations

This module does not implement the behaviours directly; instead, concrete adapter modules (built by consuming applications on top of their own Ecto repos and schemas) delegate to it. See CommBus.Storage.EntryStore and CommBus.Storage.ConversationStore for the behaviours to adopt.

Summary

Functions

Deletes an entry by ID from the database.

Fetches a single entry by ID from the database.

Loads all entries from the database via the configured repo and schema, applying optional filters.

Fetches a conversation by ID from the database.

Persists a %CommBus.Conversation{} struct via the configured Ecto repo.

Persists a %CommBus.Entry{} struct via the configured Ecto repo, upserting the record through the entry schema's changeset.

Updates an existing conversation record with the given attributes.

Types

config()

@type config() :: %{
  repo: module(),
  entry_schema: module(),
  conversation_schema: module()
}

Functions

delete_entry(id, config)

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

Deletes an entry by ID from the database.

Parameters

  • id — The entry identifier.
  • config — Adapter config map.

Returns

:ok on success, or {:error, reason} on failure.

get_entry(id, config)

@spec get_entry(term(), config()) :: {:ok, CommBus.Entry.t()} | {:error, :not_found}

Fetches a single entry by ID from the database.

Parameters

  • id — The entry identifier.
  • config — Adapter config map.

Returns

{:ok, %CommBus.Entry{}} if found, or {:error, :not_found}.

list_entries(opts, config)

@spec list_entries(
  keyword(),
  config()
) :: {:ok, [CommBus.Entry.t()]} | {:error, term()}

Loads all entries from the database via the configured repo and schema, applying optional filters.

Parameters

  • opts — Keyword filters: :enabled, :mode, :keywords.
  • config — Adapter config map with :repo, :entry_schema, :conversation_schema.

Returns

{:ok, [%CommBus.Entry{}]} with the matching entries.

load_conversation(id, config)

@spec load_conversation(term(), config()) ::
  {:ok, CommBus.Conversation.t()} | {:error, :not_found}

Fetches a conversation by ID from the database.

Parameters

  • id — The conversation identifier.
  • config — Adapter config map.

Returns

{:ok, %CommBus.Conversation{}} if found, or {:error, :not_found}.

store_conversation(conversation, config)

@spec store_conversation(CommBus.Conversation.t(), config()) ::
  {:ok, CommBus.Conversation.t()} | {:error, term()}

Persists a %CommBus.Conversation{} struct via the configured Ecto repo.

Parameters

  • conversation — A %CommBus.Conversation{} struct to persist.
  • config — Adapter config map.

Returns

{:ok, %CommBus.Conversation{}} on success or {:error, reason} on failure.

store_entry(entry, config)

@spec store_entry(CommBus.Entry.t(), config()) ::
  {:ok, CommBus.Entry.t()} | {:error, term()}

Persists a %CommBus.Entry{} struct via the configured Ecto repo, upserting the record through the entry schema's changeset.

Parameters

  • entry — A %CommBus.Entry{} struct to persist.
  • config — Adapter config map with :repo, :entry_schema, :conversation_schema.

Returns

{:ok, %CommBus.Entry{}} on success or {:error, reason} on failure.

update_conversation(id, updates, config)

@spec update_conversation(term(), map(), config()) ::
  {:ok, CommBus.Conversation.t()} | {:error, term()}

Updates an existing conversation record with the given attributes.

Parameters

  • id — The conversation identifier.
  • updates — A map of fields to update.
  • config — Adapter config map.

Returns

{:ok, %CommBus.Conversation{}} on success or {:error, reason} on failure.