CrudDB¶
The CrudDB class inherits from Database to provide standard CRUD operations for a database table.
It can be imported from fluid.db
:
fluid.db.CrudDB
dataclass
¶
CrudDB(
dsn,
echo=DBECHO,
pool_size=DBPOOL_MAX_SIZE,
max_overflow=DBPOOL_MAX_OVERFLOW,
metadata=MetaData(),
migration_path="",
app_name=APP_NAME,
_engine=None,
)
Bases: Database
A :class:.Database
with additional methods for CRUD operations
dsn
instance-attribute
¶
data source name, aka connection string
Example: "postgresql+asyncpg://user:password@localhost/dbname"
engine
property
¶
The :class:sqlalchemy.ext.asyncio.AsyncEngine
creating connection
and transactions
from_env
classmethod
¶
Create a new database container from environment variables as defaults
Source code in fluid/db/container.py
cli
¶
connection
async
¶
Context manager for obtaining an asynchronous connection
ensure_connection
async
¶
Context manager for obtaining an asynchronous connection
Source code in fluid/db/container.py
transaction
async
¶
Context manager for initializing an asynchronous database transaction
ensure_transaction
async
¶
Context manager for ensuring we a connection has initialized a database transaction
Source code in fluid/db/container.py
close
async
¶
ping
async
¶
migration
¶
db_select
async
¶
Select rows from a given table
:param table: sqlalchemy Table
:param filters: key-value pairs for filtering rows
:param conn: optional db connection
:param consumer: optional consumer (see :meth:.get_query
)
Source code in fluid/db/crud.py
db_insert
async
¶
Perform an insert into a table :param table: sqlalchemy Table :param data: key-value pairs for columns values :param conn: optional db connection
Source code in fluid/db/crud.py
db_update
async
¶
Perform an update of rows
:param table: sqlalchemy Table
:param filters: key-value pairs for filtering rows to update
:param data: key-value pairs for updating columns values of selected rows
:param conn: optional db connection
:param consumer: optional consumer (see :meth:.get_query
)
Source code in fluid/db/crud.py
db_upsert
async
¶
Perform an upsert for a single record
:param table: sqlalchemy Table
:param filters: key-value pairs for filtering rows to update
:param data: key-value pairs for updating columns values of selected rows
:param conn: optional db connection
:param consumer: optional consumer (see :meth:.get_query
)
Source code in fluid/db/crud.py
db_delete
async
¶
Delete rows from a given table
:param table: sqlalchemy Table
:param filters: key-value pairs for filtering rows
:param conn: optional db connection
:param consumer: optional consumer (see :meth:.get_query
)
Source code in fluid/db/crud.py
db_count
async
¶
Count rows in a table
:param table: sqlalchemy Table
:param filters: key-value pairs for filtering rows
:param conn: optional db connection
:param consumer: optional consumer (see :meth:.get_query
)
Source code in fluid/db/crud.py
insert_query
¶
Source code in fluid/db/crud.py
get_query
¶
Build an SqlAlchemy query :param table: sqlalchemy Table :param sql_query: sqlalchemy query type :param params: key-value pairs for the query :param consumer: optional consumer for manipulating parameters
Source code in fluid/db/crud.py
db_count_query
¶
order_by_query
¶
order_by_columns
¶
Apply ordering to a sql_query
Source code in fluid/db/crud.py
search_query
¶
Apply search to a sql_query
Source code in fluid/db/crud.py
default_filter_column
¶
Applies a filter on a field. Notes on 'ne' op: Example data: [None, 'john', 'roger'] ne:john would return only roger (i.e. nulls excluded) ne: would return john and roger Notes on 'search' op: For some reason, SQLAlchemy uses to_tsquery rather than plainto_tsquery for the match operator to_tsquery uses operators (&, |, ! etc.) while plainto_tsquery tokenises the input string and uses AND between tokens, hence plainto_tsquery is what we want here For other database back ends, the behaviour of the match operator is completely different - see: http://docs.sqlalchemy.org/en/rel_1_0/core/sqlelement.html :param field: field name :param op: 'eq', 'ne', 'gt', 'lt', 'ge', 'le' or 'search' :param value: comparison value, string or list/tuple :return: