Skip to content

Commit 759d440

Browse files
committed
typing and readme docs for rowAsArray
1 parent a640d47 commit 759d440

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,36 @@ con.promise().query("SELECT 1")
216216
.then( () => con.end());
217217
```
218218
219+
## Array results
220+
221+
If you have two columns with the same name, you might want to get results as an array rather than an object to prevent them from clashing. This is a deviation from the [Node MySQL][node-mysql] library.
222+
223+
For example: `select 1 as foo, 2 as foo`.
224+
225+
You can enable this setting at either the connection level (applies to all queries), or at the query level (applies only to that specific query).
226+
227+
### Connection Option
228+
```js
229+
const con = mysql.createConnection(
230+
{ host: 'localhost', database: 'test', user: 'root', rowsAsArray: true }
231+
);
232+
233+
```
234+
235+
### Query Option
236+
237+
```js
238+
con.query({ sql: 'select 1 as foo, 2 as foo', rowsAsArray: true }, function(err, results, fields) {
239+
console.log(results) // will be an array of arrays rather than an array of objects
240+
console.log(fields) // these are unchanged
241+
});
242+
243+
```
244+
245+
246+
### Query
247+
248+
219249
## API and Configuration
220250
221251
MySQL2 is mostly API compatible with [Node MySQL][node-mysql]. You should check their API documentation to see all available API options.

typings/mysql/lib/Connection.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,14 @@ declare namespace Connection {
147147
* object with ssl parameters or a string containing name of ssl profile
148148
*/
149149
ssl?: string | SslOptions;
150+
151+
152+
/**
153+
* Return each row as an array, not as an object.
154+
* This is useful when you have duplicate column names.
155+
* This can also be set in the `QueryOption` object to be applied per-query.
156+
*/
157+
rowsAsArray?: boolean
150158
}
151159

152160
export interface SslOptions {

typings/mysql/lib/protocol/sequences/Query.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ declare namespace Query {
5151
* You can find which field function you need to use by looking at: RowDataPacket.prototype._typeCast
5252
*/
5353
typeCast?: any;
54+
55+
/**
56+
* This overrides the same option set at the connection level.
57+
*
58+
*/
59+
rowsAsArray?: boolean
5460
}
5561

5662
export interface StreamOptions {

0 commit comments

Comments
 (0)