npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@morphdb/core

v1.1.0

Published

MorphDB Core Orchestrator, AsyncLocalStorage Context Scoping, and Unit of Work

Readme

@morphdb/core

Engine Orchestrator, Execution Scoping, Identity Map, and Unit of Work for MorphDB.


1. Responsibility

The @morphdb/core package acts as the central client orchestrator and execution context manager for MorphDB. It is responsible for:

  • Initializing database connection lifecycles via attached DatabaseAdapter instances.
  • Tracking request execution context (multi-tenant IDs, transaction handles, telemetry span context) using Node.js AsyncLocalStorage.
  • Managing transactional entity reference equality using the Identity Map and Unit of Work patterns.
  • Dispatching query operations to the AST builder and adapter layers.

2. Public API

export class MorphDBClient {
  constructor(config: MorphDBConfig);
  connect(): Promise<void>;
  disconnect(): Promise<void>;
  query<T>(schema: SchemaIR<T>): QueryBuilder<T>;
  executeQuery<T>(builder: QueryBuilder<any>): Promise<QueryResult<T>>;
  transaction<R>(fn: (txClient: MorphDBClient) => Promise<R>, options?: TransactionOptions): Promise<R>;
}

export class ContextManager {
  static run<R>(ctx: ExecutionContext, fn: () => Promise<R>): Promise<R>;
  static get(): ExecutionContext | undefined;
}

export class IdentityMap {
  get<T>(entityName: string, primaryKey: string | number): T | undefined;
  register<T>(entityName: string, primaryKey: string | number, instance: T): T;
  clear(): void;
}

export class UnitOfWork {
  registerClean<T>(entityName: string, primaryKey: string | number, entity: T): void;
  registerDirty<T>(entityName: string, primaryKey: string | number, entity: T): void;
  commit(): Promise<void>;
}

3. Folder Structure

packages/core/
├── package.json
├── tsconfig.json
├── README.md
├── src/
│   ├── index.ts               # Barrel exports
│   ├── client.ts              # MorphDBClient orchestrator
│   ├── context.ts             # AsyncLocalStorage ContextManager
│   ├── identity-map.ts        # Transactional Identity Map
│   ├── unit-of-work.ts        # Unit of Work pattern
│   └── errors.ts             # MorphDBError base class & exceptions
├── tests/
│   └── core.test.ts          # Vitest test suite
└── examples/
    └── index.ts              # Runnable usage example

4. Internal Components

  • MorphDBClient: Primary entry point connecting schema definitions, query builders, and physical database adapters.
  • ContextManager: Uses AsyncLocalStorage<ExecutionContext> to bind active transaction handles implicitly to asynchronous stack traces.
  • IdentityMap: Maintains a key-value store (${entityName}:${primaryKey}) of hydrated JavaScript objects within transaction boundaries.
  • UnitOfWork: Tracks clean, dirty, and new domain entities during transactional operations to flush updates efficiently.

5. Interfaces

export interface MorphDBConfig {
  readonly adapter: DatabaseAdapter;
}

export interface ExecutionContext {
  readonly tenantId?: string;
  readonly session?: TransactionSession;
  readonly identityMap: IdentityMap;
  readonly unitOfWork?: UnitOfWork;
  readonly traceId?: string;
}

6. Dependency Graph

graph TD
    Core["@morphdb/core"] --> AST["@morphdb/ast"]
    Core --> Schema["@morphdb/schema"]
    Core --> SDK["@morphdb/adapter-sdk"]
    Core --> QB["@morphdb/query-builder"]

7. Extension Points

  • Plugin Hooks: Hook into client execution points (beforeQuery, afterQuery, onTransactionCommit).
  • Custom Context Stores: Extend ExecutionContext with custom tenant or authorization state.

8. Design Patterns Used

  • Orchestrator Pattern: MorphDBClient coordinates schema IR, query building, and adapter execution.
  • Identity Map Pattern: Ensures object reference equality across transactional queries.
  • Unit of Work Pattern: Tracks entity changes within transaction boundaries.
  • Dependency Injection: Adapters and plugins are injected into the client upon initialization.