Skip to content

Repository files navigation

Seedex

Test Hex.pm

Seedex is a library for Ecto to easily populate your DB with seed data. It is useful, for example, if you have some master data you need to insert in your database.

This is not meant to generate data for your tests, if this is what you need, checkout ecto_fixtures or ex_machina instead.

Installation

Add seedex to your list of dependencies in mix.exs:

def deps do
  [{:seedex, "~> 0.4.0"}]
end

Usage

First, you need to configure seedex to use your repository:

config :seedex,
  repo: YourApp.Repo,
  seeds_path: "priv/repo/seeds" # not required, but can be used to customize seeds path

Then, you just need to add files under the seeds_path, which defaults to priv/repo/seeds. All files matching seeds_path/*.exs and seeds_path/MIX_ENV/*.exs if it exists will be read. To insert the data, you need to run

mix seedex.seed

Files are loaded in alphabetic order, independently of the directory they are in. However, the files are really just plain Elixir with nothing special, so you could just use mix run priv/repo/seeds/my_seed.exs, if you needed to.

Sample seed file

Here is what a seed file looks like:

import Seedex

seed_once Group, fn group ->
  group
  |> Map.put(:id, 1)
  |> Map.put(:name, "admin")
end

seed Group, fn group ->
  %{group | id: 2, name: "user"}
end

seed User, [:name], [
  %{name: "Daniel", age: 26, group_id: 1},
  %{name: "Ai", age: 24, group_id: 2},
]

Checkout the documentation for more info.

Development

Seedex 0.4 requires Elixir 1.15 or newer and Ecto 3.13 or newer. The .tool-versions file pins the current Elixir 1.20 / Erlang 29 toolchain. Tests require PostgreSQL and default to postgres on localhost, with the seedex_test database. Set PGHOST and PGPORT to use another server.

mise install
mise exec -- mix deps.get
mise exec -- mix test

Version 0.4 also raises on failed seed inserts instead of silently ignoring errors.

About

Seed data generation for Ecto

Resources

Stars

30 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages