[server]: Make return_all_data accept a list of objectTypes
This commit is contained in:
@ -1,24 +1,37 @@
|
|||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import boto3
|
import boto3
|
||||||
|
from boto3.dynamodb.conditions import Key, Attr
|
||||||
|
|
||||||
dynamodb = boto3.resource('dynamodb')
|
dynamodb = boto3.resource('dynamodb')
|
||||||
table = dynamodb.Table(os.environ['TABLE_NAME'])
|
table = dynamodb.Table(os.environ['TABLE_NAME'])
|
||||||
|
|
||||||
|
|
||||||
def lambda_handler(event, context):
|
def lambda_handler(event, context):
|
||||||
try:
|
try:
|
||||||
# Check if objectType is present in query string parameters
|
# Check if objectType is present in query string parameters
|
||||||
if 'queryStringParameters' in event and event['queryStringParameters'] and 'objectType' in event['queryStringParameters']:
|
if 'queryStringParameters' in event and event['queryStringParameters'] and 'objectType' in event[
|
||||||
|
'queryStringParameters']:
|
||||||
|
# Get objectType values and split by comma if multiple values are present
|
||||||
objectType = event['queryStringParameters']['objectType']
|
objectType = event['queryStringParameters']['objectType']
|
||||||
|
object_types = objectType.split(',')
|
||||||
|
|
||||||
# Query using objectType as the key (assumes GSI is created on objectType)
|
# Fetch items matching any of the object types using a scan with FilterExpression
|
||||||
response = table.query(
|
items = []
|
||||||
IndexName='objectTypeIndex', # Name of GSI
|
response = table.scan(
|
||||||
KeyConditionExpression=boto3.dynamodb.conditions.Key('objectType').eq(objectType)
|
FilterExpression=Attr('objectType').is_in(object_types)
|
||||||
)
|
)
|
||||||
items = response.get('Items', [])
|
items.extend(response.get('Items', []))
|
||||||
|
|
||||||
|
# Handle pagination
|
||||||
|
while 'LastEvaluatedKey' in response:
|
||||||
|
response = table.scan(
|
||||||
|
FilterExpression=Attr('objectType').is_in(object_types),
|
||||||
|
ExclusiveStartKey=response['LastEvaluatedKey']
|
||||||
|
)
|
||||||
|
items.extend(response.get('Items', []))
|
||||||
else:
|
else:
|
||||||
# Fallback to scanning the table (not recommended for large tables)
|
# Fallback to scanning the entire table
|
||||||
items = []
|
items = []
|
||||||
response = table.scan()
|
response = table.scan()
|
||||||
items.extend(response.get('Items', []))
|
items.extend(response.get('Items', []))
|
||||||
|
Reference in New Issue
Block a user