Skip to content

use $jsonGroupArray to return an array of primitive values as JSON - #115

Merged
kbarbounakis merged 3 commits into
themost-framework:masterfrom
kbarbounakis:implement-json-flat-array-syntax
Aug 24, 2026
Merged

use $jsonGroupArray to return an array of primitive values as JSON#115
kbarbounakis merged 3 commits into
themost-framework:masterfrom
kbarbounakis:implement-json-flat-array-syntax

Conversation

@kbarbounakis

@kbarbounakis kbarbounakis commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

This PR updates $jsonGroupArray dialect to return a JSON array or values e.g. [ 'orange', 'apple', 'lemon' ] when a QueryExpression is passed.

e.g.

const query = new QueryExpression().select(
                'id',
                'familyName',
                'givenName',
                'jobTitle',
                'email',
                new QueryField({
                    products: {
                        $jsonGroupArray: [
                            new QueryExpression().select(
                                new QueryField('name').from(Products),
                            ).from(Products).join(Orders).with(
                                new QueryExpression().where(
                                    new QueryField('orderedItem').from(Orders)
                                ).equal(
                                    new QueryField('id').from(Products)
                                )
                            ).where(
                                new QueryField('customer').from(Orders)
                            ).equal(
                                new QueryField('id').from(People)
                            )
                        ]
                    }
                })
            ).from(People).where('email').equal('eric.thomas@example.com');

which returns an array of purchased product names

{
  "id": 401,
  "familyName": "Thomas",
  "givenName": "Eric",
  "jobTitle": "Naval Architecture",
  "email": "eric.thomas@example.com",
  "products": [
    "Kobo Aura",
    "SanDisk Ultra Plus (256GB)",
    "Lenovo ThinkPad Tablet 2",
    "Dell XPS 27"
  ]
}

The generated SQL statement uses json_agg to execute a single-valued query

SELECT "PersonData"."id", "PersonData"."familyName", 
"PersonData"."givenName", 
"PersonData"."jobTitle", "PersonData"."email", (
  SELECT json_agg("ProductData"."name") AS "value" FROM "ProductData"
     INNER JOIN "OrderData" ON ("OrderData"."orderedItem"="ProductData"."id") 
     WHERE ("OrderData"."customer"="PersonData"."id")
) AS "products" 
FROM "PersonData" 
WHERE ("email"='eric.thomas@example.com')

@kbarbounakis kbarbounakis added the enhancement New feature or request label Aug 24, 2026
@kbarbounakis
kbarbounakis merged commit cdd1d68 into themost-framework:master Aug 24, 2026
3 checks passed
@kbarbounakis
kbarbounakis deleted the implement-json-flat-array-syntax branch August 24, 2026 09:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant