npm install --save dynamo-light
Set AWS configurations in environment variables:
export AWS_ACCESS_KEY_ID="Your AWS Access Key ID"
export AWS_SECRET_ACCESS_KEY="Your AWS Secret Access Key"
export AWS_REGION="us-west-2"Assume you have a simple table - Users with partitionKey userName (demo purpose only, use unique field - such as uuid, in real projects).
const { Table } = require("dynamo-light");
const userTable = new Table("Users");const user = await userTable.get("WarrenBuffett"); // implicit keyExpressionOR
const user = await userTable.get({ userName: "WarrenBuffett" }); // explicit keyExpressionawait userTable.put({
username: "JackMa",
age: 53,
occupation: "investor"
});await userTable.update("JackMa", {
age: 54,
occupation: "entrepreneur"
});OR
await userTable.update(
{ userName: "JackMa" },
{
age: 54,
occupation: "entrepreneur"
}
);await userTable.delete("JackMa");const users = await userTable.scan(); // with pagination
const users = await userTable.scan({}, { pagination: false }); // fetch allFor a table populationTable that has country as partitionKey and year as sortKey:
await populationTable.get({
country: "Canada",
year: 2000
});await populationTable.put({
country: "Canada",
year: 2001,
population: 31.01,
unit: "million",
alias: "CA"
});await populationTable.update(
{
country: "Canada",
year: 2001
},
{
population: 31.02
// ... other new fields
}
);await populationTable.delete({
country: "Canada",
year: 2001
});await populationTable.query(
{
country: "Canada"
},
{ pagination: false }
); // Returns all canada population recordsawait populationTable.query({
country: "Canada",
year: 1949,
sortKeyOperator: ">="
}); // Returns canada population records whose year is larger or equals to 1949await populationTable.query({
country: "Canada",
year: [1949, 1960],
sortKeyOperator: "BETWEEN"
}); // Returns canada population records whose year is between 1949 and 1960 (inclusive)Here is a list of the available sortKeyOperators.
Assume table populationTable has a global secondary index alias-year-index. Its partitionKey is alias and sortKey is year:
await populationTable.query({
indexName: "alias-year-index",
alias: "CA",
year: 1949,
sortKeyOperator: ">="
}); // Returns records whose year is larger or equals to 1949 and alias is "CA"= | < | <= | > | >= | BEGINS_WITH | BETWEEN
npm ciInstall dynamodb local:
npm run setupTestEnv
Spin up a local dynamodb and seed DB tables:
npm run startDynamo
In a different tab, you can run tests using
npm run test