libsql-client-ts icon indicating copy to clipboard operation
libsql-client-ts copied to clipboard

Receiving different shapes of `Row` from client

Open penberg opened this issue 2 years ago • 4 comments

The execute() method returns Row objects that can be accessed either as an array or an object, which confuses deepEquals() in tests, for example.

penberg avatar Sep 25 '23 08:09 penberg

I looked a little deeper into the query and ran the rowFromSql method inside a TypeScript Playground with target es2022 on v5.2.2 and its returning as an object.

adoublef avatar Sep 25 '23 12:09 adoublef

See also: https://github.com/libsql/libsql-client-ts/issues/92

CodingDoug avatar Sep 25 '23 12:09 CodingDoug

As pointed out previously by @adoublef , the collision described in #92 can also occur on columns named length.

I think the ResultSet structure should change as a result of these collision potentials to something else. Perhaps rows can be a list of [column, value] tuples which do not have the same issue of collision and are also compatible with Object.fromEntries should the user wish to parse a row into an object themselves.

As the current approach tries to allow both column index indexing and column name indexing to retrieve values , this can still be done with minimal JS from the user:

Column index indexing: row[i][1] Column name indexing Object.fromEntries(row)[name]

tvandinther avatar Sep 25 '23 13:09 tvandinther

I have an example of the code using Symbol.Iterator

I was pondering over this and feel that this could mean less of a change to the existing expectation of the client package. The row object can then still technically be used as either an object or an array via a spread operator.

The only con is that I'm unsure the minimum target for Node and Typescript this library is supposed to be. And unsure if there may be compatibility issues with earlier versions. I have only tested on TS v3.3.3 to v5.2.2 so outside that range may be unexpected behaviour using iterators

adoublef avatar Sep 25 '23 19:09 adoublef