Skip to content

Normalize response of queries. #597

@fractlrao

Description

@fractlrao

Queries return results in different shapes - not_found, array and single instance. This puts a burden on the caller to correctly handle/destructure the response. It would help to make queries return results in a uniform shape - array of instances. This will also work well with @empty modifier.

Without this, the user is force to follow a pattern similar to below:

export function safeArrayLength(arr) {
    if (!arr) {
        return 0;
    }
    // If it's already an array, return its length
    if (Array.isArray(arr)) {
        return arr.length;
    }
    // If it's an Agentlang Instance object (has attributes, name, etc.), treat as single item
    if (arr.attributes || arr.name || arr.___id) {
        return 1;
    }
    // If it has a length property, use it
    if (typeof arr.length === 'number') {
        return arr.length;
    }
    // Otherwise, not an array or recognizable structure
    return 0;
}
    {ScalarSetting {SettingKey? "weekly_summary_day"}} @as [existingWeeklyDay]
    helper.safeArrayLength(existingWeeklyDay) @as weeklyDayCount
    if (weeklyDayCount == 0) {
        "Monday" @as defaultWeeklyDay
        {ScalarSetting {
            SettingKey "weekly_summary_day"
            SettingValue defaultWeeklyDay
        }} @as createdWeeklyDay
    }

It would help to simplify this into:

    {ScalarSetting {SettingKey? "weekly_summary_day"}} 
    @empty {ScalarSetting {
            SettingKey "weekly_summary_day"
            SettingValue "Monday }} 
    @as [existingWeeklyDay]

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions