gutschilla/elixir-couchdb-client

语言: Elixir

git: https://github.com/gutschilla/elixir-couchdb-client

CouchDB客户端
A CouchDB client
README.md (中文)

CouchdbClient

功能最小但功能齐全的CouchDB客户端,具有附件支持。目前, 这是一个晚上黑客攻击的结果,所以期待相当多的API更改,直到 1.0。

错误处理

不在这里;-)没有执行异常处理。服务器响应是 预计200 OK或201 CREATED。如果有任何失败,代码将简单 崩溃(与坏匹配等)。

未来发展

将会有方法函数eitehr返回{:ok,result}或{:error, 原因}元组和相应的方法!砰! - 功能要么返回 结果或崩溃有一些明显的错误,很可能是来自的HTTP结果 HTTPpoison。

用法

有关完整文档,请参阅内联@doc或使用ex_doc生成文档。

用作OTP应用程序

该软件包可以安装为:

1. Add couchdb_client to your list of dependencies in `mix.exs`:

      def deps do
        [{:couchdb_client, "~> 0.3.2"}]
      end

2. Configure CouchDB connection parameters in your `config.exs`

      config :couchdb_client,
          scheme: "http",
          host:   "127.0.0.1",
          port:   5984,
          name:   "you_database_name"

3. Ensure couchdb_client is started before your application:

      def application do
        [applications: [:couchdb_client]]
      end

4. Start accessing CouchDB

      alias CouchdbClient.Document, as: Doc
      CouchdbClient.insert %Doc{ id: "super-id", data: %{ "foo" => "bar" } }

      # and later ...
      CouchdbClient.load "super-id" 
      |> CouchdbClient.set %{ "boom" => "bang!" } # adds key boom
      |> CouchdbClient.update

使用CouchdbClient的包装器

这将在Elixir.Agent进程中保存连接信息。

# The call to CouchdbClient.start can be omitted if started as OTP App
CouchdbClient.start host: "10.0.0.1", port: 5984, name: "test_database"

# It'll sure come handy to alias CouchdbClient.Document
doc = %CouchdbClient.Document{ data: %{ "foo" => "bar" } }
doc = CouchdbClient.save doc
doc = CouchdbClient.set doc, %{ "boom" => "bang" }
doc = CouchdbClient.save doc
attachment = %CouchdbClient.Attachment{ filename: "test.jpg", content: File.read!("test.jpg"), content_type: "image/jpeg" }
:ok = CouchdbClient.add_attachment doc, attachment
{ binary, content_type } = CouchdbClient.fetch_attachment "test.jpg"
:ok = CouchdbClient.delete_attachment doc, attachment

直接使用CouchdbClient的模块

alias CouchdbClient.Database,   as: DB
alias CouchdbClient.Document,   as: Doc
alias CouchdbClient.Attachment, as: Attachment

db  = %DB{ host: "127.0.0.1", port: "5984", name: "test_database" }
doc = %Doc{ _id: "test_document", _data: %{ "foo" => "bar" } }
doc = Doc.insert doc, db
doc = Doc.set doc, %{ "bang" => "boom" } # inserts(!) "bang" => "boom" in data
doc = Doc.update doc, db
doc = Doc.save doc, db # save automatically uses insert or update 
:ok = Doc.delete doc, db 

attachment = %Attachment{ filename: "test.jpg", content: File.read!("test.jpg"), content_type: "image/jpeg" }
:ok = Attachment.attach( doc, attachment, db )

attachment2 = %Attachment{ filename: "test.txt", content: "Müßiggang!", content_type: "text/plain;charset=utf8" }
# attaching to a document w/o _rev will auto-fetch the current revision rev:
:ok = Attachment.attach( %Doc{ _id: doc._id, database: doc.database }, attachment2 )

# or just load to refresh:
doc = Document.load doc, db
:ok = Attachment.delete doc, "test.jpg", db
{ binary, content_type } = Attachment.fetch doc, "test.txt", db

本文使用googletrans自动翻译,仅供参考, 原文来自github.com

en_README.md

CouchdbClient

A minimal yet functional CouchDB client, with attachment support. Currently,
this is a result of an evening hacking so expect quite a few API changes until
1.0.

Error handling

Not in here ;-) No exception handling is performed. Server responses are
expected to be 200 OK or 201 CREATED. If anything fails, the code will simply
crash (with a badmatch or the like).

Future development

There will be method functions that eitehr return {:ok, result} or {:error, reason} tuples and corresponding method! bang!-functions either return the
result or crash with some sensible error, most likely the bare HTTP result from
HTTPpoison.

Usage

For full documentation, see inline @doc or use ex_doc to generate docs.

Using as OTP Application

The package can be installed as:

1. Add couchdb_client to your list of dependencies in `mix.exs`:

      def deps do
        [{:couchdb_client, "~> 0.3.2"}]
      end

2. Configure CouchDB connection parameters in your `config.exs`

      config :couchdb_client,
          scheme: "http",
          host:   "127.0.0.1",
          port:   5984,
          name:   "you_database_name"

3. Ensure couchdb_client is started before your application:

      def application do
        [applications: [:couchdb_client]]
      end

4. Start accessing CouchDB

      alias CouchdbClient.Document, as: Doc
      CouchdbClient.insert %Doc{ id: "super-id", data: %{ "foo" => "bar" } }

      # and later ...
      CouchdbClient.load "super-id" 
      |> CouchdbClient.set %{ "boom" => "bang!" } # adds key boom
      |> CouchdbClient.update

Using CouchdbClient's wrapper

This will save the connection info in an Elixir.Agent process.

# The call to CouchdbClient.start can be omitted if started as OTP App
CouchdbClient.start host: "10.0.0.1", port: 5984, name: "test_database"

# It'll sure come handy to alias CouchdbClient.Document
doc = %CouchdbClient.Document{ data: %{ "foo" => "bar" } }
doc = CouchdbClient.save doc
doc = CouchdbClient.set doc, %{ "boom" => "bang" }
doc = CouchdbClient.save doc
attachment = %CouchdbClient.Attachment{ filename: "test.jpg", content: File.read!("test.jpg"), content_type: "image/jpeg" }
:ok = CouchdbClient.add_attachment doc, attachment
{ binary, content_type } = CouchdbClient.fetch_attachment "test.jpg"
:ok = CouchdbClient.delete_attachment doc, attachment

Using CouchdbClient's modules directly

alias CouchdbClient.Database,   as: DB
alias CouchdbClient.Document,   as: Doc
alias CouchdbClient.Attachment, as: Attachment

db  = %DB{ host: "127.0.0.1", port: "5984", name: "test_database" }
doc = %Doc{ _id: "test_document", _data: %{ "foo" => "bar" } }
doc = Doc.insert doc, db
doc = Doc.set doc, %{ "bang" => "boom" } # inserts(!) "bang" => "boom" in data
doc = Doc.update doc, db
doc = Doc.save doc, db # save automatically uses insert or update 
:ok = Doc.delete doc, db 

attachment = %Attachment{ filename: "test.jpg", content: File.read!("test.jpg"), content_type: "image/jpeg" }
:ok = Attachment.attach( doc, attachment, db )

attachment2 = %Attachment{ filename: "test.txt", content: "Müßiggang!", content_type: "text/plain;charset=utf8" }
# attaching to a document w/o _rev will auto-fetch the current revision rev:
:ok = Attachment.attach( %Doc{ _id: doc._id, database: doc.database }, attachment2 )

# or just load to refresh:
doc = Document.load doc, db
:ok = Attachment.delete doc, "test.jpg", db
{ binary, content_type } = Attachment.fetch doc, "test.txt", db