flatisfy.database package

Submodules

flatisfy.database.base module

This module contains the definition of the declarative SQLAlchemy base.

flatisfy.database.types module

This modules implements custom types in SQLAlchemy.

class flatisfy.database.types.StringyJSON(*args, **kwargs)[source]

Bases: sqlalchemy.sql.type_api.TypeDecorator

Stores and retrieves JSON as TEXT for SQLite.

From https://avacariu.me/articles/2016/compiling-json-as-text-for-sqlite-with-sqlalchemy.

Note

The associated field is immutable. That is, changes to the data (typically, changing the value of a dict field) will not trigger an update on the SQL side upon commit as the reference to the object will not have been updated. One should force the update by forcing an update of the reference (by performing a copy operation on the dict for instance).

impl

alias of sqlalchemy.sql.sqltypes.TEXT

process_bind_param(value, dialect)[source]

Process the bound param, serialize the object to JSON before saving into database.

process_result_value(value, dialect)[source]

Process the value fetched from the database, deserialize the JSON string before returning the object.

flatisfy.database.whooshalchemy module

This file comes from https://github.com/sfermigier/WhooshAlchemy.

WhooshAlchemy

Adds Whoosh indexing capabilities to SQLAlchemy models.

Based on Flask-whooshalchemy by Karl Gyllstrom (Flask is still supported, but not mandatory).

copyright:
  1. 2012 by Stefane Fermigier
copyright:
  1. 2012 by Karl Gyllstrom
license:

BSD (see LICENSE.txt)

class flatisfy.database.whooshalchemy.IndexService(config=None, whoosh_base=None)[source]

Bases: object

after_commit(session)[source]

Any db updates go through here. We check if any of these models have __searchable__ fields, indicating they need to be indexed. With these we update the whoosh index for the model. If no index exists, it will be created here; this could impose a penalty on the initial commit of a model.

before_commit(session)[source]
index_for_model_class(model_class)[source]

Gets the whoosh index for this model, creating one if it does not exist. in creating one, a schema is created based on the fields of the model. Currently we only support primary key -> whoosh.ID, and sqlalchemy.TEXT -> whoosh.TEXT, but can add more later. A dict of model -> whoosh index is added to the app variable.

register_class(model_class)[source]

Registers a model class, by creating the necessary Whoosh index if needed.

class flatisfy.database.whooshalchemy.Searcher(model_class, primary, index)[source]

Bases: object

Assigned to a Model class as search_query, which enables text-querying.

Module contents

This module contains functions related to the database.

flatisfy.database.init_db(database_uri=None, search_db_uri=None)[source]

Initialize the database, ensuring tables exist etc.

Parameters:
  • database_uri – An URI describing an engine to use. Defaults to in-memory SQLite database.
  • search_db_uri – Path to the Whoosh index file to use.
Returns:

A tuple of an SQLAlchemy session maker and the created engine.

flatisfy.database.set_sqlite_pragma(dbapi_connection, _)[source]

Auto enable foreign keys for SQLite.