Database-based stores

SQLAlchemy

To store data in existing databases, an SQLAlchemy based store is available:

from simplekv.db.sql import SQLAlchemyStore
from sqlalchemy import create_engine, MetaData

# use echo=True to see queries
engine = create_engine('sqlite:///:memory:', echo=True)

metadata = MetaData(bind=engine)

# init store
store = SQLAlchemyStore(engine, metadata, 'kvstore')

# create the actual table in the database (only do this once!)
metadata.create_all()
# also possible: store.table.create()

# use store normally
store.put('my_key', 'some value')

print store.get('my_key')
class simplekv.db.sql.SQLAlchemyStore

Stores data in a table in a database through SQLAlchemy.

Note that this storage is not well-suited for large binary data, as currently it does not support streaming of large blobs. In other words, every value must be read into memory, before it can be returned.

__init__(bind, metadata, tablename)

Generates a new Table for use as a backend (see table) on the supplied metadata.

Parameters:
  • bind – Any queries made by the store run execute() using this bind.
  • metadatasqlalchemy.schema.MetaData instance on which the table will be created.
  • tablename – The name for the table.
table

An sqlalchemy.schema.Table instance autogenerated by __init__(). Calling create() can be used to create the table in the database.

MongoDB

The MongoStore class requires the pymongo package to be installed.

class simplekv.db.mongo.MongoStore
__init__(db, collection)

Uses a MongoDB collection as the backend, using pickle as a serializer.

Parameters:
  • db – A (already authenticated) pymongo database.
  • collection – A MongoDB collection name.