Amplify-js: Scan Functionality for aws-amplify-react-native

Created on 2 Jan 2018  路  1Comment  路  Source: aws-amplify/amplify-js

A scan function needs to be added into aws-amplify-react-native. Currently trying to get all values from a dynamoDB table, and there is no simple function that allows me to call the values for an entire table. I am currently trying to just call a DynamoDB table to be used for a Picker list so that updating the picker list is easier and not hard-coded into react-native. (Less coding in the future)

The closest source I have found to help with this is https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.Js.04.html

A simple function such as: API.scan("API-name", "/Path") would be perfect.

feature-request

Most helpful comment

@nicholasjuncos Thank you for your feedback. For now how you can do this is to create your own lambda function which will be called from API Gateway using API.get():

The lambda function:

'use strict';
const doc = require('dynamodb-doc');
const dynamo = new doc.DynamoDB();

exports.handler = (event, context, callback) => {
    const payload = {
        TableName: 'todos',
        Limit: 50, // optional (limit the number of items to evaluate)
        Select: 'ALL_ATTRIBUTES',
    };

    dynamo.scan(payload, (err, data) => {
        const result = { data: data.Items.map(item =>{
            return item;
        }) };
        callback(err, result.data);
    });
};

If you are using awsmobile cli then it could be much easier:

  • Find app.js under awsmobilejs/backend/cloud-api/your_table

  • Add this code into it:

app.get(PATH_DEFINED_FOR_SCAN, function(req, res) {
    const payload = {
        TableName: tableName,
        Limit: 50, // optional (limit the number of items to evaluate)
        Select: 'ALL_ATTRIBUTES',
    };

    dynamodb.scan(payload, (err, data) => {
        if (err) {
          res.json({error: 'Could not load items: ' + err.message});
        }

        res.json({
            data: data.Items.map(item => {
              return item;
            })
        });
    });
});
  • Run awsmobile push to upload that lambda function.

  • Use API.get(API_NAME, PATH_DEFINED_FOR_SCAN) to scan the table.

>All comments

@nicholasjuncos Thank you for your feedback. For now how you can do this is to create your own lambda function which will be called from API Gateway using API.get():

The lambda function:

'use strict';
const doc = require('dynamodb-doc');
const dynamo = new doc.DynamoDB();

exports.handler = (event, context, callback) => {
    const payload = {
        TableName: 'todos',
        Limit: 50, // optional (limit the number of items to evaluate)
        Select: 'ALL_ATTRIBUTES',
    };

    dynamo.scan(payload, (err, data) => {
        const result = { data: data.Items.map(item =>{
            return item;
        }) };
        callback(err, result.data);
    });
};

If you are using awsmobile cli then it could be much easier:

  • Find app.js under awsmobilejs/backend/cloud-api/your_table

  • Add this code into it:

app.get(PATH_DEFINED_FOR_SCAN, function(req, res) {
    const payload = {
        TableName: tableName,
        Limit: 50, // optional (limit the number of items to evaluate)
        Select: 'ALL_ATTRIBUTES',
    };

    dynamodb.scan(payload, (err, data) => {
        if (err) {
          res.json({error: 'Could not load items: ' + err.message});
        }

        res.json({
            data: data.Items.map(item => {
              return item;
            })
        });
    });
});
  • Run awsmobile push to upload that lambda function.

  • Use API.get(API_NAME, PATH_DEFINED_FOR_SCAN) to scan the table.

Was this page helpful?
0 / 5 - 0 ratings