All Projects → supersoniko → dynamodb-paginator

supersoniko / dynamodb-paginator

Licence: MIT license
Paginate DynamoDB results easily

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to dynamodb-paginator

dynamodb-simple
Type-safe Haskell framework for AWS DynamoDB
Stars: ✭ 16 (-11.11%)
Mutual labels:  dynamodb
home-energy-monitor
ESP32-based Home Energy Monitor
Stars: ✭ 152 (+744.44%)
Mutual labels:  dynamodb
telegram-stepfunctions-bot
Serverless Telegram bot made on 4 AWS Lambda chained by AWS Step Functions. All of this written on Serverless Framework using plugins.
Stars: ✭ 26 (+44.44%)
Mutual labels:  dynamodb
react-example-paginated-list-infinite-scroll
Follow a React tutorial series with three parts to build a powerful component in React.
Stars: ✭ 43 (+138.89%)
Mutual labels:  pagination
react-native-paginated-listview
A simple paginated react-native ListView with a few customization options
Stars: ✭ 14 (-22.22%)
Mutual labels:  pagination
ag-grid
The best JavaScript Data Table for building Enterprise Applications. Supports React / Angular / Vue / Plain JavaScript.
Stars: ✭ 8,743 (+48472.22%)
Mutual labels:  pagination
reactionmenu
A library to create a discord paginator. Supports pagination with Discords Buttons feature and reactions.
Stars: ✭ 68 (+277.78%)
Mutual labels:  pagination
cakephp-api-pagination
📑 CakePHP 4 plugin that injects pagination information into API responses.
Stars: ✭ 39 (+116.67%)
Mutual labels:  pagination
fast-relay-pagination
Improve relay pagination performance with find and limit
Stars: ✭ 18 (+0%)
Mutual labels:  pagination
api-lambda-save-dynamodb
Deploy instantly on Serverless Application Repository
Stars: ✭ 55 (+205.56%)
Mutual labels:  dynamodb
aws-nestjs-starter
Serverless, AWS, NestJS, GraphQL and DynamoDB starter
Stars: ✭ 200 (+1011.11%)
Mutual labels:  dynamodb
hanami-pagination
No description or website provided.
Stars: ✭ 14 (-22.22%)
Mutual labels:  pagination
laravel-auto
Laravel Auto - a helper package to make automated lists with filters, sorting and paging like no other
Stars: ✭ 41 (+127.78%)
Mutual labels:  pagination
aws-tutorial-code
AWS tutorial code.
Stars: ✭ 114 (+533.33%)
Mutual labels:  dynamodb
serverless-todo-demo
Serverless todo web app demo
Stars: ✭ 64 (+255.56%)
Mutual labels:  dynamodb
vue-tiny-pagination
A Vue component for create a tiny pagination with Flexbox
Stars: ✭ 20 (+11.11%)
Mutual labels:  pagination
terraform-aws-lambda
A Terraform module to create AWS Lambda ressources.
Stars: ✭ 40 (+122.22%)
Mutual labels:  dynamodb
serverless-dynamodb-ttl
⚡️ Serverless Plugin to set DynamoDB TTL
Stars: ✭ 16 (-11.11%)
Mutual labels:  dynamodb
bs-table
BsTable is an AngularJS directive that adds tfoot tag with pagination and page size selection to your table and watches changes on your collection in ng-repeat attribute.
Stars: ✭ 42 (+133.33%)
Mutual labels:  pagination
rails cursor pagination
Add cursor pagination to your ActiveRecord backed application
Stars: ✭ 21 (+16.67%)
Mutual labels:  pagination

npm NPM David CircleCI codecov

NOTE: This pagination library only works on indexes with a range key.

Usage

Compatible with AWS SDK v2 and v3

import { DynamoDB } from '@aws-sdk/client-dynamodb';
import { DynamoDBDocument } from '@aws-sdk/lib-dynamodb';
import { getPaginatedResult, decodeCursor } from 'dynamodb-paginator';

interface User {
    id: string
    name: string
}

const documentClient = DynamoDBDocument.from(new DynamoDB({}));

const limit = 25;
const standardQueryParams = {
    TableName: 'Users',
    Limit: limit,
    KeyConditionExpression: 'id = :id',
    ExpressionAttributeValues: {
        ':id': '1'
    }
};
// Could be a cursor from a previous paginated result
const cursor = undefined;
const paginationParams = decodeCursor(cursor) || standardQueryParams;

const result = await documentClient.query(paginationParams);

// By default the cursors are encoded in base64, but you can supply your own encoding function
const paginatedResult = getPaginatedResult<User>(paginationParams, limit, result);
// Output:
// {
//     data: T[],
//     meta: {
//         limit: number,
//         hasMoreData: boolean,
//         cursor: string,
//         backCursor: string,
//         count: number
//     }
// }

Security disclaimer

It's important to validate that the cursor has been generated by your service before passing it to the DynamoDB. If you don't, this opens a NoSQL vulnerability. A solution for this is signing/encrypting the cursor with a key.

Without encrypting the cursor, the partition and range key are also visible to the client consuming the cursor.

If your service offers authentication, it's also wise to validate that the cursor being parsed, was originally generated for that user/session. This is to prevent replay attacks.

Cursor encryption example

A simplified example of encrypting and decrypting the generated pagination cursor.

It's recommended to encapsulate the secured pagination code in a service, for ease of use.

import { randomBytes, createCipheriv, createDecipheriv } from 'crypto';
import { getPaginatedResult, decodeCursor } from 'dynamodb-paginator';

const ENC_KEY = randomBytes(32); // set random encryption key
const IV = randomBytes(16); // set random initialisation vector
const ALGORITHM = 'aes-256-cbc';

const encrypt = ((val) => {
    const cipher = createCipheriv(ALGORITHM, ENC_KEY, IV);
    let encrypted = cipher.update(JSON.stringify(val), 'utf8', 'base64');
    encrypted += cipher.final('base64');

    return encrypted;
});

const decrypt = ((encrypted) => {
    const decipher = createDecipheriv(ALGORITHM, ENC_KEY, IV);
    const decrypted = decipher.update(encrypted, 'base64', 'utf8');

    return JSON.parse((decrypted + decipher.final('utf8')));
});

const limit = 25;
const params = { TableName: 'Users', Limit: limit };
// Example DynamoDB Output
const result = {
    Items:
        [
            { id: 1, email: '[email protected]' },
            { id: 2, email: '[email protected]' },
        ],
    Count: 2,
    LastEvaluatedKey: { id: 2 },
};

// Pass a custom encoding function
const paginatedResult = getPaginatedResult(params, limit, result, encrypt);

// Pass a custom decoding function
const decodedCursor = decodeCursor(paginatedResult.meta.cursor, decrypt);

console.log(decodedCursor);
// Output:
// {
//     TableName: 'Users',
//     Limit: 25,
//     ExclusiveStartKey: {id:2},
//     previousKeys: [{id:2}],
//     back: false
// }

API Reference

Functions

getPaginatedResult(params, limit, result, cursorEncodingFunction)PaginatedResult<T>
decodeCursor(encodedCursor, cursorDecodingFunction)DynamoDBParams | undefined

Typedefs

DynamoDBParams : Object
DynamoDBResult : Object
MetaData : Object
PaginatedResult : Object

getPaginatedResult(params, limit, result) ⇒ PaginatedResult<T>

Kind: function

Param Type
params DynamoDBParams
limit number
result DynamoDBResult
cursorEncodingFunction? (cursor: DynamoDBParams) => string

decodeCursor(cursor) ⇒ Cursor | undefined

Kind: function

Param Type
encodedCursor string
cursorDecodingFunction? (encodedCursor: string) => DynamoDBParams

DynamoDBParams : Object

Kind: object Properties

Name Type Description
TableName string The name of the table containing the requested items
[IndexName] string The name of a secondary index to scan
[AttributesToGet] any This is a legacy parameter. Use ProjectionExpression instead.
[Limit] number The maximum number of items to evaluate
[Select] any The attributes to be returned in the result
[ScanFilter] any This is a legacy parameter
[ConditionalOperator] any This is a legacy parameter
[ExclusiveStartKey] any The primary key of the first item that this operation will evaluate
[ReturnConsumedCapacity] any Adds the consumed capacity to the result
[TotalSegments] any For a parallel Scan request
[Segment] any For a parallel Scan request
[ProjectionExpression] string A string that identifies one or more attributes to retrieve from the specified table or index
[FilterExpression] string A string that contains conditions that DynamoDB applies after the Scan operation
[ExpressionAttributeNames] any One or more substitution tokens for attribute names in an expression
[ExpressionAttributeValues] any One or more values that can be substituted in an expression
[ConsistentRead] boolean A Boolean value that determines the read consistency model during the scan

DynamoDBResult : Object

Kind: object Properties

Name Type Description
[Items] any An array of item attributes that match the scan criteria
[Count] number The number of items in the response
[ScannedCount] number The number of items evaluated
[LastEvaluatedKey] any The primary key of the item where the operation stopped
[ConsumedCapacity] any The capacity units consumed by the Scan operation

MetaData : Object

Kind: object Properties

Name Type Description
limit number The limit of the amount of returned items
hasMoreData boolean True if not all items in the DynamoDB table were returned that match the query
cursor string Used for pagination if there are more items left
backCursor? string Used for paginating back to previous results
count number The amount of items returned

PaginatedResult : Object

Kind: object Properties

Name Type Description
data T The queried data
meta MetaData Metadata regarding the result
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].