@nsshunt/stsfhirpg 1.2.8 → 1.2.10

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.
@@ -1,9 +1,10 @@
1
1
  import fs from "node:fs";
2
- import { defaultLogger } from "@nsshunt/stsutils";
3
- import { isAbsoluteUrl } from "@nsshunt/stsfhirclient";
2
+ import require$$0, { randomUUID as randomUUID$1 } from "crypto";
3
+ import { Sleep, defaultLogger } from "@nsshunt/stsutils";
4
4
  import { goptions } from "@nsshunt/stsconfig";
5
+ import { createClient } from "redis";
6
+ import { isAbsoluteUrl } from "@nsshunt/stsfhirclient";
5
7
  import cluster from "cluster";
6
- import require$$0 from "crypto";
7
8
  import require$$0$3 from "events";
8
9
  import require$$1$1 from "util";
9
10
  import require$$0$2 from "dns";
@@ -6993,6 +6994,635 @@ function hashReferenceParam(reference) {
6993
6994
  function hashUriParam(uri, lowercase = false) {
6994
6995
  return murmur64Signed(lowercase ? uri.toLowerCase() : uri);
6995
6996
  }
6997
+ const RELEASE_SCRIPT = `
6998
+ if redis.call("get", KEYS[1]) == ARGV[1] then
6999
+ return redis.call("del", KEYS[1])
7000
+ else
7001
+ return 0
7002
+ end
7003
+ `;
7004
+ const RENEW_SCRIPT = `
7005
+ if redis.call("get", KEYS[1]) == ARGV[1] then
7006
+ return redis.call("pexpire", KEYS[1], ARGV[2])
7007
+ else
7008
+ return 0
7009
+ end
7010
+ `;
7011
+ class RedisDistributedLock {
7012
+ client;
7013
+ ttlMs;
7014
+ heartbeatMs;
7015
+ keyPrefix;
7016
+ constructor(client2, options) {
7017
+ this.client = client2;
7018
+ this.ttlMs = options?.ttlMs ?? 3e4;
7019
+ this.heartbeatMs = options?.heartbeatMs ?? Math.floor(this.ttlMs / 3);
7020
+ this.keyPrefix = options?.keyPrefix ?? "lock:";
7021
+ if (this.ttlMs <= 0) {
7022
+ throw new Error("ttlMs must be > 0");
7023
+ }
7024
+ if (this.heartbeatMs <= 0) {
7025
+ throw new Error("heartbeatMs must be > 0");
7026
+ }
7027
+ if (this.heartbeatMs >= this.ttlMs) {
7028
+ throw new Error("heartbeatMs should be less than ttlMs");
7029
+ }
7030
+ }
7031
+ buildKey(name) {
7032
+ return `${this.keyPrefix}${name}`;
7033
+ }
7034
+ async acquire(name, ttlMs) {
7035
+ const key = this.buildKey(name);
7036
+ const token = randomUUID$1();
7037
+ const effectiveTtlMs = ttlMs ?? this.ttlMs;
7038
+ const result2 = await this.client.set(key, token, {
7039
+ NX: true,
7040
+ PX: effectiveTtlMs
7041
+ });
7042
+ if (result2 !== "OK") {
7043
+ return null;
7044
+ }
7045
+ return {
7046
+ key,
7047
+ token,
7048
+ ttlMs: effectiveTtlMs
7049
+ };
7050
+ }
7051
+ async release(lock) {
7052
+ const result2 = await this.client.eval(RELEASE_SCRIPT, {
7053
+ keys: [lock.key],
7054
+ arguments: [lock.token]
7055
+ });
7056
+ return Number(result2) === 1;
7057
+ }
7058
+ async renew(lock, ttlMs) {
7059
+ const effectiveTtlMs = ttlMs ?? lock.ttlMs;
7060
+ const result2 = await this.client.eval(RENEW_SCRIPT, {
7061
+ keys: [lock.key],
7062
+ arguments: [lock.token, String(effectiveTtlMs)]
7063
+ });
7064
+ return Number(result2) === 1;
7065
+ }
7066
+ async isOwner(lock) {
7067
+ const currentValue = await this.client.get(lock.key);
7068
+ return currentValue === lock.token;
7069
+ }
7070
+ async runExclusive(name, task, options) {
7071
+ const lock = await this.acquire(name, options?.ttlMs);
7072
+ if (!lock) {
7073
+ if (options?.onLockNotAcquired) {
7074
+ await options.onLockNotAcquired();
7075
+ }
7076
+ return { acquired: false };
7077
+ }
7078
+ let timer = null;
7079
+ let stopped = false;
7080
+ let renewalError = null;
7081
+ const autoRenew = options?.autoRenew ?? false;
7082
+ const heartbeatMs = options?.heartbeatMs ?? this.heartbeatMs;
7083
+ const stopHeartbeat = () => {
7084
+ stopped = true;
7085
+ if (timer) {
7086
+ clearInterval(timer);
7087
+ timer = null;
7088
+ }
7089
+ };
7090
+ if (autoRenew) {
7091
+ timer = setInterval(async () => {
7092
+ if (stopped) {
7093
+ return;
7094
+ }
7095
+ try {
7096
+ const ok = await this.renew(lock, lock.ttlMs);
7097
+ if (!ok) {
7098
+ renewalError = new Error(
7099
+ `Lost lock ownership while renewing "${lock.key}"`
7100
+ );
7101
+ stopHeartbeat();
7102
+ }
7103
+ } catch (err) {
7104
+ renewalError = err instanceof Error ? err : new Error(String(err));
7105
+ stopHeartbeat();
7106
+ }
7107
+ }, heartbeatMs).unref();
7108
+ }
7109
+ try {
7110
+ const result2 = await task();
7111
+ if (renewalError) {
7112
+ throw renewalError;
7113
+ }
7114
+ return { acquired: true, result: result2 };
7115
+ } finally {
7116
+ stopHeartbeat();
7117
+ try {
7118
+ await this.release(lock);
7119
+ } catch {
7120
+ }
7121
+ }
7122
+ }
7123
+ }
7124
+ class SearchParameterManager {
7125
+ redis;
7126
+ alreadyComplete = null;
7127
+ options;
7128
+ constructor(options) {
7129
+ this.options = options;
7130
+ }
7131
+ EnsureSearchParameterDataLoaded = async () => {
7132
+ if (this.alreadyComplete) {
7133
+ return this.alreadyComplete;
7134
+ }
7135
+ if (!this.redis) {
7136
+ const redisUrl = goptions.imRedisMessageProcessorUrl;
7137
+ console.log(`SearchParameterManager(): redis trying to connect ...`);
7138
+ this.redis = createClient({
7139
+ url: redisUrl
7140
+ });
7141
+ await this.redis.connect();
7142
+ console.log(`SearchParameterManager(): redis connected`);
7143
+ }
7144
+ try {
7145
+ const pollIntervalMs = this.options?.pollIntervalMs ?? 250;
7146
+ const lockTtlMs = this.options?.lockTtlMs ?? 3e4;
7147
+ const autoRenew = this.options?.autoRenew ?? true;
7148
+ const heartbeatMs = this.options?.heartbeatMs ?? 1e4;
7149
+ const completeTtlSeconds = this.options?.completeTtlSeconds;
7150
+ const timeoutMs = this.options?.timeoutMs;
7151
+ const completeKey = `fhir:searchparam:${this.options.name}:complete`;
7152
+ const startedAt = Date.now();
7153
+ const lockManager = new RedisDistributedLock(this.redis, {
7154
+ ttlMs: 3e4,
7155
+ heartbeatMs: 1e4,
7156
+ keyPrefix: "lock:"
7157
+ });
7158
+ while (true) {
7159
+ if (typeof timeoutMs === "number" && timeoutMs > 0) {
7160
+ const elapsedMs = Date.now() - startedAt;
7161
+ if (elapsedMs >= timeoutMs) {
7162
+ throw new Error(
7163
+ `Timed out waiting for search param "${this.options.name}" to be loaded`
7164
+ );
7165
+ }
7166
+ }
7167
+ try {
7168
+ this.alreadyComplete = await this.redis.get(completeKey);
7169
+ if (this.alreadyComplete) {
7170
+ console.log(`ensureSearchParamLoaded():alreadyComplete: PID: [${process.pid}] data loaded: [${this.alreadyComplete}]`);
7171
+ return this.alreadyComplete;
7172
+ }
7173
+ } catch (error) {
7174
+ console.error(error);
7175
+ throw error;
7176
+ }
7177
+ const runResult = await lockManager.runExclusive(
7178
+ `fhir:searchparam:${this.options.name}`,
7179
+ async () => {
7180
+ console.log(`ensureSearchParamLoaded(): PID: [${process.pid}] lock acquired`);
7181
+ this.alreadyComplete = await this.redis.get(completeKey);
7182
+ if (this.alreadyComplete) {
7183
+ console.log(`ensureSearchParamLoaded():completeAfterLock: PID: [${process.pid}] data loaded: [${this.alreadyComplete}]`);
7184
+ return;
7185
+ }
7186
+ try {
7187
+ await this.#LoadDefinitions();
7188
+ } catch (error) {
7189
+ console.error(error);
7190
+ throw error;
7191
+ }
7192
+ const completeValue = JSON.stringify({
7193
+ loaded: true,
7194
+ loadedAt: (/* @__PURE__ */ new Date()).toLocaleString(),
7195
+ name: this.options.name,
7196
+ workerPid: process.pid
7197
+ });
7198
+ console.log(`ensureSearchParamLoaded(): PID: [${process.pid}] data loaded: [${completeValue}]`);
7199
+ if (typeof completeTtlSeconds === "number" && completeTtlSeconds > 0) {
7200
+ await this.redis.set(completeKey, completeValue, {
7201
+ EX: completeTtlSeconds
7202
+ });
7203
+ } else {
7204
+ await this.redis.set(completeKey, completeValue);
7205
+ }
7206
+ this.alreadyComplete = completeValue;
7207
+ },
7208
+ {
7209
+ ttlMs: lockTtlMs,
7210
+ autoRenew,
7211
+ heartbeatMs
7212
+ }
7213
+ );
7214
+ if (runResult.acquired) {
7215
+ if (this.alreadyComplete) {
7216
+ return this.alreadyComplete;
7217
+ } else {
7218
+ throw new Error(`EnsureSearchParameterDataLoaded(): runResult.acquired is true but this.alreadyComplete not set`);
7219
+ }
7220
+ }
7221
+ console.log(`ensureSearchParamLoaded(): PID: [${process.pid}] Did not get lock`);
7222
+ await Sleep(pollIntervalMs);
7223
+ }
7224
+ } catch (error) {
7225
+ console.error(`SearchParameterManager:EnsureSearchParameterDataLoaded(): Error: [${error}]`);
7226
+ throw error;
7227
+ }
7228
+ };
7229
+ Stop = async () => {
7230
+ if (this.redis) {
7231
+ await this.redis.close();
7232
+ console.log(`SearchParameterManager(): redis closed`);
7233
+ }
7234
+ };
7235
+ #LoadDefinitions = async () => {
7236
+ console.log("Data Load Starting ...");
7237
+ for (let i = 0; i < 10; i++) {
7238
+ console.log(`Loading ...: [${i}]`);
7239
+ await Sleep(500);
7240
+ }
7241
+ console.log("All Done ***********************************************************************************");
7242
+ };
7243
+ /*
7244
+ private GetTypeKey(typeName: string) {
7245
+ return `stsfhir_type_${typeName}`;
7246
+ }
7247
+
7248
+ private GetSearchParameterByFullUrlKey(fullUrl: string) {
7249
+ return `stsfhir_sp_${fullUrl}`;
7250
+ }
7251
+
7252
+ private GetResourceKey(resourceType: string) {
7253
+ return `stsfhir_resource_${resourceType}`;
7254
+ }
7255
+
7256
+ private GetSearchParameterByResourceTypeKey(resourceType: string) {
7257
+ return `stsfhir_sp_resource_${resourceType}`;
7258
+ }
7259
+
7260
+ private GetSTSCustomSearchParameterByResourceTypeKey(resourceType: string) {
7261
+ return `stsfhir_stscustom_sp_resource_${resourceType}`;
7262
+ }
7263
+
7264
+ // Remove ( ) when either are present
7265
+ private _RemoveOuterParentheses = (input: string): string => {
7266
+ return input.replace(/^\(|\)$/g, '');
7267
+ }
7268
+
7269
+ // Remove ( ) only when both are present
7270
+ private _RemoveSurroundingParentheses = (input: string): string => {
7271
+ return input.replace(/^\((.*)\)$/, '$1').trim();
7272
+ }
7273
+ */
7274
+ /*
7275
+ NamingSystem derived-from reference NamingSystem.relatedArtifact.where(type='derived-from').resource
7276
+
7277
+ NamingSystem predecessor reference NamingSystem.relatedArtifact.where(type='predecessor').resource
7278
+
7279
+ ClinicalUseDefinition product reference ClinicalUseDefinition.subject.where(resolve() is MedicinalProductDefinition)
7280
+
7281
+ OrganizationAffiliation email token OrganizationAffiliation.contact.telecom.where(system='email')
7282
+ OrganizationAffiliation phone token OrganizationAffiliation.contact.telecom.where(system='phone')
7283
+
7284
+ PackagedProductDefinition biological reference PackagedProductDefinition.packaging.containedItem.item.reference
7285
+ PackagedProductDefinition contained-item reference PackagedProductDefinition.packaging.containedItem.item.reference
7286
+
7287
+ Patient deceased token Patient.deceased.exists() and Patient.deceased != false
7288
+ Practitioner deceased token Practitioner.deceased.exists() and Practitioner.deceased != false
7289
+
7290
+ Location contains special Location.extension('http://hl7.org/fhir/StructureDefinition/location-boundary-geojson').value
7291
+
7292
+ QuestionnaireResponse item-subject reference QuestionnaireResponse.item.where(extension('http://hl7.org/fhir/StructureDefinition/questionnaireresponse-isSubject').exists()).answer.valueReference
7293
+ */
7294
+ /*
7295
+ private _ProcessExpressions = (resourceName: string, SP_EXPRESSION: string): any => {
7296
+ let expressions: any[] = [];
7297
+
7298
+ //SP_EXPRESSION = this.#RemoveSurroundingParentheses(val2.expression);
7299
+ expressions = SP_EXPRESSION.split('|').map(e => e.trim());
7300
+
7301
+ expressions = expressions.map(e => this._RemoveSurroundingParentheses(e).trim());
7302
+
7303
+ if (resourceName !== '') {
7304
+ expressions = expressions.filter(e => e.split('.')[0].trim().localeCompare(resourceName) === 0);
7305
+ }
7306
+
7307
+ let original_expressions = [ ...expressions ];
7308
+
7309
+ // ofType processing
7310
+ expressions = expressions.map((e: string) => {
7311
+ let capturedTypeOf = undefined;
7312
+ return {
7313
+ path: e.replace(
7314
+ /\.ofType\(([^)]+)\)/g,
7315
+ (_: string, typeName: string) => {
7316
+ capturedTypeOf = true;
7317
+ return typeName.charAt(0).toUpperCase() + typeName.slice(1)
7318
+ }),
7319
+ typeOf: capturedTypeOf
7320
+ }
7321
+ });
7322
+
7323
+ //@@ exists processing here is more or less hard coded to the deceased attribute.
7324
+ //@@ in future, we need an actual fhir language parser.
7325
+ expressions.forEach(expression => {
7326
+ try {
7327
+ const sp = expression as ISearchParamExpressionRecord;
7328
+ if (sp.path.indexOf('exists()') >= 0) {
7329
+ // Person.deceased.exists() and Person.deceased != false
7330
+ const parts = sp.path.split(' ');
7331
+ const i = parts[0].indexOf('exists()');
7332
+ if (i >= 0) {
7333
+ let fieldExistsEx = parts[0].substring(0, i-1);
7334
+ let op = parts[1];
7335
+ let fieldTestEx = parts[2];
7336
+ let comparator = parts[3];
7337
+ let value = parts[4];
7338
+
7339
+ const fieldExistsParts = fieldExistsEx.split('.');
7340
+ let fieldExists = fieldExistsParts.slice(1).join('.');
7341
+
7342
+ const fieldTestParts = fieldTestEx.split('.');
7343
+ let fieldTest = fieldTestParts.slice(1).join('.');
7344
+
7345
+ sp.exists = {
7346
+ fieldExists: [ `${fieldExists}Boolean`, `${fieldExists}DataTime` ], // [x]Boolean or [x]DateTime
7347
+ op,
7348
+ fieldTest: [ `${fieldTest}Boolean`, `${fieldTest}DataTime` ],
7349
+ comparator,
7350
+ value: [ value, undefined ]
7351
+ }
7352
+ }
7353
+ }
7354
+ } catch (error) {
7355
+
7356
+ }
7357
+ });
7358
+
7359
+ // where(resolve()) processing
7360
+ let capturedType: string | undefined;
7361
+ expressions = expressions.map((obj: any) => {
7362
+ capturedType = undefined;
7363
+ const { path, typeOf, exists } = obj;
7364
+ return {
7365
+ path: path.replace(
7366
+ /\.where\(resolve\(\)\s+is\s+([^)]+)\)/,
7367
+ (_: string, typeName: string) => {
7368
+ capturedType = typeName;
7369
+ //return ` // reference = ${capturedType}`; // remove the .where(...) part (you can put ` // ${capturedType}` if you want to keep a comment)
7370
+ return '';
7371
+ }),
7372
+ typeOf,
7373
+ reference: capturedType,
7374
+ exists
7375
+ }
7376
+ });
7377
+
7378
+ // .where processing
7379
+ // The element before the .where will be an array. So this test checks which element(s) from that array are satisifed.
7380
+ let capturedWhere: any = undefined;
7381
+ expressions = expressions.map((obj: any) => {
7382
+ capturedWhere = undefined;
7383
+ const { path, reference, typeOf, exists } = obj;
7384
+ const retVal: any = {
7385
+ path: path.replace(/([^.]+)\.where\(([^=]+)='([^']+)'\)/g, (_: string, field: string, subfield: string, value: string) => {
7386
+ capturedWhere = { field, subfield, value }
7387
+ return field;
7388
+ }),
7389
+ typeOf,
7390
+ reference,
7391
+ exists
7392
+ }
7393
+ if (capturedWhere) {
7394
+ capturedWhere.full = `${capturedWhere.field}[*].${capturedWhere.subfield}=${capturedWhere.value}`;
7395
+ retVal.where = capturedWhere;
7396
+ }
7397
+ return retVal;
7398
+ });
7399
+
7400
+
7401
+ return {
7402
+ expressions,
7403
+ original_expressions
7404
+ };
7405
+ }
7406
+
7407
+ #LoadDefinitions = async (): Promise<void> => {
7408
+ try {
7409
+ let usePath = '';
7410
+ if (fs.existsSync(`${specPath1}${resourcesPath}`)) {
7411
+ usePath = specPath1;
7412
+ } else {
7413
+ usePath = specPath2;
7414
+ }
7415
+ const __resources: any = JSON.parse(fs.readFileSync(`${usePath}${resourcesPath}`, 'utf-8'));
7416
+ const __types: any = JSON.parse(fs.readFileSync(`${usePath}${typesPath}`, 'utf-8'));
7417
+ const __searchParams: any = JSON.parse(fs.readFileSync(`${usePath}${searchParamsPath}`, 'utf-8'));
7418
+
7419
+ for (let i=0; i < (__resources.entry as any[]).length; i++) {
7420
+ const entry = __resources.entry[i];
7421
+ const resource: any = entry.resource;
7422
+ if (resource?.snapshot?.element) {
7423
+ const rt = resource.type as string;
7424
+ await this.redis!.set(this.GetResourceKey(rt), JSON.stringify(resource.snapshot.element));
7425
+ }
7426
+ };
7427
+
7428
+ for (let i=0; i < (__types.entry as any[]).length; i++) {
7429
+ const entry = __types.entry[i];
7430
+ const type = entry.resource;
7431
+ if (type?.snapshot?.element) {
7432
+ await this.redis!.set(this.GetTypeKey(type.type), JSON.stringify(type.snapshot.element));
7433
+ }
7434
+ };
7435
+
7436
+ for (let i=0; i < (__searchParams.entry as any[]).length; i++) {
7437
+ const entry = __searchParams.entry[i];
7438
+ await this.redis!.set(this.GetSearchParameterByFullUrlKey(entry.fullUrl), JSON.stringify(entry.resource));
7439
+ const { code, base } = entry.resource;
7440
+ if (base) {
7441
+ for (let j=0; j < base.length; j++) {
7442
+ const resourceType = base[j];
7443
+ await this.redis!.hSet(this.GetSearchParameterByResourceTypeKey(resourceType), code, JSON.stringify(entry.resource));
7444
+ }
7445
+ }
7446
+ };
7447
+
7448
+ //await this.#LoadDefinitionsPass2(__searchParams);
7449
+
7450
+ console.log(`All completed ...`);
7451
+ } catch (error) {
7452
+ console.error(error);
7453
+ throw error;
7454
+ }
7455
+ }
7456
+ */
7457
+ /*
7458
+ #LoadDefinitionsPass2 = async (__searchParams: any): Promise<void> => {
7459
+ try {
7460
+ for (let i=0; i < (__searchParams.entry as any[]).length; i++) {
7461
+
7462
+ console.log(`Processing (i) entry: [${i}]`);
7463
+
7464
+ if (i === 47 ) {
7465
+ console.log(`here...`)
7466
+ }
7467
+
7468
+ const entry = __searchParams.entry[i];
7469
+
7470
+ const { id, url, code, base, type, expression, component, target } = entry.resource;
7471
+
7472
+ if (base) {
7473
+ for (let j=0; j < base.length; j++) {
7474
+ const resourceType = base[j];
7475
+ const SP_ID = id;
7476
+ const SP_NAME = code; // code is the true SP name (but in the spec, name is usually the same as code)
7477
+ const SP_TYPE = type;
7478
+ const SP_URL = url;
7479
+ const SP_TARGET = target;
7480
+ let SP_EXPRESSION: string = expression;
7481
+ let expressions: any[] = [];
7482
+ let original_expressions: any[] = [];
7483
+ if (SP_EXPRESSION) {
7484
+ const retVal = this._ProcessExpressions(code, SP_EXPRESSION);
7485
+ expressions = retVal.expressions;
7486
+ original_expressions = retVal.original_expressions;
7487
+ }
7488
+
7489
+ const record: ISearchParamRecord = {
7490
+ SP_ID,
7491
+ SP_NAME,
7492
+ SP_TYPE,
7493
+ SP_URL,
7494
+ SP_TARGET,
7495
+ expressions,
7496
+ original_expressions,
7497
+ SP_DEFINITION: entry.resource // this.resourceHelper.GetSearchParam(SP_URL)
7498
+ };
7499
+
7500
+ for (let i=0; i < record.expressions.length; i++) {
7501
+ //console.log(record.expressions[i]);
7502
+ const rf = await this.GetResourceField(resourceType, record.expressions[i].path);
7503
+ if (rf) {
7504
+ expressions[i].RES_FIELD = rf;
7505
+ }
7506
+ }
7507
+
7508
+ if (component) {
7509
+ record.component = [ ...component ];
7510
+ for (let k=0; k < record.component.length; k++) {
7511
+ const c = record.component[k] as any;
7512
+ //c.SP_RAW_PATHS = (c.expression.split('|') as any[]).map(rp => rp.trim());
7513
+ //c.SP_PATHS = (c.SP_RAW_PATHS as any[]).map(rawPath => this.ResolveFhirChoiceType(rawPath));
7514
+ const retVal = this._ProcessExpressions('', c.expression);
7515
+ c.expressions = retVal.expressions;
7516
+ c.original_expressions = retVal.original_expressions;
7517
+
7518
+ let def = null;
7519
+ const foundSearchParameter = await this.redis!.get(this.GetSearchParameterByFullUrlKey(c.definition));
7520
+ if (foundSearchParameter) {
7521
+ def = JSON.parse(foundSearchParameter);
7522
+ }
7523
+ //const def = await this.GetSearchParam(c.definition); //@@ fails here ...
7524
+
7525
+ if (def && def.type) {
7526
+ c.type = def.type
7527
+ }
7528
+ }
7529
+ }
7530
+
7531
+ await this.redis!.hSet(this.GetSTSCustomSearchParameterByResourceTypeKey(resourceType),
7532
+ code, JSON.stringify(record));
7533
+
7534
+ console.log(`Loaded...(j): [${j}]`);
7535
+ }
7536
+ console.log(`All Loaded...`);
7537
+ }
7538
+ console.log(`Loaded...(i): [${i}]`);
7539
+ };
7540
+ console.log(`Loaded...`);
7541
+ } catch (error) {
7542
+ console.error(error);
7543
+ throw error;
7544
+ }
7545
+ }
7546
+ */
7547
+ /*
7548
+ GetType = async (typeName: string): Promise<any[]> => {
7549
+ await this.EnsureSearchParameterDataLoaded();
7550
+ const foundType = await this.redis!.get(this.GetTypeKey(typeName));
7551
+ if (foundType) {
7552
+ return JSON.parse(foundType);
7553
+ }
7554
+ return [ ];
7555
+ }
7556
+
7557
+ GetResource = async (resourceType: string): Promise<any[]> => {
7558
+ await this.EnsureSearchParameterDataLoaded();
7559
+ const foundResourceType = await this.redis!.get(this.GetResourceKey(resourceType));
7560
+ if (foundResourceType) {
7561
+ return JSON.parse(foundResourceType);
7562
+ }
7563
+ return [ ];
7564
+ }
7565
+
7566
+ GetResourceField = async (resourceType: string, fieldPath: string): Promise<any> => {
7567
+ const resource: any[] = await this.GetResource(resourceType);
7568
+ if (resource) {
7569
+ const foundRecord = resource.filter(o => o.id === fieldPath);
7570
+ if (foundRecord.length > 0) {
7571
+ return foundRecord[0];
7572
+ }
7573
+ return undefined;
7574
+ }
7575
+ }
7576
+
7577
+ GetSearchParam = async (fullUrl: string): Promise<any> => {
7578
+ await this.EnsureSearchParameterDataLoaded();
7579
+ const foundSearchParameter = await this.redis!.get(this.GetSearchParameterByFullUrlKey(fullUrl));
7580
+ if (foundSearchParameter) {
7581
+ return JSON.parse(foundSearchParameter);
7582
+ }
7583
+ return null;
7584
+ }
7585
+
7586
+ GetSearchParamsByResourceType = async (resourceType: string): Promise<SearchParameter[]> => {
7587
+ await this.EnsureSearchParameterDataLoaded();
7588
+ const all = await this.redis!.hGetAll(this.GetSearchParameterByResourceTypeKey(resourceType));
7589
+ const parsed = Object.values(all).map(v => JSON.parse(v));
7590
+ return parsed;
7591
+ }
7592
+
7593
+ GetSTSCustomSearchParametersByResourceType = async (resourceType: string): Promise<ISearchParamRecord[]> => {
7594
+ await this.EnsureSearchParameterDataLoaded();
7595
+ const all = await this.redis!.hGetAll(this.GetSTSCustomSearchParameterByResourceTypeKey(resourceType));
7596
+ const parsed = Object.values(all).map(v => JSON.parse(v));
7597
+ return parsed;
7598
+ }
7599
+
7600
+ GetSearchParamFromResourceTypeUrl = async (resourceType: string, url: string): Promise<ISearchParamRecord | undefined> => {
7601
+ const customSearchParameters = await this.GetSTSCustomSearchParametersByResourceType(resourceType);
7602
+ const filterByNameAndUrl = customSearchParameters.filter(rt => rt.SP_URL.localeCompare(url) === 0);
7603
+ if (filterByNameAndUrl.length > 0) {
7604
+ return filterByNameAndUrl[0];
7605
+ }
7606
+ }
7607
+
7608
+ GetSearchParamFromResourceType = async (resourceType: string, name: string): Promise<ISearchParamRecord | undefined> => {
7609
+ const customSearchParameters = await this.GetSTSCustomSearchParametersByResourceType(resourceType);
7610
+ const filterByName = customSearchParameters.filter(rt => rt.SP_NAME.localeCompare(name) === 0);
7611
+ if (filterByName.length > 0) {
7612
+ return filterByName[0];
7613
+ }
7614
+ }
7615
+
7616
+ GetPathsFromResourceType = async (resourceType: string, name: string): Promise<string[]> => {
7617
+ const customSearchParameters = await this.GetSTSCustomSearchParametersByResourceType(resourceType);
7618
+ const filterByName = customSearchParameters.filter(rt => rt.SP_NAME.localeCompare(name) === 0);
7619
+ if (filterByName.length > 0) {
7620
+ return filterByName[0].expressions.map(e => e.path)
7621
+ }
7622
+ return [ ];
7623
+ }
7624
+ */
7625
+ }
6996
7626
  const specPath1 = "./dist/";
6997
7627
  const specPath2 = "./node_modules/@nsshunt/stsfhirpg/dist/";
6998
7628
  const resourcesPath = "fhir-spec/profiles-resources.json";
@@ -7010,6 +7640,12 @@ class ResourceHelper {
7010
7640
  }
7011
7641
  return ResourceHelper.instance;
7012
7642
  }
7643
+ Stop = async () => {
7644
+ if (this.searchParameterManager) {
7645
+ await this.searchParameterManager.Stop();
7646
+ }
7647
+ this.searchParameterManager = void 0;
7648
+ };
7013
7649
  #LoadDefinitions = async () => {
7014
7650
  let usePath = "";
7015
7651
  if (fs.existsSync(`${specPath1}${resourcesPath}`)) {
@@ -7045,8 +7681,21 @@ class ResourceHelper {
7045
7681
  this.#definitions.searchParametersByUrl[entry.fullUrl] = entry.resource;
7046
7682
  });
7047
7683
  await this._LoadSearchParameters();
7684
+ await this.PreLoadSearchParameterData();
7048
7685
  return this.#definitions;
7049
7686
  };
7687
+ PreLoadSearchParameterData = async () => {
7688
+ console.log(`PreLoadSearchParameterData(): Start ...`);
7689
+ this.searchParameterManager = new SearchParameterManager({
7690
+ name: "stsfhirspdata",
7691
+ pollIntervalMs: 250,
7692
+ lockTtlMs: 3e4,
7693
+ autoRenew: true,
7694
+ heartbeatMs: 1e4
7695
+ });
7696
+ await this.searchParameterManager.EnsureSearchParameterDataLoaded();
7697
+ console.log(`PreLoadSearchParameterData(): End`);
7698
+ };
7050
7699
  GetDefinitions = async () => {
7051
7700
  if (!this.#definitions) {
7052
7701
  await this.#LoadDefinitions();
@@ -11372,6 +12021,9 @@ class DBSearchIndex {
11372
12021
  get searchRegistry() {
11373
12022
  return SearchRegistry.getInstance();
11374
12023
  }
12024
+ Stop = async () => {
12025
+ await this.resourceHelper.Stop();
12026
+ };
11375
12027
  #OutputSearchDetails = (resourceName, resourceId, searchFieldRecord) => {
11376
12028
  const hl = chalk.yellow.bold.italic;
11377
12029
  this.#debug("------------------");