0

I have a lambda function written in node, and I am trying to get data for a particular workout session.

The url looks something like this

workout/{workoutId}/sessions/{sessionId}

I know I can do something like the following if I just wanted to the workoutId

module.exports.getWorkoutSession = (event, context, callback) => {
  const { workoutId } = event.pathParameters;
  const params = {
    TableName: workoutTable,
    Key: {
      id: workoutId,
    },
  };

  return db.get(params).promise().then(res => {
      if(res.item) {callback(null, response(200, res.Item));} else {
          callback(null, response(404, {error: "workoutId not found"}))
      }
  })
};

but how can I modify this function so I can get the sessionData for a particular workout?

3
  • 2
    Are you asking how to get sessionId from the path parameters, or how to fetch session data from DynamoDB? The answer to the first question is event.pathParameters.sessionId. The answer to the second question depends on how you're storing the data in DDB. Or do I misunderstand your question? Commented Nov 15, 2020 at 17:03
  • I'm asking how to get the session data from the DB Commented Nov 15, 2020 at 17:12
  • Well, that depends on how you're storing sessions in DDB. Are you asking how to fetch session data you are already storing, or are you also asking how to store session data? I'm not clear if your question is about data modeling, how to write a query, or both Commented Nov 15, 2020 at 20:31

1 Answer 1

2

The answer depends on how the data is stored in Dynamo DB. If you have these two use cases -

  1. Getting data of all sessions of a workoutId (using workoutId from the event params) and
  2. Getting data of a specific session of a workout Id (using workoutId and sessionId from the event params)

Then these two can be solved by having the following table structure

Hash Key: workoutId
Range Key(Partition Key): sessionId

and the following code

/** API to get all sessions of a workout. Called by url workout/{workoutId} */
module.exports.getWorkoutSessions = async (event, context, callback) => {
    const { workoutId } = event.pathParameters;
    const params = {
        TableName: workoutTable,
        KeyConditionExpression: '#workoutId = :workoutId',
        ExpressionAttributeNames: {
            '#workoutId': 'workoutId'
        },
        ExpressionAttributeValues: {
            ':workoutId': workoutId
        }
    };

    const db = new AWS.DynamoDB.DocumentClient();
    try {
        const res = await db.query(params).promise();
        return res.Items;
    } catch (error) {
        console.error('Error while getting workout sessions', error);
        throw error;
    }
};

/** API to get a specific session of a workout. Called by url workout/{workoutId}/sessions/{sessionId} */
module.exports.getWorkoutSession = async (event, context, callback) => {
    const { workoutId, sessionId } = event.pathParameters;
    const params = {
        TableName: workoutTable,
        Key: {
            workoutId: workoutId,
            sessionId: sessionId
        }
    };

    const db = new AWS.DynamoDB.DocumentClient();
    try {
        return await db.getItem(params).promise();
    } catch (error) {
        console.error('Error while getting workout session', error);
        throw error;
    }
};
Sign up to request clarification or add additional context in comments.

2 Comments

Silly question but where do I define the hash key and the Range key? Is it in the serverlesa.yaml? I am pretty new to dyanamo db
Multiple ways are possible. Aws console, SAM templates, Serverless templates. if you are using serverless, you can follow this to define the table serverless.com/dynamodb#dynamodb-with-serverless-framework

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.