Skip to content

Commit

Permalink
feat: handle comma , query and esacaping \, and \|
Browse files Browse the repository at this point in the history
- comma query in `getStringQuery`
- handle `\,` in string and name query
- handle `\|` in token query
From : http://www.hl7.org/fhir/search.html#escaping
  • Loading branch information
Chinlinlee committed Feb 1, 2022
1 parent c0e7969 commit 1a5bef6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
25 changes: 20 additions & 5 deletions models/FHIR/queryBuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,21 @@ const _ = require('lodash');
const moment = require('moment');
const momentTimezone = require('moment-timezone');
const prefix = ["eq" , "ne" , "lt" , "gt" , "ge" , "le" , "sa" , "eb" , "ap"];

/**
*
* @param {string} value
*/
function getCommaSplitArray(value) {
value = value.replace(/\\,/g, "{COMMA}");
let valueCommaSplit = value.split(",").map(v=> v.replace(/{}/gm,","));
return valueCommaSplit;
}

/**
*
* @param {*} str value
* @param {*} key field name
* @param {string} str value
* @param {string} key field name
* @returns
*/
function stringQuery(str, key) {
Expand Down Expand Up @@ -50,15 +61,18 @@ function stringExact(str) {
* @param {string} type postfix of field e.g. field of parameter of phone in Patient is `telecom` but use query value with `telecom.value`
* @param {string} field
* @param {string} required The fixed system e.g. phone is telecom and email is email
* @param {*} isCodeableConcept if is codeable concept
* @param {boolean} isCodeableConcept if is codeable concept
* @returns
*/
function tokenQuery(item, type, field, required , isCodeableConcept = false) {
let queryBuilder = {};
let system = "";
let value = "";
item = item.replace(/\\\|/gm, "{OR}");
if (item.includes("|")) [system, value] = item.split("|");
else value = item;
system = system.replace(/{}/gm, "|");
value = value.replace(/{}/gm, "|");
if (required) {
system = required;
}
Expand Down Expand Up @@ -144,7 +158,7 @@ function addressQuery(target , key) {
}

function nameQuery(target , key) {
let totalSplit = target.split(/[\s.,]+/);
let totalSplit = getCommaSplitArray(target);
let ors = {$or:[]};

for (let index in totalSplit) {
Expand Down Expand Up @@ -518,5 +532,6 @@ module.exports = {
timingQuery: timingQuery,
quantityQuery : quantityQuery ,
referenceQuery : referenceQuery ,
arrayStringBuild : arrayStringBuild
arrayStringBuild : arrayStringBuild,
getCommaSplitArray: getCommaSplitArray
};
13 changes: 9 additions & 4 deletions models/FHIR/searchParameterQueryHandler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const queryBuild = require('./queryBuild');
const _ = require('lodash');
const { getCommaSplitArray } = require('./queryBuild');

/**
* @example <caption>Example of `address-city` of search parameter of the Patient resource</caption>
Expand Down Expand Up @@ -36,10 +37,14 @@ function getStringQuery(query, paramsSearchFields, queryFieldName) {
$or: []
};
for (let field of paramsSearchFields[queryFieldName]) {
let buildResult = {
[field] : queryBuild.stringQuery(item, field)
};
buildQs.$or.push(buildResult);
let commaSeparatedValue = getCommaSplitArray(item);
for (let index in commaSeparatedValue) {
let value = commaSeparatedValue[index];
let buildResult = {
[field] : queryBuild.stringQuery(value, field)
};
buildQs.$or.push(buildResult);
}
}
query.$and.push({
...buildQs
Expand Down

0 comments on commit 1a5bef6

Please sign in to comment.