Giter Club home page Giter Club logo

ecto_hstore's Introduction

Ecto.Hstore

Note: This package is now obsolete and is kept here for historical purposes as well as anyone who hasn't upgraded. Postgrex offers hstore support directly.

Ecto.Hstore adds Postgres Hstore compatibility to your Ecto models for storing sets of key/value pairs within a single PostgreSQL value. This can be useful in various scenarios, such as rows with many attributes that are rarely examined, or semi-structured data. Keys and values are simply text strings.

Hstore structures are represented by Elixir Maps.

Installation

Add Ecto.Hstore as a dependency in your mix.exs file.

defp deps do
  [{:postgrex, ">= 0.0.0"},
   {:ecto, "~> 0.7"},
   {:ecto_hstore, "~> 0.0.1"}]
end

After you are done, run mix deps.get in your shell to fetch the dependencies.

Usage

Enable Hstore through an ecto migration:

defmodule Repo.Migrations.EnableHstore do
  use Ecto.Migration

  def up do
    execute "CREATE EXTENSION hstore IF NOT EXISTS"
  end

  def down do
    execute "DROP EXTENSION hstore IF EXISTS"
  end
end

Add your desired Postgres hstore columns through a migration:

defmodule Repo.Migrations.CreateUsers do
  use Ecto.Migration

  def up do
    create table :users do
      add :flags, :hstore, null: false, default: ""
    end
  end

  def down do
    drop table(:users)
  end
end

Define your Ecto model's schema using the corresponding Ecto.Hstore type:

defmodule User do
  use Ecto.Model

  schema "users" do
    field :flags, Ecto.Hstore
  end
end

And now you're all set!

# Inserting a new user with flags is as simple as creating an Elixir Map:
user = Repo.insert %User{flags: %{key: "value"}}
user.flags #=> %{"key" => "value"}

# Updating the hstore value is as simple as `Dict.put`
flags = Dict.put(user.flags, "key2", "value2")
user = %User{user| flags: flags}
Repo.update user

query = from u in User,
          where: fragment("?->'?' = '?'", u.flags, "key2", "value2"),
          select: u

users = Repo.all(query) #=> [%User{flags: %{"key" => "value", "key2" => "value2"} ...]

Quirks

Elixir Maps and Postgres Hstore types have a few important differences that users should be cautious of. The current behavior of these edge cases may change in future releases of Ecto.Hstore.

  • Postgres Hstore does not allow null keys, whereas Elixir Maps do. Currently nil keys will not be serialized to Postgres.
  • Postgres Hstore does not have an atom type. All keys are stored as strings. Therefore extra caution must be taken to ensure that an Elixir Map doesn't contain two converging keys (eg: %{:a => 2, "a" => 2}), because they will be consolidated to one string key "a" in a non-deterministic manner.
  • Postgres Hstore only supports a single level of key/value pairs. Nested maps are not supported, and attempting to serialize nested maps will give undesirable results.

Contributing

To contribute you need to compile Ecto.Hstore from source and test it:

$ git clone https://github.com/stavro/ecto_hstore.git
$ cd ecto_hstore
$ mix test

License

Copyright 2015 Sean Stavropoulos

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

ecto_hstore's People

Contributors

stavro avatar jschaeff avatar

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.