Skip to content

Instantly share code, notes, and snippets.

@kibagateaux
Created October 10, 2017 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kibagateaux/ae91430632c69289fe0743b114d9883c to your computer and use it in GitHub Desktop.
Save kibagateaux/ae91430632c69289fe0743b114d9883c to your computer and use it in GitHub Desktop.
Accessing Dynamo DB with React Native AWS Javascript SDK
import AWS from 'aws-sdk';
import {AWS_ACCESS_KEY, AWS_SECRET_KEY} from 'react-native-dotenv';
import {COGNITO_USER} from '@constants/asyncStorage';
import {AsyncStorage} from 'react-native';
import {Auth} from '@lib/Auth';
const creds = () => AsyncStorage.getItem(COGNITO_USER)
.then((data) => JSON.parse(data))
.then((data) => data);
AWS.config.update({
accessKeyId: AWS_ACCESS_KEY,
secretAccessKey: AWS_SECRET_KEY,
region:'us-east-1'
});
const StatsDB = new AWS.DynamoDB.DocumentClient({
apiVersion: '2012-08-10'
});
const statsTable = "djinii-mobilehub-1897344653-stats";
const params = (username) => ({
RequestItems: {
[statsTable]: [
{
PutRequest: {
Item: {
userId: username, // main key
time: 135235231241, // sub key
strength: 141,
intelligence: 124
}
}
}
]
}
});
export const fakeDynamoRequest = (username) => {
StatsDB.batchWrite(params(username), function(err, data) {
if (err) console.log('Error writing to DB', err, err.stack); // throws "The provided key element does not match the schema"
else console.log('Wrote To DB!!!!', data); // successful response
/*
data = {
}
*/
});
};
@kibagateaux
Copy link
Author

kibagateaux commented Oct 10, 2017

tableSchema = {
  userId: string,
  time: number,
  intelligence: number,
  strength: number,
  stamina: number,
  agility: number
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment