Library

This part of the documentation covers all the public API for the SweetRPG DB library. These are common functions and classes that other SweetRPG packages make use of.

Exceptions

Common exceptions.

class sweetrpg_db.exceptions.ObjectNotFound[source]

An exception for objects not found.

MongoDB Repository

These are some classes for interacting with MongoDB.

class sweetrpg_db.mongodb.options.QueryOptions(filters: dict = {}, projection: list = [], skip: int = 0, limit: int = 0, sort: list = [])[source]

An object to store query options for a PyMongo find*() call.

Initialize the QueryOptions object. :param dict filters: A dictionary of filters to apply to the query. :param list projection: A list of attribute names to include in the returned result. If None, all attributes are returned. :param int skip: An offset to use for pagination. :param int limit: The maximum number of results to return. :param list sort: A list of key-value pairs specifying the attributes to sort on.

_filter_operators = {'eq': '$eq', 'ge': '$gte', 'gt': '$gt', 'in_': '$in', 'is_': '$exists', 'isnot': '$not', 'le': '$lte', 'lt': '$lt', 'ne': '$ne', 'notin_': '$nin'}
_sort_values = {'asc': 1, 'dsc': -1}
__init__(filters: dict = {}, projection: list = [], skip: int = 0, limit: int = 0, sort: list = [])[source]

Initialize the QueryOptions object. :param dict filters: A dictionary of filters to apply to the query. :param list projection: A list of attribute names to include in the returned result. If None, all attributes are returned. :param int skip: An offset to use for pagination. :param int limit: The maximum number of results to return. :param list sort: A list of key-value pairs specifying the attributes to sort on.

_process_filter(filter_info: dict)[source]
set_filters(filters: dict = None, from_querystring: list = None)[source]

Sets filters for the query.

Parameters:
  • filters (dict) – A dictionary of filters to set.

  • from_querystring (list) – Filters to set in querystring format.

set_projection(projection: list = None, from_querystring: list = None)[source]

Sets projections for the query.

Parameters:
  • projection (list) – A list of field names to include in the query results.

  • from_querystring (list) – Projections to set in querystring format.

_process_sort(sort_item: dict)[source]
set_sort(sort: list = None, from_querystring: list = None)[source]

Sets sorting for the query results.

Parameters:
  • sort (list) – A list of dictionaries containing field name and ordering.

  • from_querystring (list) – Sorts to set in querystring format.

class sweetrpg_db.mongodb.repo.MongoDataRepository(**kwargs)[source]

A repository class for interacting with a MongoDB database.

Create a MongoDB repository instance.

Parameters:

kwargs – Keyword arguments for setting up the repository connection.

Key model:

The class of the model for this connection.

Key document:

The class of the document for this connection.

Key db:

A PyMongo object used for connecting to the database.

__init__(**kwargs)[source]

Create a MongoDB repository instance.

Parameters:

kwargs – Keyword arguments for setting up the repository connection.

Key model:

The class of the model for this connection.

Key document:

The class of the document for this connection.

Key db:

A PyMongo object used for connecting to the database.

_handle_value(value)[source]

Convert a value to a string.

Parameters:

value (any) – The value to convert. Supports bson.objectid.ObjectId, datetime.datetime, bson.timestamp.Timestamp, and lists of any of those types.

Return str:

A string of the specified value.

_modify_record(record: dict) dict[source]
Modify a record by converting any values to strings, and renaming the internal ‘_id’

field to ‘id’.

Parameters:

record (dict) – The record to modify.

Return dict:

The modified record.

_adjust_sort(sort: tuple) str[source]
create(data: dict) Document[source]

Inserts a new object in the database with the data provided.

Parameters:

data (dict) – The data for the object

Return Document:

The inserted document.

get(record_id: str, deleted: bool = False) Document[source]

Fetch a single record from the database.

Parameters:
  • record_id (str) – The identifier for the record to fetch. This value is compared against the attribute specified in id_attr.

  • deleted (bool) – Include “deleted” objects in the query

Return Document:

An instance of the object type from model_class.

query(options: QueryOptions, deleted: bool = False) list[source]

Perform a query for objects in the database.

Parameters:

options (QueryOptions) – (Optional) Options specifying limits to the query’s returned results

Return list:

Returns a list of Document-subclass instances matching the query.

update(record_id: str, update: dict, deleted: bool = False) Document[source]

Update the specified record.

Parameters:
  • record_id (str) – The ID of the record to update.

  • update (dict) – The data to update for the record.

  • deleted (bool) – Indicates whether the update operation should look for deleted records.

Return Document:

The update version of the object.

delete(record_id: str, actually: bool = False) bool[source]
‘Delete’ the specified record. Deletion is accomplished by setting the deleted_at field to the current

timestamp, so that queries for the object will ignore it.

Parameters:
  • record_id (str) – The record ID of the object to delete. This can be a string or bson.objectid.ObjectId.

  • actually (bool) – Actually delete the record instead of just marking it “deleted”.

Return bool:

A boolean indicating whether the record was able to be marked deleted.

Raises:

DoesNotExist