csv_schema v0.2.1 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, parser: &Parser.integer!/1
    field :name, "Name", filter_by: true
    field :fiscal_code, "Fiscal Code", unique: true
    field :birth, "Date of birth", parser: &Parser.date!(&1, "{0D}/{0M}/{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

  • 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
    • filter_by - boolean; do i create a filter_by_{name} function for this field for you?
    • unique - boolean; creates a function by_{name} for you
    • parser - function; parser function used to get_changeset data from string to custom type

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 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
    • filter_by - boolean; do i create a filter_by_{name} function for this field for you?
    • unique - boolean; creates a function by_{name} for you
    • parser - function; parser function used to get_changeset data from string to custom type
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