Skip to content

Commit

Permalink
feat: cached S3, dynamo and SQS clients
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Shaw committed Sep 24, 2024
1 parent 661d1fd commit 3a3290b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
8 changes: 7 additions & 1 deletion packages/core/src/queue/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { SQSClient } from '@aws-sdk/client-sqs'

/** @type {Record<string, import('@aws-sdk/client-sqs').SQSClient>} */
const sqsClients = {}

/**
* @param {import('./types.js').QueueConnect | SQSClient} target
*/
export function connectQueue (target) {
if (target instanceof SQSClient) {
return target
}
return new SQSClient(target)
if (!sqsClients[target.region]) {
sqsClients[target.region] = new SQSClient(target)
}
return sqsClients[target.region]
}
17 changes: 14 additions & 3 deletions packages/core/src/store/index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
import { S3Client } from '@aws-sdk/client-s3'
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'

/** @type {Record<string, import('@aws-sdk/client-s3').S3Client>} */
const s3Clients = {}

/**
* @param {import('./types.js').BucketConnect | S3Client} target
*/
export function connectBucket (target) {
if (target instanceof S3Client) {
return target
}
return new S3Client(target)
if (!s3Clients[target.region]) {
s3Clients[target.region] = new S3Client(target)
}
return s3Clients[target.region]
}

/** @type {Record<string, import('@aws-sdk/client-dynamodb').DynamoDBClient>} */
const dynamoClients = {}

/**
* @param {import('./types.js').TableConnect | DynamoDBClient} target
*/
export function connectTable (target) {
if (target instanceof DynamoDBClient) {
return target
}

return new DynamoDBClient(target)
if (!dynamoClients[target.region]) {
dynamoClients[target.region] = new DynamoDBClient(target)
}
return dynamoClients[target.region]
}

/** @typedef {import('sst/constructs').TableProps} TableProps */
Expand Down

0 comments on commit 3a3290b

Please sign in to comment.