Gathering detailed insights and metrics for @baselinejs/dynamodb
Gathering detailed insights and metrics for @baselinejs/dynamodb
Gathering detailed insights and metrics for @baselinejs/dynamodb
Gathering detailed insights and metrics for @baselinejs/dynamodb
DynamoDB library for simple and optimized way to use AWS DynamoDB
npm install @baselinejs/dynamodb
Typescript
Module System
Node Version
NPM Version
51.4
Supply Chain
98.1
Quality
81.5
Maintenance
100
Vulnerability
99.6
License
TypeScript (99.51%)
JavaScript (0.49%)
Total
2,340
Last Day
12
Last Week
94
Last Month
741
Last Year
2,340
4 Stars
26 Commits
1 Watching
2 Branches
3 Contributors
Latest Version
0.2.4
Package Id
@baselinejs/dynamodb@0.2.4
Unpacked Size
147.30 kB
Size
27.24 kB
File Count
9
NPM Version
7.24.2
Node Version
20.11.1
Publised On
23 Apr 2024
Cumulative downloads
Total Downloads
Last day
1,100%
12
Compared to previous day
Last week
-2.1%
94
Compared to previous week
Last month
298.4%
741
Compared to previous month
Last year
0%
2,340
Compared to previous year
4
Baseline DynamoDB is an optimized utility library that simplifies standard DynamoDB operations. It's focused towards multi table designed applications, and aims to provide a set of functions that are tailored to the specific use cases of these applications.
1npm install @baselinejs/dynamodb
1yarn add @baselinejs/dynamodb
1pnpm install @baselinejs/dynamodb
First create a connection to your DynamoDB table.
Must specify a region.
Natively handles both local and deployed environments. See Environment Variables for more information.
1const dynamo = getDynamodbConnection({
2 region: 'us-east-1',
3});
Add a new item to your table, providing the previously created connection.
1const user = await putItem<User>({ 2 dynamoDb: dynamo, 3 table: 'user-table-staging', 4 item: { userId: '123', email: 'example@example.com', name: 'Alice' }, 5});
Get a single item from your table.
1const user = await getItem<User>({ 2 dynamoDb: dynamo, 3 table: 'user-table-staging', 4 key: { 5 userId: '123', 6 }, 7});
Update an item in your table.
Key properties will be automatically removed from fields to prevent attribute errors.
1const updatedUser = await updateItem<User>({ 2 dynamoDb: dynamo, 3 table: 'user-table-staging', 4 key: { 5 userId: '123', 6 }, 7 fields: { 8 name: 'Bob', 9 }, 10});
Delete an item from your table.
1const deletedUser = await deleteItem({
2 dynamoDb: dynamo,
3 table: 'user-table-staging',
4 key: {
5 userId: '123',
6 },
7});
Fetch all items from a table.
1const allUsers = await getAllItems<User>({ 2 dynamoDb: dynamo, 3 table: 'user-table-staging', 4});
Query items from an index.
1const users = await queryItems<User>({ 2 dynamoDb: dynamo, 3 table: 'user-table-staging', 4 keyName: 'email', 5 keyValue: 'example@example.com', 6 indexName: 'email-index', 7});
Batch get items from a table Automatically handles splitting the keys into chunks of 100.
Returned item order is not necessarily the same as the input order.
1const users = await batchGetItems<User>({ 2 dynamoDb: dynamo, 3 table: 'user-table-staging', 4 keys: [{ userId: '123' }, { userId: '456' }], 5});
Batch create items into a table. Automatically handles splitting the items into chunks of 25.
1const users = await batchPutItems<User>({ 2 dynamoDb: dynamo, 3 table: 'user-table-staging', 4 items: [ 5 { userId: '123', name: 'Alice' }, 6 { userId: '456', name: 'Bob' }, 7 ], 8});
Batch delete items from a table. Automatically handles splitting the keys into chunks of 25.
1const isDeleted = await batchDeleteItems({ 2 dynamoDb: dynamo, 3 table: 'user-table-staging', 4 keys: [{ userId: '123' }, { userId: '456' }], 5});
Query items from a table with a range key.
1const userPurchases = await queryItemsRange<Purchase>({ 2 dynamoDb: dynamo, 3 table: 'purchase-table-staging', 4 keyName: 'userId', 5 keyValue: '123', 6 rangeKeyName: 'createdAt', 7 rangeKeyValue: '2022', 8 // Fuzzy search will use a begins_with condition 9 fuzzy: true, 10 indexName: 'userId-createdAt-index', 11});
Equivalent query using queryItems
1const userPurchases = await queryItems<Purchase>({ 2 dynamoDb: dynamo, 3 table: 'purchase-table-staging', 4 keyName: 'userId', 5 keyValue: '123', 6 indexName: 'userId-createdAt-index', 7 rangeCondition: { 8 operator: 'BeginsWith', 9 field: 'createdAt', 10 value: '2022', 11 }, 12});
Query items from a table with a range key between two values.
1const userPurchases = await queryItemsRangeBetween<Purchase>({ 2 dynamoDb: dynamo, 3 table: 'purchase-table-staging', 4 keyName: 'userId', 5 keyValue: '123', 6 rangeKeyName: 'createdAt', 7 rangeKeyValueMin: '2022-01-01T00:00:00.000Z', 8 rangeKeyValueMax: '2023-01-01T00:00:00.000Z', 9 indexName: 'userId-createdAt-index', 10});
Equivalent query using queryItems
1const userPurchases = await queryItems<Purchase>({ 2 dynamoDb: dynamo, 3 table: 'purchase-table-staging', 4 keyName: 'userId', 5 keyValue: '123', 6 indexName: 'userId-createdAt-index', 7 rangeCondition: { 8 operator: 'Between', 9 field: 'createdAt', 10 value: '2022-01-01T00:00:00.000Z', 11 betweenSecondValue: '2023-01-01T00:00:00.000Z', 12 }, 13});
A conditions
array can be provided to the putItem
, updateItem
, and deleteItem
functions to specify conditions that must be met for the operation to succeed.
Conditions are combined with AND.
1try { 2 const user = await putItem<User>({ 3 dynamoDb: dynamo, 4 table: 'user-table-staging', 5 item: { userId: '123', email: 'example2@example.com', name: 'Alice' }, 6 conditions: [ 7 { 8 // Only create if this userId does not already exist 9 operator: 'AttributeNotExists', 10 field: 'userId', 11 }, 12 ], 13 }); 14} catch (error) { 15 if (error.name === 'ConditionalCheckFailedException') { 16 // error.Item contains the item that already exists with the specified userId 17 } 18}
You can limit the number of items returned by specifying the limit
parameter.
This applies to the query
functions as well as the getAllItems
function.
The function will handle pagination internally up until the limit is reached.
1const userPurchases = await queryItems<Purchase>({ 2 dynamoDb: dynamo, 3 table: 'purchase-table-staging', 4 keyName: 'userId', 5 keyValue: '123', 6 limit: 10, 7});
Projection expressions are used to limit the attributes returned from a query to only the specified fields.
To maintain type safety, you can specify the fields you want to return using the second generic type parameter.
1const userPurchases = await queryItems<Purchase, 'userId' | 'createdAt'>({ 2 dynamoDb: dynamo, 3 table: 'purchase-table-staging', 4 keyName: 'userId', 5 keyValue: '123', 6 projectionExpression: ['userId', 'createdAt'], 7});
Unmarshalling is used to convert a DynamoDB record into a JavaScript object.
This is useful when using dynamodb streams, as the new and old images are returned as DynamoDB records that need to be unmarshalled.
1const user = unmarshallItem<User>(record.dynamodb?.NewImage);
Marshalling is used to convert a JavaScript object into a DynamoDB record.
1const user = { 2 userId: '123', 3 email: 'example@example.com', 4 name: 'Alice', 5}; 6const marshalledUser = marshallItem(user);
Errors are not caught internally but are instead propagated up to the calling code.
To handle these errors effectively, wrap function calls in try-catch blocks in your application. This allows for custom error handling strategies, such as logging errors or retrying failed operations.
IS_OFFLINE
Will be "true"
in your handlers when using serverless-offline.
When "true"
will use values appropriate to work with DynamoDB Local.
region: "localhost",
endpoint: "http://localhost:8000",
FORCE_ONLINE
Set to "true"
to override the IS_OFFLINE
environment variable and use a deployed DynamoDB instance.
No vulnerabilities found.
No security vulnerabilities found.