11import express from "express" ;
2- import AWS from "aws-sdk" ;
2+ import { RDSDataClient , ExecuteStatementCommand } from "@ aws-sdk/client-rds-data " ;
33
44const app = express ( ) ;
55const port = 80 ;
66
77app . use ( express . urlencoded ( { extended : false } ) ) ;
88app . use ( express . json ( ) ) ;
99
10- const rdsDataService = new AWS . RDSDataService ( ) ;
10+ const rdsDataClient = new RDSDataClient ( { region : process . env . region } ) ;
1111
1212const SECRET_ARN = process . env . secretArn ;
1313const DB_CLUSTER_ARN = process . env . dbClusterArn ;
1414const DB_NAME = process . env . dbName ;
1515
16- let sqlParams = {
17- secretArn : SECRET_ARN ,
18- resourceArn : DB_CLUSTER_ARN ,
19- database : DB_NAME ,
20- includeResultMetadata : true ,
21- sql : "" ,
22- } ;
23-
2416app . get ( "/" , ( req , res ) => {
2517 res . status ( 200 ) . send ( `Hello Serverless Fargate! Integrating with Aurora Serverless Cluster ${ DB_NAME } Database` ) ;
2618} ) ;
2719
2820app . post ( "/createtable" , async ( req , res ) => {
2921 const tableName = req . body . tableName ;
3022 const sqlCreateTable = `CREATE TABLE IF NOT EXISTS ${ tableName } (id INT AUTO_INCREMENT PRIMARY KEY, foo VARCHAR(128), bar VARCHAR(128));` ;
31- sqlParams [ "sql" ] = sqlCreateTable ;
3223
3324 try {
34- const result = await rdsDataService . executeStatement ( sqlParams ) . promise ( ) ;
25+ const result = await rdsDataClient . send (
26+ new ExecuteStatementCommand ( {
27+ secretArn : SECRET_ARN ,
28+ resourceArn : DB_CLUSTER_ARN ,
29+ database : DB_NAME ,
30+ includeResultMetadata : true ,
31+ sql : sqlCreateTable ,
32+ } )
33+ ) ;
3534 return res . status ( 200 ) . send ( {
3635 body : "OK!" ,
3736 response : result ,
@@ -43,10 +42,17 @@ app.post("/createtable", async (req, res) => {
4342
4443app . get ( "/showtables" , async ( req , res ) => {
4544 const sqlShowTables = "SHOW TABLES;" ;
46- sqlParams [ "sql" ] = sqlShowTables ;
4745
4846 try {
49- const result = await rdsDataService . executeStatement ( sqlParams ) . promise ( ) ;
47+ const result = await rdsDataClient . send (
48+ new ExecuteStatementCommand ( {
49+ secretArn : SECRET_ARN ,
50+ resourceArn : DB_CLUSTER_ARN ,
51+ database : DB_NAME ,
52+ includeResultMetadata : true ,
53+ sql : sqlShowTables ,
54+ } )
55+ ) ;
5056 return res . status ( 200 ) . send ( {
5157 body : "OK!" ,
5258 response : result . records ,
@@ -57,6 +63,5 @@ app.get("/showtables", async (req, res) => {
5763} ) ;
5864
5965app . listen ( port , ( ) => {
60- // tslint:disable-next-line:no-console
6166 console . log ( `server started at http://localhost:${ port } ` ) ;
6267} ) ;
0 commit comments