An intuitive DynamoDB request building API for JavaScript.
npm install dynamo-request-builder
Use the DynamoRequestBuilderCreator
with a DynamoDB DocumentClient
to create a DynamoRequestBuilder
.
import DynamoRequestBuilderCreator from "dynamo-request-builder";
const requestBuilder = new DynamoRequestBuilderCreator(dynamoDocClient).create("table_name");
import { attribute, sizeOf } from "dynamo-request-builder/attributes";
import { or } from "dynamo-request-builder/operators";
import { ReturnValues } from "dynamo-request-builder/return-values";
const request = requestBuilder.delete("partitionKeyName", "partitionKeyValue")
.withSortKey("sortKeyName", "sortKeyValue") // Sort key condition
.onlyIfAttribute("attribute").eq("value") // Add an attribute condition
.onlyIfSizeOfAttribute("attribute").lte(1) // Add a size of attribute condition
.onlyIf(
or(attribute("attribute1").gte(1), attribute("attribute2").contains("value")),
sizeOf("attribute3").eq(2)
) // Add multiple attribute conditions
.returnValues(ReturnValues.AllOld); // Specify values to return
const request = requestBuilder.get("partitionKeyName", "partitionKeyValue")
.withSortKey("sortKeyName", "sortKeyValue") // Sort key condition
.getAttributes("attribute", "attribute[0]", "attribute[0].subAttribute") // Specify attributes to retrieve
.consistentRead(); // Use consistent read
import { attribute, sizeOf } from "dynamo-request-builder/attributes";
import { not } from "dynamo-request-builder/operators";
import { ReturnValues } from "dynamo-request-builder/return-values";
const item = {
...
};
const request = requestBuilder.put(item)
.ifItemNotExists("partitionKeyName", "sortKeyName") // Only put the item if it doesn't already exist
.onlyIfAttribute("attribute").eq("value") // Add an attribute condition
.onlyIfSizeOfAttribute("attribute").lte(1) // Add a size of attribute condition
.onlyIf(
not(attribute("attribute1").gte(1)),
sizeOf("attribute3").eq(2)
) // Add multiple attribute conditions
.returnValues(ReturnValues.AllOld); // Specify values to return
import { attribute, sizeOf } from "dynamo-request-builder/attributes";
import { or } from "dynamo-request-builder/operators";
const request = requestBuilder.query("partitionKeyName", "partitionKeyValue")
.whereSortKey("sortKeyName").beginsWith("value") // Add a sort key condition
.whereAttribute("attribute").eq("value") // Add an attribute condition
.whereSizeOfAttribute("attribute").lt(1) // Add a size of attribute condition
.where(
or(attribute("attribute1").gte(1), attribute("attribute2").contains("value")),
sizeOf("attribute3").eq(2)
) // Add multiple attribute conditions
.getAttributes("attribute", "attribute[0]", "attribute[0].subAttribute") // Specify attributes to retrieve
.limit(5) // Specify a limit on the number of items to return
.useIndex("indexName") // Specify an index to use
.scanIndexDescending(); // Scan the index in descending order
import { attribute, sizeOf } from "dynamo-request-builder/attributes";
import { or } from "dynamo-request-builder/operators";
const request = requestBuilder.scan()
.whereAttribute("attribute").eq("value") // Add an attribute condition
.whereSizeOfAttribute("attribute").lt(1) // Add a size of attribute condition
.where(
or(attribute("attribute1").gte(1), attribute("attribute2").contains("value")),
sizeOf("attribute3").eq(2)
) // Add multiple attribute conditions
.getAttributes("attribute", "attribute[0]", "attribute[0].subAttribute") // Specify attributes to retrieve
.limit(5) // Specify a limit on the number of items to return
.useIndex("indexName") // Specify an index to use
import { attribute, sizeOf, update } from "dynamo-request-builder/attributes";
import { or } from "dynamo-request-builder/operators";
const request = requestBuilder.update("partitionKeyName", "partitionKeyValue")
.withSortKey("sortKeyName", "sortKeyValue") // Sort key condition
.updateAttribute("attribute").incrementBy(5) // Add an update operation
.operations(update("attribute1").add(1, 2, 3), update("attribute2").remove()) // Add multiple update operations
.onlyIfAttribute("attribute").eq("value") // Add an attribute condition
.onlyIfSizeOfAttribute("attribute").lte(1) // Add a size of attribute condition
.onlyIf(
or(attribute("attribute1").gte(1), attribute("attribute2").contains("value")),
sizeOf("attribute3").eq(2)
) // Add multiple attribute conditions
.returnValues(UpdateReturnValues.UpdatedNew); // Specify values to return
You can check the generated request parameters for yourself by accessing request.params
.
The ability to select specific attributes and apply constraint conditions and update operations to them are needed for certain expressions.
Attribute Type | Attribute Selectors | Use Cases | Methods |
---|---|---|---|
Condition Attribute | attribute(attributePath) , sizeOf(attributePath) |
Condition Expressions
Filter Expressions
Key Condition Expressions
|
|
Update Operation Attribute | update(attributePath) |
Update Expressions
|
|
Logical operators are used to extend the logic of condition expressions, filter expressions and key condition expressions.
Operator | Description |
---|---|
not(conditionExpression) |
The condition expression must evaluate to false for the result to be true |
and(...conditionExpressions) |
All condition expressions must evaluate to true for the result to be true |
or(...conditionExpressions) |
Only one condition expression must evaluate to true for the result to be true |
Once a request has been created, it can be executed and the return values can be used.
Request Type | Inheriting Types | Methods |
---|---|---|
BaseRequest |
All | execFullResponse() - Execute and return the full response |
ReadManyRequest |
QueryRequest , ScanRequest
|
|
WriteRequest |
DeleteRequest , PutRequest , UpdateRequest |
exec() - Execute and return requested attributes |
GetRequest |
None | exec() - Execute and return the requested item |