@nsshunt/stsfhirpg 1.2.13 → 1.2.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7125,6 +7125,7 @@ class SearchParameterManager {
7125
7125
  redis;
7126
7126
  alreadyComplete = null;
7127
7127
  options;
7128
+ cache = {};
7128
7129
  constructor(options) {
7129
7130
  this.options = options;
7130
7131
  }
@@ -7232,451 +7233,62 @@ class SearchParameterManager {
7232
7233
  console.log(`SearchParameterManager(): redis closed`);
7233
7234
  }
7234
7235
  };
7235
- /*
7236
- private GetTypeKey(typeName: string) {
7237
- return `stsfhir_type_${typeName}`;
7238
- }
7239
-
7240
- private GetSearchParameterByFullUrlKey(fullUrl: string) {
7241
- return `stsfhir_sp_${fullUrl}`;
7242
- }
7243
-
7244
- private GetResourceKey(resourceType: string) {
7245
- return `stsfhir_resource_${resourceType}`;
7246
- }
7247
-
7248
- private GetSearchParameterByResourceTypeKey(resourceType: string) {
7249
- return `stsfhir_sp_resource_${resourceType}`;
7250
- }
7251
-
7252
- private GetSTSCustomSearchParameterByResourceTypeKey(resourceType: string) {
7253
- return `stsfhir_stscustom_sp_resource_${resourceType}`;
7254
- }
7255
- */
7236
+ #searchParametersByResourceTypeKey = `__stsfhir__searchParametersByResourceType`;
7237
+ #searchParametersByUrlKey = `__stsfhir__searchParametersByUrl`;
7238
+ #resourcesKey = `__stsfhir__resources`;
7239
+ #typesKey = `__stsfhir__types`;
7256
7240
  #LoadDefinitions = async (resources) => {
7257
7241
  console.log("Data Load Starting ...");
7258
7242
  for (const [key, value] of Object.entries(resources.searchParametersByResourceType)) {
7259
- await this.redis.hSet(`__stsfhir__searchParametersByResourceType`, key, JSON.stringify(value));
7243
+ await this.redis.hSet(this.#searchParametersByResourceTypeKey, key, JSON.stringify(value));
7260
7244
  }
7261
7245
  for (const [key, value] of Object.entries(resources.searchParametersByUrl)) {
7262
- await this.redis.hSet(`__stsfhir__searchParametersByUrl`, key, JSON.stringify(value));
7246
+ await this.redis.hSet(this.#searchParametersByUrlKey, key, JSON.stringify(value));
7263
7247
  }
7264
7248
  for (const [key, value] of Object.entries(resources.resources)) {
7265
- await this.redis.hSet(`__stsfhir__resources`, key, JSON.stringify(value));
7249
+ await this.redis.hSet(this.#resourcesKey, key, JSON.stringify(value));
7266
7250
  }
7267
7251
  for (const [key, value] of Object.entries(resources.types)) {
7268
- await this.redis.hSet(`__stsfhir__types`, key, JSON.stringify(value));
7252
+ await this.redis.hSet(this.#typesKey, key, JSON.stringify(value));
7269
7253
  }
7270
7254
  console.log("All Done ***********************************************************************************");
7271
7255
  };
7272
7256
  GetSearchParamFromResourceType = async (resourceType) => {
7273
- const retVal = await this.redis.hGet(`__stsfhir__searchParametersByResourceType`, resourceType);
7274
- return JSON.parse(retVal);
7257
+ const key = `${this.#searchParametersByResourceTypeKey}_${resourceType}`;
7258
+ if (this.cache[key]) {
7259
+ return this.cache[key];
7260
+ }
7261
+ const retVal = await this.redis.hGet(this.#searchParametersByResourceTypeKey, resourceType);
7262
+ this.cache[key] = JSON.parse(retVal);
7263
+ return this.cache[key];
7275
7264
  };
7276
7265
  GetType = async (typeName) => {
7277
- const retVal = await this.redis.hGet(`__stsfhir__types`, typeName);
7278
- return JSON.parse(retVal);
7266
+ const key = `${this.#typesKey}_${typeName}`;
7267
+ if (this.cache[key]) {
7268
+ return this.cache[key];
7269
+ }
7270
+ const retVal = await this.redis.hGet(this.#typesKey, typeName);
7271
+ this.cache[key] = JSON.parse(retVal);
7272
+ return this.cache[key];
7279
7273
  };
7280
7274
  GetResource = async (resourceType) => {
7281
- const retVal = await this.redis.hGet(`__stsfhir__resources`, resourceType);
7282
- return JSON.parse(retVal);
7275
+ const key = `${this.#resourcesKey}_${resourceType}`;
7276
+ if (this.cache[key]) {
7277
+ return this.cache[key];
7278
+ }
7279
+ const retVal = await this.redis.hGet(this.#resourcesKey, resourceType);
7280
+ this.cache[key] = JSON.parse(retVal);
7281
+ return this.cache[key];
7283
7282
  };
7284
7283
  GetSearchParam = async (url) => {
7285
- const retVal = await this.redis.hGet(`__stsfhir__searchParametersByUrl`, url);
7286
- return JSON.parse(retVal);
7284
+ const key = `${this.#searchParametersByUrlKey}_${url}`;
7285
+ if (this.cache[key]) {
7286
+ return this.cache[key];
7287
+ }
7288
+ const retVal = await this.redis.hGet(this.#searchParametersByUrlKey, url);
7289
+ this.cache[key] = JSON.parse(retVal);
7290
+ return this.cache[key];
7287
7291
  };
7288
- /*
7289
- GetSearchParams = async (): Promise<SearchParameter[]> => {
7290
- const retVal = await this.redis!.hGetAll(`__stsfhir__searchParametersByUrl`);
7291
- const searchParameters: SearchParameter[] = [ ];
7292
- for (const [key, value] of Object.entries(retVal)) {
7293
- searchParameters.push(JSON.parse(value));
7294
- }
7295
- return searchParameters;
7296
- }
7297
- */
7298
- /*
7299
- private GetTypeKey(typeName: string) {
7300
- return `stsfhir_type_${typeName}`;
7301
- }
7302
-
7303
- private GetSearchParameterByFullUrlKey(fullUrl: string) {
7304
- return `stsfhir_sp_${fullUrl}`;
7305
- }
7306
-
7307
- private GetResourceKey(resourceType: string) {
7308
- return `stsfhir_resource_${resourceType}`;
7309
- }
7310
-
7311
- private GetSearchParameterByResourceTypeKey(resourceType: string) {
7312
- return `stsfhir_sp_resource_${resourceType}`;
7313
- }
7314
-
7315
- private GetSTSCustomSearchParameterByResourceTypeKey(resourceType: string) {
7316
- return `stsfhir_stscustom_sp_resource_${resourceType}`;
7317
- }
7318
-
7319
- // Remove ( ) when either are present
7320
- private _RemoveOuterParentheses = (input: string): string => {
7321
- return input.replace(/^\(|\)$/g, '');
7322
- }
7323
-
7324
- // Remove ( ) only when both are present
7325
- private _RemoveSurroundingParentheses = (input: string): string => {
7326
- return input.replace(/^\((.*)\)$/, '$1').trim();
7327
- }
7328
- */
7329
- /*
7330
- NamingSystem derived-from reference NamingSystem.relatedArtifact.where(type='derived-from').resource
7331
-
7332
- NamingSystem predecessor reference NamingSystem.relatedArtifact.where(type='predecessor').resource
7333
-
7334
- ClinicalUseDefinition product reference ClinicalUseDefinition.subject.where(resolve() is MedicinalProductDefinition)
7335
-
7336
- OrganizationAffiliation email token OrganizationAffiliation.contact.telecom.where(system='email')
7337
- OrganizationAffiliation phone token OrganizationAffiliation.contact.telecom.where(system='phone')
7338
-
7339
- PackagedProductDefinition biological reference PackagedProductDefinition.packaging.containedItem.item.reference
7340
- PackagedProductDefinition contained-item reference PackagedProductDefinition.packaging.containedItem.item.reference
7341
-
7342
- Patient deceased token Patient.deceased.exists() and Patient.deceased != false
7343
- Practitioner deceased token Practitioner.deceased.exists() and Practitioner.deceased != false
7344
-
7345
- Location contains special Location.extension('http://hl7.org/fhir/StructureDefinition/location-boundary-geojson').value
7346
-
7347
- QuestionnaireResponse item-subject reference QuestionnaireResponse.item.where(extension('http://hl7.org/fhir/StructureDefinition/questionnaireresponse-isSubject').exists()).answer.valueReference
7348
- */
7349
- /*
7350
- private _ProcessExpressions = (resourceName: string, SP_EXPRESSION: string): any => {
7351
- let expressions: any[] = [];
7352
-
7353
- //SP_EXPRESSION = this.#RemoveSurroundingParentheses(val2.expression);
7354
- expressions = SP_EXPRESSION.split('|').map(e => e.trim());
7355
-
7356
- expressions = expressions.map(e => this._RemoveSurroundingParentheses(e).trim());
7357
-
7358
- if (resourceName !== '') {
7359
- expressions = expressions.filter(e => e.split('.')[0].trim().localeCompare(resourceName) === 0);
7360
- }
7361
-
7362
- let original_expressions = [ ...expressions ];
7363
-
7364
- // ofType processing
7365
- expressions = expressions.map((e: string) => {
7366
- let capturedTypeOf = undefined;
7367
- return {
7368
- path: e.replace(
7369
- /\.ofType\(([^)]+)\)/g,
7370
- (_: string, typeName: string) => {
7371
- capturedTypeOf = true;
7372
- return typeName.charAt(0).toUpperCase() + typeName.slice(1)
7373
- }),
7374
- typeOf: capturedTypeOf
7375
- }
7376
- });
7377
-
7378
- //@@ exists processing here is more or less hard coded to the deceased attribute.
7379
- //@@ in future, we need an actual fhir language parser.
7380
- expressions.forEach(expression => {
7381
- try {
7382
- const sp = expression as ISearchParamExpressionRecord;
7383
- if (sp.path.indexOf('exists()') >= 0) {
7384
- // Person.deceased.exists() and Person.deceased != false
7385
- const parts = sp.path.split(' ');
7386
- const i = parts[0].indexOf('exists()');
7387
- if (i >= 0) {
7388
- let fieldExistsEx = parts[0].substring(0, i-1);
7389
- let op = parts[1];
7390
- let fieldTestEx = parts[2];
7391
- let comparator = parts[3];
7392
- let value = parts[4];
7393
-
7394
- const fieldExistsParts = fieldExistsEx.split('.');
7395
- let fieldExists = fieldExistsParts.slice(1).join('.');
7396
-
7397
- const fieldTestParts = fieldTestEx.split('.');
7398
- let fieldTest = fieldTestParts.slice(1).join('.');
7399
-
7400
- sp.exists = {
7401
- fieldExists: [ `${fieldExists}Boolean`, `${fieldExists}DataTime` ], // [x]Boolean or [x]DateTime
7402
- op,
7403
- fieldTest: [ `${fieldTest}Boolean`, `${fieldTest}DataTime` ],
7404
- comparator,
7405
- value: [ value, undefined ]
7406
- }
7407
- }
7408
- }
7409
- } catch (error) {
7410
-
7411
- }
7412
- });
7413
-
7414
- // where(resolve()) processing
7415
- let capturedType: string | undefined;
7416
- expressions = expressions.map((obj: any) => {
7417
- capturedType = undefined;
7418
- const { path, typeOf, exists } = obj;
7419
- return {
7420
- path: path.replace(
7421
- /\.where\(resolve\(\)\s+is\s+([^)]+)\)/,
7422
- (_: string, typeName: string) => {
7423
- capturedType = typeName;
7424
- //return ` // reference = ${capturedType}`; // remove the .where(...) part (you can put ` // ${capturedType}` if you want to keep a comment)
7425
- return '';
7426
- }),
7427
- typeOf,
7428
- reference: capturedType,
7429
- exists
7430
- }
7431
- });
7432
-
7433
- // .where processing
7434
- // The element before the .where will be an array. So this test checks which element(s) from that array are satisifed.
7435
- let capturedWhere: any = undefined;
7436
- expressions = expressions.map((obj: any) => {
7437
- capturedWhere = undefined;
7438
- const { path, reference, typeOf, exists } = obj;
7439
- const retVal: any = {
7440
- path: path.replace(/([^.]+)\.where\(([^=]+)='([^']+)'\)/g, (_: string, field: string, subfield: string, value: string) => {
7441
- capturedWhere = { field, subfield, value }
7442
- return field;
7443
- }),
7444
- typeOf,
7445
- reference,
7446
- exists
7447
- }
7448
- if (capturedWhere) {
7449
- capturedWhere.full = `${capturedWhere.field}[*].${capturedWhere.subfield}=${capturedWhere.value}`;
7450
- retVal.where = capturedWhere;
7451
- }
7452
- return retVal;
7453
- });
7454
-
7455
-
7456
- return {
7457
- expressions,
7458
- original_expressions
7459
- };
7460
- }
7461
-
7462
- #LoadDefinitions = async (): Promise<void> => {
7463
- try {
7464
- let usePath = '';
7465
- if (fs.existsSync(`${specPath1}${resourcesPath}`)) {
7466
- usePath = specPath1;
7467
- } else {
7468
- usePath = specPath2;
7469
- }
7470
- const __resources: any = JSON.parse(fs.readFileSync(`${usePath}${resourcesPath}`, 'utf-8'));
7471
- const __types: any = JSON.parse(fs.readFileSync(`${usePath}${typesPath}`, 'utf-8'));
7472
- const __searchParams: any = JSON.parse(fs.readFileSync(`${usePath}${searchParamsPath}`, 'utf-8'));
7473
-
7474
- for (let i=0; i < (__resources.entry as any[]).length; i++) {
7475
- const entry = __resources.entry[i];
7476
- const resource: any = entry.resource;
7477
- if (resource?.snapshot?.element) {
7478
- const rt = resource.type as string;
7479
- await this.redis!.set(this.GetResourceKey(rt), JSON.stringify(resource.snapshot.element));
7480
- }
7481
- };
7482
-
7483
- for (let i=0; i < (__types.entry as any[]).length; i++) {
7484
- const entry = __types.entry[i];
7485
- const type = entry.resource;
7486
- if (type?.snapshot?.element) {
7487
- await this.redis!.set(this.GetTypeKey(type.type), JSON.stringify(type.snapshot.element));
7488
- }
7489
- };
7490
-
7491
- for (let i=0; i < (__searchParams.entry as any[]).length; i++) {
7492
- const entry = __searchParams.entry[i];
7493
- await this.redis!.set(this.GetSearchParameterByFullUrlKey(entry.fullUrl), JSON.stringify(entry.resource));
7494
- const { code, base } = entry.resource;
7495
- if (base) {
7496
- for (let j=0; j < base.length; j++) {
7497
- const resourceType = base[j];
7498
- await this.redis!.hSet(this.GetSearchParameterByResourceTypeKey(resourceType), code, JSON.stringify(entry.resource));
7499
- }
7500
- }
7501
- };
7502
-
7503
- //await this.#LoadDefinitionsPass2(__searchParams);
7504
-
7505
- console.log(`All completed ...`);
7506
- } catch (error) {
7507
- console.error(error);
7508
- throw error;
7509
- }
7510
- }
7511
- */
7512
- /*
7513
- #LoadDefinitionsPass2 = async (__searchParams: any): Promise<void> => {
7514
- try {
7515
- for (let i=0; i < (__searchParams.entry as any[]).length; i++) {
7516
-
7517
- console.log(`Processing (i) entry: [${i}]`);
7518
-
7519
- if (i === 47 ) {
7520
- console.log(`here...`)
7521
- }
7522
-
7523
- const entry = __searchParams.entry[i];
7524
-
7525
- const { id, url, code, base, type, expression, component, target } = entry.resource;
7526
-
7527
- if (base) {
7528
- for (let j=0; j < base.length; j++) {
7529
- const resourceType = base[j];
7530
- const SP_ID = id;
7531
- const SP_NAME = code; // code is the true SP name (but in the spec, name is usually the same as code)
7532
- const SP_TYPE = type;
7533
- const SP_URL = url;
7534
- const SP_TARGET = target;
7535
- let SP_EXPRESSION: string = expression;
7536
- let expressions: any[] = [];
7537
- let original_expressions: any[] = [];
7538
- if (SP_EXPRESSION) {
7539
- const retVal = this._ProcessExpressions(code, SP_EXPRESSION);
7540
- expressions = retVal.expressions;
7541
- original_expressions = retVal.original_expressions;
7542
- }
7543
-
7544
- const record: ISearchParamRecord = {
7545
- SP_ID,
7546
- SP_NAME,
7547
- SP_TYPE,
7548
- SP_URL,
7549
- SP_TARGET,
7550
- expressions,
7551
- original_expressions,
7552
- SP_DEFINITION: entry.resource // this.resourceHelper.GetSearchParam(SP_URL)
7553
- };
7554
-
7555
- for (let i=0; i < record.expressions.length; i++) {
7556
- //console.log(record.expressions[i]);
7557
- const rf = await this.GetResourceField(resourceType, record.expressions[i].path);
7558
- if (rf) {
7559
- expressions[i].RES_FIELD = rf;
7560
- }
7561
- }
7562
-
7563
- if (component) {
7564
- record.component = [ ...component ];
7565
- for (let k=0; k < record.component.length; k++) {
7566
- const c = record.component[k] as any;
7567
- //c.SP_RAW_PATHS = (c.expression.split('|') as any[]).map(rp => rp.trim());
7568
- //c.SP_PATHS = (c.SP_RAW_PATHS as any[]).map(rawPath => this.ResolveFhirChoiceType(rawPath));
7569
- const retVal = this._ProcessExpressions('', c.expression);
7570
- c.expressions = retVal.expressions;
7571
- c.original_expressions = retVal.original_expressions;
7572
-
7573
- let def = null;
7574
- const foundSearchParameter = await this.redis!.get(this.GetSearchParameterByFullUrlKey(c.definition));
7575
- if (foundSearchParameter) {
7576
- def = JSON.parse(foundSearchParameter);
7577
- }
7578
- //const def = await this.GetSearchParam(c.definition); //@@ fails here ...
7579
-
7580
- if (def && def.type) {
7581
- c.type = def.type
7582
- }
7583
- }
7584
- }
7585
-
7586
- await this.redis!.hSet(this.GetSTSCustomSearchParameterByResourceTypeKey(resourceType),
7587
- code, JSON.stringify(record));
7588
-
7589
- console.log(`Loaded...(j): [${j}]`);
7590
- }
7591
- console.log(`All Loaded...`);
7592
- }
7593
- console.log(`Loaded...(i): [${i}]`);
7594
- };
7595
- console.log(`Loaded...`);
7596
- } catch (error) {
7597
- console.error(error);
7598
- throw error;
7599
- }
7600
- }
7601
- */
7602
- /*
7603
- GetType = async (typeName: string): Promise<any[]> => {
7604
- await this.EnsureSearchParameterDataLoaded();
7605
- const foundType = await this.redis!.get(this.GetTypeKey(typeName));
7606
- if (foundType) {
7607
- return JSON.parse(foundType);
7608
- }
7609
- return [ ];
7610
- }
7611
-
7612
- GetResource = async (resourceType: string): Promise<any[]> => {
7613
- await this.EnsureSearchParameterDataLoaded();
7614
- const foundResourceType = await this.redis!.get(this.GetResourceKey(resourceType));
7615
- if (foundResourceType) {
7616
- return JSON.parse(foundResourceType);
7617
- }
7618
- return [ ];
7619
- }
7620
-
7621
- GetResourceField = async (resourceType: string, fieldPath: string): Promise<any> => {
7622
- const resource: any[] = await this.GetResource(resourceType);
7623
- if (resource) {
7624
- const foundRecord = resource.filter(o => o.id === fieldPath);
7625
- if (foundRecord.length > 0) {
7626
- return foundRecord[0];
7627
- }
7628
- return undefined;
7629
- }
7630
- }
7631
-
7632
- GetSearchParam = async (fullUrl: string): Promise<any> => {
7633
- await this.EnsureSearchParameterDataLoaded();
7634
- const foundSearchParameter = await this.redis!.get(this.GetSearchParameterByFullUrlKey(fullUrl));
7635
- if (foundSearchParameter) {
7636
- return JSON.parse(foundSearchParameter);
7637
- }
7638
- return null;
7639
- }
7640
-
7641
- GetSearchParamsByResourceType = async (resourceType: string): Promise<SearchParameter[]> => {
7642
- await this.EnsureSearchParameterDataLoaded();
7643
- const all = await this.redis!.hGetAll(this.GetSearchParameterByResourceTypeKey(resourceType));
7644
- const parsed = Object.values(all).map(v => JSON.parse(v));
7645
- return parsed;
7646
- }
7647
-
7648
- GetSTSCustomSearchParametersByResourceType = async (resourceType: string): Promise<ISearchParamRecord[]> => {
7649
- await this.EnsureSearchParameterDataLoaded();
7650
- const all = await this.redis!.hGetAll(this.GetSTSCustomSearchParameterByResourceTypeKey(resourceType));
7651
- const parsed = Object.values(all).map(v => JSON.parse(v));
7652
- return parsed;
7653
- }
7654
-
7655
- GetSearchParamFromResourceTypeUrl = async (resourceType: string, url: string): Promise<ISearchParamRecord | undefined> => {
7656
- const customSearchParameters = await this.GetSTSCustomSearchParametersByResourceType(resourceType);
7657
- const filterByNameAndUrl = customSearchParameters.filter(rt => rt.SP_URL.localeCompare(url) === 0);
7658
- if (filterByNameAndUrl.length > 0) {
7659
- return filterByNameAndUrl[0];
7660
- }
7661
- }
7662
-
7663
- GetSearchParamFromResourceType = async (resourceType: string, name: string): Promise<ISearchParamRecord | undefined> => {
7664
- const customSearchParameters = await this.GetSTSCustomSearchParametersByResourceType(resourceType);
7665
- const filterByName = customSearchParameters.filter(rt => rt.SP_NAME.localeCompare(name) === 0);
7666
- if (filterByName.length > 0) {
7667
- return filterByName[0];
7668
- }
7669
- }
7670
-
7671
- GetPathsFromResourceType = async (resourceType: string, name: string): Promise<string[]> => {
7672
- const customSearchParameters = await this.GetSTSCustomSearchParametersByResourceType(resourceType);
7673
- const filterByName = customSearchParameters.filter(rt => rt.SP_NAME.localeCompare(name) === 0);
7674
- if (filterByName.length > 0) {
7675
- return filterByName[0].expressions.map(e => e.path)
7676
- }
7677
- return [ ];
7678
- }
7679
- */
7680
7292
  }
7681
7293
  const specPath1 = "./dist/";
7682
7294
  const specPath2 = "./node_modules/@nsshunt/stsfhirpg/dist/";