Skip to content

Commit

Permalink
[CONJS-241] metaAsArray missing option in typescript description
Browse files Browse the repository at this point in the history
  • Loading branch information
rusher committed Feb 7, 2023
1 parent 695bc10 commit ba70356
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
4 changes: 2 additions & 2 deletions documentation/promise-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -710,8 +710,8 @@ This option is mainly for mysql2 compatibility.
```javascript
const [rows, meta] = await connection.query({ metaAsArray: true, sql: 'select * from animals' });
// rows = [
// [ 1, 'sea lions' ],
// [ 2, 'bird' ],
// {'id': 1, 'name': 'sea lions' },
// {'id': 2, 'name': 'bird' },
// ]
// meta = [...]
```
Expand Down
12 changes: 12 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ export interface QueryConfig {
*/
rowsAsArray?: boolean;

/**
* Compatibility option, causes Promise to return an array object,
* `[rows, metadata]` rather than the rows as JSON objects with a `meta` property.
* Default to false.
*/
metaAsArray?: boolean;

/**
* force returning insertId as Number in place of BigInt
*/
Expand Down Expand Up @@ -374,6 +381,11 @@ export interface ConnectionConfig extends UserConnectionConfig, QueryConfig {
* Default to false.
*/
metaAsArray?: boolean;

/**
* Return result-sets as array, rather than a JSON object. This is a faster way to get results
*/
rowsAsArray?: boolean;
}

export interface PoolConfig extends ConnectionConfig {
Expand Down
39 changes: 39 additions & 0 deletions types/mariadb-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,43 @@ async function testTypeCast(): Promise<void> {
}
}

async function testRowsAsArray(): Promise<void> {
const connection = await createConnection({ rowsAsArray: true });

const rows = await connection.query<[string, string][]>(`SELECT 'upper' as upper, 'lower' as lower`);

if (rows[0][0] !== 'upper') {
throw new Error('wrong value');
}

const rows2 = await connection.query<[string, string][]>({
sql: `SELECT 'upper' as upper, 'lower' as lower`,
rowsAsArray: true
});

if (rows2[0][0] !== 'upper') {
throw new Error('wrong value');
}
}

async function metaAsArray(): Promise<void> {
const connection = await createConnection({ metaAsArray: true });

const res = await connection.query<[[string, string][], FieldInfo[]]>(`SELECT 'upper' as upper, 'lower' as lower`);

if (res[1].length > 0) {
throw new Error('expected meta');
}

const res2 = await connection.query<[[string, string][], FieldInfo[]]>({
sql: `SELECT 'upper' as upper, 'lower' as lower`,
metaAsArray: true
});
if (res2[1].length > 0) {
throw new Error('expected meta');
}
}

async function testPool(): Promise<void> {
let pool;

Expand Down Expand Up @@ -343,6 +380,8 @@ async function runTests(): Promise<void> {
await testTypeCast();
await testPool();
await testPoolCluster();
await testRowsAsArray();
await metaAsArray();

console.log('done');
} catch (err) {
Expand Down

0 comments on commit ba70356

Please sign in to comment.