[server]: Make return_all_data accept a list of objectTypes
This commit is contained in:
@ -1,24 +1,37 @@
|
||||
import json
|
||||
import os
|
||||
import boto3
|
||||
from boto3.dynamodb.conditions import Key, Attr
|
||||
|
||||
dynamodb = boto3.resource('dynamodb')
|
||||
table = dynamodb.Table(os.environ['TABLE_NAME'])
|
||||
|
||||
|
||||
def lambda_handler(event, context):
|
||||
try:
|
||||
# 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']
|
||||
object_types = objectType.split(',')
|
||||
|
||||
# Query using objectType as the key (assumes GSI is created on objectType)
|
||||
response = table.query(
|
||||
IndexName='objectTypeIndex', # Name of GSI
|
||||
KeyConditionExpression=boto3.dynamodb.conditions.Key('objectType').eq(objectType)
|
||||
# Fetch items matching any of the object types using a scan with FilterExpression
|
||||
items = []
|
||||
response = table.scan(
|
||||
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:
|
||||
# Fallback to scanning the table (not recommended for large tables)
|
||||
# Fallback to scanning the entire table
|
||||
items = []
|
||||
response = table.scan()
|
||||
items.extend(response.get('Items', []))
|
||||
@ -31,7 +44,7 @@ def lambda_handler(event, context):
|
||||
'statusCode': 200,
|
||||
'body': json.dumps(items)
|
||||
}
|
||||
|
||||
|
||||
except Exception as e:
|
||||
return {
|
||||
'statusCode': 500,
|
||||
|
Reference in New Issue
Block a user