commit 56b5ad31cdf0a1e9f76a5f864a60e114da1c73be Author: Märt Kalmo Date: Wed May 3 15:17:25 2017 +0300 starting state diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7072bbc --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +node_modules + +/.settings +/.classpath +/.project + +/.idea +*.iml +*.log diff --git a/dao.js b/dao.js new file mode 100644 index 0000000..1d406dd --- /dev/null +++ b/dao.js @@ -0,0 +1,25 @@ +'use strict'; + +const mongodb = require('mongodb'); +const ObjectID = require('mongodb').ObjectID; +const COLLECTION = 'test-collection'; + +class Dao { + + connect(url) { + return mongodb.MongoClient.connect(url) + .then(db => this.db = db); + } + + findAll() { + return this.db.collection(COLLECTION).find().toArray(); + } + + close() { + if (this.db) { + this.db.close(); + } + } +} + +module.exports = Dao; diff --git a/executor.js b/executor.js new file mode 100644 index 0000000..1bfa709 --- /dev/null +++ b/executor.js @@ -0,0 +1,13 @@ + +// 1 +var promise = sqrt(4); + +promise.then(result => console.log(result)) + .catch(error => console.log('Error: ' + error)); + +function sqrt(arg) { + console.log('calculate sqrt(' + arg + ')'); + return parseInt(arg) >= 0 + ? Promise.resolve(Math.sqrt(arg)) + : Promise.reject('bad argument'); +} diff --git a/exercises.js b/exercises.js new file mode 100644 index 0000000..55bb564 --- /dev/null +++ b/exercises.js @@ -0,0 +1,63 @@ + +// 1 +var promise = sqrt(4); + +// 2 +var promise = sqrt(-1); + +// 3 +var promise = sqrt(9) + .then(result => { + return result + 1; + }); + +// 4 +var promise = sqrt(9) + .then(result => result + 1) + .then(result => result + 2); + +// 5 +var promise = sqrt(16) + .then(result => { + return sqrt(result); + }); + +// 6 +var promise = sqrt(16) + .then(result => sqrt(result)) + .then(result => result + 1); + +// 7 +var promise = sqrt(4) + .then(result => { + console.log(result); + }); + +// 8 +var promise = sqrt(4) + .then(result => { + throw 'hello'; + }) + .then(result => console.log(result)); + +// 9 +var promise = sqrt(4) + .then(() => Math.hello()) + .then(result => console.log(result)); + +// 10 +var promise = sqrt(4) + .then(() => { + throw 'error' + }).catch(error => { + console.log('logging: ' + error); + }); + +// 11 +var promise = sqrt(4) + .then(() => { + throw 'error' + }).catch(error => { + console.log('logging: ' + error); + throw error; + }); diff --git a/mongo.js b/mongo.js new file mode 100644 index 0000000..cf51e6a --- /dev/null +++ b/mongo.js @@ -0,0 +1,26 @@ +'use strict'; + +const mongodb = require('mongodb'); +const Dao = require('./dao.js'); +const ObjectID = mongodb.ObjectID; + +var url = 'mongodb://...'; + +var data = { name: 'Jill' }; + +var database; +mongodb.MongoClient.connect(url).then(db => { + database = db; + return db.collection('test-collection').insertOne(data); +}).then(() => { + closeDb(database) +}).catch(error => { + closeDb(database); + throw error; +}); + +function closeDb(database) { + if (database) { + database.close(); + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..1ec38f6 --- /dev/null +++ b/package.json @@ -0,0 +1,7 @@ +{ + "name": "exmongo", + "version": "1.0.0", + "devDependencies": { + "mongodb": "^2.2.25" + } +}