Skip to content

Instantly share code, notes, and snippets.

@awatson1978
Last active March 21, 2017 15:06
Show Gist options
  • Save awatson1978/e6f23432c500f14451013b7c20502695 to your computer and use it in GitHub Desktop.
Save awatson1978/e6f23432c500f14451013b7c20502695 to your computer and use it in GitHub Desktop.
FHIR Subscription with Meteor on FHIR

Client

var subscription = {
  criteria: "/Patient?identifier=http://acme.org/patient/123",
  status: 'active',
  channel: { 
    type: 'websocket',
    endpoint: Meteor.absoluteUrl()    
  }
}
Meteor.subscribe("todaysOrders", subscription);

Server

Meteor.publish("todaysOrders", function (subscription){
  if(subscription.status === 'active'){
    if (this.userId) {
      var criteriaObject = parseCriteriaString(subscription.criteria);
      return Mongo.Collections[criteriaObject.collection].find(criteriaObject.query);
    } else {
      return [];
    }
  } else {
    return [];
  }
});

parseCriteriaString = function(criteria){
  var result = {
    collection: '',
    query: {}
  };
  
  // is this a serialized complex object?
  if (criteria && criteria.includes('?')){
    // the base before the ? gets assigned to the collection
    var criteriaArray = criteria.split('?');
    result.query.collection = criteriaArray[0].replace('/', '').trim();
    
    // then we want to look at how many parameters were given
    var paramsArray = [];
    if(criteriaArray && criteriaArray[1]){
      paramsArray = criteriaArray[1].split('&');

      paramsArray.forEach(function(param){
        // for each parameter, figure out the field name and the value
        var fieldArray = param.split('=');

        // some data types require subfields
        if (fieldArray[0] === "identifier"){
          result.query['identifier.value'] = fieldArray[1];
        }
    
        // continue with the rest of the search specification
        // https://www.hl7.org/fhir/search.html
      });
    }    
  } 
  
  console.log('parseCriteriaString', result);
  // deserialize string into mongo query
  return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment