Update repository functions.

This commit is contained in:
2020-09-26 12:09:46 +02:00
parent 59c4b0de38
commit 5e6da61ccb
4 changed files with 78 additions and 16 deletions

View File

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

View File

@@ -1,26 +1,63 @@
const mongoClient = require('./mongoClient');
const ObjectId = require('mongodb').ObjectId;
class Repository {
/**
* If entity in parameters has an attribute named "_id", this function converts it into a MongoDB {@code ObjectId}.
*/
function convertIdToMongodbFormat(entity) {
if (!!entity._id && !(entity._id instanceof ObjectId)) {
entity._id = ObjectId(entity._id);
}
}
module.exports = class Repository {
/**
* Creates a new repository which read and write into the {@code collectionName} collection in database.
* @param {*} collectionName
*/
constructor(collectionName) {
this.collectionName = collectionName;
this.mongoClient = mongoClient;
}
/**
* Returns the entities that matches criteria in {@code query}.
* @param {*} query The query which contains criteria to find some entities.
* @param {*} callback The function to execute after getting entities.
*/
find(query, callback) {
convertIdToMongodbFormat(query);
this.mongoClient.find(this.collectionName, query, callback);
}
/**
* Insert entity in database.
* @param {*} entity The entity to insert into database.
* @param {*} callback The function to execute after inserting entity.
*/
insert(entity, callback) {
this.mongoClient.insert(this.collectionName, entity, callback);
}
/**
* Update the whole entity in database.
* @param {*} entity The entity to update into database.
* @param {*} callback The function to execute after updating entity.
*/
update(entity, callback) {
convertIdToMongodbFormat(entity);
this.mongoClient.update(this.collectionName, entity, callback);
}
/**
* Delete entity in database.
* @param {*} entityId Entity to delete id.
* @param {*} callback The function to execute after deleting entity.
*/
delete(entityId, callback) {
if (!(entityId instanceof ObjectId)) {
entityId = ObjectId(entityId);
}
this.mongoClient.delete(this.collectionName, entityId, callback);
}
}
module.exports = Repository;
};