Stream: javascript
Topic: Object models in JS
Chris Moesel (Nov 06 2018 at 21:58):
Are there any existing projects that have object models in JS allowing for simpler creation and manipulation of models? I realize it is not rocket science to use JSON directly, but I'm wondering if there are any models with helper functions for accessing extensions more easily or finding elements by slice name, etc.
Marco Descher (Jan 25 2019 at 08:16):
I have the same question - it is really tedious to do binding to the json parsed object - e.g. if you have to bind to a specific telecom property ...
nicola (RIO/SS) (Jan 25 2019 at 08:23):
We used something like this https://github.com/niquola/2way
nicola (RIO/SS) (Jan 25 2019 at 08:23):
See samples in https://github.com/niquola/2way/blob/master/test/index.spec.js
Marco Descher (Jan 25 2019 at 08:31):
Thats nice, but is it capable of handling newly added values? E.g. if I add a mobile number in form which then has to be added to the telcom array?
nicola (RIO/SS) (Jan 25 2019 at 08:32):
you always can extend it - in real projects this lib was inlinded and extended for specific needs
nicola (RIO/SS) (Jan 25 2019 at 08:34):
here is our more advanced lib - but it's in clojure - https://github.com/abcdw/ironhide
Morten Ernebjerg (Jan 25 2019 at 08:41):
The Clinical Meteor project also has explicit JS models for Resources: https://github.com/clinical-meteor/ . Never worked with them, though, so I'm not sure how "thick" they are.
Marco Descher (Jan 25 2019 at 09:00):
Thanks for the links ... I'm pretty new to javascript (seasoned java developer) and really missing the formal classes at the moment. Trying to bind in vue.js! It seems like there are some projects, but more the less everybody has it's own implementation despite the standardization.
Morten Ernebjerg (Jan 25 2019 at 09:14):
We've found that working with TypeScript rather than JS directly helps a lot since it gives you the types while still being interoperable with JS. There are pre-defined (STU3) types on NPM: https://www.npmjs.com/package/@types/fhir .
Marco Descher (Jan 25 2019 at 09:17):
Do you know how I can create such a Typescript Patient object given the JSON representation delivered by a FHIR server?
Morten Ernebjerg (Jan 25 2019 at 09:32):
I suppose one way would be to start from the official JSON Schemata for the various resources (see also this thread) and then generate the types from there (random Google-hit for the latter: https://www.npmjs.com/package/json-schema-to-typescript). As for the case of an arbitrary StructureDefinition (e.g. a Profile on a base resource), there may be tools to automatically generate JSON schema from them, I know there's been some discussion on this - @nicola (RIO/SS) do you know what the current state on that is?
Morten Ernebjerg (Jan 25 2019 at 09:38):
JSON schemata for base resources are available on the download page, e.g. for R4 http://build.fhir.org/downloads Oops, now it's of course http://hl7.org/fhir/downloads.html
nicola (RIO/SS) (Jan 25 2019 at 09:57):
Guys do not complicate things :) Use plain js objects :)
nicola (RIO/SS) (Jan 25 2019 at 09:58):
It's much more fun
nicola (RIO/SS) (Jan 25 2019 at 09:59):
Types will not help you to flatten FHIR resources for UI manipulation
nicola (RIO/SS) (Jan 25 2019 at 10:00):
even make it worse
Morten Ernebjerg (Jan 25 2019 at 10:08):
I get nervous twitches every time I manipulate complicated JS objects - maybe I'm getting old ;) Anyway, I live mainly in the backend.
Grahame Grieve (Jan 25 2019 at 19:03):
if you want to generate code, the best place to start from is the formal StructureDefinitions - these contain the most knowledge about what is really going on in the objects
Grahame Grieve (Jan 25 2019 at 19:03):
Generating code from the JSON schema would be really hard - it is not geared for code generation at all
Robert Winterbottom (Jan 25 2019 at 19:21):
We found that out the hard way, originally generating classes based on JSON schema but quickly switched to structure definitions. We just updated our generator and have R4 models working with basic getters and setters so once we work out any kinks, they will be up on our github. Not exactly sure what you all are looking for but I can link them here once they are live.
Morten Ernebjerg (Jan 27 2019 at 20:00):
Right, I should clarify that I was only talking about generating TypeScript types, which only give the shape of the objects and contain only basis info from the StructureDefinition (not even the full cardinality info).
Last updated: Apr 12 2022 at 19:14 UTC