Couchx.Adapter (Couchx v0.3.0)

Adapter to get basic query functionality into Ecto with CouchDB.

Configuration

It uses the same as Ecto pattern to config the Dbs with this format:

config :my_app, MyRepo,
  username: "username",
  password: "password",
  database: "db_name",
  hostname: "localhost",
  protocol: "http",
  port: 5984

Usage

Couchx supports 1 main repo and many dynamic supervised repos. A dynamic repo will allow you to have multiple db connections in your application. To achieve this, you will need to setup a DynamicSupervisor and a Registry in the application like:

  def start(_type, _args) do
    children = [
      {DynamicSupervisor, strategy: :one_for_one, name: CouchxSupervisor}
      {Registry, keys: :unique, name: CouchxRegistry},
      ...
    ]
    ...
  end

The Restry name is tied up to the code so it must be called CouchxRegistry.

The main Repo is configured as any other Ecto Repo, so you can start it in the application just adding it to the children list.

  def start(_type, _args) do
    children = [
      MyDb.Repo
    ]
    ...
  end

Dynamic Repo queries

The dynamic repos are implemente with a Macro that you can get into your repo as:

  use CouchxDyncamicTepo, otp_app: :my_app, name: :my_repo

This is used to setup a run function, with a callback as argument. To execute actions in a dynamic repo we follow this pattern:

  MyDynamicRepo.run( ->
    MyDynamicRepo.get(MyStruct, doc_id)
  end)

Any Repo call inside the callback function will be run in a dynamically supervised connection.

Migrations

Couchx comes with a Mango index generator.

Example

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

This will create a file under priv/my_app/repo/index/my-mango-index.exs with contents as:

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

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

  def down do
    drop_index "my-mango-index"
  end
end

The Map inside the create_index block will be added to the index json object, so any structure that can go there can be added here. Currently only supported methods are

create_index(String.t(), (-> Map.t()))

  • name: ID and Name for the index to be created in CouchDB, this will be used as id for the document persisted.
  • fun: A block that returns a formated Map for the index to be created, it will be parsed as JSON to the body of the index document.

drop_index(String.t())

  • name: Id and Name for the index document to be deleted

Examples

$ mix couchx.mango_index

Will add all indexes store under `priv/my_app/repo/index/` paths

$ mix couchx.mango_index.down -r MyApp.Repo -n my-mango-index,my-other-index

It will call down function on the Migration files

```
  priv/my_app/repo/index/my-mango-index.exs
  priv/my_app/repo/index/my-other-index.exs
```

Removing the documents from the database.

Link to this section Summary

Functions

Callback implementation for Ecto.Adapter.checked_out?/1.

Callback implementation for Ecto.Adapter.dumpers/2.

Callback implementation for Ecto.Adapter.init/1.

Callback implementation for Ecto.Adapter.loaders/2.

Link to this section Functions

Link to this function

autogenerate(atom)

Callback implementation for Ecto.Adapter.Schema.autogenerate/1.

Link to this function

checked_out?(arg)

Callback implementation for Ecto.Adapter.checked_out?/1.

Link to this function

checkout(adapter, config, result)

Callback implementation for Ecto.Adapter.checkout/3.

Link to this function

create_admin(server, name, password)

Link to this function

create_db(server, name)

Link to this function

delete(meta, meta_schema, params, opts)

Callback implementation for Ecto.Adapter.Schema.delete/4.

Link to this function

delete_admin(server, name)

Link to this function

delete_db(server, name)

Link to this function

do_insert(errors, repo, constraints, fields, returning, meta)

Link to this function

dumpers(arg1, type)

Callback implementation for Ecto.Adapter.dumpers/2.

Link to this function

ensure_all_started(repo, type)

Callback implementation for Ecto.Adapter.ensure_all_started/2.

Link to this function

execute(meta, meta, design, view, query_opts)

Callback implementation for Ecto.Adapter.Queryable.execute/5.

Link to this function

execute(atom, meta, design, view, key, query_opts)

Callback implementation for Ecto.Adapter.init/1.

Link to this function

insert(meta, repo, fields, on_conflict, returning, options)

Callback implementation for Ecto.Adapter.Schema.insert/6.

Link to this function

insert_all(a, b, c, d, e, f, g)

Link to this function

insert_all(meta, repo, fields, data, on_conflict, schema, returning, opts)

Callback implementation for Ecto.Adapter.Schema.insert_all/8.

Link to this function

loaders(arg1, type)

Callback implementation for Ecto.Adapter.loaders/2.

Link to this function

parse_bulk_response(map, data, schema)

Link to this function

prepare(atom, query)

Callback implementation for Ecto.Adapter.Queryable.prepare/2.

Link to this function

stream(a, b, c, d, e)

Callback implementation for Ecto.Adapter.Queryable.stream/5.

Link to this function

update(meta, repo, fields, identity, returning, opts)

Callback implementation for Ecto.Adapter.Schema.update/6.

Link to this function

update!(meta, repo, fields, identity, returning, a)