csv_schema v0.2.4 Csv.Schema

Csv schema is a library helping you to build Ecto schema-like files having as source a csv file.

The idea behind this library is give the possibility to create, at compile-time, getters function for a CSV inside codebase.

APIs related to this macro are similar to Ecto.Schema; Eg.

defmodule Person do
  use Csv.Schema, headers: true, separator: ?,
  alias Csv.Schema.Parser

  schema "path/to/person.csv" do
    field :id, "id", key: true
    field :first_name, "first_name", filter_by: true
    field :last_name, "last_name", sort: :asc
    field :identifier, ["first_name", "last_name"], key: true, join: " "
    field :email, "email", unique: true
    field :gender, "gender", filter_by: true, sort: :desc
    field :ip_address, "ip_address"
    field :date_of_birth, "date_of_birth", parser: &Parser.date!(&1, "{0M}/{0D}/{0YYYY}")
  end
end

At the end of compilation now your module is a Struct and has 3 kind of getters:

  • by_{key_field_name} -> returns single records object or nil

  • filter_by_{field_name} -> returns list of records matching provided property

  • get_all -> returns all records

    Back to the example in the module will be created:

  • __MODULE__.by_id/1 expecting integer as arg

  • __MODULE__.filter_by_name/1 expecting string as arg

  • __MODULE__.by_fiscal_code/1 expecting string as arg

  • __MODULE__.get_all/0

    Some example definitions could be found here

Link to this section Summary

Functions

It's possible to set a :separator argument to macro to let the macro split csv for you using provided separator. Moreover, if your csv file does not have headers, it's possible to set headers to false and configure fields by index (1..N)

Configure a new field (csv column). Parameters are

Schema macro helps you to build a block of fields. First parameter should be the relative path to csv file in your project. Second parameter should be a field list included in do-end block

Link to this section Types

Link to this type

changeset()

changeset() :: [{any(), map()}]
Link to this type

order()

order() :: :asc | :desc

Link to this section Functions

Link to this macro

__using__(opts)

(macro)

It's possible to set a :separator argument to macro to let the macro split csv for you using provided separator. Moreover, if your csv file does not have headers, it's possible to set headers to false and configure fields by index (1..N)

Link to this macro

field(name, col, opts \\ [])

(macro)

Configure a new field (csv column). Parameters are

  • name - new struct field name
  • column - header name or column index (if headers: false) in csv file
  • opts - list of configuration values

    • :key - boolean; at most one key must be set. It is something similar to a primary key
    • :unique - boolean; creates a function by_{name} for you
    • :filter_by - boolean; do i create a filter_by_{name} function for this field for you?
    • :parser - function; parser function used to get_changeset data from string to custom type
    • :sort - :asc or :desc; It sorts according to Erlang's term ordering with nil exception
    • :join - string; if present it joins the given fields into a binary using the separator
Link to this macro

schema(file_path, list)

(macro)

Schema macro helps you to build a block of fields. First parameter should be the relative path to csv file in your project. Second parameter should be a field list included in do-end block