AshSqlite.Repo behaviour (ash_sqlite v0.3.0)

Copy Markdown View Source

Resources that use AshSqlite.DataLayer use a Repo to access the database.

This repo is a thin wrapper around an Ecto.Repo.

You can use Ecto.Repo's init/2 to configure your repo like normal, but instead of returning {:ok, config}, use super(config) to pass the configuration to the AshSqlite.Repo implementation.

Additional Repo Configuration

Because an AshPostgres.Repo is also an Ecto.Repo, it has all of the same callbacks.

In the Ecto.Repo.init/2 callback, you can configure the following additional items:

  • :tenant_migrations_path - The path where your tenant migrations are stored (only relevant for a multitenant implementation)
  • :snapshots_path - The path where the resource snapshots for the migration generator are stored.

In the repo's application config, as it is read before init/2 runs:

Summary

Callbacks

Use this to inform the data layer about what extensions are installed

The path where your migrations are stored

Use this to inform the data layer about the oldest potential sqlite version it will be run on.

Allows overriding a given migration type for all fields, for example if you wanted to always use :timestamptz for :utc_datetime fields

The repo instance, a pid or a registered name, that tenant's statements run on.

Whether Ash may wrap write actions on this repo's resources in a transaction

Callbacks

installed_extensions()

@callback installed_extensions() :: [String.t()]

Use this to inform the data layer about what extensions are installed

migrations_path()

@callback migrations_path() :: String.t() | nil

The path where your migrations are stored

min_sqlite_version()

@callback min_sqlite_version() :: Version.t()

Use this to inform the data layer about the oldest potential sqlite version it will be run on.

override_migration_type(atom)

@callback override_migration_type(atom()) :: atom()

Allows overriding a given migration type for all fields, for example if you wanted to always use :timestamptz for :utc_datetime fields

tenant_repo(tenant, context)

(optional)
@callback tenant_repo(
  tenant :: term(),
  context :: %{resource: Ash.Resource.t(), type: :read | :mutate}
) ::
  pid() | atom()

The repo instance, a pid or a registered name, that tenant's statements run on.

Implementing this makes the repo a tenant repo, for strategy :context: each tenant has its own database file, and the tenant is applied by choosing the instance. Every resource on a tenant repo must use strategy :context, so put tables shared across tenants on a repo module of their own.

AshSqlite.MultiTenancy.tenant_repo/3 is a default implementation that opens one database file per tenant.

context.type is :read for queries and aggregates, and :mutate for writes and for opening a transaction.

write_transactions?()

@callback write_transactions?() :: boolean()

Whether Ash may wrap write actions on this repo's resources in a transaction