DatabaseProxy Class
An interface to interact with a specific database.
This class should not be instantiated directly. Instead use the get_database_client method.
A database contains one or more containers, each of which can contain items, stored procedures, triggers, and user-defined functions.
A database can also have associated users, each of which is configured with a set of permissions for accessing certain containers, stored procedures, triggers, user-defined functions, or items.
An Azure Cosmos DB SQL API database has the following system-generated properties. These properties are read-only:
_rid: The resource ID.
_ts: When the resource was last updated. The value is a timestamp.
_self: The unique addressable URI for the resource.
_etag: The resource etag required for optimistic concurrency control.
_colls: The addressable path of the collections resource.
_users: The addressable path of the users resource.
Constructor
DatabaseProxy(client_connection: CosmosClientConnection, id: str, properties: dict[str, Any] | None = None)
Parameters
| Name | Description |
|---|---|
|
client_connection
Required
|
<xref:ClientSession>
Client from which this database was retrieved. |
|
id
Required
|
ID (name) of the database. |
|
properties
|
Default value: None
|
Variables
| Name | Description |
|---|---|
|
id
|
The ID (name) of the database. |
Methods
| create_container |
Create a new container with the given ID (name). If a container with the given ID already exists, a CosmosResourceExistsError is raised. |
| create_container_if_not_exists |
Create a container if it does not exist already. If the container already exists, the existing settings are returned. Note: it does not check or update the existing container settings or offer throughput if they differ from what was passed into the method. |
| create_user |
Create a new user in the container. To update or replace an existing user, use the <xref:ContainerProxy.upsert_user> method. |
| delete_container |
Delete a container. |
| delete_user |
Delete the specified user from the container. |
| get_container_client |
Get a ContainerProxy for a container with specified ID (name). |
| get_throughput |
Get the ThroughputProperties object for this database. If no ThroughputProperties already exist for the database, an exception is raised. |
| get_user_client |
Get a UserProxy for a user with specified ID. |
| list_containers |
List the containers in the database. |
| list_users |
List all the users in the container. |
| query_containers |
List the properties for containers in the current database. |
| query_users |
Return all users matching the given query. |
| read |
Read the database properties. |
| read_offer |
Get the ThroughputProperties object for this database. If no ThroughputProperties already exist for the database, an exception is raised. |
| replace_container |
Reset the properties of the container. Property changes are persisted immediately. Any properties not specified will be reset to their default values. |
| replace_throughput |
Replace the database-level throughput. |
| replace_user |
Replaces the specified user if it exists in the container. |
| upsert_user |
Insert or update the specified user. If the user already exists in the container, it is replaced. If the user does not already exist, it is inserted. |
create_container
Create a new container with the given ID (name).
If a container with the given ID already exists, a CosmosResourceExistsError is raised.
create_container(id: str, partition_key: PartitionKey, indexing_policy: dict[str, Any] | None = None, default_ttl: int | None = None, populate_query_metrics: bool | None = None, offer_throughput: int | ThroughputProperties | None = None, unique_key_policy: dict[str, Any] | None = None, conflict_resolution_policy: dict[str, Any] | None = None, *, initial_headers: dict[str, str] | None = None, analytical_storage_ttl: int | None = None, computed_properties: list[dict[str, str]] | None = None, vector_embedding_policy: dict[str, Any] | None = None, change_feed_policy: dict[str, Any] | None = None, full_text_policy: dict[str, Any] | None = None, return_properties: Literal[False] = False, **kwargs: Any) -> ContainerProxy
Parameters
| Name | Description |
|---|---|
|
args
Required
|
args |
|
id
Required
|
ID (name) of container to create. |
|
partition_key
Required
|
The partition key to use for the container. |
|
indexing_policy
Required
|
The indexing policy to apply to the container. |
|
default_ttl
Required
|
Default time to live (TTL) for items in the container. If unused, items do not expire. |
|
offer_throughput
Required
|
The provisioned throughput for this offer. |
|
unique_key_policy
Required
|
The unique key policy to apply to the container. |
|
conflict_resolution_policy
Required
|
The conflict resolution policy to apply to the container. |
Keyword-Only Parameters
| Name | Description |
|---|---|
|
initial_headers
|
Initial headers to be sent as part of the request. |
|
response_hook
|
A callable invoked with the response metadata. |
|
analytical_storage_ttl
|
Analytical store time to live (TTL) for items in the container. A value of None leaves analytical storage off and a value of -1 turns analytical storage on with no TTL. Please note that analytical storage can only be enabled on Synapse Link enabled accounts. |
|
computed_properties
|
Sets The computed properties for this container in the Azure Cosmos DB Service. For more Information on how to use computed properties visit here: https://free.blessedness.top/azure/cosmos-db/nosql/query/computed-properties?tabs=dotnet |
|
vector_embedding_policy
|
The vector embedding policy for the container. Each vector embedding possesses a predetermined number of dimensions, is associated with an underlying data type, and is generated for a particular distance function. |
|
change_feed_policy
|
The change feed policy to apply 'retentionDuration' to the container. |
|
full_text_policy
|
provisional The full text policy for the container. Used to denote the default language to be used for all full text indexes, or to individually assign a language to each full text index path. |
|
return_properties
|
Specifies whether to return either a ContainerProxy or a Tuple of a ContainerProxy and the container properties. |
Returns
| Type | Description |
|---|---|
|
A ContainerProxy instance representing the new container or a tuple of the ContainerProxy and CosmosDict with the container properties. |
Exceptions
| Type | Description |
|---|---|
|
The container creation failed. |
Examples
Create a container with default settings:
container_name = "products"
try:
container = database.create_container(
id=container_name, partition_key=PartitionKey(path="/productName")
)
except exceptions.CosmosResourceExistsError:
container = database.get_container_client(container_name)
Create a container with specific settings; in this case, a custom partition key:
customer_container_name = "customers"
try:
customer_container = database.create_container(
id=customer_container_name,
partition_key=PartitionKey(path="/city"),
default_ttl=200,
)
except exceptions.CosmosResourceExistsError:
customer_container = database.get_container_client(customer_container_name)
create_container_if_not_exists
Create a container if it does not exist already.
If the container already exists, the existing settings are returned. Note: it does not check or update the existing container settings or offer throughput if they differ from what was passed into the method.
create_container_if_not_exists(id: str, partition_key: PartitionKey, indexing_policy: dict[str, Any] | None = None, default_ttl: int | None = None, populate_query_metrics: bool | None = None, offer_throughput: int | ThroughputProperties | None = None, unique_key_policy: dict[str, Any] | None = None, conflict_resolution_policy: dict[str, Any] | None = None, *, initial_headers: dict[str, str] | None = None, analytical_storage_ttl: int | None = None, computed_properties: list[dict[str, str]] | None = None, vector_embedding_policy: dict[str, Any] | None = None, change_feed_policy: dict[str, Any] | None = None, full_text_policy: dict[str, Any] | None = None, return_properties: Literal[False] = False, **kwargs: Any) -> ContainerProxy
Parameters
| Name | Description |
|---|---|
|
args
Required
|
args |
|
id
Required
|
ID (name) of container to create. |
|
partition_key
Required
|
The partition key to use for the container. |
|
indexing_policy
Required
|
The indexing policy to apply to the container. |
|
default_ttl
Required
|
Default time to live (TTL) for items in the container. If unused, items do not expire. |
|
offer_throughput
Required
|
The provisioned throughput for this offer. |
|
unique_key_policy
Required
|
The unique key policy to apply to the container. |
|
conflict_resolution_policy
Required
|
The conflict resolution policy to apply to the container. |
Keyword-Only Parameters
| Name | Description |
|---|---|
|
initial_headers
|
Initial headers to be sent as part of the request. |
|
response_hook
|
A callable invoked with the response metadata. |
|
analytical_storage_ttl
|
Analytical store time to live (TTL) for items in the container. A value of None leaves analytical storage off and a value of -1 turns analytical storage on with no TTL. Please note that analytical storage can only be enabled on Synapse Link enabled accounts. |
|
computed_properties
|
Sets The computed properties for this container in the Azure Cosmos DB Service. For more Information on how to use computed properties visit here: https://free.blessedness.top/azure/cosmos-db/nosql/query/computed-properties?tabs=dotnet |
|
vector_embedding_policy
|
The vector embedding policy for the container. Each vector embedding possesses a predetermined number of dimensions, is associated with an underlying data type, and is generated for a particular distance function. |
|
change_feed_policy
|
The change feed policy to apply 'retentionDuration' to the container. |
|
full_text_policy
|
provisional The full text policy for the container. Used to denote the default language to be used for all full text indexes, or to individually assign a language to each full text index path. |
|
return_properties
|
Specifies whether to return either a ContainerProxy or a Tuple of a ContainerProxy and the container properties. |
Returns
| Type | Description |
|---|---|
|
A ContainerProxy instance representing the new container or a tuple of the ContainerProxy and CosmosDict with the container properties. |
Exceptions
| Type | Description |
|---|---|
|
The container read or creation failed. |
create_user
Create a new user in the container.
To update or replace an existing user, use the <xref:ContainerProxy.upsert_user> method.
create_user(body: dict[str, Any], **kwargs: Any) -> UserProxy
Parameters
| Name | Description |
|---|---|
|
body
Required
|
A dict-like object with an id key and value representing the user to be created. The user ID must be unique within the database, and consist of no more than 255 characters. |
Keyword-Only Parameters
| Name | Description |
|---|---|
|
response_hook
|
A callable invoked with the response metadata. |
Returns
| Type | Description |
|---|---|
|
A UserProxy instance representing the new user. |
Exceptions
| Type | Description |
|---|---|
|
If the given user couldn't be created. |
Examples
Create a database user:
try:
database.create_user(dict(id="Walter Harp"))
except exceptions.CosmosResourceExistsError:
print("A user with that ID already exists.")
except exceptions.CosmosHttpResponseError as failure:
print("Failed to create user. Status code:{}".format(failure.status_code))
delete_container
Delete a container.
delete_container(container: str | ContainerProxy | Mapping[str, Any], populate_query_metrics: bool | None = None, *, initial_headers: dict[str, str] | None = None, **kwargs: Any) -> None
Parameters
| Name | Description |
|---|---|
|
container
Required
|
The ID (name) of the container to delete. You can either pass in the ID of the container to delete, a ContainerProxy instance or a dict representing the properties of the container. |
|
populate_query_metrics
|
Default value: None
|
Keyword-Only Parameters
| Name | Description |
|---|---|
|
session_token
|
Token for use with Session consistency. |
|
initial_headers
|
Initial headers to be sent as part of the request. Default value: None
|
|
etag
|
An ETag value, or the wildcard character (*). Used to check if the resource has changed, and act according to the condition specified by the match_condition parameter. |
|
match_condition
|
The match condition to use upon the etag. |
|
response_hook
|
A callable invoked with the response metadata. |
Returns
| Type | Description |
|---|---|
Exceptions
| Type | Description |
|---|---|
|
If the container couldn't be deleted. |
delete_user
Delete the specified user from the container.
delete_user(user: str | UserProxy | Mapping[str, Any], **kwargs: Any) -> None
Parameters
| Name | Description |
|---|---|
|
user
Required
|
The ID (name), dict representing the properties or UserProxy instance of the user to be deleted. |
Keyword-Only Parameters
| Name | Description |
|---|---|
|
response_hook
|
A callable invoked with the response metadata. |
Returns
| Type | Description |
|---|---|
Exceptions
| Type | Description |
|---|---|
|
The user wasn't deleted successfully. |
|
|
The user does not exist in the container. |
get_container_client
Get a ContainerProxy for a container with specified ID (name).
get_container_client(container: str | ContainerProxy | Mapping[str, Any]) -> ContainerProxy
Parameters
| Name | Description |
|---|---|
|
container
Required
|
The ID (name) of the container, a ContainerProxy instance, or a dict representing the properties of the container to be retrieved. |
Returns
| Type | Description |
|---|---|
|
A ContainerProxy instance representing the retrieved database. |
Examples
Get an existing container, handling a failure if encountered:
database = client.get_database_client(database_name)
container = database.get_container_client(container_name)
get_throughput
Get the ThroughputProperties object for this database.
If no ThroughputProperties already exist for the database, an exception is raised.
get_throughput(*, response_hook: Callable[[Mapping[str, Any], list[dict[str, Any]]], None] | None = None, **kwargs: Any) -> ThroughputProperties
Keyword-Only Parameters
| Name | Description |
|---|---|
|
response_hook
|
A callable invoked with the response metadata. Default value: None
|
Returns
| Type | Description |
|---|---|
|
ThroughputProperties for the database. |
Exceptions
| Type | Description |
|---|---|
|
No throughput properties exists for the container or the throughput properties could not be retrieved. |
get_user_client
Get a UserProxy for a user with specified ID.
get_user_client(user: str | UserProxy | Mapping[str, Any]) -> UserProxy
Parameters
| Name | Description |
|---|---|
|
user
Required
|
The ID (name), dict representing the properties or UserProxy instance of the user to be retrieved. |
Returns
| Type | Description |
|---|---|
|
A UserProxy instance representing the retrieved user. |
list_containers
List the containers in the database.
list_containers(max_item_count: int | None = None, populate_query_metrics: bool | None = None, *, initial_headers: dict[str, str] | None = None, response_hook: Callable[[Mapping[str, Any], ItemPaged[dict[str, Any]]], None] | None = None, **kwargs: Any) -> ItemPaged[dict[str, Any]]
Parameters
| Name | Description |
|---|---|
|
max_item_count
|
Max number of items to be returned in the enumeration operation. Default value: None
|
|
populate_query_metrics
|
Default value: None
|
Keyword-Only Parameters
| Name | Description |
|---|---|
|
session_token
|
Token for use with Session consistency. |
|
initial_headers
|
Initial headers to be sent as part of the request. Default value: None
|
|
response_hook
|
A callable invoked with the response metadata. Default value: None
|
Returns
| Type | Description |
|---|---|
|
An Iterable of container properties (dicts). |
Examples
List all containers in the database:
database = client.get_database_client(database_name)
for container_dict in database.list_containers():
print("Container ID: {}".format(container_dict['id']))
list_users
List all the users in the container.
list_users(max_item_count: int | None = None, *, response_hook: Callable[[Mapping[str, Any], ItemPaged[dict[str, Any]]], None] | None = None, **kwargs: Any) -> ItemPaged[dict[str, Any]]
Parameters
| Name | Description |
|---|---|
|
max_item_count
|
Max number of users to be returned in the enumeration operation. Default value: None
|
Keyword-Only Parameters
| Name | Description |
|---|---|
|
response_hook
|
A callable invoked with the response metadata. Default value: None
|
Returns
| Type | Description |
|---|---|
|
An Iterable of user properties (dicts). |
query_containers
List the properties for containers in the current database.
query_containers(query: str | None = None, parameters: list[dict[str, Any]] | None = None, max_item_count: int | None = None, populate_query_metrics: bool | None = None, *, initial_headers: dict[str, str] | None = None, response_hook: Callable[[Mapping[str, Any], ItemPaged[dict[str, Any]]], None] | None = None, **kwargs: Any) -> ItemPaged[dict[str, Any]]
Parameters
| Name | Description |
|---|---|
|
query
|
The Azure Cosmos DB SQL query to execute. Default value: None
|
|
parameters
|
Optional array of parameters to the query. Ignored if no query is provided. Default value: None
|
|
max_item_count
|
Max number of items to be returned in the enumeration operation. Default value: None
|
|
populate_query_metrics
|
Default value: None
|
Keyword-Only Parameters
| Name | Description |
|---|---|
|
initial_headers
|
Initial headers to be sent as part of the request. Default value: None
|
|
response_hook
|
A callable invoked with the response metadata. Default value: None
|
Returns
| Type | Description |
|---|---|
|
An Iterable of container properties (dicts). |
query_users
Return all users matching the given query.
query_users(query: str, parameters: list[dict[str, Any]] | None = None, max_item_count: int | None = None, *, response_hook: Callable[[Mapping[str, Any], ItemPaged[dict[str, Any]]], None] | None = None, **kwargs: Any) -> ItemPaged[dict[str, Any]]
Parameters
| Name | Description |
|---|---|
|
query
Required
|
The Azure Cosmos DB SQL query to execute. |
|
parameters
|
Optional array of parameters to the query. Ignored if no query is provided. Default value: None
|
|
max_item_count
|
Max number of users to be returned in the enumeration operation. Default value: None
|
Keyword-Only Parameters
| Name | Description |
|---|---|
|
response_hook
|
A callable invoked with the response metadata. Default value: None
|
Returns
| Type | Description |
|---|---|
|
An Iterable of user properties (dicts). |
read
Read the database properties.
read(populate_query_metrics: bool | None = None, *, initial_headers: dict[str, str] | None = None, **kwargs: Any) -> CosmosDict
Parameters
| Name | Description |
|---|---|
|
populate_query_metrics
|
Default value: None
|
Keyword-Only Parameters
| Name | Description |
|---|---|
|
initial_headers
|
Initial headers to be sent as part of the request. Default value: None
|
|
response_hook
|
A callable invoked with the response metadata. |
Returns
| Type | Description |
|---|---|
|
A dict representing the database properties. |
Exceptions
| Type | Description |
|---|---|
|
If the given database couldn't be retrieved. |
read_offer
Get the ThroughputProperties object for this database.
If no ThroughputProperties already exist for the database, an exception is raised.
read_offer(**kwargs: Any) -> ThroughputProperties
Keyword-Only Parameters
| Name | Description |
|---|---|
|
response_hook
|
A callable invoked with the response metadata. |
Returns
| Type | Description |
|---|---|
|
ThroughputProperties for the database. |
Exceptions
| Type | Description |
|---|---|
|
No throughput properties exists for the container or the throughput properties could not be retrieved. |
replace_container
Reset the properties of the container.
Property changes are persisted immediately. Any properties not specified will be reset to their default values.
replace_container(container: str | ContainerProxy | Mapping[str, Any], partition_key: PartitionKey, indexing_policy: dict[str, Any] | None = None, default_ttl: int | None = None, conflict_resolution_policy: dict[str, Any] | None = None, populate_query_metrics: bool | None = None, *, initial_headers: dict[str, str] | None = None, analytical_storage_ttl: int | None = None, computed_properties: list[dict[str, str]] | None = None, full_text_policy: dict[str, Any] | None = None, return_properties: Literal[False] = False, vector_embedding_policy: dict[str, Any] | None = None, **kwargs: Any) -> ContainerProxy
Parameters
| Name | Description |
|---|---|
|
args
Required
|
args |
|
container
Required
|
The ID (name), dict representing the properties or ContainerProxy instance of the container to be replaced. |
|
partition_key
Required
|
The partition key to use for the container. |
|
indexing_policy
Required
|
The indexing policy to apply to the container. |
|
default_ttl
Required
|
Default time to live (TTL) for items in the container. If unspecified, items do not expire. |
|
conflict_resolution_policy
Required
|
The conflict resolution policy to apply to the container. |
Keyword-Only Parameters
| Name | Description |
|---|---|
|
initial_headers
|
Initial headers to be sent as part of the request. |
|
analytical_storage_ttl
|
Analytical store time to live (TTL) for items in the container. A value of None leaves analytical storage off and a value of -1 turns analytical storage on with no TTL. Please note that analytical storage can only be enabled on Synapse Link enabled accounts. |
|
computed_properties
|
Sets The computed properties for this container in the Azure Cosmos DB Service. For more Information on how to use computed properties visit here: https://free.blessedness.top/azure/cosmos-db/nosql/query/computed-properties?tabs=dotnet |
|
response_hook
|
A callable invoked with the response metadata. |
|
full_text_policy
|
provisional The full text policy for the container. Used to denote the default language to be used for all full text indexes, or to individually assign a language to each full text index path. |
|
return_properties
|
Specifies whether to return either a ContainerProxy or a Tuple of a ContainerProxy and the container properties. |
Returns
| Type | Description |
|---|---|
|
A ContainerProxy instance representing the new container or a tuple of the ContainerProxy and CosmosDict with the container properties. |
Exceptions
| Type | Description |
|---|---|
|
Raised if the container couldn't be replaced. This includes if the container with given id does not exist. |
Examples
Reset the TTL property on a container, and display the updated properties:
# Set the TTL on the container to 3600 seconds (one hour)
database.replace_container(container, partition_key=PartitionKey(path='/productName'), default_ttl=3600)
# Display the new TTL setting for the container
container_props = database.get_container_client(container_name).read()
print("New container TTL: {}".format(json.dumps(container_props['defaultTtl'])))
replace_throughput
Replace the database-level throughput.
replace_throughput(throughput: int | ThroughputProperties, **kwargs: Any) -> ThroughputProperties
Parameters
| Name | Description |
|---|---|
|
throughput
Required
|
The throughput to be set (an integer). |
Keyword-Only Parameters
| Name | Description |
|---|---|
|
response_hook
|
A callable invoked with the response metadata. |
Returns
| Type | Description |
|---|---|
|
ThroughputProperties for the database, updated with new throughput. |
Exceptions
| Type | Description |
|---|---|
|
If no throughput properties exists for the database or if the throughput properties could not be updated. |
replace_user
Replaces the specified user if it exists in the container.
replace_user(user: str | UserProxy | Mapping[str, Any], body: dict[str, Any], **kwargs: Any) -> UserProxy
Parameters
| Name | Description |
|---|---|
|
user
Required
|
The ID (name), dict representing the properties or UserProxy instance of the user to be replaced. |
|
body
Required
|
A dict-like object representing the user to replace. |
Keyword-Only Parameters
| Name | Description |
|---|---|
|
response_hook
|
A callable invoked with the response metadata. |
Returns
| Type | Description |
|---|---|
|
A UserProxy instance representing the user after replace went through. |
Exceptions
| Type | Description |
|---|---|
|
If the replace operation failed or the user with given ID does not exist. |
upsert_user
Insert or update the specified user.
If the user already exists in the container, it is replaced. If the user does not already exist, it is inserted.
upsert_user(body: dict[str, Any], **kwargs: Any) -> UserProxy
Parameters
| Name | Description |
|---|---|
|
body
Required
|
A dict-like object representing the user to update or insert. |
Keyword-Only Parameters
| Name | Description |
|---|---|
|
response_hook
|
A callable invoked with the response metadata. |
Returns
| Type | Description |
|---|---|
|
A UserProxy instance representing the upserted user. |
Exceptions
| Type | Description |
|---|---|
|
If the given user could not be upserted. |