Welcome to Modular-ODM’s documentation!

Contents:

Basic Usage

<todo>

Query Syntax

Operators

Equality

Keyword Operator Description
eq equal to  
ne not equal to  

Comparison

Keyword Operator Description
gt greater than  
gte greater than or equal to  
lt less than  
lte less than or equal to  

Membership

Keyword Operator Description
in in  
nin not in  

String Comparison

Keyword Operator Description
startswith starts with  
endswith ends with  
contains contains  
icontains contains (case insensitive)  

StoredObject

class modularodm.storedobject.StoredObject(**kwargs)[source]

Base class to be used for models.

get_changed_fields(cached_data, storage_data)[source]

Get fields that differ between the cache_sandbox and the current object. Validation and after_save methods should only be run on diffed fields.

Parameters:
  • cached_data – Storage-formatted data from cache_sandbox
  • storage_data – Storage-formatted data from object
Returns:

List of diffed fields

classmethod load(*args, **kwargs)[source]

Get a record by its primary key.

classmethod migrate_all()[source]

Migrate all records in this collection.

classmethod migrate(old, new, verbose=True, dry_run=False, rm_refs=True)[source]

Migrate record to new schema.

Parameters:
  • old – Record from original schema
  • new – Record from new schema
  • verbose – Print detailed info
  • dry_run – Dry run; make no changes if true
  • rm_refs – Remove references on deleted fields
validate_record()[source]

Apply record-level validation. Run on save.

save(*args, **kwargs)[source]

Save a record.

Parameters:force (bool) – Save even if no fields have changed; used to update back-references
Returns:List of changed fields
update_fields(**kwargs)[source]

Update multiple fields, specified by keyword arguments.

Example:

person.update(given_name='Fred', family_name='Mercury')

... is equivalent to ...

person.given_name = 'Fred'
person.family_name = 'Mercury'
Parameters:**kwargs

field names and the values to set

classmethod find(*args, **kwargs)[source]
Parameters:
  • query
  • kwargs
Returns:

an iterable of StoredObject instances

classmethod delegate(method, conflict=None, *args, **kwargs)[source]

Execute or queue a database action. Variable positional and keyword arguments are passed to the provided method.

Parameters:
  • method (function) – Method to execute or queue
  • conflict (bool) – Potential conflict between cache_sandbox and backend, e.g., in the event of bulk updates or removes that bypass the cache_sandbox
classmethod start_queue()[source]

Start the queue. Between calling start_queue and commit_queue, all writes will be deferred to the queue.

classmethod clear_queue()[source]

Clear the queue.

classmethod cancel_queue()[source]

Cancel any pending actions. This method clears the queue and also clears caches if any actions are pending.

classmethod commit_queue()[source]

Commit all queued actions. If any actions fail, clear caches. Note: the queue will be cleared whether an error is raised or not.

classmethod subscribe(signal_name, weak=True)[source]
Parameters:
  • signal_name (str) – Name of signal to subscribe to; must be found in signals.py.
  • weak (bool) – Create weak reference to callback
Returns:

Decorator created by Signal::connect_via

Raises:

ValueError if signal is not found

Example usage:

>>> @Schema.subscribe('before_save')
... def listener(cls, instance):
...     instance.value += 1
classmethod remove_one(*args, **kwargs)[source]

Remove an object, along with its references and back-references. Remove the object from the cache_sandbox and sets its _detached flag to True.

Parameters:
  • which – Object selector: Query, StoredObject, or primary key
  • rm – Remove data from backend
classmethod remove(*args, **kwargs)[source]

Remove objects by query.

Parameters:query – Query object

Backends

class modularodm.storage.base.Storage[source]

Abstract base class for storage objects. Subclasses (e.g. PickleStorage, MongoStorage, etc.) must define insert, update, get, remove, flush, and find_all methods.

translator = <modularodm.translators.DefaultTranslator object>
logger = <modularodm.storage.base.Logger object>
insert(this, *args, **kwargs)[source]

Insert a new record.

Parameters:
  • primary_name (str) – Name of primary key
  • key – The value of the primary key
  • value (dict) – The dictionary of attribute:value pairs
update(this, *args, **kwargs)[source]

Update multiple records with new data.

Parameters:
  • query – A query object.
  • data (dict) – Dictionary of key:value pairs.
get(this, *args, **kwargs)[source]

Get a single record.

Parameters:
  • primary_name (str) – The name of the primary key.
  • key – The value of the primary key.
remove(this, *args, **kwargs)[source]

Remove records.

flush(this, *args, **kwargs)[source]

Flush the database.

find_one(this, *args, **kwargs)[source]

Gets a single object from the collection.

If no matching documents are found, raises NoResultsFound. If >1 matching documents are found, raises MultipleResultsFound.

Params:One or more Query or QuerySet objects may be passed
Returns:The selected document
find(this, *args, **kwargs)[source]

Return a generator of query results. Takes optional by_pk keyword argument; if true, return keys rather than values.

Parameters:query
Returns:a generator of StoredObject instances

Ephemeral

class modularodm.storage.ephemeralstorage.EphemeralStorage(*args, **kwargs)[source]

Mongo

class modularodm.storage.mongostorage.MongoStorage(db, collection)[source]

Wrap a MongoDB collection. Note: store is a property instead of an attribute to handle passing db as a proxy.

Parameters:
  • db (Database) –
  • collection (str) –

Pickle

class modularodm.storage.picklestorage.PickleStorage(collection_name, prefix='db_', ext='pkl')[source]

Storage backend using pickle.

Build pickle file name and load data if exists.

Parameters:
  • collection_name – Collection name
  • prefix – File prefix.
  • ext – File extension.

Indices and tables