tursodatabase / libsql-client-ts

TypeScript/JavaScript client API for libSQL
https://docs.turso.tech/sdk/ts/quickstart
MIT License
226 stars 32 forks source link

Receiving different shapes of `Row` from client #101

Open penberg opened 1 year ago

penberg commented 1 year ago

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

adoublef commented 1 year ago

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.

CodingDoug commented 1 year ago

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

tvandinther commented 1 year ago

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]

adoublef commented 1 year ago

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