|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright CERN and copyright holders of ALICE O2. This software is |
| 4 | + * distributed under the terms of the GNU General Public License v3 (GPL |
| 5 | + * Version 3), copied verbatim in the file "COPYING". |
| 6 | + * |
| 7 | + * See http://alice-o2.web.cern.ch/license for full licensing information. |
| 8 | + * |
| 9 | + * In applying this license CERN does not waive the privileges and immunities |
| 10 | + * granted to it by virtue of its status as an Intergovernmental Organization |
| 11 | + * or submit itself to any jurisdiction. |
| 12 | + */ |
| 13 | + |
| 14 | +const { TagAdapter } = require('../adapters'); |
| 15 | +const { models: { Tag } } = require('../'); |
| 16 | + |
| 17 | +/** |
| 18 | + * Sequelize implementation of the TagRepository. |
| 19 | + */ |
| 20 | +class TagRepository { |
| 21 | + /** |
| 22 | + * Returns the total number of entities. |
| 23 | + * |
| 24 | + * @returns {Promise<Number>} Promise object representing the total number of entities. |
| 25 | + */ |
| 26 | + async count() { |
| 27 | + return Tag.count(); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Returns all entities. |
| 32 | + * |
| 33 | + * @param {Object} queryBuilder The QueryBuilder to use. |
| 34 | + * @returns {Promise} Promise object representing the full mock data |
| 35 | + */ |
| 36 | + async findAll(queryBuilder) { |
| 37 | + return Tag.findAll(queryBuilder.toImplementation()).map(TagAdapter.toEntity); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Returns a specific entity. |
| 42 | + * |
| 43 | + * @param {Object} queryBuilder The QueryBuilder to use. |
| 44 | + * @returns {Promise|Null} Promise object representing the full mock data |
| 45 | + */ |
| 46 | + async findOne(queryBuilder) { |
| 47 | + queryBuilder.limit(1); |
| 48 | + const result = await Tag.findOne(queryBuilder.toImplementation()); |
| 49 | + return result ? TagAdapter.toEntity(result) : null; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Insert entity. |
| 54 | + * |
| 55 | + * @param {Object} entity entity to insert. |
| 56 | + * @returns {Promise} Promise object represents the just inserted Tag. |
| 57 | + */ |
| 58 | + async insert(entity) { |
| 59 | + const result = await Tag.create(TagAdapter.toDatabase(entity)); |
| 60 | + return TagAdapter.toEntity(result); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +module.exports = new TagRepository(); |
0 commit comments