Code Monkey home page Code Monkey logo

drill's Introduction

Drill

Seed data handling for Elixir

Drill is an elixir seeder library inspired by Seed Fu and Phinx.

Documentation

Official documentation on hexdocs

Usage

  1. Create your seeder modules. The directory where the seeder modules should be located is described on mix drill documentation.

In my_app/priv/repo/seeds/user.exs:

defmodule MyApp.Seeds.User do
  use Drill, key: :users, source: MyApp.Accounts.User

  def factory do
    %{
      first_name: Person.first_name(),
      last_name: Person.last_name()
    }
  end

  def run(_context) do
    [
      seed(email: "[email protected]"),
      seed(email: "[email protected]"),
      seed(email: "[email protected]")
    ]
  end
end

In my_app/priv/repo/seeds/post.exs:

defmodule MyApp.Seeds.Post do
  use Drill, key: :posts, source: MyApp.Blogs.Post
  alias Faker.Lorem

  def deps do
    [MyApp.Seeds.User]
  end

  def factory do
    %{content: Lorem.paragraph()}
  end

  def run(%Drill.Context{seeds: %{users: [user1, user2, user3 | _]}}) do
    [
      seed(user_id: user1.id),
      seed(user_id: user2.id),
      seed(user_id: user3.id)
    ]
  end
end
  1. Run mix drill -r MyApp.Repo in the terminal with your project root as the current working directory

Installation

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

def deps do
  [
    {:drill, "~> 1.2"}
  ]
end

Configurations

Timeout

Default timeout is 600 seconds or 10 minutes. You may configure the task timeout in your config.exs file. For example: config :drill, :timeout, 10_000

use Drill options

  • source (required) - source is the schema module
  • key (required) - once the seeder module runs, the inserted result will be saved to %Drill.Context{}.seeds[key]. Drill.Context struct is passed to one of Drill's callback which is run/1 to be discussed in the Callbacks section below.
  • returning (optional) - selects which fields to return. Defaults to true. See Ecto.Repo.insert_all/3

Callbacks

  • constraints/0 (optional) - returns a list of column names to verify for conflicts. If a conflict occurs all fields will just be updated. This prevents insertion of new records based on the constraints when drill is run again.
  • on_conflict/0 (optional) - returns the conflict strategy. The default is :replace_all. Only works when constraints/0 returns a non-empty list. See Ecto.Repo.insert_all/4 for more details.
  • deps/0 (optional) - returns a list of seeder modules that should be run prior to the current seeder
  • factory/0 (optional) - set default values for the fields. This is used when you call seed/1 from the seeder module.
  • run/1 (required) - returns a list of seeds (a call to Drill.seed/1 function or anything you want to include in the context seed). Autogenerated fields such as :inserted_at or :updated_at may not be defined. The first argument is the Drill.Context struct, which you can use to get the inserted records from previously run seeder modules (see Usage section above).

Command line options

  • --repo - specifies the repository to use
  • --seeds-path - overrides the default seeds path
  • Command line options for mix app.start documented here

Caveat

Can only be used on Postgres database for now

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/drill.

drill's People

Contributors

dependabot[bot] avatar dgigafox avatar samhamilton avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

samhamilton

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.