Skip to content

[Feature] Consider the usage of json functions for getting related objects #124

@kbarbounakis

Description

@kbarbounakis

Description
@themost/data module uses @themost/query#QueryExpression class for creating complex queries, getting related objects etc.
It seems very useful to optimise fetch operations and use JSON functions that are available in many database engines like MySQL, MSSQL, Oracle, Sqlite etc. Such an operation will try to get related objects as JSON objects in a single query statement prepared by the @themost/query which already supports JSON queries e.g. get order customer and ordered item as a JSON object

SELECT OrderData.id,
       json_object(
               'id', customer.id,'familyName', customer.familyName,'givenName', customer.givenName
                ) as customer,
      json_object(
               'id', orderedItem.id,'name', orderedItem.name,
               'category', orderedItem.category, 'price', orderedItem.price
                ) as orderedItem
FROM OrderData
INNER JOIN (
    SELECT id, familyName, givenName FROM PersonData
) customer ON OrderData.customer = customer.id

INNER JOIN (
    SELECT id, name, category, price FROM ProductData
) orderedItem ON OrderData.orderedItem = orderedItem.id
limit 25

The resultset of the previous query is something like the following table:

id customer orderedItem
1 {"id":391,"familyName":"Nicholls","givenName":"Meghan"} {"id":35,"name":"Google Nexus 7 (2013)","category":"Tablets and E-Readers","price":401.81}
2 {"id":399,"familyName":"Hawkins","givenName":"Jesse"} {"id":55,"name":"AMD Radeon R9 290","category":"PC Components","price":452.35}

where customer and orderedItem are serialized JSON objects.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions