Database

Wrapper-Service

Pinyto internally uses pymongo to access the MongoDB database on the server. Because of the restrictive sandbox architecture a wrapper service for the database interface is used which basically exposes the most used functionality of pymongo to the assemblies. For security reasons no direct access to pymongo could be allowed because Pinyto must make sure the data of other users stays untouched.

class service.database.CollectionWrapper(collection, assembly_name, only_own_data=True)[source]

This wrapper is user to expose the db to the users assemblies.

count(query)[source]

Use this function to get a count from the database.

Parameters:query (dict) –
Returns:The number of documents matching the query
Return type:int
find(query, skip=0, limit=0, sorting=None, sort_direction='asc')[source]

Use this function to read from the database. This method encodes all fields beginning with _ for returning a valid json response.

Parameters:
  • query (dict) –
  • skip (int) – Count of documents which should be skipped in the query. This is useful for pagination.
  • limit (int) – Number of documents which should be returned. This number is of course the maximum.
  • sorting (str) – String identifying the key which is used for sorting.
  • sort_direction (str) – ‘asc’ or ‘desc’
Returns:

The list of found documents. If no document is found the list is empty.

Return type:

list

find_distinct(query, attribute)[source]

Return a list representing the diversity of a given attribute in the documents matched by the query.

Parameters:
  • query (str) – json
  • attribute (str) – String describing the attribute
Returns:

A list of values the attribute can have in the set of documents described by the query

Return type:

list

find_document_for_id(document_id)[source]

Find the document with the given ID in the database. On success this returns a single document.

Parameters:document_id (string) –
Returns:The document with the given _id
Return type:dict
find_documents(query, skip=0, limit=0, sorting=None, sort_direction='asc')[source]

Use this function to read from the database. This method returns complete documents with _id fields. Do not use this to construct json responses!

Parameters:
  • query (dict) –
  • skip (int) – Count of documents which should be skipped in the query. This is useful for pagination.
  • limit (int) – Number of documents which should be returned. This number is of course the maximum.
  • sorting (str) – String identifying the key which is used for sorting.
  • sort_direction (str) – ‘asc’ or ‘desc’
Returns:

The list of found documents. If no document is found the list is empty.

Return type:

list

insert(document)[source]

Inserts a document. If the given document has a ID the ID is removed and a new ID will be generated. Time will be set to now.

Parameters:document (dict) –
Returns:The ObjectId of the insrted document
Return type:str
remove(document)[source]

Deletes the document. The document must have a valid _id

Parameters:document (dict) –
save(document)[source]

Saves the document. The document must have a valid _id

Parameters:document (dict) –
Returns:The ObjectId of the insrted document
Return type:str

For coders the helpers used in this service may be of interest:

service.database.encode_underscore_fields(data)[source]

Removes _id

Parameters:data (dict) –
Return type:dict
service.database.encode_underscore_fields_list(data_list)[source]

Removes _id for every dict in the list

Parameters:data_list (list) –
Return type:list
service.database.inject_object_id(query)[source]

Traverses all fields of the query dict and converts all ‘_id’ to ObjectId instances.

Parameters:query (dict) –
Return type:dict

Default-API

Some API functions are used in nearly every assembly. To prevent users from writing the same code over and over some default functions for assemblies are implemented and can be called by every assembly.

Warning

Default API-functions hide explicitly defined functions in the assembly with the same name at the moment. This may change in future versions where assemblies can overwrite default functionality.

Database statistics

The database app also exposes an API-function for loading database statistics of the user. The statistics are:

  • ‘time_budget’: Sum of all the CPU time (in seconds) used by the user.
  • ‘storage_budget’: Integral over the storage the user user over time. The value is in bytes*seconds.
  • ‘current_storage’: The amount of of storage (in bytes) the user uses at the moment.
  • ‘last_calculation’: Timestamp of the last time the budgets were calculated. This is needed if the frontend tries to calculate the storage budget up to the current time.
  • ‘assembly_count’: Number of assemblies the user owns.
  • ‘installed_assemblies_count’: Number of assemblies the user has installed.
  • ‘all_assemblies_count’: Number of assemblies available for the user.