Ximpia provides a data layer to connect to your data sources. These data sources can be relational (SQL) data sources or noSQL data sources.
We follow philosophy that service layer do not call django model data access methods directly and use data classes (what could be similar to a django model manager).
You may have your data operations return django models or simple entities like dictionaries with attribute calling (customer.name) when you deal with data sources and noSQL.
Advantages of data layer:
Currenly we offer CommonDAO which deals with django data model access. This class has common data operations.
In case you use master/slave data nodes, you don’t need to have routers to route to master or slaves. We think a simpler design is good. Depending on view or action request, the data layer hits master or slave nodes. In case no slaves defined, will allways hit default data node.
Keeps attributes about auditory (create_user and update_user) and create and update fields.
All django models would extend this model.
Keeps delete management. When we delete data, we write isDeleted=True.
You can delete data for ever with is_real attribute in delete operations.
Your data layer classes will be related to django models, like:
from ximpia.xpcore.data import CommonDAO
class UserMetaDAO(CommonDAO):
model = UserMeta
def my_data_operation(self, customer_id):
# you return django queryset, django model, attribute dictionary or plain dictionary
# good place to have complex queries, Q objects, etc...
Implementation:
db_customer = self._instances(CustomerDAO)[0]
customers = db_customer.search(id=23)
Will return a django queryset, where you can filter (customers.filter(name='john'))
In case you need to access model (from services and business) and do operations on model, do:
db_customer.model.objects.all()
Exception
Will raise XpMsgException with error literal message and attribute origin='data':
from ximpia.xpcore.models import XpMsgException
try:
db_customers.get(id=34)
except XpMsgException:
# treat exception
Checks if exists
Attributes
keyword attributes, like:
if db_customer.check(name='john', status='new'): # more...Returns
True/False
Will create model with attributes
customer = db_customer.create(name='john', status='new')Attributes
keyword attributes
Returns
Django model created
Will delete rows that match the keyword attributes
db_customer.delete(name='john', status='new')Attributes
- is_real (boolean)
keywork attributes
Returns
None
Will delete rows that match the keyword attributes in case exists. If not, does not throw exception.
db_customer.delete_if_exists(name='john', status='new')Attributes
- is_real (boolean)
keywork attributes
Returns
None
Delete by primary key
db_customer.delete_by_id(23) db_customer.delete_by_id(23, is_real=True)Attributes
- pk (long)
Optional Attributes
- is_real (boolean)
Returns
None
Search model with ordering and paging
db_customer.filter_data(status='OK') db_customer.filter_data(status='OK', xpNumberMatches=100)Attributes
keyword attributes
Optional Attributes
- xpNumberMatches (int) : default 100
- xpPage (int) : default 1
- xpOrderBy (tuple)
keywork attributes
Returns
queryset
Get model which match attributes
Attributes
keyword attributes for query
Returns
django model
Get all results for model
Returns
django queryset
Get object. In case does not exist, create model object
Attributes
Keyword attributes
Returns
(object, created) <model, boolean>
Get container (dictionary) of {id: object, ...} for list of ids
Attributes
- id_list (list) : List of ids
Returns
Dictionary with ids and objects
Get object by id
Attributes
- field_id
Returns
Model object
Saves the model
customer = CustomerDAO.model(name='john', status='OK') customer.save() db_customer = self._instances(CustomerDAO)[0] customer = db_customer.get_by_id(23) customer.name='james' customer.save()Returns
None
Search model to get queryset (like filter)
Search model, like:
customers = db_customer.search(name='john')Attributes
- qs_args (dict) : Keywork attributes like attr=value
Returns
Django queryset
Search table with paging, ordering for set of fields. listMap allows mapping from keys to model fields.
** Attributes **
- fields:tuple<str>
Optional Attributes
- page_start:int [optional] [default:1]
- page_end:int [optional]
- number_results:int [optional] [default:from settings]
- order_by:tuple<str> [optional] [default:[]]
keyword attributes
** Returns **
Returns the query set with values(*fields).
xpList:ValuesQueryset