asfernandes / node-firebird-drivers

Node.js Firebird Drivers
MIT License
53 stars 17 forks source link

Add methods executeReturningAsObject and fetchAsObject to return records as objects instead of arrays #30

Closed pupupulp closed 4 years ago

pupupulp commented 4 years ago

Is there any way to return headers along with the result set rows or format the result set rows along with the header?

something like this

{ header1: value1, header2: value2, header3: value3 }

the rows i get only have

{ value1, value2, value3 }

asfernandes commented 4 years ago

I suppose you want [{ column1: value1, column2: value2 }, ...] instead of [[value1, value2], ...] ?

pupupulp commented 4 years ago

yes yes thats it, is it possible to have an output like this [{ column1: value1, column2: value2 }, ...] ?

pupupulp commented 4 years ago

it would be a lot of help if we could get the result in that format, i checked through the code and examples but i cant seem to find how to do it using entirely the library, right now we created a separate array for the keys and used reduce function but it seems slow and takes a lot of processing time specially when the result set is large

mreis1 commented 4 years ago

I must agree that this would be very helpful, specially if we rely on wildcards to fetch all the columns from a given table.

A suggestion would be to pass a "options object" to methods such as: 2019-10-09_10-21-40

mreis1 commented 4 years ago

@asfernandes do you have any thoughts on this?

There's an implementation for this on xdenders lib. Maybe this can inspire you on how to implement this piece of functionality.

https://github.com/xdenser/node-firebird-libfbclient/blob/master/src/fb-bindings-fbresult.cc#L585

I realize that there's an issue with my initial proposal. Since executeQuery doesn't fetch results, the json propery should be set at resultSet.fetch({fetchSize?: number; json?: boolean})

Since executeReturning, automatically fetches the resultSet, then json property must be set as follows: attachment.executeReturning(tx, sqlStmt, params, { json?: boolean; prepareOptions?, executeOptions? })