-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda.js
37 lines (30 loc) · 983 Bytes
/
lambda.js
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
const { MongoClient } = require("mongodb");
const uri = process.env.MONGODB_URI;
exports.handler = async (event) => {
const client = new MongoClient(uri);
try {
await client.connect();
console.log("Connected correctly to server");
const db = client.db("test");
const collection = db.collection("example");
await collection.deleteMany();
const result = await collection.insertMany(sampleData);
console.log(`${result.insertedCount} documents were inserted`);
const data = await collection.find({}).toArray();
return {
statusCode: 200,
body: data,
};
} catch (e) {
console.error(e);
} finally {
await client.close();
}
};
// Sample data to be inserted
const sampleData = [
{ name: "John Doe", age: 30, profession: "Engineer" },
{ name: "Jane Doe", age: 25, profession: "Designer" },
{ name: "Mike Smith", age: 35, profession: "Developer" },
{ name: "Sara Wilson", age: 28, profession: "Manager" },
];