forked from irinazheltisheva/fastsite-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd.js
More file actions
46 lines (37 loc) · 1.06 KB
/
add.js
File metadata and controls
46 lines (37 loc) · 1.06 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
const { createClient } = require("@astrajs/collections")
exports.handler = async function (event, context, callback) {
const access_token = process.env.FUNCTIONS_ACCESS_TOKEN;
const token = event.headers["x-access-token"];
if(token != access_token){
return {
statusCode: 401,
body: "Unauthorized",
}
}
const astraClient = await createClient({
astraDatabaseId: process.env.ASTRA_DB_ID,
astraDatabaseRegion: process.env.ASTRA_DB_REGION,
username: process.env.ASTRA_DB_USERNAME,
password: process.env.ASTRA_DB_PASSWORD,
});
let collection = "sites";
if(event.queryStringParameters.site){
collection = event.queryStringParameters.site;
}
const users = astraClient
.namespace(process.env.ASTRA_DB_KEYSPACE)
.collection(collection);
try {
const user = await users.create(JSON.parse(event.body).id, event.body)
return {
statusCode: 200,
body: JSON.stringify(user),
};
} catch (e) {
console.error(e);
return {
statusCode: 500,
body: JSON.stringify(e),
}
}
}