Skip to content

Instantly share code, notes, and snippets.

@DennisKo
Created October 27, 2019 16:20
Show Gist options
  • Save DennisKo/d32e6a6aa16923757d8d0a0c431679fa to your computer and use it in GitHub Desktop.
Save DennisKo/d32e6a6aa16923757d8d0a0c431679fa to your computer and use it in GitHub Desktop.
DyanmoDB PUT
import { NextApiRequest, NextApiResponse } from "next";
import * as AWS from "aws-sdk";
import uuidv4 from "uuid/v4";
AWS.config.update({
region: "eu-central-1",
accessKeyId: process.env.AWS_ACCESS_KEY,
secretAccessKey: process.env.AWS_SECRET_KEY
});
const docClient = new AWS.DynamoDB.DocumentClient({
httpOptions: {
timeout: 5000
},
maxRetries: 2
});
export default async function featuredItems(
req: NextApiRequest,
res: NextApiResponse
) {
try {
const userItem = `${req.body.userId}#${uuidv4()}`;
const params = {
TableName: "pony",
Item: {
...req.body,
userItem,
createdAt: new Date().toISOString()
},
ConditionExpression: "attribute_not_exists(userItem)"
};
const data = await docClient.put(params).promise();
res.status(200).json(data);
} catch (error) {
console.error(error);
res.status(error.statusCode || 500).end(error.message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment