Skip to content

Commit cc50cd8

Browse files
amaksimogithub-actions[bot]
authored andcommitted
Sync examples from connectors
1 parent e14caab commit cc50cd8

8 files changed

Lines changed: 291 additions & 201 deletions

File tree

javascript/postgres-js/src/alternatives/websocket/package-lock.json

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

javascript/postgres-js/src/alternatives/websocket/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88
"lint:fix": "eslint . --fix"
99
},
1010
"dependencies": {
11-
"@aws-sdk/client-sts": "^3.1001.0",
12-
"@aws-sdk/types": "^3.973.5",
11+
"@aws-sdk/client-sts": "^3.1021.0",
12+
"@aws-sdk/types": "^3.973.7",
1313
"@aws/aurora-dsql-postgresjs-connector": "^0.2.1",
1414
"buffer": "^6.0.3",
1515
"net-browserify": "^0.2.4",
1616
"performance-now": "^2.1.0",
1717
"postgres": "^3.4.7",
1818
"process": "^0.11.10",
1919
"react": "^19.2.4",
20-
"react-dom": "^19.2.4",
20+
"react-dom": "^19.2.5",
2121
"setimmediate": "^1.0.5",
2222
"timers-browserify": "^2.0.12"
2323
},

php/pdo-pgsql/src/example_preferred.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,29 @@
1010
use Aws\AuroraDsql\PdoPgsql\AuroraDsql;
1111
use Aws\AuroraDsql\PdoPgsql\DsqlConfig;
1212

13+
// Works with both admin and non-admin users:
14+
// - Admin users operate in the default "public" schema
15+
// - Non-admin users operate in a custom "myschema" schema
1316
function main(): void
1417
{
1518
$clusterEndpoint = getenv('CLUSTER_ENDPOINT') ?: throw new RuntimeException(
1619
'CLUSTER_ENDPOINT environment variable is required'
1720
);
21+
$clusterUser = getenv('CLUSTER_USER') ?: 'admin';
22+
23+
// Determine schema based on user type
24+
$schema = $clusterUser === 'admin' ? 'public' : 'myschema';
1825

1926
$config = new DsqlConfig(
2027
host: $clusterEndpoint,
28+
user: $clusterUser,
2129
occMaxRetries: 3,
2230
);
2331
$pdo = AuroraDsql::connect($config);
2432

33+
// Set search_path for the appropriate schema
34+
$pdo->exec("SET search_path = '{$schema}'");
35+
2536
// Simple read
2637
$stmt = $pdo->query('SELECT 1 AS result');
2738
$row = $stmt->fetch(PDO::FETCH_ASSOC);
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use aurora_dsql_sqlx_connector::{txn, OCCRetryExt};
5+
use sqlx::{Executor, Row};
6+
7+
#[tokio::main]
8+
async fn main() -> anyhow::Result<()> {
9+
let cluster_endpoint = std::env::var("CLUSTER_ENDPOINT")
10+
.expect("CLUSTER_ENDPOINT environment variable is not set");
11+
let cluster_user = std::env::var("CLUSTER_USER").unwrap_or_else(|_| "admin".to_string());
12+
13+
let conn_str = format!("postgres://{}@{}/postgres", cluster_user, cluster_endpoint);
14+
15+
let mut conn = aurora_dsql_sqlx_connector::connection::connect(&conn_str).await?;
16+
17+
// Create table
18+
conn.execute(
19+
"CREATE TABLE IF NOT EXISTS owner(
20+
id uuid NOT NULL DEFAULT gen_random_uuid(),
21+
name varchar(30) NOT NULL,
22+
city varchar(80) NOT NULL,
23+
PRIMARY KEY (id))",
24+
)
25+
.await?;
26+
27+
// -- Transactional write WITH OCC retry --
28+
conn.transaction_with_retry(None, |tx| {
29+
txn!({
30+
sqlx::query("INSERT INTO owner(name, city) VALUES($1, $2)")
31+
.bind("John Doe")
32+
.bind("Anytown")
33+
.execute(&mut **tx)
34+
.await?;
35+
Ok(())
36+
})
37+
})
38+
.await?;
39+
40+
// Query it back
41+
let row = sqlx::query("SELECT * FROM owner WHERE name = $1")
42+
.bind("John Doe")
43+
.fetch_one(&mut conn)
44+
.await?;
45+
46+
let name: &str = row.get("name");
47+
let city: &str = row.get("city");
48+
println!("name={}, city={}", name, city);
49+
50+
assert_eq!(name, "John Doe");
51+
assert_eq!(city, "Anytown");
52+
53+
// Clean up
54+
sqlx::query("DELETE FROM owner WHERE name = $1")
55+
.bind("John Doe")
56+
.execute(&mut conn)
57+
.await?;
58+
59+
println!("Connection exercised successfully");
60+
Ok(())
61+
}

0 commit comments

Comments
 (0)