View Source EctoPostgresJson

Hex Version Hex Docs Total Download License Last Updated

Ecto utilities to work with PostgreSQL JSONb columns.

indexes

Indexes

Provides a json_index/3 function to use alongside Ecto migrations that handles the creation of key and column GIN based indexes for JSONb columns.

installation

Installation

The package can be installed by adding :ecto_postgres_json to deps/0 function in mix.exs:

{:ecto_postgres_json, "~> 0.1.0"},

usage

Usage

Import the utility module in your migration:

defmodule MyApp.Repo.Migrations.CreatePosts do
  use Ecto.Migration
  import EctoPostgresJson.Index
...
end

Define indexes using EctoPostgresJson.Index.json_index/3:

defmodule MyApp.Repo.Migrations.CreatePosts do
  ...
  def change do
    create table(:posts) do
      add :name, :string
      add :payload, :map

      timestamps()
    end
    create json_index(:posts, :payload)
    create json_index(:posts, ["payload", "tags"], name: :post_tags_index)
  end
end

Copyright (c) 2022 Eduardo Moraga

This work is free. You can redistribute it and/or modify it under the terms of the MIT License. See the LICENSE.md file for more details.