Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add api_id to the client output file amplify_outputs.json. #1913

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Commits on Aug 24, 2024

  1. Add api_id to the client output file amplify_outputs.json.

    This allows for the backend to import the json and use raw DBB queries on tables using the api_id
    
    for example:
    
    import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
    import { DynamoDBDocumentClient, TransactWriteCommand } from "@aws-sdk/lib-dynamodb";
    
    import config from '../../../../amplify_outputs.json';
    
    const client = new DynamoDBClient({});
    const docClient = DynamoDBDocumentClient.from(client);
    
    const createTableName = (name: string) => {
        return `${name}-${config.data.api_id}-NONE`;
    }
    
    export const handler = async (event: any) => {
        const { userId, amount } = event.arguments;
    
        const transactItems = [
            // Update UserProfile
            {
                Update: {
                    TableName: createTableName('UserProfile'),
                    Key: { id: userId },
                    UpdateExpression: 'ADD currentPoints :pointsToAdd',
                    ExpressionAttributeValues: { ':pointsToAdd': amount }
                }
            }
        ];
    
        try {
            const results = await docClient.send(new TransactWriteCommand({ TransactItems: transactItems }));
    
            console.log(results)
    
            return results['$metadata']
        } catch (error) {
            console.error('Error:', error);
            throw new Error('Failed to process the transaction');
        }
    };
    michael-trinity committed Aug 24, 2024
    Configuration menu
    Copy the full SHA
    d7c5f70 View commit details
    Browse the repository at this point in the history