-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.ts
More file actions
108 lines (95 loc) · 2.85 KB
/
run.ts
File metadata and controls
108 lines (95 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import express from 'express';
import AWS from 'aws-sdk';
import { handler } from './index';
const app = express();
AWS.config.update({
region: 'localhost',
});
// Create a new instance of the DynamoDB client
const dynamoDB = new AWS.DynamoDB({ apiVersion: '2012-08-10', endpoint: 'http://localhost:8000'});
// scan operation in Dynamo is slow and cost expensive
// use query is cheaper, but can query rely on primary-key
// GSI (global secondary index) is another way to fetch
app.get('/getall', (req, res) => {
const params: AWS.DynamoDB.ScanInput = {
TableName: 'ShareConfig',
};
// Use the DynamoDB client to fetch data from the table
dynamoDB.scan(params, (err, data) => {
if (err) {
console.error(err);
res.status(500).send('Error fetching data from DynamoDB');
} else {
res.send(data);
}
});
});
app.post('/add', (req, res) => {
const createParams = {
TableName: 'ShareConfig',
Item: {
'PK': { S: 'some-partition-key-value' },
'SK': { S: 'some-sort-key-value' },
'attribute1': { S: 'some-attribute-value' },
'attribute2': { N: '123' },
},
};
dynamoDB.putItem(createParams, (err, data) => {
if (err) {
console.error(`Error creating item: ${err}`);
res.status(500).json(err);
} else {
console.log(`Item created successfully: ${data}`);
res.status(200).json('success');
}
});
});
app.get('/init', (req, res) => {
const params: AWS.DynamoDB.CreateTableInput = {
TableName: 'ShareConfig',
KeySchema: [
{ AttributeName: 'HashKey', KeyType: 'HASH' }, // PK
{ AttributeName: 'UPId', KeyType: 'RANGE'}, // SK
],
AttributeDefinitions: [
{ AttributeName: 'HashKey', AttributeType: 'S' },
{ AttributeName: 'UPId', AttributeType: 'S' },
{ AttributeName: 'ConfigValue', AttributeType: 'S' },
{ AttributeName: 'NLEId', AttributeType: 'S' },
{ AttributeName: 'LOCId', AttributeType: 'S' },
{ AttributeName: 'UserUuid', AttributeType: 'S' },
],
ProvisionedThroughput: {
ReadCapacityUnits: 5,
WriteCapacityUnits: 5,
},
};
// Use the DynamoDB client to create the table
dynamoDB.createTable(params, (err, data) => {
if (err) {
console.error(err);
res.status(500).json(err);
} else {
console.log('DynamoDB table created');
res.status(200).json('create success');
}
});
})
app.delete('/clean', (req, res) => {
const params: AWS.DynamoDB.DeleteTableInput = {
TableName: "ShareConfig",
}
dynamoDB.deleteTable(params, (err, data) => {
if (err) {
console.error(`Error deleting table: ${err}`);
res.sendStatus(500);
} else {
console.log(`Table deleted successfully: ${data}`);
res.sendStatus(200);
}
});
});
// Start the Express app
app.listen(3000, () => {
console.log('Express app listening on port 3000');
});