When you look at the Deno Mysql documentation you can use two functions to run your query :
client.execute(/*SQL*/)
client.query(/*SQL*/)
I wasn't sure when to use what, so I consulted the code to find out.
Both function work the same as the query
function is internally using the execute
function. But they have not the same return value:
The
execute
function return anExecuteResult
object.While the
query
function return either anExecuteResult
object or anarray
.
Basicaly it's means that query
is more interesting if you want to retrieve the content of the "rows" directly (wich is mostly the case on a SELECT query)
export type ExecuteResult = {
affectedRows?: number;
lastInsertId?: number;
fields?: FieldInfo[];
rows?: any[];
iterator?: any;
};
async query(sql: string, params?: any[]): Promise<ExecuteResult | any[]> {
const result = await this.execute(sql, params);
if (result && result.rows) {
return result.rows;
} else {
return result;
}
}
When to use them?
For HTTP calls:
Use
query
forGET
endpointUse
execute
forPOST/PUT/UPDATE/DELETE
endpoint
In terms of SQL:
Use
query
onSELECT
statements or any other SQL operation that returns rows from the database.Use
execute
onINSERT, UPDATE, DELETE
, or any other SQL operation that modifies data in the database