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

Connection.query() return type #185

Open
taeh98 opened this issue Dec 3, 2021 · 5 comments
Open

Connection.query() return type #185

taeh98 opened this issue Dec 3, 2021 · 5 comments

Comments

@taeh98
Copy link

taeh98 commented Dec 3, 2021

Hello, I hope this finds you well.

Please could you add a meaningful return type to Connection.query() (specifically, in the TypeScript typing of it)? At the moment, it returns a Promise<any>. Using an any value means that the value could be of any type, and defeats the purpose of using typed TypeScript over untyped JavaScript to begin with. As far as I can tell, it will either return an OkPacket or throw an error. Please could you, therefore, change the return type to something like Promise<OkPacket>? I don't think an OkPacket TypeScript class exists right now but, as far as I can tell, it wouldn't be too complex to create.

Thank you for your time and consideration.

@rusher
Copy link
Collaborator

rusher commented Dec 7, 2021

It would be nice to have, but not as simple than that.
Problem is that result type depends on options.

For update/insert there is already an UpsertResult type defined, return would be Promise

await connection.query('CREATE TABLE animals (' +
                       'id MEDIUMINT NOT NULL AUTO_INCREMENT,' +
                       'name VARCHAR(30) NOT NULL,' +
                       'PRIMARY KEY (id))');
let res = await connection.query('INSERT INTO animals(name) value (?)', ['sea lions']);
console.log(res);
//log : { affectedRows: 1, insertId: 1, warningStatus: 0 }
res = await connection.query('select * from animals');
console.log(res); 
// [ 
//    { id: 1, name: 'sea lions' }, 
//    { id: 2, name: 'bird' }, 
//    meta: [ ... ]
// ]

That's for the default option value. but let me show the problem with only 2 options rowsAsArray and nestTables:

res = await connection.query({ rowsAsArray: true, sql: 'select * from animals' });
console.log(res); 
// [ 
//    [ 1, 'sea lions' ], 
//    [ 2, 'bird' ],
//    meta: [...]
// ]
res = await connection.query({
  nestTables: true, 
  sql:'select a.name, a.id, b.name from animals a, animals b where b.id=1'
});
console.log(res); 
//[ 
//  { 
//     a: { name: 'sea lions', id: 1 }, 
//     b: { name: 'sea lions' } 
//  },
//  { 
//     a: { name: 'bird', id: 2 }, 
//     b: { name: 'sea lions' } 
//  },
//  meta: [...]
//]

another options multipleStatements make INSERT/UPDATE command not result in Promise, permitting multiple result.

The only choice would be to remove rowsAsArray, nestTables and multipleStatements from typescript.
The result would then be
query(sql: string): Promise<UpsertResult> | Promise<[]>;

That would not be so interesting.

Maybe another notation like in JDBC that have 2 differents commands executeUpdate and executeQuery that do the same thing, one expecting to do insert/update while the second return a resultset, to return precise results:

executeUpdate(sql: string): Promise<UpsertResult>;
executeQuery(sql: string): Promise<[]>;

That might have more interest this way

@whitebyte
Copy link

Since a programmer knows what kind of query he calls this could be passed as a generic param, something like

const res: RowArrayResult  = await connection.query<RowArrayResult>({ rowsAsArray: true, sql: 'select * from animals' });
const res: RowObjectResult  = await connection.query<RowObjectResult>({ sql: 'select * from animals' });

@rusher
Copy link
Collaborator

rusher commented Dec 16, 2021

definitively a better approch. I've created https://jira.mariadb.org/browse/CONJS-184 for this.

@rusher
Copy link
Collaborator

rusher commented Jan 25, 2022

Do i miss something or JSON type is still now permitted in Typescript ?
default return type for a result-set would be Promise<Json[]>.
Since i don't found a real json type, the only solution would be Promise<any[]> that would not help at all.

@whitebyte
Copy link

There is no JSON type in TS. Maybe Promise<Record<string, any>> or something like that would work?

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

No branches or pull requests

3 participants