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.
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.
- set_filters(filters: dict = None, from_querystring: list = None)[source]¶
Sets filters for the query.
- 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.
- 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.
- 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.
- 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:
- Return bool:
A boolean indicating whether the record was able to be marked deleted.
- Raises:
DoesNotExist –