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

DOCSConcepts


Linked QL supports a few JSON-like syntaxes for modelling structure right within the language.

These syntax sugars are designed to give your queries an extra dose of clarity and beauty!

Syntax Syntax Equivalent Function (PostgreSQL) Equivalent Function (MySQL)
Modelling objects { expr1, expr2 AS key, ... } JSON_BUILD_OBJECT() JSON_OBJECT()
Modelling arrays [ expr1, expr2 ] JSON_BUILD_ARRAY() JSON_ARRAY()
Aggregating expressions expr1[] JSON_AGG() JSON_ARRAYAGG()

Example 1:

// Model objects and arrays
const result = await client.query(
    `SELECT
        name,
        email,
        { email, phone AS mobile } AS format1,
        [ email, phone ] AS format2
    FROM users`
);
console.log(result);
Console
[
    {
        name: 'John Doe',
        email: 'johndoed@example.com',
        format1: {
            email: 'johndoed@example.com',
            mobile: '(555) 123-4567'
        },
        format2: [ 'johndoed@example.com', '(555) 123-4567' ]
    },
    {
        name: 'Alice Blue',
        email: 'aliceblue@example.com',
        format1: {
            email: 'aliceblue@example.com',
            mobile: '(888) 123-4567'
        },
        format2: [ 'aliceblue@example.com', '(888) 123-4567' ]
    }
]

Example 2:

// Aggregate a column
const result = await client.query(
    `SELECT
        email[] as emails
    FROM users`
);
console.log(result);
Console
[
    {
        emails: [
           'jd@example.com',
           'ab@example.com'
        ]
    }
]
Clone this wiki locally