Update repository functions.
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
const mongodb = require('mongodb');
|
||||
const assert = require('assert');
|
||||
|
||||
const configuration = require('../configuration');
|
||||
const mongoConfig = configuration.database;
|
||||
|
||||
class MongoClient {
|
||||
constructor() {
|
||||
mongodb.MongoClient.connect(mongoConfig.url, (error, client) => {
|
||||
assert.equal(null, error, `Unable to connect to mongodb: ${error}.`);
|
||||
console.log(error);
|
||||
if (error !== null) {
|
||||
throw new Error(`Unable de connect to Mongo database: ${error}`);
|
||||
}
|
||||
|
||||
console.log('Connected successfuly to mongodb');
|
||||
this.db = client.db(mongoConfig.database);
|
||||
});
|
||||
@@ -19,12 +21,17 @@ class MongoClient {
|
||||
console.log(`Entities ${collectionName} founded.`);
|
||||
callback(results);
|
||||
})
|
||||
.catch(error => console.error(error));
|
||||
.catch(error => {
|
||||
throw new Error(`Unable to find entities in collection ${collectionName}: ${error}`);
|
||||
});
|
||||
}
|
||||
|
||||
insert(collectionName, entity, callback) {
|
||||
this.db.collection(collectionName).insert(entity, (error, result) => {
|
||||
assert.equal(null, error, `Unable to insert ${collectionName} entity: ${error}.`);
|
||||
this.db.collection(collectionName).insertOne(entity, (error, result) => {
|
||||
if (error !== null) {
|
||||
throw new Error(`Unable to insert ${collectionName} entity: ${error}`);
|
||||
}
|
||||
|
||||
console.log(`Entity ${collectionName} inserted.`);
|
||||
// Return only the inserted document.
|
||||
callback(result.ops[0]);
|
||||
@@ -32,8 +39,11 @@ class MongoClient {
|
||||
}
|
||||
|
||||
update(collectionName, entity, callback) {
|
||||
this.db.collection(collectionName).save(entity, (error) => {
|
||||
assert.equal(null, error, `Unable to update ${collectionName} entity: ${error}.`);
|
||||
this.db.collection(collectionName).updateOne({_id: mongodb.ObjectId(entity._id)}, {$set: entity}, {upsert: true}, (error) => {
|
||||
if (error !== null) {
|
||||
throw new Error(`Unable to update ${collectionName} entity: ${error}`);
|
||||
}
|
||||
|
||||
console.log(`Entity ${collectionName} updated.`);
|
||||
callback();
|
||||
});
|
||||
@@ -41,12 +51,16 @@ class MongoClient {
|
||||
|
||||
delete(collectionName, entityId, callback) {
|
||||
this.db.collection(collectionName).deleteOne({_id: mongodb.ObjectId(entityId)}, (error) => {
|
||||
assert.equal(null, error, `Unable to delete ${collectionName} entity with id ${entityId}: ${error}.`);
|
||||
if (error !== null) {
|
||||
throw new Error(`Unable to delete ${collectionName} entity with id ${entityId}: ${error}`);
|
||||
}
|
||||
|
||||
console.log(`Entity ${collectionName} with id ${entityId} deleted.`);
|
||||
callback();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Define a singleton of class "MongoClient".
|
||||
const mongoClient = new MongoClient();
|
||||
module.exports = mongoClient;
|
||||
Reference in New Issue
Block a user