CosmosDbEx.create_document
create_document
, go back to CosmosDbEx module for more information.
Specs
create_document(CosmosDbEx.Container.t(), map(), String.t()) :: {:ok | :bad_request | :conflict | :entity_too_large | :not_found | :storage_limit_reached | :unauthorized, CosmosDbEx.Response.t()}
Creates a new document in the specified database and container.
Documents can be any struct that implements the CosmosDbEx.Client.Documents.Identifiable protocol, or any struct or map that contains an id field. The Identifiable protocol contains a single function that must be implemented, called get_id(). You can return a string in any format to represent the id given to CosmosDb.
NOTE: You must implement the Jason.Encoder protocol for any struct that will be used as the document being created. You can do this by adding the following to your struct definition:
@dervie {Jason.Encoder, only: [....]}
defstruct ...
Every request will return a tuple containing the status of the request as well as any information Cosmos Db returned in the body of the response. The only exception is when our call to the Rest API fails for a non-CosmosDb related issue
Here are the tuple pairs returned for the following situations:
{:ok, %CosmosDbEx.Response{}}
- The operation was successful.{:bad_request, %CosmosDbEx.Response{}}
- The JSON body is invalid.{:storage_limit_reached, %CosmosDbEx.Response{}}
- The operation could not be completed because the storage limit of the partition has been reached.{:conflict, %CosmosDbEx.Response{}}
- The ID provided for the new document has been taken by an existing document.{:entity_too_large, %CosmosDbEx.Response{}}
- The document size in the request exceeded the allowable document size.{:error, error}
- Any errors encountered by our http client that aren't related to CosmosDb.
Examples
iex> item = %{ name: "ACME hair dryer", id: "ACME-HD-WOLF01234", location: "Bottom of a cliff"}
iex> container = CosmosDbEx.Container.new("database", "container")
iex> container |> CosmosDbEx.create_document(item, item.name)
{:ok,
%CosmosDbEx.Response{
body: %{
"_attachments" => "attachments/",
"_etag" => "00000000-0000-0000-0000-000000000000",
"_rid" => "AAAAAAAAAAAAAAAAA==",
"_self" => "dbs/AAAAAA==/colls/AAAAAAAAAAA=/docs/AAAAAAAAAAAAAAAAAAAAAA==/",
"_ts" => 1620141668,
"id" => "ACME-HD-WOLF01234",
"location" => "Bottom of a cliff",
"name" => "ACME hair dryer"
},
properties: %{
request_charge: "6.29",
request_duration: "5.328"
}
}
}