Created
October 10, 2017 14:44
-
-
Save kibagateaux/ae91430632c69289fe0743b114d9883c to your computer and use it in GitHub Desktop.
Accessing Dynamo DB with React Native AWS Javascript SDK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = { | |
} | |
*/ | |
}); | |
}; |
Author
kibagateaux
commented
Oct 10, 2017
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment