This repository has been archived by the owner on Nov 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial code for notesApp example. Fix isFromExt detection.
- Loading branch information
1 parent
8504a3c
commit d755bd4
Showing
5 changed files
with
99 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
A Notes app | ||
=========== | ||
|
||
To run this example | ||
|
||
```bash | ||
$ docker run -p 172.17.0.1:27017:27017 --name notes-mongo -d mongo | ||
|
||
$ cd notesBackend | ||
$ npm install | ||
$ lever deploy | ||
``` | ||
|
||
... To be continued. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
|
||
'use strict'; | ||
|
||
const MongoClient = require('mongodb').MongoClient; | ||
const lodash = require('lodash'); | ||
|
||
const mongoUrl = 'mongodb://172.17.0.1:27017/notesApp' | ||
|
||
module.exports.addNote = (note, callback) => { | ||
getDb((err, db) => { | ||
if (err) { | ||
callback(err); | ||
return; | ||
} | ||
|
||
db.collection('notes').insertOne({ | ||
note, | ||
}, (err, result) => { | ||
if (err) { | ||
console.log(err); | ||
callback(err); | ||
return; | ||
} | ||
|
||
callback(null, null); | ||
}); | ||
}); | ||
}; | ||
|
||
module.exports.getNotes = (callback) => { | ||
getDb((err, db) => { | ||
if (err) { | ||
callback(err); | ||
return; | ||
} | ||
|
||
db.collection('notes').find({}).toArray((err, docs) => { | ||
if (err) { | ||
console.log(err); | ||
callback(err); | ||
return; | ||
} | ||
|
||
const notes = lodash.map(docs, (doc) => { | ||
return doc.note; | ||
}); | ||
callback(null, notes); | ||
}); | ||
}); | ||
}; | ||
|
||
let cachedDb = null; | ||
function getDb(callback) { | ||
if (cachedDb !== null) { | ||
callback(null, cachedDb); | ||
return; | ||
} | ||
MongoClient.connect(mongoUrl, (err, db) => { | ||
if (err) { | ||
console.log(err); | ||
callback(err); | ||
return; | ||
} | ||
cachedDb = db; | ||
callback(null, db); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "notesBackend", | ||
"jsEntry": "backend.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "notes-backend", | ||
"version": "0.0.0", | ||
"description": "", | ||
"main": "backend.js", | ||
"author": "", | ||
"license": "Apache-2.0", | ||
"private": true, | ||
"dependencies": { | ||
"lodash": "^4.13.1", | ||
"mongodb": "^2.1.21" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters