46 lines
1.2 KiB
JavaScript
46 lines
1.2 KiB
JavaScript
const validator = require('validator');
|
|
const { v4: uuidv4 } = require('uuid');
|
|
const Sequelize = require('./models').Sequelize;
|
|
|
|
module.exports = class Utils {
|
|
/**
|
|
* Check if value is a valid UUID
|
|
* @param {*} value - The value to check
|
|
* @returns {boolean} - True if valid UUID, false otherwise
|
|
*/
|
|
static isValidUuid(value) {
|
|
return Boolean(value && validator.isUUID(String(value)));
|
|
}
|
|
|
|
/**
|
|
* Generate a new UUID v4
|
|
* @returns {string} - A new UUID v4 string
|
|
*/
|
|
static generateUuid() {
|
|
return uuidv4();
|
|
}
|
|
|
|
/**
|
|
* Filter array to only valid UUIDs
|
|
* @param {Array} values - Array of values to filter
|
|
* @returns {string[]} - Array containing only valid UUIDs
|
|
*/
|
|
static filterValidUuids(values) {
|
|
return values.filter((v) => this.isValidUuid(v));
|
|
}
|
|
|
|
/**
|
|
* Case-insensitive LIKE query
|
|
* @param {string} model - The model/table name
|
|
* @param {string} column - The column name
|
|
* @param {string} value - The value to search for
|
|
* @returns {Object} - Sequelize where clause
|
|
*/
|
|
static ilike(model, column, value) {
|
|
return Sequelize.where(
|
|
Sequelize.fn('lower', Sequelize.col(`${model}.${column}`)),
|
|
{ [Sequelize.Op.like]: `%${value}%`.toLowerCase() },
|
|
);
|
|
}
|
|
};
|