Amazon Aurora DSQL is provided as a Preview service.
To learn more, see Betas and Previews
Using Node.js to interact with Amazon Aurora DSQL
This section describes how to use Node.js to interact with Aurora DSQL.
Before you begin, make sure that you have created a cluster in Aurora DSQL. Also make sure that you have installed Node. You must have installed version 18 or higher. Use the following command to check which version you have.
node --version
Connect to your Aurora DSQL cluster and run queries
Use the following JavaScript to connect to your cluster in Aurora DSQL.
import { DsqlSigner } from "@aws-sdk/dsql-signer";
import pg from "pg";
import assert from "node:assert";
const { Client } = pg;
async function example(clusterEndpoint) {
let client;
const region = "us-east-1";
try {
// The token expiration time is optional, and the default value 900 seconds
const signer = new DsqlSigner({
hostname: clusterEndpoint,
region,
});
const token = await signer.getDbConnectAdminAuthToken();
client = new Client({
host: clusterEndpoint,
user: "admin",
password: token,
database: "postgres",
port: 5432,
// <https://node-postgres.com/announcements> for version 8.0
ssl: true
});
// Connect
await client.connect();
// Create a new table
await client.query(`CREATE TABLE IF NOT EXISTS owner (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name VARCHAR(30) NOT NULL,
city VARCHAR(80) NOT NULL,
telephone VARCHAR(20)
)`);
// Insert some data
await client.query("INSERT INTO owner(name, city, telephone) VALUES($1, $2, $3)",
["John Doe", "Anytown", "555-555-1900"]
);
// Check that data is inserted by reading it back
const result = await client.query("SELECT id, city FROM owner where name='John Doe'");
assert.deepEqual(result.rows[0].city, "Anytown")
assert.notEqual(result.rows[0].id, null)
await client.query("DELETE FROM owner where name='John Doe'");
} catch (error) {
console.error(error);
} finally {
client?.end()
}
Promise.resolve()
}
export { example }