Skip to content

Commit a51fa04

Browse files
committed
Restructure examples with alternatives folder structure
1 parent 99d173b commit a51fa04

45 files changed

Lines changed: 1397 additions & 72 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/python-psycopg2-integ-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,4 @@ jobs:
6868
pip list
6969
echo "$GITHUB_WORKSPACE" >> $GITHUB_PATH
7070
wget https://www.amazontrust.com/repository/AmazonRootCA1.pem -O root.pem
71-
python src/example.py
71+
python src/example_preferred.py

.github/workflows/python-psycopg3-integ-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,5 @@ jobs:
6868
pip list
6969
echo "$GITHUB_WORKSPACE" >> $GITHUB_PATH
7070
wget https://www.amazontrust.com/repository/AmazonRootCA1.pem -O root.pem
71-
python src/example.py
71+
python src/example_preferred.py
7272

javascript/node-postgres/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "index.js",
66
"type": "module",
77
"scripts": {
8-
"test": "node --experimental-vm-modules node_modules/.bin/jest --testPathPattern='test/smoke.test.js' --runInBand --detectOpenHandles --forceExit"
8+
"test": "node --experimental-vm-modules node_modules/.bin/jest --testPathPattern='test/example_preferred.test.js' --runInBand --detectOpenHandles --forceExit"
99
},
1010
"author": "",
1111
"license": "ISC",

javascript/node-postgres/src/index.js renamed to javascript/node-postgres/src/alternatives/no_connection_pool/example_with_no_connection_pool.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
import { AuroraDSQLClient } from "@aws/aurora-dsql-node-postgres-connector";
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
26
import assert from "node:assert";
7+
import { AuroraDSQLClient } from "@aws/aurora-dsql-node-postgres-connector";
38

49
const ADMIN = "admin";
510
const NON_ADMIN_SCHEMA = "myschema";
@@ -11,15 +16,14 @@ async function getConnection(clusterEndpoint, user) {
1116
});
1217

1318
await client.connect();
14-
console.log("Successfully opened connection");
1519
return client;
1620
}
1721

1822
async function example() {
1923
const clusterEndpoint = process.env.CLUSTER_ENDPOINT;
20-
assert(clusterEndpoint);
24+
assert(clusterEndpoint, "CLUSTER_ENDPOINT environment variable is not set");
2125
const user = process.env.CLUSTER_USER;
22-
assert(user);
26+
assert(user, "CLUSTER_USER environment variable is not set");
2327

2428
let client;
2529
try {
@@ -51,11 +55,12 @@ async function example() {
5155
assert.notEqual(result.rows[0].id, null);
5256

5357
await client.query("DELETE FROM owner where name='John Doe'");
58+
console.log("Completed successfully");
5459
} catch (error) {
5560
console.error(error);
5661
throw error;
5762
} finally {
58-
client?.end();
63+
await client?.end();
5964
}
6065
}
6166

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { DsqlSigner } from "@aws-sdk/dsql-signer";
2+
import pg from "pg";
3+
import assert from "node:assert";
4+
const { Client } = pg;
5+
6+
const ADMIN = "admin";
7+
const NON_ADMIN_SCHEMA = "myschema";
8+
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;
39+
}
40+
41+
async function example() {
42+
43+
const clusterEndpoint = process.env.CLUSTER_ENDPOINT;
44+
assert(clusterEndpoint);
45+
const user = process.env.CLUSTER_USER;
46+
assert(user);
47+
const region = process.env.REGION;
48+
assert(region);
49+
50+
let client;
51+
try {
52+
client = await getConnection(clusterEndpoint, user, region);
53+
54+
if (user !== ADMIN) {
55+
await client.query("SET search_path=" + NON_ADMIN_SCHEMA)
56+
}
57+
58+
// Create a new table
59+
await client.query(`CREATE TABLE IF NOT EXISTS owner (
60+
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
61+
name VARCHAR(30) NOT NULL,
62+
city VARCHAR(80) NOT NULL,
63+
telephone VARCHAR(20)
64+
)`);
65+
66+
// Insert some data
67+
await client.query("INSERT INTO owner(name, city, telephone) VALUES($1, $2, $3)",
68+
["John Doe", "Anytown", "555-555-1900"]
69+
);
70+
71+
// 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)
75+
76+
await client.query("DELETE FROM owner where name='John Doe'");
77+
78+
} catch (error) {
79+
console.error(error);
80+
throw error;
81+
} finally {
82+
client?.end()
83+
}
84+
}
85+
86+
export { example }
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import assert from "node:assert";
7+
import { AuroraDSQLPool } from "@aws/aurora-dsql-node-postgres-connector";
8+
9+
const ADMIN = "admin";
10+
const NON_ADMIN_SCHEMA = "myschema";
11+
12+
function createPool(clusterEndpoint, user) {
13+
return new AuroraDSQLPool({
14+
host: clusterEndpoint,
15+
user: user,
16+
max: 10,
17+
idleTimeoutMillis: 30000,
18+
connectionTimeoutMillis: 10000,
19+
});
20+
}
21+
22+
async function example() {
23+
const clusterEndpoint = process.env.CLUSTER_ENDPOINT;
24+
assert(clusterEndpoint, "CLUSTER_ENDPOINT environment variable is not set");
25+
const user = process.env.CLUSTER_USER;
26+
assert(user, "CLUSTER_USER environment variable is not set");
27+
28+
const pool = createPool(clusterEndpoint, user);
29+
30+
try {
31+
// Get a client from the pool
32+
const client = await pool.connect();
33+
34+
try {
35+
if (user !== ADMIN) {
36+
await client.query("SET search_path=" + NON_ADMIN_SCHEMA);
37+
}
38+
39+
// Create a new table
40+
await client.query(`CREATE TABLE IF NOT EXISTS owner (
41+
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
42+
name VARCHAR(30) NOT NULL,
43+
city VARCHAR(80) NOT NULL,
44+
telephone VARCHAR(20)
45+
)`);
46+
47+
// Insert some data
48+
await client.query(
49+
"INSERT INTO owner(name, city, telephone) VALUES($1, $2, $3)",
50+
["John Doe", "Anytown", "555-555-1900"]
51+
);
52+
53+
// Check that data is inserted by reading it back
54+
const result = await client.query(
55+
"SELECT id, city FROM owner where name='John Doe'"
56+
);
57+
assert.deepEqual(result.rows[0].city, "Anytown");
58+
assert.notEqual(result.rows[0].id, null);
59+
60+
await client.query("DELETE FROM owner where name='John Doe'");
61+
console.log("Completed successfully");
62+
} finally {
63+
// Release the client back to the pool
64+
client.release();
65+
}
66+
} catch (error) {
67+
console.error(error);
68+
throw error;
69+
} finally {
70+
await pool.end();
71+
}
72+
}
73+
74+
export { example };
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import assert from "node:assert";
7+
import { AuroraDSQLPool } from "@aws/aurora-dsql-node-postgres-connector";
8+
9+
const NUM_CONCURRENT_QUERIES = 8;
10+
11+
function createPool(clusterEndpoint, user) {
12+
return new AuroraDSQLPool({
13+
host: clusterEndpoint,
14+
user: user,
15+
max: 10,
16+
idleTimeoutMillis: 30000,
17+
connectionTimeoutMillis: 10000,
18+
});
19+
}
20+
21+
async function worker(pool, workerId) {
22+
const result = await pool.query("SELECT $1::int as worker_id", [workerId]);
23+
console.log(`Worker ${workerId} result: ${result.rows[0].worker_id}`);
24+
assert.strictEqual(result.rows[0].worker_id, workerId);
25+
}
26+
27+
async function example() {
28+
const clusterEndpoint = process.env.CLUSTER_ENDPOINT;
29+
assert(clusterEndpoint, "CLUSTER_ENDPOINT environment variable is not set");
30+
const user = process.env.CLUSTER_USER;
31+
assert(user, "CLUSTER_USER environment variable is not set");
32+
33+
const pool = createPool(clusterEndpoint, user);
34+
35+
try {
36+
// Run concurrent queries using the connection pool
37+
const workers = [];
38+
for (let i = 1; i <= NUM_CONCURRENT_QUERIES; i++) {
39+
workers.push(worker(pool, i));
40+
}
41+
42+
// Wait for all workers to complete
43+
await Promise.all(workers);
44+
45+
console.log("Connection pool with concurrent connections exercised successfully");
46+
} catch (error) {
47+
console.error(error);
48+
throw error;
49+
} finally {
50+
await pool.end();
51+
}
52+
}
53+
54+
export { example };
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { example } from '../../../src/alternatives/no_connection_pool/example_with_no_connection_pool.js';
2+
3+
test('Smoke test - example_with_no_connection_pool', async () => {
4+
await example();
5+
}, 30000);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { example } from '../../../src/alternatives/no_connection_pool/example_with_no_connector.js';
2+
3+
test('Smoke test - example_with_no_connector', async () => {
4+
await example();
5+
}, 30000);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { example } from '../../../src/alternatives/pool/example_with_nonconcurrent_connection_pool.js';
2+
3+
test('Smoke test - example_with_nonconcurrent_connection_pool', async () => {
4+
await example();
5+
}, 30000);

0 commit comments

Comments
 (0)