DynamoDB Client
ElectroDB supports both the v2 and v3 aws clients. The client can be supplied in several ways:
- When creating a new Entity or Service
- Added to an Entity/Service instance via the
setClient()method - Dynamically at query time via the
clientexecution option
On the instantiation of an Entity:
import { Entity } from "electrodb";
import { DocumentClient } from "aws-sdk/clients/dynamodb";
const table = "my_table_name";
const client = new DocumentClient({
region: "us-east-1",
});
const task = new Entity(
{
// your model
},
{
client, // <----- client
table,
},
);
On the instantiation of an Service:
import { Entity } from "electrodb";
import { DocumentClient } from "aws-sdk/clients/dynamodb";
const table = "my_table_name";
const client = new DocumentClient({
region: "us-east-1",
});
const task = new Entity({
// your model
});
const user = new Entity({
// your model
});
const service = new Service(
{ task, user },
{
client, // <----- client
table,
},
);
Via the setClient method:
import { Entity } from "electrodb";
import { DocumentClient } from "aws-sdk/clients/dynamodb";
const table = "my_table_name";
const client = new DocumentClient({
region: "us-east-1",
});
const task = new Entity({
// your model
});
task.setClient(client);
At query execution time:
import { Entity } from "electrodb";
import { DocumentClient } from "aws-sdk/clients/dynamodb";
const client = new DocumentClient({
region: "us-east-1",
});
const task = new Entity({
// your model
});
// Pass client dynamically when executing the query
await task.get({ id: "my-id" }).go({ client });
This approach is useful for scenarios like multi-region routing, per-request client selection, or testing with different clients. The client provided at query time takes precedence over any client set at instantiation.
V2 Client
The v2 sdk will work out of the box with the DynamoDB DocumentClient.
Example:
import { DocumentClient } from "aws-sdk/clients/dynamodb";
const client = new DocumentClient({
region: "us-east-1",
});
V3 Client
The v3 client will work out of the box with the DynamoDBClient.
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
const client = new DynamoDBClient({
region: "us-east-1",
});