Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/lib/database/DatabaseManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class DatabaseManager {
logging: config.database.logging ? this.logger.debug.bind(this.logger) : false,
dialect: 'postgres',
define: {
underscored: false,
underscored: true,
schema: this.schema,
},
});
Expand Down
70 changes: 70 additions & 0 deletions app/lib/database/adapters/PeriodAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* @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.
*/


class PeriodAdapter {
/**
* Converts the given database object to an entity object.
*
* @param {SequelizePeriod} databaseObject Object to convert.
* @returns {Period} Converted entity object.
*/
toEntity(databaseObject) {
const {
id,
name,
year,
BeamTypeId,
BeamType,
energy,
} = databaseObject.dataValues; // TODO strange behaviour when using custom column (energy as result as function) without acces dataValues

return {
id,
name,
year,
beamTypeId: BeamTypeId,
beamType: BeamType?.name,
energy,
}
}

/**
* Converts the given entity object to database object.
*
* @param {Period} databaseObject Object to convert.
* @returns {SequelizePeriod} Converted entity object.
*/
toDatabase(entityObject) {
const {
id,
name,
year,
beamTypeId,
beamTypeName,
} = entityObject;

return {
id,
name,
year,
BeamTypeId: beamTypeId,
BeamType: {
id: beamTypeId,
name: beamTypeName,
}
}
}
}

module.exports = PeriodAdapter;
3 changes: 3 additions & 0 deletions app/lib/database/adapters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
* or submit itself to any jurisdiction.
*/

const PeriodAdapter = require('./PeriodAdapter');
const RunAdapter = require('./RunAdapter');

const runAdapter = new RunAdapter();
const periodAdapter = new PeriodAdapter;

module.exports = {
runAdapter,
periodAdapter,
}
31 changes: 31 additions & 0 deletions app/lib/database/models/BeamType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

/**
* @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 Sequelize = require('sequelize');

module.exports = (sequelize) => {
const BeamType = sequelize.define('BeamType', {
name: {
type: Sequelize.STRING,
unique: true,
field: 'beam_type',
},
}, { timestamps: false, tableName: 'beams_dictionary'});

BeamType.associate = (models) => {
BeamType.hasMany(models.Period);
};

return BeamType;
};
34 changes: 34 additions & 0 deletions app/lib/database/models/Period.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @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 Sequelize = require('sequelize');
const { extractPeriodYear } = require('../../utils');

module.exports = (sequelize) => {
const Period = sequelize.define('Period', {
name: {
type: Sequelize.STRING,
unique: true,
},
year: {
type: Sequelize.INTEGER,
},
}, { timestamps: false });

Period.associate = (models) => {
Period.hasMany(models.Run);
Period.belongsTo(models.BeamType, {foreginKey: 'beam_type_id'})
};

return Period;
};
7 changes: 6 additions & 1 deletion app/lib/database/models/Run.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
const Sequelize = require('sequelize');

module.exports = (sequelize) => {
const Run = sequelize.define('run', {
const Run = sequelize.define('Run', {
runNumber: {
type: Sequelize.INTEGER,
primaryKey: true,
Expand Down Expand Up @@ -92,6 +92,11 @@ module.exports = (sequelize) => {
field: 'dipole_current',
},
}, { timestamps: false });


Run.associate = (models) => {
Run.belongsTo(models.Period);
};

return Run;
};
5 changes: 4 additions & 1 deletion app/lib/database/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@

const Sequelize = require('sequelize');

const BeamType = require('./BeamType.js');
const Period = require('./Period.js');
const Run = require('./Run.js');


/**
*
* @param {Sequelize} sequelize instance
* @returns {Object<string, Sequelize.Model>} dict modelName -> sequelize model
*/
const modelsFactory = (sequelize) => {
let models = {
BeamType,
Period,
Run,
};
models = Object.entries(models).map(([modelName, model]) => [modelName, model(sequelize)]); // instantiate models
Expand Down
23 changes: 23 additions & 0 deletions app/lib/database/models/typdefs/SequelizePeriod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @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.
*/

/**
* @typedef SequelizePeriod
*
* @property {number} id
* @property {string} name
* @property {number} year
* @property {number} BeamTypeId
* @property {SequelizeBeamType} BeamType
* @property {number|null} energy
*/
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@
* or submit itself to any jurisdiction.
*/

const Joi = require('joi');
const { emptyDTO, tokenDTO } = require('./commons.dto');

const ApiDocsDTO = Joi.object({
query: tokenDTO,
params: emptyDTO,
body: emptyDTO,
});

module.exports = ApiDocsDTO;
/**
* @typedef SequelizeBeamType
*
* @property {number} id
* @property {string} name
*/
6 changes: 3 additions & 3 deletions app/lib/database/repositories/Repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ class Repository {
/**
* Returns all entities.
*
* @param {Object} findQuery the find query (see sequelize findAll options) or a find query builder
* @param {Object} queryClauses the find query (see sequelize findAll options) or a find query builder
* @returns {Promise<array>} Promise object representing the full mock data
*/
async findAll(findQuery = {}) {
return this.model.findAll(findQuery);
async findAll(queryClauses = {}) {
return this.model.findAll(queryClauses);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
const Joi = require('joi');
const { emptyDTO, tokenDTO } = require('./commons.dto');

const AllRunsDTO = Joi.object({
const STDEntityDTO = Joi.object({
query: tokenDTO, //TODO extend with filters
params: emptyDTO,
body: emptyDTO,
});

module.exports = AllRunsDTO;
module.exports = STDEntityDTO;
6 changes: 2 additions & 4 deletions app/lib/domain/dtos/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
* or submit itself to any jurisdiction.
*/

const AllRunsDTO = require('./AllRuns.dto');
const ApiDocsDTO = require('./ApiDocs.dto');
const STDEntityDTO = require('./STDEntity.dto');

module.exports = {
AllRunsDTO,
ApiDocsDTO,
STDEntityDTO,
};
23 changes: 23 additions & 0 deletions app/lib/domain/entities/Period.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @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.
*/

/**
* @typedef Period
*
* @property {number} id
* @property {string} name
* @property {number} year
* @property {number} beamTypeId
* @property {string} beamType
* @property {number|null} energy
*/
4 changes: 2 additions & 2 deletions app/lib/server/controllers/ApiDocumentation.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* or submit itself to any jurisdiction.
*/

const { ApiDocsDTO } = require('../../domain/dtos');
const { STDEntityDTO } = require('../../domain/dtos');
const { validateDTO } = require('../utilities');

const getApiDocsAsJson = (routes) => routes.map(({ method, path, description }) => ({ method, path, description }));
Expand All @@ -33,7 +33,7 @@ class ApiDocumentationCotroller {
* Express hanlder for whole api description request
*/
async getDocsHandler(req, res) {
const validatedDTO = await validateDTO(ApiDocsDTO, req, res);
const validatedDTO = await validateDTO(STDEntityDTO, req, res);
if (!validatedDTO) {
return;
}
Expand Down
1 change: 1 addition & 0 deletions app/lib/server/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@

module.exports = {
RunController: require('./run.controller.js'),
PeriodController: require('./period.controller.js'),
apiDocumentationCotroller: require('./ApiDocumentation.controller.js'),
};
37 changes: 37 additions & 0 deletions app/lib/server/controllers/period.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @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 { periodService } = require('../../services/periods/PeriodService');
const { STDEntityDTO } = require('../../domain/dtos');
const { validateDTO } = require('../utilities');

/**
* List All runs in db
* @param {Object} req express HTTP request object
* @param {Object} res express HTTP response object
* @param {Object} next express next handler
* @returns {undefined}
*/
const listPeriodsHandler = async (req, res, next) => {
const validatedDTO = await validateDTO(STDEntityDTO, req, res);
if (validatedDTO) {
const runs = await periodService.getAll(validatedDTO.query);
res.json({
data: runs,
});
}
};

module.exports = {
listPeriodsHandler,
};
6 changes: 3 additions & 3 deletions app/lib/server/controllers/run.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
* or submit itself to any jurisdiction.
*/

const runService = require('../../services/runs/RunService');
const { AllRunsDTO } = require('../../domain/dtos');
const { runService } = require('../../services/runs/RunService');
const { STDEntityDTO } = require('../../domain/dtos');
const { validateDTO } = require('../utilities');

/**
Expand All @@ -23,7 +23,7 @@ const { validateDTO } = require('../utilities');
* @returns {undefined}
*/
const listRunsHandler = async (req, res, next) => {
const validatedDTO = await validateDTO(AllRunsDTO, req, res);
const validatedDTO = await validateDTO(STDEntityDTO, req, res);
if (validatedDTO) {
const runs = await runService.getAll(validatedDTO.query);
res.json({
Expand Down
Loading