asyncpg_simpleorm package¶
Subpackages¶
Submodules¶
asyncpg_simpleorm.abstract module¶
-
class
asyncpg_simpleorm.abstract.AsyncContextManagerABC(*args, **kwargs)[source]¶ Bases:
objectAbstract representation of an async context manager.
Ensures a class has
__aenter__()and__aexit__()methods.
-
class
asyncpg_simpleorm.abstract.AsyncModelABC(*args, **kwargs)[source]¶ Bases:
objectAbstract representation of an async database model.
Ensures that an object declares,
column_names(),tablename(),connection(),from_record(),__init_subclass__()that allows a connection manager to be registered with subclasses.-
abstractmethod classmethod
column_names()[source]¶ Return the database column names.
Return type: Iterable[str]
-
abstractmethod classmethod
connection()[source]¶ Return an async context manager, that returns an
asyncpg.Connectioninstance when used in anasync withblock.Return type: AsyncContextManagerABC
-
abstractmethod classmethod
asyncpg_simpleorm.async_model module¶
-
class
asyncpg_simpleorm.async_model.AsyncModel(**kwargs)[source]¶ Bases:
asyncpg_simpleorm.async_model.BaseModelExtends the
BaseModelclass to include helpful database queries. Implements theAsyncModelABCinterface.By default
get()andget_one()methods returnasyncpg.Recordinstances. This option can be toggled class wide, by setting a class attributereturn_recordstoFalseon a subclass, or can be toggled during a single method callThis class uses the
__init_subclass__()syntax new inpython-3.6. That allowskwargsto be passed into the class declaration.A user should subclass this providing a
ConnectionManagerorPoolManagerto provide the database connection.Raises: RuntimeError – If subclass does not provide a connectionkwarg in the class declaration.Example:
''' The following example would be for a postgres table created by. CREATE TABLE users ( _id uuid PRIMARY KEY, name varchar(100) NOT NULL ) ''' import uuid DBURI = 'postgres://user:password@localhost:5432/database' class User(AsyncModel, connection=ConnectionManager(DBURI)): __tablename__ = 'users' id = Column('_id', primary_key=True, default=uuid.uuid4) name = Column() # Or using a ``PoolManager``. class User(AsyncModel, connection=PoolManager(DBURI)): ... # Create an instance using ``kwargs`` user = User(name='foo') print(user) # User(id=95ccbcd3-2ded-4ad8-9f68-d60f0b9590a9, name='foo')
-
classmethod
connection()[source]¶ Obtain the connection manager that was registered with the subclass during creation.
This is typically used with
async withto gain access to anasyncpg.Connectioninstance.Example:
>>> async with User.connection() as conn: # do something with the connection.
Return type: Union[ConnectionManager,PoolManager]
-
await
delete()[source]¶ Delete an instance from the database.
Raises: exceptions.ExecutionFailure – If no records were deleted from the database. Return type: None
-
classmethod await
execute(*args, **kwargs)[source]¶ Execute’s a database query inside a transaction block. This cuts down on the boiler plate nested
async withstatements to execute a query.This is equivalent to:
>>> async with Model.connection() as conn: ... async with conn.transaction(): ... return await conn.execute(*args, **kwargs)
-
classmethod
from_record()[source]¶ Return an instance of the class from an
asyncpg.Recordobject.Parameters: record ( Record) – Anasyncpg.Recordinstance to create a database model instance from.Return type: Any
-
classmethod await
get(**kwargs)[source]¶ Get a list of the table items from the database.
Parameters: - records (
Optional[bool]) – Optional bool. IfTruethen we returnasyncpg.Record’s. IfFalsethen we will return instances of theAsyncModelsubclass. IfNone, then we default to what’s set on the subclass at the return_records attribute (default isTrue). - kwargs – Optional kwargs that are passed into a
whereclause.
Example:
>>> await User.get(records=False) [User(id=95ccbcd3-2ded-4ad8-9f68-d60f0b9590a9, name='foo'), ...] >>> await User.get() [<Record(_id=UUID('95ccbcd3-2ded-4ad8-9f68-d60f0b9590a9'), name='foo'>, ...]
Return type: Iterable[Any]- records (
-
classmethod await
get_one(**kwargs)[source]¶ Get a single item from the database.
Parameters: - record (
Optional[bool]) – Optional bool. IfTruethen we returnasyncpg.Record’s. IfFalsethen we will return instances of theAsyncModelsubclass. IfNone, then we default to what’s set on the subclass at the return_records attribute (default isTrue). - kwargs – Optional kwargs that are passed into a
whereclause.
Example:
>>> await User.get_one(name='foo') <Record _id=UUID('95ccbcd3-2ded-4ad8-9f68-d60f0b9590a9'), name='foo'> >>> await User.get_one(record=False, name='foo') User(id=456, name='bar')
Return type: Any- record (
-
return_records= True¶
-
await
save()[source]¶ Update or insert an instance to the database.
Raises: exceptions.ExecutionFailure – If no records were updated or saved to the database. Example:
>>> user = User(name='foo') >>> await user.save()
Return type: None
-
classmethod
-
class
asyncpg_simpleorm.async_model.BaseModel(**kwargs)[source]¶ Bases:
objectBase class for
AsyncModel. This class should typically not be used directly, unless building a custom statement generating class.This class allows column instance values to be set either by the attribute name the column was declared as, or the database column name (which are not always the same).
Example:
>>> class User(BaseModel): id = Column('_id', primary_key=True) name = Column() >>> u1 = User(id=123, name='foo') >>> u2 = User(_id=456, name='bar') >>> print(u1.id, u2.id) (123, 456)
Parameters: kwargs – Key word args that are set on an instance. These would typically be the same keysas the declaredColumn’s on a subclass.-
classmethod
attr_name_for_column()[source]¶ Get the class attribute name for a given database column name.
This is used when setting instance values for
Column’s, and allows access to the attribute name, whether thecolumn_namewas parsed using the actual database column name, or the attribute name (which can be different depending on theColumn).Parameters: column_name ( str) – The database column name to get the attribute name for.Raises: ValueError – If no Columnis found for that column name.Example:
>>> class User(BaseModel): id = Column('_id', primary_key=True) name = Column() >>> User.attr_name_for_column('_id') 'id' >>> User.attr_name_for_column('id') 'id'
Return type: str
-
classmethod
column_names()[source]¶ Returns a tuple of the column names for the class.
Return type: Tuple[str]
-
classmethod
ensured_column_name()[source]¶ This is helper to always return the database column name for the input.
This is essentially the opposite of
attr_name_for_column().Parameters: column_name – The attribute name to get the column name for. Raises: ValueError – If no Columnis found for that attribute name.Example:
>>> class User(BaseModel): id = Column('_id', primary_key=True) name = Column() >>> User.ensured_column_name('_id') '_id' >>> User.ensured_column_name('id') '_id'
Return type: str
-
classmethod
-
class
asyncpg_simpleorm.async_model.ModelMeta(*args, **kwargs)[source]¶ Bases:
typeMeta class for
BaseModel, which ensures the column’s keys for the model are set, or will default to the attribute name aColumnwas declared for.This allows the
keyattribute of aColumnto be optional.Example:
>>> class User(BaseModel): id = Column('_id', primary_key=True) name = Column() >>> User.id.key '_id' >>> User.name.key 'name'
asyncpg_simpleorm.connection_managers module¶
-
class
asyncpg_simpleorm.connection_managers.ConnectionManager(*args, keep_alive=False, **kwargs)[source]¶ Bases:
asyncpg_simpleorm.abstract.AsyncContextManagerABCAn async context manager that mimics the
asyncpg.connect()function, used with subclasses ofAsyncModel.Parameters: - args – Passed to
asyncpg.connect()function. - keep_alive (
bool) – IfTruethen the connection is kept alive for re-use. Else it is closed after use (default). - kwargs – Passed to
asyncpg.connect()function.
- args – Passed to
-
class
asyncpg_simpleorm.connection_managers.PoolManager(*args, **kwargs)[source]¶ Bases:
asyncpg_simpleorm.abstract.AsyncContextManagerABCAn async context manager that mimics the
asyncpg.create_pool()function, used with subclasses ofAsyncModel.Parameters: - args – Passed to
asyncpg.create_pool()function. - kwargs – Passed to
asyncpg.create_pool()function.
- args – Passed to
asyncpg_simpleorm.exceptions module¶
-
exception
asyncpg_simpleorm.exceptions.BaseException(*args, **kwargs)[source]¶ Bases:
ExceptionThe base exception class.
-
exception
asyncpg_simpleorm.exceptions.ExecutionFailure(*args, **kwargs)[source]¶ Bases:
asyncpg_simpleorm.exceptions.BaseException,ValueErrorRaised if execution of a database statement fails.
-
exception
asyncpg_simpleorm.exceptions.InvalidModel(*args, **kwargs)[source]¶ Bases:
asyncpg_simpleorm.exceptions.BaseException,TypeErrorRaised if a model does not pass an
issubclassorisinstancecheck againstModelABC.
asyncpg_simpleorm.table_utils module¶
-
await
asyncpg_simpleorm.table_utils.create_table(model)[source]¶ Create a database table.
Parameters: model – An AsyncModelABCsubclass to drop the table for.Return type: None
-
await
asyncpg_simpleorm.table_utils.drop_table(model, cascade=False)[source]¶ Drop a database table.
Parameters: - model – An
AsyncModelABCsubclass to drop the table for. - cascade – If
Truethen we add theCASCADEclause to the statement when executed.
Return type: None- model – An
-
asyncpg_simpleorm.table_utils.ensure_model(fn)[source]¶ Decorator to ensure the first arg derives from
AsyncModelABC.
-
await
asyncpg_simpleorm.table_utils.truncate_table(model, cascade=False)[source]¶ Truncate a table, leaving the table schema intact.
Parameters: - model – An
AsyncModelABCsubclass to drop the table for. - cascade (
bool) – IfTruethen we add theCASCADEclause to the statement when executed.
Return type: None- model – An
Module contents¶
-
class
asyncpg_simpleorm.AsyncModelABC(*args, **kwargs)[source]¶ Bases:
objectAbstract representation of an async database model.
Ensures that an object declares,
column_names(),tablename(),connection(),from_record(),__init_subclass__()that allows a connection manager to be registered with subclasses.-
abstractmethod classmethod
column_names()[source]¶ Return the database column names.
Return type: Iterable[str]
-
abstractmethod classmethod
connection()[source]¶ Return an async context manager, that returns an
asyncpg.Connectioninstance when used in anasync withblock.Return type: AsyncContextManagerABC
-
abstractmethod classmethod
-
class
asyncpg_simpleorm.AsyncContextManagerABC(*args, **kwargs)[source]¶ Bases:
objectAbstract representation of an async context manager.
Ensures a class has
__aenter__()and__aexit__()methods.
-
class
asyncpg_simpleorm.StatementABC(*args, **kwargs)[source]¶ Bases:
objectAbstract representation of a database query statement.
-
model¶ Return the
AsyncModelset on an instance.
-
abstractmethod
query()[source]¶ Return an iterable that can be used in an
asyncpgquery. The first value should be thequery_string, followed by the flattened query argsAn example return value:
('SELECT * FROM users WHERE name = $1 and email = $2', 'foo', 'foo@example.com')
Return type: Iterable[Any]
-
abstractmethod
query_args()[source]¶ Return the query arguments set on an instance.
Return type: Iterable[Any]
-
-
class
asyncpg_simpleorm.ColumnTypeABC(*args, **kwargs)[source]¶ Bases:
objectAbstract representation of a postgres column type.
-
pg_type_string¶ Return the postgres type string.
Return type: str
-
-
class
asyncpg_simpleorm.AsyncModel(**kwargs)[source]¶ Bases:
asyncpg_simpleorm.async_model.BaseModelExtends the
BaseModelclass to include helpful database queries. Implements theAsyncModelABCinterface.By default
get()andget_one()methods returnasyncpg.Recordinstances. This option can be toggled class wide, by setting a class attributereturn_recordstoFalseon a subclass, or can be toggled during a single method callThis class uses the
__init_subclass__()syntax new inpython-3.6. That allowskwargsto be passed into the class declaration.A user should subclass this providing a
ConnectionManagerorPoolManagerto provide the database connection.Raises: RuntimeError – If subclass does not provide a connectionkwarg in the class declaration.Example:
''' The following example would be for a postgres table created by. CREATE TABLE users ( _id uuid PRIMARY KEY, name varchar(100) NOT NULL ) ''' import uuid DBURI = 'postgres://user:password@localhost:5432/database' class User(AsyncModel, connection=ConnectionManager(DBURI)): __tablename__ = 'users' id = Column('_id', primary_key=True, default=uuid.uuid4) name = Column() # Or using a ``PoolManager``. class User(AsyncModel, connection=PoolManager(DBURI)): ... # Create an instance using ``kwargs`` user = User(name='foo') print(user) # User(id=95ccbcd3-2ded-4ad8-9f68-d60f0b9590a9, name='foo')
-
classmethod
connection()[source]¶ Obtain the connection manager that was registered with the subclass during creation.
This is typically used with
async withto gain access to anasyncpg.Connectioninstance.Example:
>>> async with User.connection() as conn: # do something with the connection.
Return type: Union[ConnectionManager,PoolManager]
-
await
delete()[source]¶ Delete an instance from the database.
Raises: exceptions.ExecutionFailure – If no records were deleted from the database. Return type: None
-
classmethod await
execute(*args, **kwargs)[source]¶ Execute’s a database query inside a transaction block. This cuts down on the boiler plate nested
async withstatements to execute a query.This is equivalent to:
>>> async with Model.connection() as conn: ... async with conn.transaction(): ... return await conn.execute(*args, **kwargs)
-
classmethod
from_record()[source]¶ Return an instance of the class from an
asyncpg.Recordobject.Parameters: record ( Record) – Anasyncpg.Recordinstance to create a database model instance from.Return type: Any
-
classmethod await
get(**kwargs)[source]¶ Get a list of the table items from the database.
Parameters: - records (
Optional[bool]) – Optional bool. IfTruethen we returnasyncpg.Record’s. IfFalsethen we will return instances of theAsyncModelsubclass. IfNone, then we default to what’s set on the subclass at the return_records attribute (default isTrue). - kwargs – Optional kwargs that are passed into a
whereclause.
Example:
>>> await User.get(records=False) [User(id=95ccbcd3-2ded-4ad8-9f68-d60f0b9590a9, name='foo'), ...] >>> await User.get() [<Record(_id=UUID('95ccbcd3-2ded-4ad8-9f68-d60f0b9590a9'), name='foo'>, ...]
Return type: Iterable[Any]- records (
-
classmethod await
get_one(**kwargs)[source]¶ Get a single item from the database.
Parameters: - record (
Optional[bool]) – Optional bool. IfTruethen we returnasyncpg.Record’s. IfFalsethen we will return instances of theAsyncModelsubclass. IfNone, then we default to what’s set on the subclass at the return_records attribute (default isTrue). - kwargs – Optional kwargs that are passed into a
whereclause.
Example:
>>> await User.get_one(name='foo') <Record _id=UUID('95ccbcd3-2ded-4ad8-9f68-d60f0b9590a9'), name='foo'> >>> await User.get_one(record=False, name='foo') User(id=456, name='bar')
Return type: Any- record (
-
return_records= True¶
-
await
save()[source]¶ Update or insert an instance to the database.
Raises: exceptions.ExecutionFailure – If no records were updated or saved to the database. Example:
>>> user = User(name='foo') >>> await user.save()
Return type: None
-
classmethod
-
class
asyncpg_simpleorm.BaseModel(**kwargs)[source]¶ Bases:
objectBase class for
AsyncModel. This class should typically not be used directly, unless building a custom statement generating class.This class allows column instance values to be set either by the attribute name the column was declared as, or the database column name (which are not always the same).
Example:
>>> class User(BaseModel): id = Column('_id', primary_key=True) name = Column() >>> u1 = User(id=123, name='foo') >>> u2 = User(_id=456, name='bar') >>> print(u1.id, u2.id) (123, 456)
Parameters: kwargs – Key word args that are set on an instance. These would typically be the same keysas the declaredColumn’s on a subclass.-
classmethod
attr_name_for_column()[source]¶ Get the class attribute name for a given database column name.
This is used when setting instance values for
Column’s, and allows access to the attribute name, whether thecolumn_namewas parsed using the actual database column name, or the attribute name (which can be different depending on theColumn).Parameters: column_name ( str) – The database column name to get the attribute name for.Raises: ValueError – If no Columnis found for that column name.Example:
>>> class User(BaseModel): id = Column('_id', primary_key=True) name = Column() >>> User.attr_name_for_column('_id') 'id' >>> User.attr_name_for_column('id') 'id'
Return type: str
-
classmethod
column_names()[source]¶ Returns a tuple of the column names for the class.
Return type: Tuple[str]
-
classmethod
ensured_column_name()[source]¶ This is helper to always return the database column name for the input.
This is essentially the opposite of
attr_name_for_column().Parameters: column_name – The attribute name to get the column name for. Raises: ValueError – If no Columnis found for that attribute name.Example:
>>> class User(BaseModel): id = Column('_id', primary_key=True) name = Column() >>> User.ensured_column_name('_id') '_id' >>> User.ensured_column_name('id') '_id'
Return type: str
-
classmethod
-
class
asyncpg_simpleorm.Column(key=None, _type=None, *, default=None, primary_key=False)[source]¶ Bases:
objectA descriptor class that represents a table column.
Parameters: - key (
Optional[str]) – The table column name in the database. If not set, then this will be set to the attribute name used on theAsyncModelsubclass the column was declared on. - type – The
ColumnTypesubclass to use for the column. - default (
Optional[Any]) – A value or callable that is used for a default value. If this is callable, then it should recieve no input and return a value when called. - primary_key (
bool) – Set’s if the column is a primary key column. Primary key columns are used in certain query statements, such asAsyncModel.save()
-
default¶
-
key¶
-
pg_column_string¶ Return type: str
-
primary_key¶
- key (
-
class
asyncpg_simpleorm.ColumnType(string=None)[source]¶ Bases:
objectImplementation of
ColumnTypeABC.This can be used as a generic column type, by passing a string into the constructor. That string will be used, else we will use a value that’s passed into the class declaration at
pg_type_string.Parameters: string ( Optional[str]) – An optional input string that will be used for the column type. This string is not checked or validated, so if it’s not valid errors will bubble up when trying to create the table.Examples:
>>> str(ColumnType('text')) 'text' >>> class Text(ColumnType, pg_type_string='text'): pass >>> str(Text()) 'text'
Custom subclasses should implement
__slots__. If they don’t need to set any attributes on the class (which is typically the case) and they derive fromColumnTypethen the__slots__will be automatically added to the subclass.-
pg_type_string¶ Return’s the postgres column type string for an instance.
Raises: TypeError – If neither an input string was passed in or pg_type_stringin the class declaration.Return type: str
-
-
class
asyncpg_simpleorm.String(n=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres string column. Dependening on the context this will either produce a
textcolumn orvarchar(n)column.Parameters: n ( Optional[int]) – An optional integer for the length of the string. If this is present then we usevarchar(n), else we usetext.
-
class
asyncpg_simpleorm.UUID(string=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
uuidtype.
-
class
asyncpg_simpleorm.Boolean(string=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
booltype.
-
class
asyncpg_simpleorm.Integer(string=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
integertype.
-
class
asyncpg_simpleorm.Number(string=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
numerictype.
-
class
asyncpg_simpleorm.Date(string=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
datetype.
-
class
asyncpg_simpleorm.Time(with_timezone=False)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
timeortimetztype.Parameters: with_timezone ( bool) – IfTruethen it is time with timezone. IfFalse(default) then it time without timezone.
-
class
asyncpg_simpleorm.Timestamp(with_timezone=False)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
timestamportimestamptztype.Parameters: with_timezone ( bool) – IfTruethen it is timestamp with timezone type. IfFalse(default) then it timestamp without timezone type.
-
class
asyncpg_simpleorm.TimeInterval(string=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
intervaltype.
-
class
asyncpg_simpleorm.Array(_type, n=None, dimensions=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
ARRAYtype.Parameters: - _type – The type of the elements in the array. This would be another
ColumnTypesubclass. - n – An optional
int, that will make asizedarray. - dimensions – An optional
intthat will create a dimensional array.
Example:
>>> Array(Integer()).pg_type_string integer ARRAY >>> Array(Integer(), 3).pg_type_string integer ARRAY[3] >>> Array(Integer(), 3, 3).pg_type_string integer [3][3][3]
- _type – The type of the elements in the array. This would be another
-
class
asyncpg_simpleorm.BigInteger(string=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
int8type.
-
class
asyncpg_simpleorm.Bit(n, fixed_length=False)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
bitorvarbittype.Parameters: - n – The number of bits.
- fixed_length – An optional boolean. If
Truewe will usebit(n). IfFalse(default), we will usevarbit(n).
-
class
asyncpg_simpleorm.BigSerial(string=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
serial8type.
-
class
asyncpg_simpleorm.Binary(string=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
byteatype.
-
class
asyncpg_simpleorm.FixedLengthString(n)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
chartype.Parameters: n ( int) – The number of characters.
-
class
asyncpg_simpleorm.Money(string=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
moneytype.
-
class
asyncpg_simpleorm.IPAddress(inet=False)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
cidrorinettypes.Parameters: inet – An optional bool. If Truethen we will useinet. IfFalse(default) then we usecidr.
-
class
asyncpg_simpleorm.MACAddress(string=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
macaddrtype.
-
class
asyncpg_simpleorm.Box(string=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
boxtype.
-
class
asyncpg_simpleorm.Line(string=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
linetype.
-
class
asyncpg_simpleorm.LineSegment(string=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
lsegtype.
-
class
asyncpg_simpleorm.Circle(string=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
circletype.
-
class
asyncpg_simpleorm.Path(string=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
pathtype.
-
class
asyncpg_simpleorm.Point(string=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
pointtype.
-
class
asyncpg_simpleorm.Polygon(string=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
polygontype.
-
class
asyncpg_simpleorm.Double(string=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
float8type.
-
class
asyncpg_simpleorm.Json(string=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
jsontype.
-
class
asyncpg_simpleorm.JsonB(string=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
jsonbtype.This is not supported by all postgres versions.
-
class
asyncpg_simpleorm.PGLogSequenceNumber(string=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
pg_lsntype.This is not supported by all postgres versions.
-
class
asyncpg_simpleorm.Real(string=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
float4type.
-
class
asyncpg_simpleorm.SmallInteger(string=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
int2type.
-
class
asyncpg_simpleorm.SmallSerial(string=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
serial2type.
-
class
asyncpg_simpleorm.Serial(string=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
serial4type.
-
class
asyncpg_simpleorm.TextSearchQuery(string=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
tsquerytype.
-
class
asyncpg_simpleorm.TextSearchVector(string=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
tsvectortype.
-
class
asyncpg_simpleorm.TransactionID(string=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
txid_snapshottype.
-
class
asyncpg_simpleorm.XML(string=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
xmltype.
-
class
asyncpg_simpleorm.IntegerRange(big=False)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
int4rangeorint8rangetypes.Parameters: big – A boolean if Truethen will represent anint8range. IfFalse(default) then we will useint4range.
-
class
asyncpg_simpleorm.NumericRange(string=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
numrangetype.
-
class
asyncpg_simpleorm.DateRange(string=None)[source]¶ Bases:
asyncpg_simpleorm.column.base_column_type.ColumnTypeRepresentation of postgres
daterangetype.
-
class
asyncpg_simpleorm.ConnectionManager(*args, keep_alive=False, **kwargs)[source]¶ Bases:
asyncpg_simpleorm.abstract.AsyncContextManagerABCAn async context manager that mimics the
asyncpg.connect()function, used with subclasses ofAsyncModel.Parameters: - args – Passed to
asyncpg.connect()function. - keep_alive (
bool) – IfTruethen the connection is kept alive for re-use. Else it is closed after use (default). - kwargs – Passed to
asyncpg.connect()function.
- args – Passed to
-
class
asyncpg_simpleorm.PoolManager(*args, **kwargs)[source]¶ Bases:
asyncpg_simpleorm.abstract.AsyncContextManagerABCAn async context manager that mimics the
asyncpg.create_pool()function, used with subclasses ofAsyncModel.Parameters: - args – Passed to
asyncpg.create_pool()function. - kwargs – Passed to
asyncpg.create_pool()function.
- args – Passed to
-
class
asyncpg_simpleorm.BaseStatement(model, kwargs={}, arg_count=1)[source]¶ Bases:
asyncpg_simpleorm.statements.abstract.StatementABCImplementation of the
StatementABC.-
count¶
-
kwargs¶
-
model¶ Return the database model set on an instance. And ensures that the model is derived from
AsyncModelABC.Raises: asyncpg_simpleorm.InvalidModel – If trying to set an invalid model on an instance.
-
query_args()[source]¶ Get the query args set on the instance. This will return an empty tuple if no args have been set on the instance yet.
If args are set on the instance, then they will be returned in the order corresponding to their place holder in the
query_string.Return type: Iterable[Any]
-
query_string(sep='n')[source]¶ Get the query string set on the instance.
Parameters: sep – A seperator used to join the statement and the clauses. Defaults to ‘n’. Raises: TypeError – If a statement has not yet been set on an instance. Return type: str
-
set_count(count)[source]¶ Reset’s the counter for an instance.
Parameters: count ( int) – The number to reset the counter to.Return type: None
-
set_statement(key, query_string, args=None)[source]¶ Set the statement for an instance, and return
selffor method chaining.Return type: BaseStatement
-
-
class
asyncpg_simpleorm.Statement(model, kwargs={}, arg_count=1)[source]¶ Bases:
asyncpg_simpleorm.statements.base_statement.BaseStatementExtends the
BaseStatementclass. This class is normally not instantiated directly, but returned from one of the statement factory functions.-
count¶
-
delete()[source]¶ Set’s the statement as a
DELETEstatement. This will automatically set awherestatement, using the primary keys.
-
insert(**kwargs)[source]¶ Set’s the statement as an
INSERTstatement.Parameters: kwargs – Values to use as (column names, query args) for the statement. These are probably not used much as you should normally just set the model to an instance of ModelABCfor the column names and values.
-
kwargs¶
-
update(**kwargs)[source]¶ Set’s the statement as an
UPDATEstatement. This will automatically set awherestatement, using the primary keys.Parameters: kwargs – Values to use as column names, query args for the statement. These are probably not used much as you should normally just set the model to an instance of ModelABCfor the column names and values.Raises: TypeError – If no args were able to be parsed for the statement.
-
where(primary_keys=False, safe_call=False, **kwargs)[source]¶ Add’s the
wherestring to the statement.Parameters: - primary_keys – If
True, only parse primary keys for thewherestring and args. This is primarily an implementation detail used in conjunction with other statement callbacks, and typically not necessarily set by a caller. - safe_call – An optional boolean to suppress errors. Default is
False. - kwargs – An optional mapping of column names and values to use for the where statement.
Raises: - ValueError – If not using the primary keys and the
kwargsset on the instance include an invalid key. - TypeError – If nothing was able to be parsed.
- primary_keys – If
-
-
asyncpg_simpleorm.delete(model, **kwargs)[source]¶ Delete statement factory.
Parameters: - model – A
ModelABCsubclass instance to create the statement for. - kwargs – Used for instance values, if the statement is created with a class, not an instance.
- model – A
-
asyncpg_simpleorm.insert(model=None, **kwargs)[source]¶ Insert statement factory.
Parameters: - model – A
ModelABCsubclass instance to create the statement for. - kwargs – Used for instance values, if the statement is created with a class, not an instance.
- model – A
-
asyncpg_simpleorm.select(model)[source]¶ Select statement factory.
Parameters: model – A ModelABCsubclass to create the statement for.
-
asyncpg_simpleorm.update(model=None, **kwargs)[source]¶ Update statement factory.
Parameters: - model – A
ModelABCsubclass instance to create the statement for. - kwargs – Used for instance values, if the statement is created with a class, not an instance.
- model – A
-
await
asyncpg_simpleorm.create_table(model)[source]¶ Create a database table.
Parameters: model – An AsyncModelABCsubclass to drop the table for.Return type: None
-
await
asyncpg_simpleorm.drop_table(model, cascade=False)[source]¶ Drop a database table.
Parameters: - model – An
AsyncModelABCsubclass to drop the table for. - cascade – If
Truethen we add theCASCADEclause to the statement when executed.
Return type: None- model – An
-
await
asyncpg_simpleorm.truncate_table(model, cascade=False)[source]¶ Truncate a table, leaving the table schema intact.
Parameters: - model – An
AsyncModelABCsubclass to drop the table for. - cascade (
bool) – IfTruethen we add theCASCADEclause to the statement when executed.
Return type: None- model – An
-
exception
asyncpg_simpleorm.BaseException(*args, **kwargs)[source]¶ Bases:
ExceptionThe base exception class.
-
exception
asyncpg_simpleorm.ExecutionFailure(*args, **kwargs)[source]¶ Bases:
asyncpg_simpleorm.exceptions.BaseException,ValueErrorRaised if execution of a database statement fails.
-
exception
asyncpg_simpleorm.InvalidModel(*args, **kwargs)[source]¶ Bases:
asyncpg_simpleorm.exceptions.BaseException,TypeErrorRaised if a model does not pass an
issubclassorisinstancecheck againstModelABC.