Better and more autonomous types for query
and execute
#2126
Replies: 2 comments
-
also when you call a stored procedure that has multiple statements - no need for the Example: await connection.query(`
drop procedure if exists getUsers;
`);
await connection.query(`
CREATE PROCEDURE getUsers()
BEGIN
SELECT 1 as one;
SELECT 1 as two;
SELECT 1 as three;
END
`);
const [rows, fields] = await connection.query('CALL getUsers()');
console.log(rows, fields); Prints
|
Beta Was this translation helpful? Give feedback.
-
Hey @sidorares, for Procedure Calls we can use the Using your example: const [rows] = await conn.query<ProcedureCallPacket<RowDataPacket[]>>(
'CALL getUsers()',
);
rows.forEach((users) => {
if (!isResultSetHeader(users)) {
users.forEach((user) => {
console.log(user);
});
}
});
/**
* { one: 1 }
* { two: 1 }
* { three: 1 }
*/ The return from a Procedure Call is From TypeScript documentation, you can follow about the |
Beta Was this translation helpful? Give feedback.
-
Important
This isn't for this moment, only in a future plan, but I would like to keep that idea open.
Bringing up an idea for a major release 💡
Reading and basing from various past discussions, the types from
query
andexecute
really can be better.How would it be?
query
andexecute
will be just:multiStatements
astrue
, the types will be:A simple optional helper to handle the return from queries 🤹🏻♀️
A simple helper like
isResultSetHeader
can ensure the queries return, especially for Procedure Calls.When the query return is not known, this can be helpful and can also be easily documented.
A basic end-use in practice:
About TypeScript
For TypeScript users, this can be particularly useful.
Including, this feature can already be seen from current examples:
node-mysql2/examples/typescript/procedure-call-packet.ts
Lines 18 to 32 in 9051caf
And working here:
node-mysql2/examples/typescript/procedure-call-packet.ts
Lines 78 to 87 in 9051caf
As before, this is just an idea and it's not for this time 🙋🏻♂️
Beta Was this translation helpful? Give feedback.
All reactions