starting state

This commit is contained in:
Märt Kalmo 2017-05-03 15:17:25 +03:00
commit 56b5ad31cd
6 changed files with 143 additions and 0 deletions

9
.gitignore vendored Normal file
View File

@ -0,0 +1,9 @@
node_modules
/.settings
/.classpath
/.project
/.idea
*.iml
*.log

25
dao.js Normal file
View File

@ -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;

13
executor.js Normal file
View File

@ -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');
}

63
exercises.js Normal file
View File

@ -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;
});

26
mongo.js Normal file
View File

@ -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();
}
}

7
package.json Normal file
View File

@ -0,0 +1,7 @@
{
"name": "exmongo",
"version": "1.0.0",
"devDependencies": {
"mongodb": "^2.2.25"
}
}