Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@kibagateaux 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