API Reference

The following section outlines the API of postDB.

Model

class postDB.Model

Base class for all the models.

classmethod all_models() → List[Type[postDB.model.model.Model]]

Returns a list of all Model subclasses.

as_dict(*columns)dict

Returns a dict of attribute:value, only containing the columns specified.

async classmethod create_pool(uri: str, *, min_con: int = 1, max_con: int = 10, timeout: float = 10.0, loop: asyncio.base_events.BaseEventLoop = None, **pool_kwargs) → None

Populate the internal pool keyword.

async classmethod create_table(*, verbose: bool = False, exists_ok: bool = True)

Create the PostgreSQL Table for this Model.

classmethod create_table_sql(*, exists_ok: bool = True)str

Generates the CREATE TABLE SQL statement.

async classmethod drop_table(*, verbose: bool = False, cascade: bool = True, exists_ok: bool = True)

Drop the PostgreSQL Table for this Model.

classmethod drop_table_sql(*, exists_ok: bool = True, cascade: bool = False)str

Generates the DROP TABLE SQL statement.

Column

class postDB.Column

Class to define a column in a Model.

generate_create_table_sql()str

Generates the SQL for this column for the CREATE TABLE statement.

Column types

Base class

class postDB.types.SQLType

Base class for all the other types.

classmethod from_dict(data: dict)postDB.types.SQLType

Create a type instance from a dict.

is_real_type()bool

Returns a bool stating if the type is a real PostgreSQL type or if it has been defined as a type for ease of use

to_dict()dict

Returns a dict of the class attributes.

to_sql()str

Returns the SQL of the type.

Types

class postDB.types.Array(sql_type: Union[Type[postDB.types.SQLType], postDB.types.SQLType])

Type for python list. {type} ARRAY in PostgreSQL.

class postDB.types.Binary

Type for python bytes. BYTEA in PostgreSQL.

class postDB.types.Boolean

Type for python bool. BOOLEAN in PostgreSQL.

class postDB.types.Date

Type for python datetime.date. DATE in PostgreSQL.

class postDB.types.DateTime(*, timezone: bool = False)

Type for python datetime.datetime. TIMESTAMP WITH TIME ZONE or TIMESTAMP WITHOUT TIME ZONE in PostgreSQL.

Optional timezone with the timezone attribute.

class postDB.types.Float

Type for python float. FLOAT in PostgreSQL.

class postDB.types.ForeignKey(model: str, column: str, *, sql_type: Union[Type[postDB.types.SQLType], postDB.types.SQLType, None] = None, on_delete: str = 'CASCADE', on_update: str = 'NO ACTION')

Reference to another column in another model.

class postDB.types.Integer(*, big: bool = False, small: bool = False)

Type for python int. INTEGER or BIG INT or SMALL INT in PostgreSQL.

Optional big or small integer with big and small

class postDB.types.Interval(field: str = None)

Type for python datetime.timedelta. INTERVAL or INTERVAL {field} in PostgreSQL.

Optional field argument for setting the interval type with the field. field argument needs to be in this list:

  • "YEAR"

  • "MONTH"

  • "DAY"

  • "HOUR"

  • "MINUTE"

  • "SECOND"

  • "YEAR TO MONTH"

  • "DAY TO HOUR"

  • "DAY TO MINUTE"

  • "DAY TO SECOND"

  • "HOUR TO MINUTE"

  • "HOUR TO SECOND"

  • "MINUTE TO SECOND"

class postDB.types.JSON

Type for python dict. JSON in PostgreSQL.

class postDB.types.Numeric(*, precision: int = None, scale: int = None)

Type for python decimal.Decimal. NUMERIC or NUMERIC({precision}, {scale}) in PostgreSQL.

Optional precision and scale with precision and scale

class postDB.types.Real

Type for python float. REAL in PostgreSQL.

class postDB.types.Serial(*, big: bool = False, small: bool = False)

Type for python int that autoincrements. SERIAL or BIG SERIAL or SMALL SERIAL in PostgreSQL.

Optional big or small integer with big and small

class postDB.types.String(*, length: int = None, fixed: bool = False)

Type for python str. TEXT or CHAR({length}) or VARCHAR({length}) in PostgreSQL.

Optional length and fixed with length and fixed

class postDB.types.Time(*, timezone: bool = False)

Type for python datetime.time. TIME WITH TIME ZONE or TIME WITHOUT TIME ZONE in PostgreSQL.

Optional timezone with the timezone attribute.

Exceptions

The following exceptions are thrown by the module. Other exceptions might be raised by asyncpg.

exception postDB.exceptions.SchemaError

Base error for the module.

exception postDB.exceptions.UniqueViolationError

Raised when a unique constraint is violated.