Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

Allow DynamoDB consumers to bring their own config #82

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
42 changes: 32 additions & 10 deletions src/services/dynamodb/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use aws_config::ConfigLoader;
use aws_sdk_dynamodb::{config::Builder as ConfigBuilder, Client as DynamoDBClient, Endpoint};
use aws_types::region::Region;
use cached::proc_macro::cached;
Expand All @@ -11,6 +12,19 @@ pub use client_extensions::{
DynamoDBClientExt, DynamoDBModel, Version, VersionField, Versioned, VersionedPutError,
};

fn determine_endpoint(config: SdkConfig) -> DynamoDBClient {
match std::env::var("TARGET").map(|target| target.eq_ignore_ascii_case("local")) {
Ok(true) => {
let dynamo_config = ConfigBuilder::from(&config)
.endpoint_resolver(Endpoint::immutable("http://localhost:8000").unwrap())
.build();

DynamoDBClient::from_conf(dynamo_config)
}
_ => DynamoDBClient::new(&config),
}
}

async fn dynamodb_client(region: Option<&'static str>) -> DynamoDBClient {
let config_builder = aws_config::from_env();

Expand All @@ -21,19 +35,27 @@ async fn dynamodb_client(region: Option<&'static str>) -> DynamoDBClient {
.load()
.await;

match std::env::var("TARGET").map(|target| target.eq_ignore_ascii_case("local")) {
Ok(true) => {
let dynamo_config = ConfigBuilder::from(&config)
.endpoint_resolver(Endpoint::immutable("http://localhost:8000").unwrap())
.build();
determine_endpoint(config)
}

DynamoDBClient::from_conf(dynamo_config)
}
_ => DynamoDBClient::new(&config),
}
async fn dynamodb_client(config_loader: Option<ConfigLoader>) -> DynamoDBClient {
let config = {
let Some(config) = config_loader else {
aws_config::from_env()
};

config.load().await
};

determine_endpoint(config)
}

#[cached]
pub async fn dynamodb(config_loader: Option<ConfigLoader>) -> DynamoDBClient {
dynamodb_client(config_loader).await
}

#[cached]
pub async fn dynamodb(region: Option<&'static str>) -> DynamoDBClient {
dynamodb_client(region).await
dynamodb_client(config_loader).await
}