watson.db.services¶
- class watson.db.services.Base(session)[source]¶
Provides common interactions with the SQLAlchemy session.
Example:
class MyService(Base): __model__ = models.MyModel # sqlalchemy_session is a reference to a Session object service = MyService(sqlalchemy_session) mymodel = service.new(attr='Value') print(mymodel.attr) # 'Value' service.save(mymodel)
- session¶
Session
The SqlAlchemy session
- __model__¶
mixed
The model object the server interacts with
- delete(model)[source]¶
Deletes a model from the database.
Parameters: model (mixed) – The model to delete
- delete_all(*models)[source]¶
Delete a list of models.
If deleting more than a single model of the same type, then a Service.find(**kwargs).delete() should be called (and wrapped in a transaction_scope) instead.
Parameters: models (list) – The models to delete
- find(**kwargs)[source]¶
Shorthand for the filter_by method.
Should be used when performing query specific operations (such as bulk deletion)
Parameters: kwargs – The fields to search for
- first(**kwargs)[source]¶
Return the first matching result for the query.
Parameters: kwargs – The fields to search for Returns: The object found, or None if nothing was returned Return type: mixed
- get(id, error_on_not_found=False)[source]¶
Retrieve a single object based on it’s ID.
Parameters: - id (int) – The primary key of the record
- error_on_not_found (bool) – Raise an exception if not found
Returns: The matching model object
Return type: mixed
Raises: Exception when no matching results are found. –
- new(**kwargs)[source]¶
Creates a new instance of the model object
Parameters: kwargs (mixed) – The initial values for the model Returns: The newly created model Return type: mixed