Couchx.Adapter (Couchx v0.5.1)
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: 5984Usage
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 Registry in the application like:
def start(_type, _args) do
children = [
{Registry, keys: :unique, name: CouchxRegistry},
...
]
...
endThe 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
]
...
endDynamic 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_repoThis 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
idfor 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.