@openhealth/oht-custom-parser-lib
v0.5.14
Published
Shared nodejs lib with with reusable functions
Downloads
3,409
Keywords
Readme
oht-custom-parser-lib
oht-custom-parser-lib is a shared library designed for use across multiple Node.js cloud functions. It contains common MongoDB connectors and data utilities that can be reused to avoid code duplication and ensure consistency.
Table of Contents
Installation
To install oht-custom-parser-lib, you can add it as a dependency in your project using npm:
npm install oht-custom-parser-libOr, if you want to install a specific version:
npm install oht-custom-parser-lib@<version>Usage
After installing, you can import and use the library in your cloud functions:
const { connectToMongoDB } = require('oht-custom-parser-lib');Logging
- Production / GCP: Logs are emitted as JSON (one line per log) for Cloud Logging.
- Local / development: When
NODE_ENV !== 'production', logs are pretty-printed in the terminal (colors, timestamps, readable structure). You can override:LOG_PRETTY=trueto force pretty output, orLOG_PRETTY=falseto force JSON when not in production.
Processing logger: request-scoped state
The processing logger (processingLogger, runWithContextAsync, getRequestIdFromContext) uses a registry + facade so each request has isolated state and logs never leak context between concurrent invocations (e.g. in Cloud Functions).
- Registry: One
ProcessingStateper request, keyed by request id (fileUploadId ?? messageId ?? cloudEventId ?? generatedId). The same id is used for the whole request. - AsyncLocalStorage: Holds only the current request id; state is resolved via
registry.get(id). - Entry point: Wrap your handler in
runWithContextAsync(context, async () => { ... }). The lib creates a registry entry, runs your callback with the id in AsyncLocalStorage, and removes the entry in afinallyblock. - Clear-state rule: If there is no request id or the id is not in the registry (e.g. logging outside
runWithContextAsyncor after teardown), the logger uses a transient empty state for that call only (never stored). That way one request never overwrites another’s context.
Use processingLogger.logInfo, logWarn, logError, logProgress, etc. inside the callback so logs include the same context and are searchable by fileUploadId / filename.
Development
Patch Versions
When making changes to the oht-custom-parser-lib, you need to update the version number before publishing. Follow these steps:
Update the version number: Use npm's version command to bump the version:
- Patch for bug fixes:
npm version patch - Minor for new features:
npm version minor - Major for breaking changes:
npm version major
This command will automatically update the version in the
package.jsonand create a git tag.- Patch for bug fixes:
Commit Changes: Ensure your changes and the version bump are committed to the repository.
Publish to npm Registry
After updating the version, you can publish the library to the npm registry:
Login to npm (if you haven't already):
npm loginPublish the package:
npm publishIf this is a private package, use:
npm publish --access restrictedVerify the publish: You can check the package on npmjs.com to verify that the new version is available.
Using the Library in Cloud Functions
To use oht-custom-parser-lib in your cloud functions:
Install the Library: Add
oht-custom-parser-libas a dependency in the cloud function'spackage.json:{ "dependencies": { "oht-custom-parser-lib": "^1.0.0" } }Replace
^1.0.0with the appropriate version as needed.Install Dependencies: Run
npm installto install the library along with other dependencies.Deploy Your Function: Deploy your cloud function as usual, ensuring that the updated dependencies are included.
