View Source Appwrite.Services.Database (appwrite v0.1.9)

The Database service allows you to create structured collections of documents, query and filter lists of documents, and manage an advanced set of read and write access permissions.

All the data in the database service is stored in structured JSON documents. The Appwrite database service also allows you to nest child documents in parent documents and use deep filters to both search and query your data.

Each database document structure in your project is defined using the Appwrite collection rules. The collections rules help you ensure all your user-submitted data is validated and stored according to the collection structure.

Using Appwrite permissions architecture, you can assign read or write access to each document in your project for either a specific user, team, user role, or even grant it with public access (*).

Summary

Functions

create_document(database_id, collection_id, document_id \\ nil, data, permissions \\ nil)

@spec create_document(
  String.t(),
  String.t(),
  String.t() | nil,
  map(),
  [String.t()] | nil
) :: {:ok, Appwrite.Types.Document.t()} | {:error, any()}

Creates a new document in a specified collection.

Parameters

  • database_id (String.t): The database ID.
  • collection_id (String.t): The collection ID.
  • document_id (String.t): The document ID.
  • data (map): Document data.
  • permissions (list(String.t)): Optional permissions.

Returns

  • {:ok, %Document{}} on success.
  • {:error, reason} on failure.

delete_document(database_id, collection_id, document_id)

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

Deletes a document by its ID.

Parameters

  • database_id (String.t): The database ID.
  • collection_id (String.t): The collection ID.
  • document_id (String.t): The document ID.

Returns

  • {:ok, %{}} on success.
  • {:error, reason} on failure.

get_document(database_id, collection_id, document_id, queries \\ nil)

@spec get_document(String.t(), String.t(), String.t(), [String.t()] | nil) ::
  {:ok, Appwrite.Types.Document.t()} | {:error, any()}

Retrieves a document by its ID.

Parameters

  • database_id (String.t): The database ID.
  • collection_id (String.t): The collection ID.
  • document_id (String.t): The document ID.
  • queries (list(String.t)): Optional list of queries.

Returns

  • {:ok, %Document{}} on success.
  • {:error, reason} on failure.

list_documents(database_id, collection_id, queries \\ nil)

@spec list_documents(String.t(), String.t(), [String.t()] | nil) ::
  {:ok, Appwrite.Types.DocumentList.t()} | {:error, any()}

Lists documents in a specified collection.

Parameters

  • database_id (String.t): The database ID.
  • collection_id (String.t): The collection ID.
  • queries (list(String.t)): Optional list of queries.

Returns

  • {:ok, %DocumentList{}} on success.
  • {:error, reason} on failure.

update_document(database_id, collection_id, document_id, data \\ nil, permissions \\ nil)

@spec update_document(
  String.t(),
  String.t(),
  String.t(),
  map() | nil,
  [String.t()] | nil
) :: {:ok, Appwrite.Types.Document.t()} | {:error, any()}

Updates a document by its ID.

Parameters

  • database_id (String.t): The database ID.
  • collection_id (String.t): The collection ID.
  • document_id (String.t): The document ID.
  • data (map): Partial data to update.
  • permissions (list(String.t)): Optional permissions.

Returns

  • {:ok, %Document{}} on success.
  • {:error, reason} on failure.