What is @aws-sdk/client-rds-data?
@aws-sdk/client-rds-data is an AWS SDK for JavaScript package that allows you to interact with Amazon RDS Data Service. This service enables you to run SQL statements on Amazon Aurora Serverless clusters through a secure HTTP endpoint without managing database connections.
What are @aws-sdk/client-rds-data's main functionalities?
Execute SQL Statements
This feature allows you to execute SQL statements on your Amazon Aurora Serverless cluster. The code sample demonstrates how to set up the client, define the parameters, and execute a SQL statement to retrieve data from a table.
const { RDSDataClient, ExecuteStatementCommand } = require('@aws-sdk/client-rds-data');
const client = new RDSDataClient({ region: 'us-west-2' });
const params = {
resourceArn: 'arn:aws:rds:us-west-2:123456789012:cluster:my-aurora-cluster',
secretArn: 'arn:aws:secretsmanager:us-west-2:123456789012:secret:my-secret',
sql: 'SELECT * FROM my_table',
database: 'my_database'
};
const run = async () => {
try {
const data = await client.send(new ExecuteStatementCommand(params));
console.log(data);
} catch (err) {
console.error(err);
}
};
run();
Batch Execute SQL Statements
This feature allows you to execute a batch of SQL statements in a single request. The code sample demonstrates how to set up the client, define the parameters, and execute multiple SQL insert statements in a batch.
const { RDSDataClient, BatchExecuteStatementCommand } = require('@aws-sdk/client-rds-data');
const client = new RDSDataClient({ region: 'us-west-2' });
const params = {
resourceArn: 'arn:aws:rds:us-west-2:123456789012:cluster:my-aurora-cluster',
secretArn: 'arn:aws:secretsmanager:us-west-2:123456789012:secret:my-secret',
sql: 'INSERT INTO my_table (id, name) VALUES (:id, :name)',
database: 'my_database',
parameterSets: [
[
{ name: 'id', value: { longValue: 1 } },
{ name: 'name', value: { stringValue: 'John' } }
],
[
{ name: 'id', value: { longValue: 2 } },
{ name: 'name', value: { stringValue: 'Jane' } }
]
]
};
const run = async () => {
try {
const data = await client.send(new BatchExecuteStatementCommand(params));
console.log(data);
} catch (err) {
console.error(err);
}
};
run();
Begin Transaction
This feature allows you to begin a new transaction. The code sample demonstrates how to set up the client, define the parameters, and begin a transaction on your Amazon Aurora Serverless cluster.
const { RDSDataClient, BeginTransactionCommand } = require('@aws-sdk/client-rds-data');
const client = new RDSDataClient({ region: 'us-west-2' });
const params = {
resourceArn: 'arn:aws:rds:us-west-2:123456789012:cluster:my-aurora-cluster',
secretArn: 'arn:aws:secretsmanager:us-west-2:123456789012:secret:my-secret',
database: 'my_database'
};
const run = async () => {
try {
const data = await client.send(new BeginTransactionCommand(params));
console.log(data);
} catch (err) {
console.error(err);
}
};
run();
Commit Transaction
This feature allows you to commit a transaction. The code sample demonstrates how to set up the client, define the parameters, and commit a transaction on your Amazon Aurora Serverless cluster.
const { RDSDataClient, CommitTransactionCommand } = require('@aws-sdk/client-rds-data');
const client = new RDSDataClient({ region: 'us-west-2' });
const params = {
resourceArn: 'arn:aws:rds:us-west-2:123456789012:cluster:my-aurora-cluster',
secretArn: 'arn:aws:secretsmanager:us-west-2:123456789012:secret:my-secret',
transactionId: 'your-transaction-id'
};
const run = async () => {
try {
const data = await client.send(new CommitTransactionCommand(params));
console.log(data);
} catch (err) {
console.error(err);
}
};
run();
Rollback Transaction
This feature allows you to roll back a transaction. The code sample demonstrates how to set up the client, define the parameters, and roll back a transaction on your Amazon Aurora Serverless cluster.
const { RDSDataClient, RollbackTransactionCommand } = require('@aws-sdk/client-rds-data');
const client = new RDSDataClient({ region: 'us-west-2' });
const params = {
resourceArn: 'arn:aws:rds:us-west-2:123456789012:cluster:my-aurora-cluster',
secretArn: 'arn:aws:secretsmanager:us-west-2:123456789012:secret:my-secret',
transactionId: 'your-transaction-id'
};
const run = async () => {
try {
const data = await client.send(new RollbackTransactionCommand(params));
console.log(data);
} catch (err) {
console.error(err);
}
};
run();
Other packages similar to @aws-sdk/client-rds-data
mysql
The 'mysql' package is a popular Node.js client for MySQL databases. It allows you to connect to a MySQL database, execute queries, and manage transactions. Unlike @aws-sdk/client-rds-data, it requires you to manage database connections and is not specific to AWS services.
pg
The 'pg' package is a PostgreSQL client for Node.js. It provides a way to connect to PostgreSQL databases, execute SQL queries, and handle transactions. Similar to the 'mysql' package, it requires manual connection management and is not tailored for AWS services.
sequelize
Sequelize is a promise-based Node.js ORM for various SQL databases, including MySQL, PostgreSQL, and SQLite. It provides a higher-level abstraction for database operations, including model definitions and query building. Unlike @aws-sdk/client-rds-data, it is not specific to AWS and offers more advanced ORM features.
knex
Knex.js is a SQL query builder for Node.js that supports multiple database types, including MySQL, PostgreSQL, and SQLite. It provides a flexible and powerful way to build and execute SQL queries. Unlike @aws-sdk/client-rds-data, it is not specific to AWS and requires manual connection management.