Sovite.Core.Repo (sovite v0.2.0)

Copy Markdown View Source

Sovite's database, through Ecto.

The adapter comes from the [database] config section: SQLite (the default, a single file), PostgreSQL, or MySQL. Each adapter has its own repo module, since Ecto fixes the adapter at compile time; this module picks the one the config asks for.

The database is migrated at startup, so a new Sovite version updates the schema by itself. Migrations are compiled modules, listed in migrations/0, not .exs files, so they work in releases.

Functions take a repo reference, {module, name}, so several databases can be open in one VM (tests use one per test).

Summary

Types

t()

A running repo: the repo module and its process name or pid.

Functions

The driver an adapter needs, for error messages.

Runs the pending migrations.

The migrations, in order, as {version, module}.

Returns the repo module for adapter, or nil if its driver is missing.

Returns the reference for the repo started from config (the [database] section) under name. name defaults to the repo module.

Runs fun with the repo module, inside the process that calls it, using the repo named in ref.

Starts the repo for config and migrates the database. With name: nil the repo is unnamed: use the returned pid in the reference.

Types

adapter()

@type adapter() :: :sqlite | :postgres | :mysql

t()

@type t() :: {module(), GenServer.server()}

A running repo: the repo module and its process name or pid.

Functions

driver(atom)

@spec driver(adapter()) :: atom()

The driver an adapter needs, for error messages.

migrate(arg)

@spec migrate(t()) :: :ok | {:error, term()}

Runs the pending migrations.

migrations()

@spec migrations() :: [{pos_integer(), module()}]

The migrations, in order, as {version, module}.

module(atom)

@spec module(adapter()) :: module() | nil

Returns the repo module for adapter, or nil if its driver is missing.

ref(config, name \\ nil)

@spec ref(map(), atom() | nil) :: t()

Returns the reference for the repo started from config (the [database] section) under name. name defaults to the repo module.

run(arg, fun)

@spec run(t(), (module() -> result)) :: result when result: term()

Runs fun with the repo module, inside the process that calls it, using the repo named in ref.

start_link(config, name \\ nil)

@spec start_link(map(), atom() | nil) :: {:ok, pid()} | {:error, term()}

Starts the repo for config and migrates the database. With name: nil the repo is unnamed: use the returned pid in the reference.