Skip to content

Improvement: parse query results with user-supplied function #2508

Open
@paul-avisseau

Description

@paul-avisseau

First and foremost, thanks for the great job done here!

I am running a website with ExpressJs that executes a bunch of queries that return arrays of data. I was wondering if there is a possibility to turn the raw data from the database into meaningful objects in a simple manner. I usually use queries and then walk through their results with a for loop in order to turn the raw data into objects that can be serialized with JSON.

let query = {
    text: 'SELECT ...',
    values: [ ... ],
    rowMode: 'array'
};
dbClient.query(query, (error, results) => {
    let data = new Array(results.rows.length);
    let i = 0;
    for (let row of results.rows) {
        data[i++] = MyClass.fromDatabase(row);
    }
    response.status(200).json(data);
});

This actually creates two arrays: the one that comes from PostgreSQL, and the one I create from the results. Not very good for memory!
Instead, it would be much simpler I think to specify a transformation function in the query object, in order to directly turn the raw data into objects:

let query = {
    text: 'SELECT ...',
    values: [ ... ],
    rowMapper: MyClass.fromDatabase
};
dbClient.query(query, (error, results) => {
    response.status(200).json(results.rows);
});

What I propose is to add a function to the query object, that will allow to convert a single row to an object. The outcome of the function will be added to the final results array. No more memory overhead.

The template for the function would be:

function convert(row, rowNumber);

In the Java world, Spring JDBC has similar concepts (RowMapper), and it is very efficient.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions