oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.49k stars 2.71k forks source link

Getting column-type information from a statement/query #7134

Open kwando opened 10 months ago

kwando commented 10 months ago

What is the problem this feature would solve?

I would like more information about the columns the will be returned from a statement, right now I can only get the columnName but more info like the one we can get from better-sqlite would be nice.

Stolen from their docs:

.name: the name (or alias) of the result column.
.column: the name of the originating table column, or null if it's an expression or subquery.
.table: the name of the originating table, or null if it's an expression or subquery.
.database: the name of the originating database, or null if it's an expression or subquery.
.type: the name of the [declared type](https://www.sqlite.org/datatype3.html#determination_of_column_affinity), or null if it's an expression or subquery.

https://github.com/WiseLibs/better-sqlite3/blob/HEAD/docs/api.md#columns---array-of-objects

What is the feature you are proposing to solve the problem?

We are wanna do some automagic formatting of the result based the column type. And yes, I'm aware there is only like 5 datatypes and that they can be mixed.

What alternatives have you considered?

Using better-sqlite3 instead of buns built in variant.

https://github.com/WiseLibs/better-sqlite3/blob/HEAD/docs/api.md#columns---array-of-objects

Jarred-Sumner commented 10 months ago

This would be great as a contribution.

For any potential contributors, I suggest looking at the initializeColumnNames function for code to use as an example from:

https://github.com/oven-sh/bun/blob/600a4744260d8be3fcf45a3806aad8f7a72fc398/src/bun.js/bindings/sqlite/JSSQLStatement.cpp#L218-L332

It would look a lot like that, but expose more information on those 5 fields.

The JS wrapper code is in here:

https://github.com/oven-sh/bun/blob/600a4744260d8be3fcf45a3806aad8f7a72fc398/src/js/bun/sqlite.ts#L1-L100

Tests are mostly in here (though there are a few more elswhere)

https://github.com/oven-sh/bun/blob/600a4744260d8be3fcf45a3806aad8f7a72fc398/test/js/bun/sqlite/sqlite.test.js#L611

bhardwajakshit commented 10 months ago

@Jarred-Sumner - i'd love to work on this, could you assign this to me?

thanks!

Jarred-Sumner commented 10 months ago

@Jarred-Sumner - i'd love to work on this, could you assign this to me?

thanks!

Definitely feel free to work in it and submit a PR but it doesn’t make sense to assign it to someone until there’s a PR that is close to merge-able

bhardwajakshit commented 10 months ago

hey @Jarred-Sumner, just raised a PR for this here: https://github.com/oven-sh/bun/pull/7242

i'd appreciate your feedback on the changes.