Add src folder and correct base repository functions.

This commit is contained in:
Pierre THIERRY
2020-09-05 19:18:51 +02:00
parent 80253cabf8
commit c618675a9a
4 changed files with 30 additions and 21 deletions

12
app.js
View File

@@ -1,12 +0,0 @@
const express = require('express');
const Mongo = require('./mongo');
const app = express();
const port = 3000;
app.get('/test', (request, response) => {
const mongoClient = new Mongo();
})
app.listen(port, () => console.log('Mock is listening at port ', port, '\n'));

View File

@@ -4,7 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon ./app.js localhost 3000",
"start": "nodemon ./src/js/app.js localhost 3000",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],

19
src/js/app.js Normal file
View File

@@ -0,0 +1,19 @@
const express = require('express');
const MongoClient = require('./mongoClient');
const app = express();
const port = 3000;
const mongoClient = new MongoClient();
app.get('/test', (request, response) => {
mongoClient.insert('test', {creationDate: new Date()}, () => {
mongoClient.find('test', {}, results => {
console.log(results);
response.send(JSON.stringify(results));
});
});
});
app.listen(port, () => console.log('Mock is listening at port ', port, '\n'));

View File

@@ -8,7 +8,7 @@ const mongoConfig = {
database: 'express-test'
};
class Mongo {
class MongoClient {
constructor() {
mongodb.MongoClient.connect(mongoConfig.url, (err, client) => {
assert.equal(null, err);
@@ -19,16 +19,18 @@ class Mongo {
results.forEach(console.log);
});
client.close();
// client.close();
});
}
find(collectionName, query, callback) {
this.db.collection(collectionName).find(query, (error, result) => {
assert.equal(null, error, `Unable to find ${collectionName} entities: ${error}.`);
this.db.collection(collectionName).find(query).toArray()
.then(results => {
console.log(`Entities ${collectionName} founded.`);
callback(result);
});
console.log(results);
callback(results);
})
.catch(error => console.error(error));
}
insert(collectionName, entity, callback) {
@@ -56,4 +58,4 @@ class Mongo {
}
}
module.exports = Mongo;
module.exports = MongoClient;