Documentation#

class deta.Deta(project_key: Optional[str] = None, *, env: Optional[str] = None, session: Optional[ClientSession] = None, loop: Optional[AbstractEventLoop] = None)[source]#

Bases: object

Base class for Deta

Parameters:
  • project_key (str | None) – Project key to be used for requests

  • env (str | None) – Name of the environment variable to be used

  • session (aiohttp.ClientSession | None) – External client session to be used for requests

  • loop (asyncio.AbstractEventLoop | None) – Event loop to be used for requests

base(name: str) Base[source]#

Create a lazy instance of Base

Parameters:

name (str) – Name of the base

Returns:

Instance of Base

Return type:

Base

async close()[source]#

Close the client session

drive(name: str) Drive[source]#

Create a lazy instance of Drive

Parameters:

name (str) – Name of the drive

Returns:

Instance of Drive

Return type:

Drive

class deta.Base(name: str, project_id: str, session: ClientSession)[source]#

Bases: object

Represents a Deta Base instance

Parameters:
  • name (str) – Name of the base

  • project_id (str) – Project ID to be used for requests

  • session (aiohttp.ClientSession) – External client session to be used for requests

async close()[source]#

Close the client session

async delete(key: str) Dict[str, Any][source]#

Delete a record from the base

Parameters:

key (str) – Key of the record to be deleted

Returns:

Response from the API

Return type:

Dict[str, Any]

Notes

If the key does not exist, the API will still return a 200 response with the following body:

{"key": "key"}

async fetch(queries: Optional[List[Query]] = None, *, limit: Optional[int] = None, last: Optional[str] = None, sort: bool = False) Dict[str, Any][source]#

Fetch records from the base

Parameters:
  • queries (List[Query] | None) – List of Query objects to be applied to the fetch operation

  • limit (int | None) – Maximum number of records to be fetched (defaults to 1000)

  • last (str | None) – Key of the last record fetched in the previous fetch operation

  • sort (bool) – Whether to sort the results by key in descending order (defaults to False)

Returns:

Response from the API

Return type:

Dict[str, Any]

Raises:

BadRequest – If request body is invalid

async fetch_all(queries: Optional[List[Query]] = None) List[Dict[str, Any]][source]#

Fetch all records from the base

Parameters:

queries (List[Query] | None) – List of Query objects to be applied to the fetch operation

Returns:

List of records fetched from the base

Return type:

List[Dict[str, Any]]

Raises:

BadRequest – If request body is invalid

async get(key: str) Dict[str, Any][source]#

Get a record from the base

Parameters:

key (str) – Key of the record to be fetched

Returns:

Response from the API

Return type:

Dict[str, Any]

Raises:
  • ValueError – If key is empty or None

  • NotFound – If the key does not exist in the base

async insert(record: Record) Dict[str, Any][source]#

Insert a record into the base

Parameters:

record (Record) – Record to be inserted into the base

Returns:

Response from the API

Return type:

Dict[str, Any]

Raises:
async put(*records: Record)[source]#

Put records into the base (max 25 records at a time)

Parameters:

*records (Tuple[Record]) – Records to be put into the base

Returns:

Response from the API

Return type:

Dict[str, Any]

Raises:
  • ValueError – If no records are provided or more than 25 records are provided

  • BadRequest – If request body is invalid

async update(key: str, updater: Updater) Dict[str, Any][source]#

Update a record in the base

Parameters:
  • key (str) – Key of the record to be updated

  • updater (Updater) – Object containing the update operations

Returns:

Response from the API

Return type:

Dict[str, Any]

Raises:
  • ValueError – If key is empty or None

  • NotFound – If the key does not exist in the base

  • BadRequest – If request body is invalid

class deta.Drive(name: str, project_key: str, session: ClientSession)[source]#

Bases: object

Represents a Deta Drive instance

Parameters:
  • name (str) – Name of the drive

  • project_key (str) – Project key of the drive

  • session (aiohttp.ClientSession) – External client session to be used for requests

async close()[source]#

Close the client session

async delete(*names: str) Dict[str, Any][source]#

Delete files from the drive

Parameters:

*names (Tuple[str]) – Names of the files to delete

Returns:

Response from the API

Return type:

Dict[str, Any]

async files(limit: Optional[int] = None, prefix: Optional[str] = None, last: Optional[str] = None) Dict[str, Any][source]#

Get a list of files in the drive

if no parameters are provided, names of all files in the drive are returned

Parameters:
  • limit (int | None) – Number of files to return

  • prefix (str | None) – Prefix to filter files by

  • last (str | None) – Last id returned in the previous request

Returns:

Response from the API

Return type:

Dict[str, Any]

async get(name: str, *, _range: Optional[Tuple[int, int]] = None) StreamReader[source]#

Get a file from the drive

Parameters:
  • name (str) – Name of the file to get

  • _range (Tuple[int, int] | None) – Range of bytes to get from the remote file buffer

Returns:

The file content as a stream reader

Return type:

aiohttp.StreamReader

Raises:
async put(content: bytes, *, save_as: Optional[str], folder: Optional[str] = None, content_type: Optional[str] = 'application/octet-stream') Dict[str, Any][source]#

Put a file into the drive

Parameters:
  • content (bytes) – Content of the file in bytes

  • save_as (str) – Name of the file to be saved as

  • folder (str | None) – Name of the folder to put the file in

  • content_type (str | None) – Content type of the file

Returns:

Response from the API

Return type:

Dict[str, Any]

Raises:
  • BadRequest – If request body is invalid

  • NotFound – If the file is not found during chunked upload finalization

  • PayloadTooLarge – If the file size is greater than 10MB for direct upload or single chunk of chunked upload

  • IncompleteUpload – If the file is not uploaded completely during chunked upload

async size_of(name: str) int[source]#

Get the size of a file in the drive in bytes

Parameters:

name (str) – Name of the file to get the size of

class deta.Query[source]#

Bases: object

Represents a query to be used in the base

contains(field: str, value: Any)[source]#

Works as contains operator in the query (in)

equals(field: str, value: Any)[source]#

Works as equality operator in the query (==)

greater_equal(field: str, value: Any)[source]#

Works as greater than or equal to operator in the query (>=)

greater_than(field: str, value: Any)[source]#

Works as greater than operator in the query (>)

less_equal(field: str, value: Any)[source]#

Works as less than or equal to operator in the query (<=)

less_than(field: str, value: Any)[source]#

Works as less than operator in the query (<)

not_contains(field: str, value: Any)[source]#

Works as not contains operator in the query (not in)

not_equals(field: str, value: Any)[source]#

Works as inequality operator in the query (!=)

prefix(field: str, value: str)[source]#

Works as prefix operator in the query (startswith)

range(field: str, start: Union[int, float], end: Union[int, float])[source]#

Works as range operator in the query (range(start, end))

class deta.Updater[source]#

Bases: object

Represents an updater to update a record in the base

append(field: str, value: List[Any])[source]#

Append a value to a field (must be a list)

Parameters:
  • field (str) – Field to be appended to

  • value (List[Any]) – Value to be appended

delete(field: str)[source]#

Delete a field from the record

Parameters:

field (str) – Field to be deleted

increment(field: str, value: Union[int, float] = 1)[source]#

Increment a field by a value (default: 1)

Parameters:
  • field (str) – Field to be incremented

  • value (Union[int, float], optional) – Value to be incremented by, by default 1

prepend(field: str, value: List[Any])[source]#

Prepend a value to a field (must be a list)

Parameters:
  • field (str) – Field to be prepended to

  • value (List[Any]) – Value to be prepended

set(field: str, value: Any)[source]#

Set a value to a field

Parameters:
  • field (str) – Field to be set

  • value (Any) – Value to be set

class deta.Record[source]#

Bases: TypedDict

Represents a record to be put into the base

Parameters:
  • key (str) – Key of the record

  • expire_at (datetime) – Unix time at which the record will expire

  • expire_after (int | float) – Time in seconds after which the record will expire

class deta.Unauthorized(message)[source]#

Bases: Exception

Raised when the API key is invalid

class deta.NotFound(message)[source]#

Bases: Exception

Raised when a resource is not found

class deta.BadRequest(message)[source]#

Bases: Exception

Raised when a request body is invalid

class deta.PayloadTooLarge(message)[source]#

Bases: Exception

Raised when the payload size exceeds the limit of 10MB

class deta.KeyConflict(message)[source]#

Bases: Exception

Raised when a key already exists in the base

class deta.IncompleteUpload(message)[source]#

Bases: Exception

Raised when a chunked upload is finalized without uploading all the chunks

class deta.DetaUnknownError(message)[source]#

Bases: Exception

Raised when a generic error occurs

Indices and tables#