Skip to content

Commit

Permalink
feat: added TransactionHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
mvegter committed May 4, 2020
1 parent 76d35d3 commit 4649722
Show file tree
Hide file tree
Showing 13 changed files with 152 additions and 53 deletions.
18 changes: 18 additions & 0 deletions lib/application/interfaces/database/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @license
* Copyright CERN and copyright holders of ALICE O2. This software is
* distributed under the terms of the GNU General Public License v3 (GPL
* Version 3), copied verbatim in the file "COPYING".
*
* See http://alice-o2.web.cern.ch/license for full licensing information.
*
* In applying this license CERN does not waive the privileges and immunities
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/

const utilities = require('./utilities');

module.exports = {
utilities,
};
16 changes: 16 additions & 0 deletions lib/application/interfaces/database/utilities/TransactionHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* TransactionHelper
*/
class TransactionHelper {
/**
* Provides a transaction. This will handle committing or rolling back the transaction automatically.
*
* @param {Function} cb Callback for the transaction.
* @returns {Promise} Promise object represents ...
*/
static async provide(cb) {
return Promise.reject('The method or operation is not implemented.');
}
}

module.exports = TransactionHelper;
18 changes: 18 additions & 0 deletions lib/application/interfaces/database/utilities/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @license
* Copyright CERN and copyright holders of ALICE O2. This software is
* distributed under the terms of the GNU General Public License v3 (GPL
* Version 3), copied verbatim in the file "COPYING".
*
* See http://alice-o2.web.cern.ch/license for full licensing information.
*
* In applying this license CERN does not waive the privileges and immunities
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/

const TransactionHelper = require('./TransactionHelper');

module.exports = {
ITransactionHelper: TransactionHelper,
};
32 changes: 9 additions & 23 deletions lib/application/usecases/log/CreateLogUseCase.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,27 @@
*/

const { IUseCase } = require('../../interfaces');
const { ILogRepository } = require('../../interfaces/repositories');
const {
repositories: {
LogRepository,
},
utilities: {
TransactionHelper,
},
} = require('../../../database');

/**
* CreateLogUseCase
*/
class CreateLogUseCase extends IUseCase {
/**
* Constructor
*/
constructor() {
super();

// Load the default interface
this.logRepository = new ILogRepository();
}

/**
* Sets the (DI) Log Repository.
*
* @param {Object} logRepository (DI) Log Repository
* @returns {Object} Current UseCase
*/
setLogRepository(logRepository) {
this.logRepository = logRepository;
return this;
}

/**
* Executes this use case.
*
* @param {Object} data The data that will be inserted.
* @returns {Promise} Promise object represents the result of this use case.
*/
async execute(data) {
return this.logRepository.insert(data);
return TransactionHelper.provide(() => LogRepository.insert(data));
}
}

Expand Down
32 changes: 9 additions & 23 deletions lib/application/usecases/log/GetAllLogsUseCase.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,26 @@
*/

const { IUseCase } = require('../../interfaces');
const { ILogRepository } = require('../../interfaces/repositories');
const {
repositories: {
LogRepository,
},
utilities: {
TransactionHelper,
},
} = require('../../../database');

/**
* GetAllLogsUseCase
*/
class GetAllLogsUseCase extends IUseCase {
/**
* Creates a new `GetAllLogs UseCase` instance.
*/
constructor() {
super();

// Load the default interface
this.logRepository = new ILogRepository();
}

/**
* Sets the (DI) Log Repository.
*
* @param {Object} logRepository (DI) Log Repository
* @returns {Object} Current UseCase
*/
setLogRepository(logRepository) {
this.logRepository = logRepository;
return this;
}

/**
* Executes this use case.
*
* @returns {Promise} Promise object represents the result of this use case.
*/
async execute() {
return this.logRepository.findAll();
return TransactionHelper.provide(() => LogRepository.findAll());
}
}

Expand Down
11 changes: 11 additions & 0 deletions lib/database/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* or submit itself to any jurisdiction.
*/

const cls = require('cls-hooked');
const path = require('path');
const { Sequelize } = require('sequelize');
const Umzug = require('umzug');
Expand All @@ -27,6 +28,9 @@ class SequelizeDatabase extends IDatabase {
constructor() {
super();

this.namespace = cls.createNamespace('sequelize');
Sequelize.useCLS(this.namespace);

this.sequelize = new Sequelize(Config.database, Config.username, Config.password, {
host: Config.host,
dialect: Config.dialect,
Expand All @@ -47,6 +51,13 @@ class SequelizeDatabase extends IDatabase {
return require('./repositories');
}

/**
* Returns all available utilities.
*/
get utilities() {
return require('./utilities')(this.sequelize);
}

/**
* Performs connection to the database.
*
Expand Down
21 changes: 21 additions & 0 deletions lib/database/utilities/TransactionHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { ITransactionHelper } = require('../../application/interfaces/database/utilities');
const { isPromise } = require('../../utilities');

module.exports = (sequelize) => {
/**
* Sequelize implementation of the TransactionHelper interface.
*/
class TransactionHelper extends ITransactionHelper {
/**
* Provides a transaction. This will handle committing or rolling back the transaction automatically.
*
* @param {Function} cb Callback for the transaction.
* @returns {Promise} Promise object represents ...
*/
static async provide(cb) {
return await sequelize.transaction(async () => isPromise(cb) ? await cb() : cb());
}
}

return TransactionHelper;
};
18 changes: 18 additions & 0 deletions lib/database/utilities/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @license
* Copyright CERN and copyright holders of ALICE O2. This software is
* distributed under the terms of the GNU General Public License v3 (GPL
* Version 3), copied verbatim in the file "COPYING".
*
* See http://alice-o2.web.cern.ch/license for full licensing information.
*
* In applying this license CERN does not waive the privileges and immunities
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/

const TransactionHelper = require('./TransactionHelper');

module.exports = (sequelize) => ({
TransactionHelper: TransactionHelper(sequelize),
});
3 changes: 0 additions & 3 deletions lib/server/controllers/logs.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

const { log: { CreateLogUseCase, GetAllLogsUseCase } } = require('../../application/usecases');
const { dtos: { CreateLogDto } } = require('../../domain');
const { repositories: { LogRepository } } = require('../../database');

/**
* Create a new log
Expand Down Expand Up @@ -43,7 +42,6 @@ const create = async (request, response, next) => {
}

const log = await new CreateLogUseCase()
.setLogRepository(LogRepository)
.execute(createLogDto);

response.status(201).json({
Expand Down Expand Up @@ -108,7 +106,6 @@ const getAttachment = (request, response, next) => {
*/
const index = async (request, response, next) => {
const logs = await new GetAllLogsUseCase()
.setLogRepository(LogRepository)
.execute();
response.status(200).json({
data: logs,
Expand Down
31 changes: 31 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"dependencies": {
"@aliceo2/web-ui": "1.11.0",
"cls-hooked": "4.2.2",
"deepmerge": "4.2.2",
"mariadb": "2.3.1",
"sequelize": "5.21.7",
Expand Down
2 changes: 0 additions & 2 deletions test/application/usecases/log/CreateLogUseCase.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ module.exports = () => {
const nLogsBefore = await LogRepository.count();

await new CreateLogUseCase()
.setLogRepository(LogRepository)
.execute(createLogDto);

const nLogsAfter = await LogRepository.count();
Expand All @@ -43,7 +42,6 @@ module.exports = () => {

createLogDto.title = expectedTitle;
const result = await new CreateLogUseCase()
.setLogRepository(LogRepository)
.execute(createLogDto);

expect(result.title).to.equal(expectedTitle);
Expand Down
2 changes: 0 additions & 2 deletions test/application/usecases/log/GetAllLogsUseCase.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
* or submit itself to any jurisdiction.
*/

const { repositories: { LogRepository } } = require('../../../../lib/database');
const { log: { GetAllLogsUseCase } } = require('../../../../lib/application/usecases');
const chai = require('chai');

Expand All @@ -20,7 +19,6 @@ const { expect } = chai;
module.exports = () => {
it('should return an array', async () => {
const result = await new GetAllLogsUseCase()
.setLogRepository(LogRepository)
.execute();

expect(result).to.be.an('array');
Expand Down

0 comments on commit 4649722

Please sign in to comment.