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

What is the correct syntax for including SQL values AND rowsAsArray option in a query? #3130

Open
millerbryan opened this issue Oct 18, 2024 Discussed in #3095 · 1 comment

Comments

@millerbryan
Copy link

millerbryan commented Oct 18, 2024

Discussed in #3095

This is really a documentation enhancement request and should not be moved to a discussion.

There is no place in the docs where the correct syntax for using both rowsAsArray and a SQL value is shown.

Originally posted by millerbryan October 2, 2024
I've looked at all the examples in the docs which use rowsAsArray and it isn't clear how to specify it in a query (rather than in the connection) when passing in SQL values. Is this possible to pass both?

// simplified code
const sql = "select county_name from static_web_data.state_counties where state_name = ?";
const state = "TX"

        try {
          const [rows, fields] = await pool.query({sql, [state], rowsAsArray: true});
          if (rows) {
            return rows;
          }
        } catch (err) {
          console.log(err);
        }
```</div>
@wellwelwel
Copy link
Collaborator

wellwelwel commented Oct 18, 2024

Thanks, @millerbryan 🤝

Based on the types, there are two ways to use it:

query<T extends QueryResult>(
options: QueryOptions,
): Promise<[T, FieldPacket[]]>;
query<T extends QueryResult>(
options: QueryOptions,
values: any,
): Promise<[T, FieldPacket[]]>;

/**
* The SQL for the query
*/
sql: string;
/**
* The values for the query
*/
values?: any | any[] | { [param: string]: any };

// A
pool.query({ sql: '', values: [], rowsAsArray: true });

// B
pool.query({ sql: '', rowsAsArray: true }, []);

I'll check it out in practice and also what happens if both are used at the same time with different values:

🧐💭

pool.query({ sql: '', values: [], rowsAsArray: true }, []);

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

No branches or pull requests

2 participants