jqlUtils.js 1.1 KB

1234567891011121314151617181920212223242526272829
  1. const createQuery = (searchStr, searchFieldNames) => {
  2. let result = [];
  3. if (searchStr) {
  4. for (let searchFieldName of searchFieldNames) {
  5. //result[searchFieldName] = searchFieldName === '_id' ? searchStr : `/${searchStr}/`;
  6. result.push({ [searchFieldName]: searchFieldName === '_id' ? searchStr : `/${searchStr}/` });
  7. }
  8. }
  9. return result.length == 0 ? {} : { $or: result };
  10. }
  11. const createQueryExt = (searchQuery = {}, queryExt = {}) => {
  12. if (!queryExt)
  13. return searchQuery;
  14. return { $and: [searchQuery, queryExt] };
  15. }
  16. const createQueryPaging = (fromPage, pageSize) => {
  17. let result = {};
  18. if (fromPage !== undefined && pageSize !== undefined) {
  19. result["skip"] = [fromPage * pageSize];
  20. result["limit"] = [pageSize];
  21. }
  22. return result;
  23. }
  24. export const createFullQuery = ({ searchStr, searchFieldNames, queryExt = {} }, { fromPage, pageSize } = {}) => {
  25. return { q: JSON.stringify([createQueryExt(createQuery(searchStr, searchFieldNames), queryExt), createQueryPaging(fromPage, pageSize)]) };
  26. }