|
| 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 | +/** |
| 15 | + * Sequelize implementation of the QueryBuilder. |
| 16 | + */ |
| 17 | +class QueryBuilder { |
| 18 | + /** |
| 19 | + * Creates a new `QueryBuilder` instance. |
| 20 | + */ |
| 21 | + constructor() { |
| 22 | + this.options = {}; |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * The numbers of items to return. |
| 27 | + * |
| 28 | + * @param {Number} number The numbers of items to return. |
| 29 | + * @returns {Object} The current QueryBuilder instance. |
| 30 | + */ |
| 31 | + limit(number) { |
| 32 | + this.options.limit = number; |
| 33 | + return this; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * The number of items to skip before starting to collect the result set. |
| 38 | + * |
| 39 | + * @param {Number} number The number of items to skip. |
| 40 | + * @returns {Object} The current QueryBuilder instance. |
| 41 | + */ |
| 42 | + offset(number) { |
| 43 | + this.options.offset = number; |
| 44 | + return this; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Set the order of elements. |
| 49 | + * |
| 50 | + * @param {String|Array} order Either a string or an array of strings. |
| 51 | + * @returns {Object} The current QueryBuilder instance. |
| 52 | + */ |
| 53 | + orderBy(order) { |
| 54 | + if (Array.isArray(order)) { |
| 55 | + for (const orderElement of order) { |
| 56 | + this.orderBy(orderElement); |
| 57 | + } |
| 58 | + return this; |
| 59 | + } |
| 60 | + |
| 61 | + if (!this.options.order) { |
| 62 | + this.options.order = []; |
| 63 | + } |
| 64 | + |
| 65 | + if (order.startsWith('-')) { |
| 66 | + this.options.order.push([order.substr(1), 'DESC']); |
| 67 | + } else { |
| 68 | + this.options.order.push([order, 'ASC']); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Returns the implementation specific query. |
| 74 | + * |
| 75 | + * @returns {Object} Implementation specific query. |
| 76 | + */ |
| 77 | + toImplementation() { |
| 78 | + return this.options; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Adds a filter on the given column value pair. |
| 83 | + * |
| 84 | + * @param {*} column The target column. |
| 85 | + * @param {*} value The required value. |
| 86 | + * @returns {Object} The current QueryBuilder instance. |
| 87 | + */ |
| 88 | + where(column, value) { |
| 89 | + if (!this.options.where) { |
| 90 | + this.options.where = {}; |
| 91 | + } |
| 92 | + |
| 93 | + this.options.where[column] = value; |
| 94 | + return this; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +module.exports = QueryBuilder; |
0 commit comments