-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
40 lines (28 loc) · 968 Bytes
/
handler.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
38
39
40
'use strict';
const mongoose = require('mongoose');
const discSchema = require('./schemas/disc');
const dbConfig = require('./config/config');
let conn = null;
module.exports.getDiscs = async (event, context) => {
context.callbackWaitsForEmptyEventLoop = false;
let response = {
isBase64Encoded: false,
statusCode: 200,
headers: null,
body: null
}
if (conn == null) {
conn = await mongoose.createConnection(dbConfig.server, {
// Buffering means mongoose will queue up operations if it gets
// disconnected from MongoDB and send them when it reconnects.
// With serverless, better to fail fast if not connected.
bufferCommands: false, // Disable mongoose buffering
bufferMaxEntries: 0 // and MongoDB driver buffering
});
const discModel = conn.model('discs', discSchema);
}
const M = conn.model('discs');
const results = await M.find();
response.body = JSON.stringify({ discs: results});
return response;
};