Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d2b795f

Browse files
(features) added connect to database function; added create report function; set dotenv; added bluebird; wrapped all functions in connect wrappers; added callback event loop wait;
1 parent 8b0e690 commit d2b795f

File tree

4 files changed

+63
-16
lines changed

4 files changed

+63
-16
lines changed

‎handler.js

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,36 @@
11
'use strict';
22

33
require('dotenv').config({ path: './variables.env' });
4+
const bluebird = require('bluebird');
45
const mongoose = require('mongoose');
5-
mongoose.connect(process.env.DB,{useMongoClient: true});
6-
mongoose.Promise=global.Promise;
6+
mongoose.Promise=bluebird;
7+
letcachedDb;
78
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+
}
827

928
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+
)
1134
.then(note => callback(null, {
1235
statusCode: 200,
1336
body: JSON.stringify(note)
@@ -20,7 +43,11 @@ module.exports.create = (event, context, callback) => {
2043
}
2144

2245
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+
)
2451
.then(note => callback(null, {
2552
statusCode: 200,
2653
body: JSON.stringify(note)
@@ -33,7 +60,11 @@ module.exports.getOne = (event, context, callback) => {
3360
};
3461

3562
module.exports.getAll = (event, context, callback) => {
36-
Note.find()
63+
context.callbackWaitsForEmptyEventLoop = false;
64+
return connectToDatabase()
65+
.then(() =>
66+
Note.find()
67+
)
3768
.then(notes => callback(null, {
3869
statusCode: 200,
3970
body: JSON.stringify(notes)
@@ -42,11 +73,15 @@ module.exports.getAll = (event, context, callback) => {
4273
statusCode: err.statusCode || 500,
4374
headers: { 'Content-Type': 'text/plain' },
4475
body: 'Could not fetch the notes.'
45-
}));
76+
}))
4677
};
4778

4879
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+
)
5085
.then(note => callback(null, {
5186
statusCode: 200,
5287
body: JSON.stringify(note)
@@ -59,7 +94,11 @@ module.exports.update = (event, context, callback) => {
5994
};
6095

6196
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+
)
63102
.then(note => callback(null, {
64103
statusCode: 200,
65104
body: JSON.stringify({ message: 'Removed note with id: ' + note._id, note: note })

‎package-lock.json

Lines changed: 8 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@
44
"description": "",
55
"main": "handler.js",
66
"dependencies": {
7+
"bluebird": "^3.5.1",
78
"dotenv": "^4.0.0",
89
"mongoose": "^4.13.7"
910
},
1011
"devDependencies": {
1112
"serverless-offline": "^3.16.0"
1213
},
1314
"scripts": {
14-
"test": "echo \"Error: no test specified\" && exit 1"
15+
"test": "echo \"Error: no test specified\" && exit 1",
16+
"offline": "sls offline start --skipCacheInvalidation"
1517
},
16-
"author": "",
18+
"author": "Adnan Rahić",
1719
"license": "ISC"
1820
}

‎serverless.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
service: building-a-serverless-rest-api-with-nodejs
1+
service: sls-rest-api
22

33
provider:
44
name: aws
55
runtime: nodejs6.10
66
memorySize: 128
7+
timeout: 10
78
stage: dev
8-
region: eu-central-1
9-
9+
region: us-east-1
10+
1011
functions:
1112
create:
1213
handler: handler.create

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /