Blink.Store (blink v0.4.1)

View Source

The central data structure and operations for the Blink seeding pipeline.

This module provides the Store struct and functions for building and inserting seed data into your database.

A Store holds:

  • :tables — data that will be inserted into the database.
  • :table_order - the order in which tables were added, used to ensure inserts respect foreign key constraints.
  • :context — auxiliary data available while constructing the Store, and will not be inserted into the database.

Summary

Functions

Adds context to the store by calling the provided builder function.

Adds a table to the store by calling the provided builder function.

Inserts all table records from a Store into the given repository. Iterates over the tables in order when seeding the database.

Creates an empty Store.

Types

empty()

@type empty() :: %Blink.Store{context: %{}, table_order: [], tables: %{}}

key()

@type key() :: binary() | atom()

t()

@type t() :: %Blink.Store{context: map(), table_order: [key()], tables: map()}

Functions

add_context(store, key, builder)

@spec add_context(
  store :: t(),
  key :: key(),
  builder :: (t(), key() -> any())
) :: t()

Adds context to the store by calling the provided builder function.

The builder function should take a store and key and return the context data.

add_table(store, table_name, builder)

@spec add_table(
  store :: t(),
  table_name :: key(),
  builder :: (t(), key() -> [map()])
) :: t()

Adds a table to the store by calling the provided builder function.

The builder function should take a store and table name and return a list of maps representing the table data.

insert(store, repo, opts \\ [])

@spec insert(store :: t(), repo :: Ecto.Repo.t(), opts :: Keyword.t()) ::
  {:ok, any()} | {:error, any()}

Inserts all table records from a Store into the given repository. Iterates over the tables in order when seeding the database.

The repo parameter must be a module that implements the Ecto.Repo behaviour and is configured with a Postgres adapter (e.g., Ecto.Adapters.Postgres).

Data stored in the Store's context is ignored.

is_key(key)

(macro)

new()

@spec new() :: empty()

Creates an empty Store.

Example

iex> Blink.Store.new()
%Blink.Store{tables: %{}, table_order: [], context: %{}}