Skip to content

Add indexed properties to RowDataPacket #2164

Open
@nickshanks

Description

@nickshanks

I would like to be able to access fields by their index in the query, without having to modify the query to include an 'AS' field alias. For example, given:

SELECT COUNT(*), MAX(some_field) + 1 FROM table

I want to be able to refer to the results like this:

const count = rows[0][0];
const max_thingy = rows[0][1];

Instead of:

const count = rows[0]['COUNT(*)'];
const max_thingy = rows[0]['MAX(some_field) + 1'];

You can imagine how these field names can get very long and unwieldy.
The solution is to add one line to the end of the for loop in the parse function of RowDataPacket (after line 49 in the current code):

Object.defineProperty(this, i, { value });

Metadata

Metadata

Assignees

No one assigned

    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