Skip to content

Commit b58ae6d

Browse files
committed
Update Python and JavaScript samples to use DSQL connectors
- Python psycopg/psycopg2: Use aurora-dsql-python-connector - JavaScript node-postgres: Use @aws/aurora-dsql-node-postgres-connector - JavaScript postgres-js: Use @aws/aurora-dsql-postgresjs-connector - Update READMEs to reference connector documentation
1 parent 8932139 commit b58ae6d

14 files changed

Lines changed: 1487 additions & 1699 deletions

File tree

javascript/node-postgres/README.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ you to interact with PostgreSQL databases using JavaScript code.
1111

1212
## About the code example
1313

14+
This example uses the [Aurora DSQL Node.js Connector](https://github.com/awslabs/aurora-dsql-nodejs-connector) which automatically handles IAM token generation for authentication.
15+
1416
The example demonstrates a flexible connection approach that works for both admin and non-admin users:
1517

16-
* When connecting as an **admin user**, the example uses the `public` schema and generates an admin authentication
17-
token.
18-
* When connecting as a **non-admin user**, the example uses a custom `myschema` schema and generates a standard
19-
authentication token.
18+
* When connecting as an **admin user**, the example uses the `public` schema and generates an admin authentication token.
19+
* When connecting as a **non-admin user**, the example uses a custom `myschema` schema and generates a standard authentication token.
2020

2121
The code automatically detects the user type and adjusts its behavior accordingly.
2222

@@ -76,9 +76,6 @@ export CLUSTER_USER="<your user>"
7676

7777
# e.g. "foo0bar1baz2quux3quuux4.dsql.us-east-1.on.aws"
7878
export CLUSTER_ENDPOINT="<your endpoint>"
79-
80-
# e.g. "us-east-1"
81-
export REGION="<your region>"
8279
```
8380

8481
Run the example:
@@ -93,8 +90,10 @@ The example contains comments explaining the code and the operations being perfo
9390
## Additional resources
9491

9592
* [Amazon Aurora DSQL Documentation](https://docs.aws.amazon.com/aurora-dsql/latest/userguide/what-is-aurora-dsql.html)
93+
* [Aurora DSQL Node.js Connector](https://github.com/awslabs/aurora-dsql-nodejs-connector/tree/main/packages/node-postgres)
9694
* [node-postgres Documentation](https://node-postgres.com/)
97-
* [AWS SDK for JavaScript Documentation](https://docs.aws.amazon.com/sdk-for-javascript/)
95+
96+
**Note:** The connector automatically extracts the region from the cluster endpoint and defaults to the `postgres` database.
9897

9998
---
10099

javascript/node-postgres/package-lock.json

Lines changed: 712 additions & 732 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

javascript/node-postgres/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
"author": "",
1111
"license": "ISC",
1212
"dependencies": {
13-
"@aws-sdk/dsql-signer": "^3.705.0",
13+
"@aws/aurora-dsql-node-postgres-connector": "^0.1.5",
1414
"assert": "2.1.0",
15-
"pg": "^8.13.1",
1615
"uuid": "^11.0.2"
1716
},
1817
"devDependencies": {
Lines changed: 22 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,32 @@
1-
import { DsqlSigner } from "@aws-sdk/dsql-signer";
2-
import pg from "pg";
1+
import { AuroraDSQLClient } from "@aws/aurora-dsql-node-postgres-connector";
32
import assert from "node:assert";
4-
const { Client } = pg;
53

64
const ADMIN = "admin";
75
const NON_ADMIN_SCHEMA = "myschema";
86

9-
async function getConnection(clusterEndpoint, user, region) {
10-
const signer = new DsqlSigner({
11-
hostname: clusterEndpoint,
12-
region,
13-
});
14-
let token;
15-
// Generate a fresh password token for each connection, to ensure the token is
16-
// not expired when the connection is established
17-
if (user === ADMIN) {
18-
token = await signer.getDbConnectAdminAuthToken();
19-
}
20-
else {
21-
signer.user = user;
22-
token = await signer.getDbConnectAuthToken()
23-
}
24-
let client = new Client({
25-
host: clusterEndpoint,
26-
user: user,
27-
password: token,
28-
database: "postgres",
29-
port: 5432,
30-
ssl: {
31-
rejectUnauthorized: true,
32-
}
33-
});
34-
35-
// Connect
36-
await client.connect();
37-
console.log("Successfully opened connection");
38-
return client;
7+
async function getConnection(clusterEndpoint, user) {
8+
const client = new AuroraDSQLClient({
9+
host: clusterEndpoint,
10+
user: user,
11+
});
12+
13+
await client.connect();
14+
console.log("Successfully opened connection");
15+
return client;
3916
}
4017

4118
async function example() {
42-
4319
const clusterEndpoint = process.env.CLUSTER_ENDPOINT;
4420
assert(clusterEndpoint);
4521
const user = process.env.CLUSTER_USER;
4622
assert(user);
47-
const region = process.env.REGION;
48-
assert(region);
4923

5024
let client;
5125
try {
52-
client = await getConnection(clusterEndpoint, user, region);
26+
client = await getConnection(clusterEndpoint, user);
5327

5428
if (user !== ADMIN) {
55-
await client.query("SET search_path=" + NON_ADMIN_SCHEMA)
29+
await client.query("SET search_path=" + NON_ADMIN_SCHEMA);
5630
}
5731

5832
// Create a new table
@@ -64,24 +38,25 @@ async function example() {
6438
)`);
6539

6640
// Insert some data
67-
await client.query("INSERT INTO owner(name, city, telephone) VALUES($1, $2, $3)",
41+
await client.query(
42+
"INSERT INTO owner(name, city, telephone) VALUES($1, $2, $3)",
6843
["John Doe", "Anytown", "555-555-1900"]
6944
);
7045

7146
// Check that data is inserted by reading it back
72-
const result = await client.query("SELECT id, city FROM owner where name='John Doe'");
73-
assert.deepEqual(result.rows[0].city, "Anytown")
74-
assert.notEqual(result.rows[0].id, null)
47+
const result = await client.query(
48+
"SELECT id, city FROM owner where name='John Doe'"
49+
);
50+
assert.deepEqual(result.rows[0].city, "Anytown");
51+
assert.notEqual(result.rows[0].id, null);
7552

7653
await client.query("DELETE FROM owner where name='John Doe'");
77-
7854
} catch (error) {
7955
console.error(error);
80-
raise
56+
throw error;
8157
} finally {
82-
client?.end()
58+
client?.end();
8359
}
84-
Promise.resolve()
8560
}
8661

87-
export { example }
62+
export { example };

javascript/postgres-js/README.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ you to interact with PostgreSQL databases using JavaScript code.
1111

1212
## About the code example
1313

14+
This example uses the [Aurora DSQL Node.js Connector](https://github.com/awslabs/aurora-dsql-nodejs-connector) which automatically handles IAM token generation for authentication.
15+
1416
The example demonstrates a flexible connection approach that works for both admin and non-admin users:
1517

16-
* When connecting as an **admin user**, the example uses the `public` schema and generates an admin authentication
17-
token.
18-
* When connecting as a **non-admin user**, the example uses a custom `myschema` schema and generates a standard
19-
authentication token.
18+
* When connecting as an **admin user**, the example uses the `public` schema and generates an admin authentication token.
19+
* When connecting as a **non-admin user**, the example uses a custom `myschema` schema and generates a standard authentication token.
2020

2121
The code automatically detects the user type and adjusts its behavior accordingly.
2222

@@ -76,9 +76,6 @@ export CLUSTER_USER="<your user>"
7676

7777
# e.g. "foo0bar1baz2quux3quuux4.dsql.us-east-1.on.aws"
7878
export CLUSTER_ENDPOINT="<your endpoint>"
79-
80-
# e.g. "us-east-1"
81-
export REGION="<your region>"
8279
```
8380

8481
Run the example:
@@ -102,8 +99,10 @@ interactions.
10299
## Additional resources
103100

104101
* [Amazon Aurora DSQL Documentation](https://docs.aws.amazon.com/aurora-dsql/latest/userguide/what-is-aurora-dsql.html)
102+
* [Aurora DSQL Node.js Connector](https://github.com/awslabs/aurora-dsql-nodejs-connector/tree/main/packages/postgres-js)
105103
* [Postgres.js Documentation](https://github.com/porsager/postgres)
106-
* [AWS SDK for JavaScript Documentation](https://docs.aws.amazon.com/sdk-for-javascript/)
104+
105+
**Note:** The connector automatically extracts the region from the cluster endpoint and defaults to the `postgres` database.
107106

108107
---
109108

0 commit comments

Comments
 (0)