Data Querying
Querying data from the database is well supported by JSONClasses ORM integrations. Query can be a expensive operation. Data query in JSONClasses supports async syntax and normal syntax.
# async syntaxawait User.find().order('created_at', -1)# normal syntaxUser.find().order('created_at', -1).exec()
#
QueryThere are mainly 3 kinds of query. ID query, single query and batch query. Use id
, one
and find
to perform corresponding query.
await User.id(str_id_value) # a result with idawait User.one(name='His/Her Name') # single result which matchesawait User.one(name='His/Her Name').optional # can return Noneawait User.find(age=20) # a list of results
#
SortingBatch results can be sorted and ordered.
await Analysis.find().order('created_at', 1) # ASC, old firstawait Analysis.find().order('created_at', -1) # DESC, new first
Multiple sorting descriptors can be chained together.
#
PaginationPage size and page number can be used to calculate the value of skip
and limit
.
await Analysis.find().skip((page_number - 1) * page_size).limit(page_size)
#
IncludingQuerying an object without it's relationships is not handy and useful some of the times. JSONClasses supports querying object with it's linked objects included.
await User.id(user_id).include('profile')await Author.find().include('posts', Post.find(title={'$regex': 'Python'}))
Including can be chained and nested unlimited levels.