Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions javascript/node-postgres/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ you to interact with PostgreSQL databases using JavaScript code.

## About the code example

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.

The example demonstrates a flexible connection approach that works for both admin and non-admin users:

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

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

Expand Down Expand Up @@ -76,9 +76,6 @@ export CLUSTER_USER="<your user>"

# e.g. "foo0bar1baz2quux3quuux4.dsql.us-east-1.on.aws"
export CLUSTER_ENDPOINT="<your endpoint>"

# e.g. "us-east-1"
export REGION="<your region>"
```

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

* [Amazon Aurora DSQL Documentation](https://docs.aws.amazon.com/aurora-dsql/latest/userguide/what-is-aurora-dsql.html)
* [Aurora DSQL Node.js Connector](https://github.com/awslabs/aurora-dsql-nodejs-connector/tree/main/packages/node-postgres)
* [node-postgres Documentation](https://node-postgres.com/)
* [AWS SDK for JavaScript Documentation](https://docs.aws.amazon.com/sdk-for-javascript/)

**Note:** The connector automatically extracts the region from the cluster endpoint and defaults to the `postgres` database.

---

Expand Down
1,444 changes: 712 additions & 732 deletions javascript/node-postgres/package-lock.json

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions javascript/node-postgres/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
"author": "",
"license": "ISC",
"dependencies": {
"@aws-sdk/dsql-signer": "^3.705.0",
"@aws/aurora-dsql-node-postgres-connector": "^0.1.5",
"assert": "2.1.0",
"pg": "^8.13.1",
"uuid": "^11.0.2"
},
"devDependencies": {
Expand Down
69 changes: 22 additions & 47 deletions javascript/node-postgres/src/index.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,32 @@
import { DsqlSigner } from "@aws-sdk/dsql-signer";
import pg from "pg";
import { AuroraDSQLClient } from "@aws/aurora-dsql-node-postgres-connector";
import assert from "node:assert";
const { Client } = pg;

const ADMIN = "admin";
const NON_ADMIN_SCHEMA = "myschema";

async function getConnection(clusterEndpoint, user, region) {
const signer = new DsqlSigner({
hostname: clusterEndpoint,
region,
});
let token;
// Generate a fresh password token for each connection, to ensure the token is
// not expired when the connection is established
if (user === ADMIN) {
token = await signer.getDbConnectAdminAuthToken();
}
else {
signer.user = user;
token = await signer.getDbConnectAuthToken()
}
let client = new Client({
host: clusterEndpoint,
user: user,
password: token,
database: "postgres",
port: 5432,
ssl: {
rejectUnauthorized: true,
}
});

// Connect
await client.connect();
console.log("Successfully opened connection");
return client;
async function getConnection(clusterEndpoint, user) {
const client = new AuroraDSQLClient({
host: clusterEndpoint,
user: user,
});

await client.connect();
console.log("Successfully opened connection");
return client;
}

async function example() {

const clusterEndpoint = process.env.CLUSTER_ENDPOINT;
assert(clusterEndpoint);
const user = process.env.CLUSTER_USER;
assert(user);
const region = process.env.REGION;
assert(region);

let client;
try {
client = await getConnection(clusterEndpoint, user, region);
client = await getConnection(clusterEndpoint, user);

if (user !== ADMIN) {
await client.query("SET search_path=" + NON_ADMIN_SCHEMA)
await client.query("SET search_path=" + NON_ADMIN_SCHEMA);
}

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

// Insert some data
await client.query("INSERT INTO owner(name, city, telephone) VALUES($1, $2, $3)",
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)
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);
raise
throw error;
} finally {
client?.end()
client?.end();
}
Promise.resolve()
}

export { example }
export { example };
15 changes: 7 additions & 8 deletions javascript/postgres-js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ you to interact with PostgreSQL databases using JavaScript code.

## About the code example

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.

The example demonstrates a flexible connection approach that works for both admin and non-admin users:

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

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

Expand Down Expand Up @@ -76,9 +76,6 @@ export CLUSTER_USER="<your user>"

# e.g. "foo0bar1baz2quux3quuux4.dsql.us-east-1.on.aws"
export CLUSTER_ENDPOINT="<your endpoint>"

# e.g. "us-east-1"
export REGION="<your region>"
```

Run the example:
Expand All @@ -102,8 +99,10 @@ interactions.
## Additional resources

* [Amazon Aurora DSQL Documentation](https://docs.aws.amazon.com/aurora-dsql/latest/userguide/what-is-aurora-dsql.html)
* [Aurora DSQL Node.js Connector](https://github.com/awslabs/aurora-dsql-nodejs-connector/tree/main/packages/postgres-js)
* [Postgres.js Documentation](https://github.com/porsager/postgres)
* [AWS SDK for JavaScript Documentation](https://docs.aws.amazon.com/sdk-for-javascript/)

**Note:** The connector automatically extracts the region from the cluster endpoint and defaults to the `postgres` database.

---

Expand Down
Loading
Loading