Skip to content

Instantly share code, notes, and snippets.

@meerzulee
Created April 30, 2024 09:17
Show Gist options
  • Save meerzulee/b4d5527a07f4c4863f340a54ab0ea89d to your computer and use it in GitHub Desktop.
Save meerzulee/b4d5527a07f4c4863f340a54ab0ea89d to your computer and use it in GitHub Desktop.
DynamoDB pagination example code - SST
const LIMIT = 5
const PK = "STORE#123"
const getAuctionItems = (items: any[])=> {
return items.map((auction)=>(getAuctionItem(auction)))
}
const getPageKey = (item: any) => ({PK:item.PK, SK:item.SK})
const encodeBase64 = (jsonItem: object)=> ( Buffer.from(JSON.stringify(jsonItem)).toString("base64"))
const decodeBase64 = (item: string)=> ( JSON.parse(Buffer.from(item, "base64").toString()))
export const main = handler(async (event) => {
const LIMIT_PLUS_ONE = LIMIT + 1
console.log(event)
const params: DocumentClient.QueryInput = {
TableName: Table.Auction.tableName,
// Limit:3,
Limit: LIMIT_PLUS_ONE,
KeyConditionExpression: "PK = :PK and begins_with(SK, :SKprefix)",
// FilterExpression: `#status = :filterStatus`,
ExpressionAttributeValues: {
":PK": PK,
":SKprefix": "AUC#",
// ":filterStatus": 'active'
},
// ExpressionAttributeNames: {
// "#status":"status"
// }
};
const pageAfter = event.queryStringParameters?.['pageAfter']
const pageBefore = event.queryStringParameters?.['pageBefore']
const filterStatus = event.queryStringParameters?.['filterStatus']
if(pageAfter && pageBefore){
throw new Error("Provide only one pageCursor")
}
const isForward = !pageBefore
if(pageAfter){
params.ExclusiveStartKey = decodeBase64(pageAfter)
}
if(pageBefore){
params.ScanIndexForward = false;
params.ExclusiveStartKey = decodeBase64(pageBefore);
}
let FilterExpression = ''
if(filterStatus){
}
const result = await dynamoDb.query(params);
if(!result.Items){
return []
}
const pagination:any = {
}
console.log("result",result)
const isLastOrFirstPage = result.Items.length < LIMIT_PLUS_ONE
const slicedItems = isLastOrFirstPage ? result.Items : result.Items.slice(0,-1)
let auctionItems;
if(isForward){
auctionItems = getAuctionItems(slicedItems)
pagination.nextPageKey = isLastOrFirstPage ? undefined : encodeBase64(getPageKey(slicedItems.at(-1)))
pagination.prevPageKey = !pageAfter && !pageBefore ? undefined : encodeBase64(getPageKey(slicedItems.at(0)))
}else {
const orderItems = slicedItems.reverse()
auctionItems = getAuctionItems(orderItems)
pagination.nextPageKey = encodeBase64(getPageKey(orderItems.at(-1)))
pagination.prevPageKey = isLastOrFirstPage ? undefined : encodeBase64(getPageKey(orderItems.at(0)))
}
return JSON.stringify({
list:auctionItems,
pagination
});
// if(isForward){
// const isLastPage = result.Items.length < LIMIT_PLUS_ONE
// const slicedItems = isLastPage ? result.Items : result.Items.slice(0,-1)
// const auctionItems = getAuctionItems(slicedItems)
// pagination.nextPageKey = isLastPage ? undefined : encodeBase64(getPageKey(slicedItems.at(-1)))
// pagination.prevPageKey = !pageAfterKey && !pageBeforeKey ? undefined : encodeBase64(getPageKey(slicedItems.at(0)))
// return JSON.stringify({
// list:auctionItems,
// pagination
// });
// }else {
// const isFirstPage = result.Items.length < LIMIT_PLUS_ONE
// const slicedItems = isFirstPage ? result.Items : result.Items.slice(0,-1)
// const orderItems = slicedItems.reverse()
// const auctionItems = getAuctionItems(orderItems)
// pagination.nextPageKey = encodeBase64(getPageKey(orderItems.at(-1)))
// pagination.prevPageKey = isFirstPage ? undefined : encodeBase64(getPageKey(orderItems.at(0)))
// return JSON.stringify({
// list:auctionItems,
// pagination
// });
// }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment