Created
May 13, 2020 02:39
-
-
Save juliusdejon/2251192a1c740b01cb062728d96af1b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Merge Search fields to form a query. | |
* @param {Array, String} | |
* @returns {Object} | |
*/ | |
export function merge_searchable_fields(searchable_fields, q) { | |
let query = {}; | |
const searchQuery = searchable_fields.map((field) => ({ | |
[field]: { | |
[Op.like]: `%${q}%`, | |
}, | |
})); | |
query = { | |
[Op.or]: searchQuery, | |
}; | |
return query; | |
} | |
/** | |
* Convert Operation. | |
* eg: Appends Op to an Operation | |
* see Sequelize Op | |
* | |
* @param {String} gte | |
* @returns {Symbol} Op.gte | |
*/ | |
function _convert_operation(operation) { | |
return Op[operation]; | |
} | |
/** | |
* Merge Filter fields to form a query. | |
* @param {Object} | |
* @returns {Object} | |
*/ | |
export function merge_filters(filters) { | |
let query = {}; | |
Object.keys(filters).forEach((field) => { | |
query[field] = []; | |
const conditions = filters[field]; | |
Object.keys(conditions).forEach((operation) => { | |
const converted_operation = _convert_operation(operation); | |
switch (operation) { | |
case "like": | |
case "ILike": | |
case "notLike": | |
case "notILike": { | |
conditions[operation] = `%${conditions[operation]}%`; | |
break; | |
} | |
case "lte": { | |
conditions[operation] = `${conditions[operation]} 23:59:59`; | |
break; | |
} | |
default: | |
conditions[operation]; | |
} | |
query[field] = { | |
...query[field], | |
[converted_operation]: conditions[operation], | |
}; | |
}); | |
}); | |
return query; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage