Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Expose LogicalType(s) for columns in QueryResult, fix #24 #25

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

ovr
Copy link

@ovr ovr commented Nov 2, 2023

Hello!

Fix - #24

Thanks

@ovr ovr force-pushed the feat/query-result-expose-column-metadata branch from fc51bba to 3ea861a Compare November 2, 2023 09:54
@ovr ovr force-pushed the feat/query-result-expose-column-metadata branch from 3ea861a to f0e40e7 Compare November 2, 2023 09:56
@@ -218,12 +225,9 @@ Connection.prototype.each = function (sql) {
* @param {...*} params
* @yields row chunks
*/
Connection.prototype.stream = async function* (sql) {
Connection.prototype.stream = async function (sql) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Description of change:

The old version used AsyncIterator (function was marked with *), which will lead to the wrong return type (not QueryResult) + unexpected iteration over another iterator.

Thanks

Copy link
Collaborator

@carlopi carlopi Nov 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What problem does this solve?
This will break the current API and require changes elsewhere that is doable but unsure if it makes sense here.
Could the GetColumns itself not be an async function?

Copy link
Author

@ovr ovr Nov 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It started to return QueryResult which is declared in typings, when the old one return AsyncGenerator.
It's not possible to call getColumns on top of AsyncIterator.

Example to demo that async * returns AsyncGenerator.

❯ node
Welcome to Node.js v16.19.1.
Type ".help" for more information.
> class MyClass { kek() { return true }; };
undefined
> const fn = async function* () { return new MyClass(); }
undefined
> console.log(fn())
Object [AsyncGenerator] {}
undefined

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the current version returns a class wrapping an AsyncGenerator.

Something like:

class MyClass {
        someSyncMethod() {                                                                                 
                return 0;
                }
        async someAsyncMethod() {
                await setTimeout(1);
                return 42;
                }
        async *[Symbol.asyncIterator]() { 
                for (var i = 0; i<10; i++)
                        yield i;
        }
};

An instance of this class can behave like an async generator (so var c = new MyClass; for await (var obj of c) ..., but can also have methods of its own like c.someSyncMethod() or await c.someAsyncMethod().

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The biggest problem that, old variant is an AsyncGenerator on top of QueryResult (local scoped), which implements asyncIterator.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure I see the problem as you are seeing it, could you go over it once again?

Copy link
Author

@ovr ovr Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

image

is it clear?

@ovr ovr marked this pull request as ready for review November 2, 2023 10:08
@Maxxen
Copy link
Member

Maxxen commented Nov 2, 2023

Perhaps it would be better do something similar to the Columns method on statements instead of just returning the type id and name:

Napi::Value Statement::Columns(const Napi::CallbackInfo &info) {

if you were to use the TypeToObject function (feel free to move it) youd get the extra type info recursively so that you can determine e.g. the width of decimals or the member types of structs.

@@ -717,7 +718,7 @@ Statement.prototype.sql;

/**
* @method
* @return {ColumnInfo[]} - Array of column names and types
* @return {ColumnInfo[] | null} - Array of column names and types
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prof:

image

@@ -262,7 +262,7 @@ export class Statement {

run(...args: [...any, Callback<void>] | any[]): Statement;

columns(): ColumnInfo[];
columns(): ColumnInfo[] | null;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants