2015-05-21 06:46:07 +00:00
|
|
|
# Isn
|
2015-05-05 20:15:40 +00:00
|
|
|
|
2016-01-09 10:23:23 +00:00
|
|
|
[![Build Status][4]][5]
|
2015-06-18 16:02:27 +00:00
|
|
|
|
2015-05-27 17:13:05 +00:00
|
|
|
Isn adds a [`Postgrex.Extension`][1] and [`Ecto.Type`][2] definitions
|
|
|
|
|
for the datatypes defined in the [`isn`][3] PostgreSQL module.
|
|
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
|
|
1. Add the package to your Mixfile:
|
2016-01-09 10:23:23 +00:00
|
|
|
```elixir
|
|
|
|
|
defp deps do
|
|
|
|
|
[{:isn, "~> 0.1"}]
|
|
|
|
|
end
|
|
|
|
|
```
|
2015-05-27 17:33:15 +00:00
|
|
|
2. Add the isn extension to your database
|
2016-01-09 10:23:23 +00:00
|
|
|
```elixir
|
|
|
|
|
{:ok, pid} = Postgrex.Connection.start_link(
|
|
|
|
|
hostname: "localhost",
|
|
|
|
|
database: "isn_test"
|
|
|
|
|
)
|
|
|
|
|
Postgrex.Connection.query!(pid, "CREATE EXTENSION isn;", [])
|
|
|
|
|
```
|
|
|
|
|
3. Register the postgrex extension
|
|
|
|
|
```elixir
|
|
|
|
|
Postgrex.Connection.start_link(
|
|
|
|
|
database: "isn_test",
|
|
|
|
|
extensions: [{Isn, {}}])
|
|
|
|
|
```
|
|
|
|
|
4. Start using all of the `isn` goodness in your project.
|
2015-05-27 17:13:05 +00:00
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
|
|
Here are a few small snippets for how to use these types in various
|
|
|
|
|
contexts. I have extracted these snippets from a phoenix project, so
|
|
|
|
|
if you want to use them in something else, you might have to modify them
|
|
|
|
|
a bit.
|
|
|
|
|
|
|
|
|
|
### Ecto migrations
|
|
|
|
|
|
|
|
|
|
```elixir
|
|
|
|
|
defmodule MyApp.Repo.Migrations.CreateBook do
|
|
|
|
|
use Ecto.Migration
|
|
|
|
|
|
|
|
|
|
def change do
|
|
|
|
|
create table(:books) do
|
|
|
|
|
add :isbn, :isbn13
|
|
|
|
|
# other fields
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Ecto Models
|
|
|
|
|
|
|
|
|
|
```elixir
|
|
|
|
|
defmodule MyApp.Book do
|
|
|
|
|
use MyApp.Web, :model
|
|
|
|
|
|
|
|
|
|
schema "books" do
|
|
|
|
|
field :isbn, Isn.ISBN13
|
|
|
|
|
# other fields
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Defined types
|
|
|
|
|
|
|
|
|
|
`Isn` adds the following ecto and corresponding postgrex types:
|
|
|
|
|
|
|
|
|
|
Ecto.Type | Postgrex type
|
|
|
|
|
-------------|--------------
|
|
|
|
|
`Isn.ISBN` | `:isbn`
|
|
|
|
|
`Isn.ISBN13` | `:isbn13`
|
|
|
|
|
`Isn.ISMN` | `:ismn`
|
|
|
|
|
`Isn.ISMN13` | `:ismn13`
|
|
|
|
|
`Isn.ISSN` | `:issn`
|
|
|
|
|
`Isn.ISSN13` | `:issn13`
|
|
|
|
|
`Isn.EAN13` | `:ean13`
|
|
|
|
|
`Isn.UPC` | `:upc`
|
|
|
|
|
|
|
|
|
|
[1]: http://hexdocs.pm/postgrex/Postgrex.Extension.html
|
2016-01-09 10:23:23 +00:00
|
|
|
[2]: http://hexdocs.pm/ecto/Ecto.Type.html
|
2015-05-27 17:13:05 +00:00
|
|
|
[3]: http://www.postgresql.org/docs/9.4/static/isn.html
|
2016-01-09 10:23:23 +00:00
|
|
|
[4]: https://semaphoreci.com/api/v1/projects/be7c4c34-c49e-45c7-9320-3fcc4f7f476a/458429/badge.svg
|
|
|
|
|
[5]: https://semaphoreci.com/frost/isn
|