hook.database#

class immp.hook.database.DatabaseHook(name, config, host)#

Bases: ResourceHook, _ModelsMixin

Hook that provides generic data access to other hooks, backed by Peewee. Because models are in the global scope, they can only be attached to a single database, therefore this hook acts as the single source of truth for obtaining that “global” database.

Hooks should subclass BaseModel for their data structures, in order to gain the database connection. At startup, they can register their models to the database connection by calling add_models() (obtained from host.resources[DatabaseHook]), which will create any needed tables the first time models are added.

Any database types supported by Peewee can be used, though the usual caveats apply: if a hook requires fields specific to a single database type, the system as a whole is effectively locked-in to that type.

async start()#

Perform any underlying operations needed to ready this resource for use, such as opening connections to an external network or API.

If using an event-driven framework that yields and runs in the background, you should use a signal of some form (e.g. asyncio.Condition) to block this method until the framework is ready for use.

async stop()#

Perform any underlying operations needed to stop using this resource and tidy up, such as terminating open network connections.

Like with start(), this should block as needed to wait for other frameworks – this method should return only when ready to be started again.

class immp.hook.database.AsyncDatabaseHook(name, config, host)#

Bases: ResourceHook, _ModelsMixin

Hook that provides generic data access to other hooks, backed by tortoise. Because models are in the global scope, they can only be attached to a single database, therefore this hook acts as the single source of truth for obtaining that “global” database.

Hooks should register their models to the database connection at startup by calling add_models() (obtained from host.resources[AsyncDatabaseHook]), which will create any needed tables the first time models are added.

async start()#

Perform any underlying operations needed to ready this resource for use, such as opening connections to an external network or API.

If using an event-driven framework that yields and runs in the background, you should use a signal of some form (e.g. asyncio.Condition) to block this method until the framework is ready for use.

async stop()#

Perform any underlying operations needed to stop using this resource and tidy up, such as terminating open network connections.

Like with start(), this should block as needed to wait for other frameworks – this method should return only when ready to be started again.