PPlusFireStore.API (pplus_firestore v0.1.5)

View Source

Module to interact with Google Firestore API

Summary

Types

error_code()

@type error_code() :: :not_found | :already_exists

Functions

create_document(auth_token, parent, collection, data, opts \\ [])

@spec create_document(
  auth_token :: String.t(),
  parent :: String.t(),
  collection :: String.t(),
  data :: map(),
  opts :: Keyword.t()
) ::
  {:ok, PPlusFireStore.Model.Document.t()}
  | {:error, error_code(), Tesla.Env.t()}
  | {:error, any()}

Create document

Parameters

Example:

iex> PPlusFireStore.API.create_document("token", "projects/my_project/databases/(default)/documents", "books", %{author: "John Doe"})
{:ok,
  %PPlusFireStore.Model.Document{
    path: "projects/my_project/databases/(default)/documents/books/esgXQM7pqNCwQwYRJeBJ",
    data: %{"author" => "John Due"},
    created_at: ~U[2025-01-10 17:14:04.738331Z],
    updated_at: ~U[2025-01-10 17:14:04.738331Z]
  }}

Note: One of the optional parameters is documentId, which is the id of the document to be created. If not provided, Firestore will generate an id automatically. It is important to pass the document id if you want to control the creation of duplicate documents.

delete_document(auth_token, path, opts \\ [])

@spec delete_document(
  auth_token :: String.t(),
  path :: String.t(),
  opts :: Keyword.t()
) :: :ok | {:error, error_code(), Tesla.Env.t()} | {:error, any()}

Delete document from firestore

Parameters

Example:

iex> PPlusFireStore.API.delete_document("token", "projects/my_project/databases/(default)/documents/books/esgXQM7pqNCwQwYRJeBJ")
{:ok, :deleted}

get_document(auth_token, path, opts \\ [])

@spec get_document(
  auth_token :: String.t(),
  path :: String.t(),
  opts :: Keyword.t()
) ::
  {:ok, PPlusFireStore.Model.Document.t()}
  | {:error, error_code(), Tesla.Env.t()}
  | {:error, any()}

Get document from firestore

Parameters

Example:

iex> PPlusFireStore.API.get_document("token", "projects/my_project/databases/(default)/documents/books/esgXQM7pqNCwQwYRJeBJ")
{:ok,
  %PPlusFireStore.Model.Document{
    path: "projects/my_project/databases/(default)/documents/books/esgXQM7pqNCwQwYRJeBJ",
    data: %{"author" => "John Due"},
    created_at: ~U[2025-01-10 17:14:04.738331Z],
    updated_at: ~U[2025-01-10 17:14:04.738331Z]
  }}

list_documents(auth_token, parent, collection, opts \\ [])

@spec list_documents(
  auth_token :: String.t(),
  parent :: String.t(),
  collection :: String.t(),
  opts :: Keyword.t()
) ::
  {:ok, PPlusFireStore.Model.Page.t(PPlusFireStore.Model.Document.t())}
  | {:error, error_code(), Tesla.Env.t()}
  | {:error, any()}

List documents from firestore

Parameters

Example:

iex> PPlusFireStore.API.list_documents("token", "projects/my_project/databases/(default)/documents", "books", pageSize: 1)
{:ok,
  %PPlusFireStore.Model.Page{
    data: [
      %PPlusFireStore.Model.Document{
        path: "projects/my_project/databases/(default)/documents/books/esgXQM7pqNCwQwYRJeBJ",
        data: %{"author" => "John Due"},
        created_at: ~U[2025-01-10 17:14:04.738331Z],
        updated_at: ~U[2025-01-10 17:14:04.738331Z]
      }
    ],
    next_page_token: "AFTOeJwGTcAtgJAapbJ0K7tPwpH9saWYfm4bG991Kk4qdP3NXq9pFfp5IW-E6lwbnRW661DKMJjo5EA7y2iF8GFjaCPLlXN7c0jMYATSRgclgLEChgsSIBjt"
  }}

run_query(auth_token, parent, query, opts \\ [])

@spec run_query(
  auth_token :: String.t(),
  parent :: String.t(),
  query :: GoogleApi.Firestore.V1.Model.StructuredQuery.t(),
  opts :: Keyword.t()
) ::
  {:ok, PPlusFireStore.Model.Page.t(PPlusFireStore.Model.Document.t())}
  | {:error, Tesla.Env.t()}
  | {:error, any()}

update_document(auth_token, path, data, opts \\ [])

@spec update_document(
  auth_token :: String.t(),
  path :: String.t(),
  data :: map(),
  opts :: Keyword.t()
) ::
  {:ok, PPlusFireStore.Model.Document.t()}
  | {:error, error_code(), Tesla.Env.t()}
  | {:error, any()}

Update document in firestore

Parameters

Example:

iex> PPlusFireStore.API.update_document("token", "projects/my_project/databases/(default)/documents/books/esgXQM7pqNCwQwYRJeBJ", %{author: "John Doe da Silva"})
{:ok,
  %PPlusFireStore.Model.Document{
    path: "projects/my_project/databases/(default)/documents/books/esgXQM7pqNCwQwYRJeBJ",
    data: %{"author" => "John Due da Silva"},
    created_at: ~U[2025-01-10 17:14:04.738331Z],
    updated_at: ~U[2025-01-10 17:14:04.738331Z]
  }}