Sofa.Doc (sofa v0.1.3)

View Source

Documentation for Sofa.Doc, a test-driven idiomatic Apache CouchDB client.

If the only tool you have is CouchDB, then everything looks like {:ok, :relax}

Examples

iex> Sofa.Doc.new()

Summary

Functions

Coerces a CouchDB "type" field to an existing atom. It is assumed that there will be a related Elixir Module Type of the same name. Elixir prefixes Module names with Elixir. and then elides this in iex, tests, and elsewhere, but here we need to make that explicit.

delete doc

Check if doc exists via HEAD /:db/:doc and returns either

Check if doc exists via HEAD /:db/:doc and returns either true or false

Converts CouchDB-native JSON-friendly map to internal %Sofa.Doc{} format

GET doc and returns standard HTTP status, or the requested doc

Creates a new (empty) document

Optimistically write/update doc assuming rev matches

Converts internal %Sofa.Doc{} format to CouchDB-native JSON-friendly map

Types

t()

@type t() :: %Sofa.Doc{
  attachments: map(),
  body: map(),
  id: binary(),
  rev: nil | binary(),
  type: atom() | nil
}

Functions

coerce_to_elixir_type(type)

@spec coerce_to_elixir_type(String.t()) :: atom()

Coerces a CouchDB "type" field to an existing atom. It is assumed that there will be a related Elixir Module Type of the same name. Elixir prefixes Module names with Elixir. and then elides this in iex, tests, and elsewhere, but here we need to make that explicit.

The "user" type is special-cased as it is already present in CouchDB /_users database.

This function is expected to be paired up with Ecto Schemas to properly manage the appropriate fields in your document body.

delete(sofa, doc)

@spec delete(Sofa.t(), t()) :: {:error, any()} | {:ok, Sofa.t(), any()}

delete doc

exists(sofa, doc)

@spec exists(Sofa.t(), String.t()) :: {:error, any()} | {:ok, %{}}

Check if doc exists via HEAD /:db/:doc and returns either:

  • {:error, not_found} # doc doesn't exist
  • {:ok, %Sofa.Doc{}} # doc exists and has metadata

exists?(sofa, doc)

@spec exists?(Sofa.t(), String.t()) :: false | true

Check if doc exists via HEAD /:db/:doc and returns either true or false

from_map(m)

@spec from_map(map()) :: t()

Converts CouchDB-native JSON-friendly map to internal %Sofa.Doc{} format

Examples

  iex> %{ "_id" => "smol", "_rev" => "1-cute", "yes" => true}
      |> from_map()

  %Sofa.Doc{
    attachments: nil,
    body: %{"yes" => true},
    id: "smol",
    rev: "1-cute",
    type: nil
  }

get(sofa, doc)

@spec get(Sofa.t(), String.t()) :: {:error, any()} | t()

GET doc and returns standard HTTP status, or the requested doc

  • # doc doesn't exist, or similar HTTP status
  • %Sofa.Doc{} # doc exists and has metadata

new(id)

@spec new(String.t() | %{}) :: t()

Creates a new (empty) document

put(sofa, doc)

@spec put(Sofa.t(), t()) :: {:ok, t()} | {:error, any()}

Optimistically write/update doc assuming rev matches

to_map(doc)

@spec to_map(t()) :: map()

Converts internal %Sofa.Doc{} format to CouchDB-native JSON-friendly map

Examples

  iex> %Sofa.Doc{id: "smol", rev: "1-cute", body: %{"yes" => true}}
    |> to_map()

  %{ "_id" => "smol", "_rev" => "1-cute", "yes" => true}