clients
AsyncDatabaseClient
Bases: ABC
commit(stmt)
abstractmethod
async
Run a command against the database. This is useful for statements where you need to change the database in some way E.g. ALTER, CREATE, DROP statements etc.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stmt |
str
|
The statement to run |
required |
read(query, params=None, model=None)
abstractmethod
async
Read results from postgres and return an AsyncGenerator. This allows you to read large amounts of data without having to store them in memory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query |
str
|
The query you want to return data for |
required |
params |
Dict
|
Any params you need to pass to the query |
None
|
model |
Type[BaseModel]
|
An optional pydantic.BaseModel that each row will be parsed to |
None
|
Returns:
Type | Description |
---|---|
AsyncGenerator
|
An AsyncGenerator |
read_all(query, params=None, model=None)
async
In some cases you might want to just return the data without dealing with iteration you can use this. We'll return all the records in a list. Be careful using this for large datasets as it will try and load everything in memory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query |
str
|
The query you want to return data for |
required |
params |
Dict
|
Any params you need to pass to the query |
None
|
model |
Type[BaseModel]
|
An optional pydantic.BaseModel we'll use as the row return type |
None
|
Returns:
Type | Description |
---|---|
Union[List[Dict], Type[BaseModel]]
|
A List of Records |
write(stmt, params)
abstractmethod
async
Write data to a table with the given statement and data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stmt |
str
|
The Insert statement you want to run |
required |
params |
Tuple
|
The data to pass as params |
required |
Returns:
Type | Description |
---|---|
None
|
None |
writer(stmt)
return a writer for the given statement.
Basically just curry's the write method into a coroutine that accepts a batch of parameters and writes using the given statement. Obviously this is useful if you are batching inserts.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stmt |
str
|
The Insert statement you want to run |
required |