Skip to content
Oxford Harrison edited this page Nov 9, 2024 · 16 revisions

Linked QL supports a few JSON-like syntaxes for modelling structure.

Syntax Equivalent Function (Postgres) Equivalent Function (MySQL)
{ expr1, expr2 AS key, ... } JSON_BUILD_OBJECT() JSON_OBJECT()
[ expr1, expr2 ] JSON_BUILD_ARRAY() JSON_ARRAY()
expr1[] JSON_AGG() JSON_ARRAYAGG()

Example 1:

Select fields in both object and array forms

// Object and array sugars
const result = await client.query(
    `SELECT
        name,
        email,
        { email, phone AS mobile } AS contact1,
        [ email, phone ] AS contact2
    FROM users`
);
console.log(result);
Console
[
    {
        name: 'John Doe',
        contact1: {
            email: '[email protected]',
            mobile: '(555) 123-4567'
        },
        contact2: [ '[email protected]', '(555) 123-4567' ]
    },
    {
        name: 'Alice Blue',
        contact1: {
            email: '[email protected]',
            mobile: '(888) 123-4567'
        },
        contact2: [ '[email protected]', '(888) 123-4567' ]
    }
]

Example 2:

Select an aggregate of a column

// Aggregation sugar
const result = await client.query(
    `SELECT
        email[] as emails
    FROM users`
);
console.log(result);
Console
[
    {
        emails: [
           '[email protected]',
           '[email protected]'
        ]
    }
]
Clone this wiki locally