Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/danhper/seedex
Seed data generation for Ecto
https://github.com/danhper/seedex
Last synced: about 1 month ago
JSON representation
Seed data generation for Ecto
- Host: GitHub
- URL: https://github.com/danhper/seedex
- Owner: danhper
- License: mit
- Created: 2016-05-20T09:21:17.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2022-12-28T14:12:28.000Z (almost 2 years ago)
- Last Synced: 2024-09-18T14:10:51.633Z (3 months ago)
- Language: Elixir
- Homepage: https://hex.pm/packages/seedex
- Size: 18.6 KB
- Stars: 29
- Watchers: 2
- Forks: 9
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Seedex
[![Build Status](https://travis-ci.org/danhper/seedex.svg?branch=master)](https://travis-ci.org/danhper/seedex)
[![Hex.pm](https://img.shields.io/hexpm/v/seedex.svg)](https://hex.pm/packages/seedex)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](https://github.com/dockyard/ecto_fixtures) or
[ex_machina](https://github.com/thoughtbot/ex_machina) instead.## Installation
Add `seedex` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[{:seedex, "~> 0.3.0"}]
end
```## Usage
First, you need to configure `seedex` to use your repository:
```elixir
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:
```elixir
import Seedexseed_once Group, fn group ->
group
|> Map.put(:id, 1)
|> Map.put(:name, "admin")
endseed Group, fn group ->
%{group | id: 2, name: "user"}
endseed User, [:name], [
%{name: "Daniel", age: 26, group_id: 1},
%{name: "Ai", age: 24, group_id: 2},
]
```Checkout the [documentation](https://hexdocs.pm/seedex/Seedex.html) for more info.