jsonapi.bulk_database

This package contains a container for other database adapters. If your application uses different database engines, you can use this adapter, which provides a transparent layer and selects the correct database for each type automatic.

Tutorial

Our application has a User and Session model. The users are stored in an sql database and the sessions in redis for performance reasons. To add both models to the JSONapi, you can use the bulk database:

bulk_db = jsonapi.bulk_database.Database()
redis_db = jsonapi.redis.Database() # Does not exist yet :(
sql_db = jsonapi.sqlalchemy.Database()

api = jsonapi.base.api.API("/api", db=bulk_db)

api.add_type(user_schema)
api.add_type(session_schema)

# Tell the bulk database, which database driver it must use for the
# models.
bulk_db.add_type(user_schema, sql_db)
bulk_db.add_type(session_schema, redis_db)

API

class jsonapi.bulk_database.database.Database(api)[source]

Bases: jsonapi.base.database.Database

This adapter is only a proxy. You must associate each type with a database adapter, on setup.

session()[source]
get_db(typename)[source]

Returns the database adapter associated with the type typename.

Parameters:typename (str) –
Raises:KeyError – If the typename is no associated with a database.
add_type(typename, db)[source]

Associates the type typename with the database adapter db. The models must be registered on the api.

db.add_type("users", sql_db)
db.add_type("session", redis_db)
Parameters:
Seealso:

add_schema()

add_schema(schema, db)[source]

Associates the schema with the database adapter db.

db.add_schema(User, sql_db)
db.add_schema(Session, redis_db)