Tuesday, 24 September 2019

Data Storage with DynamoDB by Raj Gupta

const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB({region: 'us-east-1', apiVersion: '2012-08-10'});

exports.handler = (event, context, callback) => {
    const params = {
        Item: {
            "UserId": {
                S: "gakhhhih"
            },
            "Age": {
                N: "28"
            },
            "Income": {
                N: "2500"
            }
        },
        TableName: "compare-yourself"
    };
    dynamodb.putItem(params, function(err, data) {
        if (err) {
            console.log(err);
            callback(err);
        } else {
            console.log(data);
            callback(null, data);
        }
    });
};

---------------------------------------------------------------------------------------------------------

#set($inputRoot = $input.path('$'))
{
  "age" : "$inputRoot.age",
  "height": "$inputRoot.height",
  "income": "$inputRoot.income"
}


-----------------------------------------------------------------------------------------------------------

const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB({region: 'us-east-1', apiVersion: '2012-08-10'});

exports.handler = (event, context, callback) => {
    const params = {
        Item: {
            "UserId": {
                S: "user_" + Math.random()
            },
            "Age": {
                N: event.age
            },
            "Height": {
                N: event.height
            },
            "Income": {
                N: event.income
            }
        },
        TableName: "compare-yourself"
    };
    dynamodb.putItem(params, function(err, data) {
        if (err) {
            console.log(err);
            callback(err);
        } else {
            console.log(data);
            callback(null, data);
        }
    });
};

------------------------------------------------------------------------------------------------------------------------

var xhr = new XMLHttpRequest();

xhr.open('POST', 'https://ktl8ycz4w8.execute-api.us-east-1.amazonaws.com/dev/compare-yourself');

xhr.onreadystatechange = function(event) {

  console.log(event.target.response);

}
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({age: 26, height: 71, income: 2100}));

----------------------------------------------------------------------------------------------------

{
  "type": "all",
  "key2": "value2",
  "key3": "value3"
}

--------------------------------------------------------------------

const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB({region: 'us-east-1', apiVersion: '2012-08-10'});

exports.handler = (event, context, callback) => {
    const type = event.type;
    if (type == 'all') {
        const params = {
            TableName: 'compare-yourself'
        };
        dynamodb.scan(params, function(err, data) {
            if (err) {
                console.log(err);
                callback(err);
            } else {
                console.log(data);
                callback(null, data);
            }
        });
    } else if (type == 'single') {
        callback(null, 'Just my data');
    } else {
        callback(null, 'Hello from Lambda');
    }   
};


-------------------------------------------------------------------------------------------

const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB({region: 'us-east-1', apiVersion: '2012-08-10'});

exports.handler = (event, context, callback) => {
    const type = event.type;
    if (type == 'all') {
        const params = {
            TableName: 'compare-yourself'
        };
        dynamodb.scan(params, function(err, data) {
            if (err) {
                console.log(err);
                callback(err);
            } else {
                console.log(data);
                const items = data.Items.map(
                    (dataField) => {
                        return {age: +dataField.Age.N, height: +dataField.Height.N, income: +dataField.Income.N};
                    }
                );
                callback(null, items);
            }
        });
    } else if (type == 'single') {
        callback(null, 'Just my data');
    } else {
        callback(null, 'Hello from Lambda');
    }   
};


-------------------------------------------------------------------------------------------------

const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB({region: 'us-east-1', apiVersion: '2012-08-10'});

exports.handler = (event, context, callback) => {
    const type = event.type;
    if (type == 'all') {
        const params = {
            TableName: 'compare-yourself'
        };
        dynamodb.scan(params, function(err, data) {
            if (err) {
                console.log(err);
                callback(err);
            } else {
                console.log(data);
                const items = data.Items.map(
                    (dataField) => {
                        return {age: +dataField.Age.N, height: +dataField.Height.N, income: +dataField.Income.N};
                    }
                );
                callback(null, items);
            }
        });
    } else if (type == 'single') {
        const params = {
            Key: {
                "UserId": {
                    S: "gakhhhih"
                }
            },
            TableName: "compare-yourself"
        };
        dynamodb.getItem(params, function(err, data) {
            if (err) {
                console.log(err);
                callback(err);
            } else {
                console.log(data);
                callback(null, data);
            }
         
        });
     
    } else {
        callback('Something went to wrong');
    } 
};

------------------------------------------------------------------------------------------------------------

const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB({region: 'us-east-1', apiVersion: '2012-08-10'});

exports.handler = (event, context, callback) => {
    const type = event.type;
    if (type == 'all') {
        const params = {
            TableName: 'compare-yourself'
        };
        dynamodb.scan(params, function(err, data) {
            if (err) {
                console.log(err);
                callback(err);
            } else {
                console.log(data);
                const items = data.Items.map(
                    (dataField) => {
                        return {age: +dataField.Age.N, height: +dataField.Height.N, income: +dataField.Income.N};
                    }
                );
                callback(null, items);
            }
        });
    } else if (type == 'single') {
        const params = {
            Key: {
                "UserId": {
                    S: "gakhhhih"
                }
            },
            TableName: "compare-yourself"
        };
        dynamodb.getItem(params, function(err, data) {
            if (err) {
                console.log(err);
                callback(err);
            } else {
                console.log(data);
                callback(null, {age: +data.Item.Age.N, income: +data.Item.Income.N});
            }
           
        });
        
    } else {
        callback('Something went to wrong');
    }   
};

-------------------------------------------------------------------------------------------------------------------

var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://ktl8ycz4w8.execute-api.us-east-1.amazonaws.com/dev/single');
xhr.onreadystatechange = function(event) {
  console.log(JSON.parse(event.target.response));
}
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send();

---------------------------------------------------------------------------------------------------------

const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB({region: 'us-east-1', apiVersion: '2012-08-10'});

exports.handler = (event, context, callback) => {
    const params = {
        Key: {
            "UserId": {
                S: "gakhhhih"
            }
        },
        TableName: "compare-yourself"
    };
    dynamodb.deleteItem(params, function (err, data) {
        if (err) {
            console.log(err);
            callback(err);
        } else {
            console.log(data);
            callback(null, data);
        }
    });
 
};

-------------------------------------------------------------------------------------------------------

var xhr = new XMLHttpRequest();
xhr.open('DELETE', 'https://ktl8ycz4w8.execute-api.us-east-1.amazonaws.com/dev/compare-yourself');
xhr.onreadystatechange = function(event) {
  console.log(JSON.parse(event.target.response));
}
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send();


------------------------------------------------------------------------------------------------------------

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "CompareData",
  "type": "array",
  "items": {
        "type": "object",
        "properties": {
        "age": {"type": "integer"},
        "height": {"type": "integer"},
        "income": {"type": "integer"}
  },
  "required": ["age", "height", "income"]
  }
}

--------------------------------------------------------------------------------------------



1 comment:

  1. Hello everyone , here’s your opportunity for you to achieve your dreams of being a multi million dollar rich through trading , I once loss all I got through trading but was fortunate to come across a woman with great virtue and selfless heart (Mary ) i was introduce to her masterclass strategy while searching online which has revived me of all my losses and made me gain more and more . With her unique strategy you are entitled to daily signals and instant withdraw ,be rest assured of getting a refund of all your loss investment with any platform that has denied you in one way or the other in getting your money . Mrs Mary masterclass strategy is simply the best for beginners and those that are finding it difficult to succeed through trading she’ll help you with just a simple step . Email her ( maryshea03 @ Gmail .com) WhatsApp +1 562 384 7738 . Remember this is absolutely free!!!

    ReplyDelete