Skip to content

Postgrex.Extension and Ecto.Type for PostgreSQL cube module

License

Notifications You must be signed in to change notification settings

bebus77/cubecto

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cubecto

Cubecto adds a Postgrex.Extension and Ecto.Type definitions for the datatypes defined in the cube PostgreSQL module.

Installation

Add the package to your Mixfile

defp deps do
  [{:cubecto, "~> 0.1.0"}]
end

Add Cubecto.Extension to the Postgrex types file somewhere in lib/my_app/

Postgrex.Types.define(
  MyApp.PostgrexTypes,
  [Cubecto.Extension] ++ Ecto.Adapters.Postgres.extensions()
)

Configure Repo to use custom Postgrex types in config/config.exs

config :my_app, MyApp.Repo,
  types: MyApp.PostgrexTypes

Create the cube extension in the database

defmodule MyApp.Repo.Migrations.CreateCubeExtension do
  use Ecto.Migration

  def change do
    execute(
      "CREATE EXTENSION IF NOT EXISTS cube",
      "DROP EXTENSION IF EXISTS cube"
    )
  end
end

Usage

Add a column with the cube datatype to a table

defmodule MyApp.Repo.Migrations.CreateUser do
  use Ecto.Migration

  def change do
    create table("users") do
      add :cube, :cube
      # other fields
    end
  end
end

Use Cubecto.Type for the cube field in an Ecto.Schema module

defmodule MyApp.Accounts.User do
  use Ecto.Schema

  schema "users" do
    field :cube, Cubecto.Type
    # other fields
  end
end

Manipulate the field as a list

iex> user = MyApp.Repo.insert!(%MyApp.Accounts.User{cube: [1, 2, -3]})
%MyApp.Accounts.User{
  __meta__: #Ecto.Schema.Metadata<:loaded, "users">,
  id: 1,
  cube: [1, 2, -3]
}

iex> user |>  Ecto.Changeset.change(%{cube: [0.1, -0.2, 12.345, 123]}) |> Repo.update!()
%MyApp.Accounts.User{
  __meta__: #Ecto.Schema.Metadata<:loaded, "users">,
  id: 1,
  cube: [0.1, -0.2, 12.345, 123]
}

About

Postgrex.Extension and Ecto.Type for PostgreSQL cube module

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Elixir 100.0%