# Couchx

### CouchDB Adapter for Ecto3.

The Adapter supports 1 main Repo, but also dynamic repos where you can querie on multiple Dbs with a dynamic supervisor.

The supported functions in this version are:

Repo.get Struct, doc_id
Repo.all from doc in Struct, where: doc._id in ^doc_ids_list
Repo.all from doc in Struct, where: doc.field == ^field from doc in Struct, where doc.field == ^field from doc in Struct, where doc.field == ^field, select: ~w[field1 field2]a
Repo.insert Struct, doc
Repo.delete Struct, %{_id: doc_id}
Repo.update changeset

It adds a simple way to execute JS view queries with:

  def query(name, design, view_map, key, opts \\ []) do
    {adapter, meta} = Ecto.Repo.Registry.lookup(name)
    adapter.execute(:view, meta, design, view_map, key, opts)

Not ideal, so open to suggestions here.

### Mango support

Currently support for mango is limited to do queries on single property, on previously created indexes.
The selector is a map with the `type` property preset to the model namespace, such as:

  selector: %{
    type: "user",
    email: ""

This will be the query generated by:

import Ecto.Query

Repo.all from u in User, where: == ""

It will also work with:

import Ecto.Query

email = ""
Repo.all from u in User, where: == ^email

## Mango Index

Couchx have a couple of tasks to handle database indexing:

$ mix couchx.gen.mango_index -r MyApp.Repo -n my-mango-index -f name,email

This command will generate a index file on `priv/my_app/repo/index/my-mango-index.exs`
It rely on repos declared under `config.exs`

import Config

config :my_app, ecto_repos: ["repo", "custom_repo"]

And build a file with contents such as:

defmodule MyApp.Repo.Index.MyMangoIndex do
  use Couchx.MangoIndex, repo_name: MyApp.Repo

  def up do
    create_index "my-mango-index" do
      %{fields: ["name", "email"]}

  def down do

This file will be executed with

$ mix couchx.mango_index

which will persist the index document in the database defined by the repo.
And if you want to remove the index you can call:

`$ mix couch.mango_index.down -r MyApp.Repo, -n my-mango-index`

## TODO:

* Repo.insert_all
* Repo.delete_all
* Bulk doc updates
* Better error handling
* More Mango Queries
* A better view query pattern
* Auto indexing requested JS queries
* JS view index management
* Add tests

## Installation

The package can be installed by adding `couchx` to your list of dependencies in `mix.exs`:

def deps do
    {:couchx, "~> 0.1.0"}

Documentation can be generated with [ExDoc](
and published on [HexDocs]( Once published, the docs can
be found at [](