wuttjamaican.db.sess

WuttJamaican - database sessions

class wuttjamaican.db.sess.Session[source]

SQLAlchemy session class used for all (normal) app database connections.

See the upstream sqlalchemy.orm.Session docs for more info.

class wuttjamaican.db.sess.short_session(config=None, factory=None, session=None, commit=False)[source]

Context manager for a short-lived database session.

A canonical use case for this is when the config object needs to grab a single setting value from the DB, but it does not have an active DB session to do it. This context manager is used to produce the session, and close it when finished. For example:

with short_session(config) as s:
   result = s.query("select something from somewhere").scalar()

How it goes about producing the session instance will depend on which of the following 3 params are given (explained below):

  • config

  • factory

  • session

Note that it is also okay if you provide none of the above params, in which case the main Session class will be used as the factory.

Parameters:
  • config – Optional app config object. If a new session must be created, the config will be consulted to determine the factory which is used to create the new session.

  • factory – Optional factory to use when making a new session. If specified, this will override the config mechanism.

  • session – Optional SQLAlchemy session instance. If a valid session is provided here, it will be used instead of creating a new/temporary session.

  • commit – Whether the temporary session should be committed before it is closed. This flag has no effect if a valid session instance is provided, since no temporary session will be created.