Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```ts
- //* Create DynamoDB Table
- const assetTable = new aws.dynamodb.Table("assetTable", {
- attributes: [
- {
- name: "id",
- type: "S"
- },
- {
- name: "ts",
- type: "N"
- }
- ],
- hashKey: "id",
- rangeKey: "ts",
- ttl: {
- attributeName: "expiration",
- enabled: true
- },
- billingMode: "PAY_PER_REQUEST"
- });
- //* Create API to read DynamoDB
- const endpoint = new awsx.apigateway.API("assetQuery", {
- routes: [
- {
- path: "/",
- method: "GET",
- eventHandler: (request, ctx, cb) => {
- const AWS = require("aws-sdk");
- const ddb = new AWS.DynamoDB.DocumentClient({
- apiVersion: "2012-10-08"
- });
- const tableName = assetTable.name.value;
- const params = {
- TableName: tableName
- };
- ddb.scan(params, (err, data) => {
- const features = data.Items.map(item => {
- const point = turf.point([item.longitude, item.latitude], {
- id: item.id,
- speed: item.speed
- });
- return point;
- });
- const featureCollection = turf.featureCollection(features);
- cb(undefined, {
- statusCode: 200,
- body: Buffer.from(
- JSON.stringify(featureCollection),
- "utf8"
- ).toString("base64"),
- isBase64Encoded: true,
- headers: { "content-type": "application/json" }
- });
- });
- }
- }
- ],
- stageName: "dev"
- });
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement