module.exports = Self => { /** * Returns a set of allowed values defined on table scheme * @param {String} column - Model or Table column name * @return {Array} - Array of set values */ Self.getSetValues = async function(column) { let model = this.app.models[this.modelName].definition; let properties = model.properties; let columnName; let tableName = this.modelName; let schema = null; if (model.settings && model.settings.mysql) { let tableSplit = model.settings.mysql.table.split('.'); tableName = tableSplit.pop(); schema = tableSplit.pop() || null; } if (properties[column]) { columnName = column; if (properties[column].mysql) columnName = properties[column].mysql.columnName; } let findColumn = Object.keys(properties).find(prop => { return properties[prop].mysql && properties[prop].mysql.columnName === column; }); if (findColumn) columnName = properties[findColumn].mysql.columnName; let type = await this.rawSql( `SELECT DISTINCT column_type FROM information_schema.columns WHERE table_name = ? AND table_schema = IFNULL(?, DATABASE()) AND column_name = ?`, [tableName, schema, columnName] ); if (!type) return; let setValues; setValues = type[0].column_type; setValues = setValues.replace(/set\((.*)\)/i, '$1'); setValues = setValues.replace(/'/g, ''); setValues = setValues.match(new RegExp(/(\w+)+/, 'ig')); let values = []; setValues.forEach(setValue => { values.push({value: setValue}); }); return values; }; };