Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS RDS Data API executeStatement not return column names

I'm playing with the New Data API for Amazon Aurora Serverless

Is it possible to get the table column names in the response?

If for example I run the following query in a user table with the columns id, first_name, last_name, email, phone:

const sqlStatement = `
    SELECT *
    FROM user
    WHERE id = :id 
`;
const params = {
    secretArn: <mySecretArn>,
    resourceArn: <myResourceArn>,
    database: <myDatabase>,
    sql: sqlStatement,
    parameters: [
        {
            name: "id",
            value: {
                "stringValue": 1
            }
        }
    ]
};
let res = await this.RDS.executeStatement(params)
console.log(res);

I'm getting a response like this one, So I need to guess which column corresponds with each value:

{
    "numberOfRecordsUpdated": 0,
    "records": [
        [
            {
                "longValue": 1
            },
            {
                "stringValue": "Nicolas"
            },
            {
                "stringValue": "Perez"
            },
            {
                "stringValue": "[email protected]"
            },
            {
                "isNull": true
            }
        ]
    ]
}

I would like to have a response like this one:

{
    id: 1,
    first_name: "Nicolas",
    last_name: "Perez",
    email: "[email protected]",
    phone: null
}

update1

I have found an npm module that wrap Aurora Serverless Data API and simplify the development

like image 767
niqui Avatar asked Jun 30 '19 03:06

niqui


People also ask

Can I use the Amazon RDS data API in additional AWS regions?

You can now use the Amazon Relational Database Service (Amazon RDS) Data API and Amazon RDS Query editor in additional AWS Regions.

How do I run a SQL statement in AWS RDS?

You can run a SQL statement using the aws rds-data execute-statement CLI command. You can run the SQL statement in a transaction by specifying the transaction identifier with the --transaction-id option. You can start a transaction using the aws rds-data begin-transaction CLI command.

How do I enable the RDS data API?

You can enable the Data API when you create or modify the DB cluster. You can enable the Data API by using the RDS console when you create or modify an Aurora Serverless DB cluster. When you create an Aurora Serverless DB cluster, you do so by enabling the Data API in the RDS console's Connectivity section.

What is the Amazon RDS query editor?

The Amazon RDS Query Editor provides an easy way for admins and developers to run SQL queries without having to install and set up an external JDBC/ODBC client. Query results are instantly visible within the Amazon RDS Management Console.


2 Answers

We decided to take the current approach because we were trying to cut down on the response size and including column information with each record was redundant.

You can explicitly choose to include column metadata in the result. See the parameter: "includeResultMetadata".

https://docs.aws.amazon.com/rdsdataservice/latest/APIReference/API_ExecuteStatement.html#API_ExecuteStatement_RequestSyntax

like image 162
AravindR Avatar answered Sep 23 '22 16:09

AravindR


Agree with the consensus here that there should be an out of the box way to do this from the data service API. Because there is not, here's a JavaScript function that will parse the response.

const parseDataServiceResponse = res => {
    let columns = res.columnMetadata.map(c => c.name);
    let data = res.records.map(r => {
        let obj = {};
        r.map((v, i) => {
            obj[columns[i]] = Object.values(v)[0]
        });
        return obj
    })
    return data
}
like image 34
C. Slack Avatar answered Sep 22 '22 16:09

C. Slack