Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. ```ts
  2. //* Create DynamoDB Table
  3. const assetTable = new aws.dynamodb.Table("assetTable", {
  4. attributes: [
  5. {
  6. name: "id",
  7. type: "S"
  8. },
  9. {
  10. name: "ts",
  11. type: "N"
  12. }
  13. ],
  14. hashKey: "id",
  15. rangeKey: "ts",
  16. ttl: {
  17. attributeName: "expiration",
  18. enabled: true
  19. },
  20. billingMode: "PAY_PER_REQUEST"
  21. });
  22.  
  23. //* Create API to read DynamoDB
  24. const endpoint = new awsx.apigateway.API("assetQuery", {
  25. routes: [
  26. {
  27. path: "/",
  28. method: "GET",
  29. eventHandler: (request, ctx, cb) => {
  30. const AWS = require("aws-sdk");
  31. const ddb = new AWS.DynamoDB.DocumentClient({
  32. apiVersion: "2012-10-08"
  33. });
  34. const tableName = assetTable.name.value;
  35. const params = {
  36. TableName: tableName
  37. };
  38. ddb.scan(params, (err, data) => {
  39. const features = data.Items.map(item => {
  40. const point = turf.point([item.longitude, item.latitude], {
  41. id: item.id,
  42. speed: item.speed
  43. });
  44. return point;
  45. });
  46. const featureCollection = turf.featureCollection(features);
  47. cb(undefined, {
  48. statusCode: 200,
  49. body: Buffer.from(
  50. JSON.stringify(featureCollection),
  51. "utf8"
  52. ).toString("base64"),
  53. isBase64Encoded: true,
  54. headers: { "content-type": "application/json" }
  55. });
  56. });
  57. }
  58. }
  59. ],
  60. stageName: "dev"
  61. });
  62. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement