src/functions/get.ts   A
last analyzed

Complexity

Total Complexity 2
Complexity/F 0

Size

Lines of Code 32
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 26
mnd 2
bc 2
fnc 0
dl 0
loc 32
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import {Handler} from "@netlify/functions";
2
import {MongoClient} from "mongodb";
3
4
const uri = `mongodb+srv://${process.env.MONGODB_USER}:${process.env.MONGODB_PASSWORD}@cluster0.k6gbu.mongodb.net/test_database?retryWrites=true&w=majority`;
5
const dbName = "replayerDB";
6
const collectionName = "media";
7
8
const handler: Handler = async (event, context) => {
9
  if (event.httpMethod !== "POST") {
10
    return {statusCode: 405, body: "Method Not Allowed"};
11
  }
12
13
  if (!event.body) {
14
    return {statusCode: 400, body: "Bad Request"};
15
  }
16
17
  // @ts-ignore
18
  const client = new MongoClient(uri, {useNewUrlParser: true, useUnifiedTopology: true});
19
  await client.connect();
20
  const collection = client.db(dbName).collection(collectionName);
21
  const body = JSON.parse(event.body);
22
  const document = await collection.findOne({"resourceId": body.resourceId});
23
  await client.close();
24
25
  return {
26
    statusCode: 200,
27
    body: JSON.stringify({result: document || null})
28
  };
29
};
30
31
export {handler};
32