1
1
'use strict' ;
2
2
3
3
require ( 'dotenv' ) . config ( { path : './variables.env' } ) ;
4
+ const bluebird = require ( 'bluebird' ) ;
4
5
const mongoose = require ( 'mongoose' ) ;
5
- mongoose . connect ( process . env . DB , { useMongoClient : true } ) ;
6
- mongoose . Promise = global . Promise ;
6
+ mongoose . Promise = bluebird ;
7
+ let cachedDb ;
7
8
const Note = mongoose . model ( 'Note' , { content : String } ) ;
9
+ const Report = mongoose . model ( 'Report' , { content : String , env : String , createdAt : { type : Date , default : Date . now } } ) ;
10
+
11
+ function createReport ( env ) {
12
+ Report . create ( { content : 'Connection request' , env : env } ) ;
13
+ }
14
+
15
+ function connectToDatabase ( ) {
16
+ if ( cachedDb ) {
17
+ console . log ( '=> using cached database instance' ) ;
18
+ createReport ( 'using cached database instance' ) ;
19
+ return Promise . resolve ( ) ;
20
+ }
21
+
22
+ console . log ( '=> using new database instance' ) ;
23
+ createReport ( 'using new database instance' ) ;
24
+ return mongoose . connect ( process . env . DB , { useMongoClient : true } )
25
+ . then ( db => { cachedDb = db ; } ) ;
26
+ }
8
27
9
28
module . exports . create = ( event , context , callback ) => {
10
- Note . create ( JSON . parse ( event . body ) )
29
+ context . callbackWaitsForEmptyEventLoop = false ;
30
+ return connectToDatabase ( )
31
+ . then ( ( ) =>
32
+ Note . create ( JSON . parse ( event . body ) )
33
+ )
11
34
. then ( note => callback ( null , {
12
35
statusCode : 200 ,
13
36
body : JSON . stringify ( note )
@@ -20,7 +43,11 @@ module.exports.create = (event, context, callback) => {
20
43
}
21
44
22
45
module . exports . getOne = ( event , context , callback ) => {
23
- Note . findById ( event . pathParameters . id )
46
+ context . callbackWaitsForEmptyEventLoop = false ;
47
+ return connectToDatabase ( )
48
+ . then ( ( ) =>
49
+ Note . findById ( event . pathParameters . id )
50
+ )
24
51
. then ( note => callback ( null , {
25
52
statusCode : 200 ,
26
53
body : JSON . stringify ( note )
@@ -33,7 +60,11 @@ module.exports.getOne = (event, context, callback) => {
33
60
} ;
34
61
35
62
module . exports . getAll = ( event , context , callback ) => {
36
- Note . find ( )
63
+ context . callbackWaitsForEmptyEventLoop = false ;
64
+ return connectToDatabase ( )
65
+ . then ( ( ) =>
66
+ Note . find ( )
67
+ )
37
68
. then ( notes => callback ( null , {
38
69
statusCode : 200 ,
39
70
body : JSON . stringify ( notes )
@@ -42,11 +73,15 @@ module.exports.getAll = (event, context, callback) => {
42
73
statusCode : err . statusCode || 500 ,
43
74
headers : { 'Content-Type' : 'text/plain' } ,
44
75
body : 'Could not fetch the notes.'
45
- } ) ) ;
76
+ } ) )
46
77
} ;
47
78
48
79
module . exports . update = ( event , context , callback ) => {
49
- Note . findByIdAndUpdate ( event . pathParameters . id , JSON . parse ( event . body ) , { new : true } )
80
+ context . callbackWaitsForEmptyEventLoop = false ;
81
+ return connectToDatabase ( )
82
+ . then ( ( ) =>
83
+ Note . findByIdAndUpdate ( event . pathParameters . id , JSON . parse ( event . body ) , { new : true } )
84
+ )
50
85
. then ( note => callback ( null , {
51
86
statusCode : 200 ,
52
87
body : JSON . stringify ( note )
@@ -59,7 +94,11 @@ module.exports.update = (event, context, callback) => {
59
94
} ;
60
95
61
96
module . exports . delete = ( event , context , callback ) => {
62
- Note . findByIdAndRemove ( event . pathParameters . id )
97
+ context . callbackWaitsForEmptyEventLoop = false ;
98
+ return connectToDatabase ( )
99
+ . then ( ( ) =>
100
+ Note . findByIdAndRemove ( event . pathParameters . id )
101
+ )
63
102
. then ( note => callback ( null , {
64
103
statusCode : 200 ,
65
104
body : JSON . stringify ( { message : 'Removed note with id: ' + note . _id , note : note } )
0 commit comments