const mongoose = require('mongoose/browser'); <% if datatype.downcase == 'component' || datatype.downcase == 'facilitylocation' || datatype.downcase == 'diagnosiscomponent'%> const Code = require('../basetypes/Code'); const Interval = require('../basetypes/Interval'); const Quantity = require('../basetypes/Quantity'); const DateTime = require('../basetypes/DateTime'); const QDMDate = require('../basetypes/QDMDate'); const Any = require('../basetypes/Any'); <%if datatype.downcase == 'component'%> const [Schema] = [mongoose.Schema];<% end %> <% elsif datatype.downcase == 'resultcomponent'%> const { ComponentSchemaFunction } = require('./Component'); const Code = require('../basetypes/Code'); const Interval = require('../basetypes/Interval'); const Quantity = require('../basetypes/Quantity'); const DateTime = require('../basetypes/DateTime'); const QDMDate = require('../basetypes/QDMDate'); const Any = require('../basetypes/Any'); <% elsif datatype.downcase == 'entity'%> const { IdentifierSchema } = require('./Identifier'); const Code = require('../basetypes/Code'); const Interval = require('../basetypes/Interval'); const Quantity = require('../basetypes/Quantity'); const DateTime = require('../basetypes/DateTime'); const QDMDate = require('../basetypes/QDMDate'); const Any = require('../basetypes/Any'); const [Schema] = [mongoose.Schema]; <% elsif datatype.downcase == 'carepartner' || datatype.downcase == 'organization' || datatype.downcase == 'practitioner' || datatype.downcase == 'patiententity' || datatype.downcase == 'location'%> const { EntitySchemaFunction } = require('./Entity'); const Code = require('../basetypes/Code'); const Interval = require('../basetypes/Interval'); const Quantity = require('../basetypes/Quantity'); const DateTime = require('../basetypes/DateTime'); const QDMDate = require('../basetypes/QDMDate'); const Any = require('../basetypes/Any'); <% else %> <% unless datatype.downcase == 'identifier' %>const { IdentifierSchema } = require('./attributes/Identifier');<% end %> const { DataElementSchema } = require('./basetypes/DataElement'); const Code = require('./basetypes/Code'); const Interval = require('./basetypes/Interval'); const Quantity = require('./basetypes/Quantity'); const DateTime = require('./basetypes/DateTime'); const QDMDate = require('./basetypes/QDMDate'); const Any = require('./basetypes/Any'); const AnyEntity = require('./basetypes/AnyEntity'); const { ComponentSchema } = require('./attributes/Component'); const { FacilityLocationSchema } = require('./attributes/FacilityLocation'); const { EntitySchema } = require('./attributes/Entity'); <% unless datatype.downcase != 'encounterperformed' %>const { DiagnosisComponentSchema } = require('./attributes/DiagnosisComponent');<% end %> <% end %> const [Number, String] = [ mongoose.Schema.Types.Number, mongoose.Schema.Types.String, ]; <% if datatype.downcase == 'component' || datatype.downcase == 'facilitylocation' || datatype.downcase == 'entity' || datatype.downcase == 'diagnosiscomponent'%> const <%= datatype -%>Schema = new mongoose.Schema({ <%- for attribute in attrs_with_extras -%> <% if attribute[:default] %><%= attribute[:name] %>: { type: <%= TYPE_LOOKUP_JS[attribute[:type]] -%>, default: '<%= attribute[:default] %>' },<% else %><%= attribute[:name] %>: <%= TYPE_LOOKUP_JS[attribute[:type]] -%>,<%- end %> <%- end %> }); <% elsif datatype.downcase == 'resultcomponent'%> const <%= datatype -%>Schema = ComponentSchemaFunction({ <%- for attribute in attrs_with_extras -%> <% if attribute[:default] %><%= attribute[:name] %>: { type: <%= TYPE_LOOKUP_JS[attribute[:type]] -%>, default: '<%= attribute[:default] %>' },<% else %><%= attribute[:name] %>: <%= TYPE_LOOKUP_JS[attribute[:type]] -%>,<%- end %> <%- end %> }); <% elsif datatype.downcase == 'carepartner' || datatype.downcase == 'organization' || datatype.downcase == 'practitioner' || datatype.downcase == 'patiententity' || datatype.downcase == 'location'%> const <%= datatype -%>Schema = EntitySchemaFunction({ <%- for attribute in attrs_with_extras -%> <% if attribute[:default] %><%= attribute[:name] %>: { type: <%= TYPE_LOOKUP_JS[attribute[:type]] -%>, default: '<%= attribute[:default] %>' },<% else %><%= attribute[:name] %>: <%= TYPE_LOOKUP_JS[attribute[:type]] -%>,<%- end %> <%- end %> }); <% else %> const <%= datatype -%>Schema = DataElementSchema({ <%- for attribute in attrs_with_extras -%> <% if attribute[:default] %><%= attribute[:name] %>: { type: <%= TYPE_LOOKUP_JS[attribute[:type]] -%>, default: '<%= attribute[:default] %>' },<% else %><%= attribute[:name] %>: <%= TYPE_LOOKUP_JS[attribute[:type]] -%>,<%- end %> <%- end %> }); <% end %> module.exports.<%= datatype %>Schema = <%= datatype %>Schema; class <%= datatype %> extends mongoose.Document { constructor(object) { super(object, <%= datatype %>Schema); this._type = 'QDM::<%= datatype %>'; <%- for attribute in attrs_with_extras -%> <%- if RESERVED_WORDS[attribute[:name].to_s] -%> if (object && object.<%= RESERVED_WORDS[attribute[:name].to_s] %>) { this.<%= attribute[:name] %> = object.<%= RESERVED_WORDS[attribute[:name].to_s] %>; } <%- end -%> <%- end -%> } } <% if datatype.downcase == 'entity' || datatype.downcase == 'component'%> function <%= datatype %>SchemaFunction(add, options) { const extended = new Schema({ <%- for attribute in attrs_with_extras.select{|a| a[:name] != 'id'} -%> <% if attribute[:default] %><%= attribute[:name] %>: { type: <%= TYPE_LOOKUP_JS[attribute[:type]] -%>, default: '<%= attribute[:default] %>' },<% else %><%= attribute[:name] %>: <%= TYPE_LOOKUP_JS[attribute[:type]] -%>,<%- end %> <%- end %> <%- if datatype.downcase == 'entity'%> id: { type: String, default() { return this._id ? this._id.toString() : mongoose.Types.ObjectId().toString(); }, }, <%- end %> }, options); if (add) { extended.add(add); } /* eslint no-underscore-dangle: 0 */ extended.methods._is = function _is(typeSpecifier) { return this._typeHierarchy().some( t => t.type === typeSpecifier.type && t.name === typeSpecifier.name ); }; extended.methods._typeHierarchy = function _typeHierarchy() { const typeName = this._type.replace(/QDM::/, ''); const ver = this.qdmVersion.replace('.', '_'); return [ { name: `{urn:healthit-gov:qdm:v${ver}}${typeName}`, type: 'NamedTypeSpecifier', }, ]; }; return extended; } <% end %> module.exports.<%= datatype %> = <%= datatype %>; <% if datatype.downcase == 'entity' || datatype.downcase == 'component'%>module.exports.<%= datatype %>SchemaFunction = <%= datatype %>SchemaFunction;<% end %>