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

@ojson/models

v1.0.0

Published

Declarative data retrieval with automatic memoization for server-side TypeScript applications

Readme

Models

Quality Gate Status Coverage Maintainability Rating Reliability Rating Security Rating

Declarative data retrieval with automatic memoization for server-side TypeScript applications.

Overview

This library provides infrastructure helpers for building server-side applications (e.g., Express.js) using a declarative data retrieval pattern with automatic memoization. Instead of passing data through function parameters, you retrieve it at the point of use—models are computed once per request and cached automatically.

Core Principle: Models retrieve data at the point of use with automatic memoization. This eliminates redundant computations across the call stack and makes your code more maintainable.

Key Features

  • Automatic Memoization - Models are computed once per request and cached automatically
  • Declarative Data Retrieval - Get data where you need it, not where it's available
  • Composable Helpers - Mix and match caching, telemetry, deadlines, and overrides
  • Type-Safe - Full TypeScript support with strict typing
  • Server-Side Focused - Designed for Express.js and similar frameworks

Quick Start

Installation

npm install @ojson/models

Basic Example (Express.js)

import express from 'express';
import {Context, withModels} from '@ojson/models';

const app = express();
const registry = new Map(); // Create once, reuse per request

// Define models
function RequestParams(props, ctx) {
  return {
    userId: ctx.req.query.userId,
    token: ctx.req.headers.authorization
  };
}
RequestParams.displayName = 'RequestParams';

async function AuthModel(props, ctx) {
  const response = await fetch(`/api/auth/verify`, {
    headers: {Authorization: props.token}
  });
  return await response.json();
}
AuthModel.displayName = 'AuthModel';

// Use in route handler
app.get('/api/user', async (req, res) => {
  const baseCtx = new Context('http-get');
  baseCtx.req = req;
  baseCtx.res = res;
  
  const ctx = withModels(registry)(baseCtx);
  
  try {
    // RequestParams is computed once, even if called multiple times
    const params = await ctx.request(RequestParams);
    const auth = await ctx.request(AuthModel, {token: params.token});
    
    if (!auth.valid) {
      return res.status(401).json({error: 'Unauthorized'});
    }
    
    res.json({userId: params.userId, auth});
  } catch (error) {
    res.status(500).json({error: error.message});
  } finally {
    ctx.end();
  }
});

Core Concepts

Context

Context represents the execution context for a request lifecycle. It tracks:

  • Name and parent context (hierarchical structure)
  • Start/end time and duration
  • Errors
const ctx = new Context('request-name');
ctx.end(); // Mark as complete
ctx.fail(error); // Mark as failed with error

Models

A Model is a deterministic function f(OJson) -> JSON with:

  • Deterministic: Same parameters → same result
  • Serializable: Parameters can be serialized to create cache keys
  • JSON-compatible: Input is OJson (object at top level), output is any JSON-serializable value

Models must have a static displayName property for identification. Models can also have optional properties used by various modules:

function UserModel(props, ctx) {
  return {id: props.id, name: 'John'};
}
UserModel.displayName = 'UserModel'; // Required

// Optional: used by withCache
UserModel.cacheStrategy = CacheFirst;

// Optional: used by withTelemetry
UserModel.displayProps = {id: true};
UserModel.displayResult = {name: true};
UserModel.displayTags = {'model.version': '1.0.0'};

OJson Type

OJson (Object JSON) is a subset of JSON where the top level is always an object:

  • Top level must be an object (unlike JSON which can be any value)
  • Values can be any JSON-serializable value (Json): primitives, arrays, nested objects, etc.
type OJson = {
  [prop: string]: Json;
};

This restriction ensures predictable parameter structures for models and enables deterministic serialization for cache keys.

Memoization

Models are automatically memoized based on their displayName and serialized parameters. Subsequent calls with the same model and props return the cached result without recomputation.

Modules

This library is organized into composable modules:

withModels

Core module - Adds model execution with automatic memoization.

  • Declarative data retrieval
  • Automatic memoization within a request
  • Support for sync, async, and generator models
  • Execution control (kill(), isAlive())

withCache

Adds configurable caching strategies for cross-request caching.

  • Multiple strategies: CacheFirst, NetworkOnly, CacheOnly, StaleWhileRevalidate
  • Configurable TTL per strategy
  • Interruption-aware caching (never caches interrupted executions: InterruptedError)
  • Works alongside withModels memoization

withDeadline

Adds timeout/deadline support for bounding request execution time.

  • Context-level timeouts
  • Automatic cancellation via ctx.kill()
  • No-op mode when timeout is disabled

withOverrides

Enables model substitution for testing and feature flags.

  • Runtime model replacement
  • Transitive overrides (A → B → C)
  • Inherited by child contexts
  • Perfect for mocking in tests

withTelemetry

Integrates OpenTelemetry tracing for observability.

  • Automatic span creation for contexts
  • Model props/result/error tracking
  • Parent-child span relationships
  • Configurable field filtering

Composing Modules

Modules are designed to be composed together. Use the compose utility to combine multiple wrappers:

import {
  Context,
  withModels,
  withCache,
  withTelemetry,
  withDeadline,
  MemoryCache,
  compose,
} from '@ojson/models';

const registry = new Map();
const cacheProvider = new MemoryCache();

// Using compose utility
const wrap = compose([
  withModels(registry),
  withCache(
    {default: {ttl: 3600}},
    cacheProvider,
    (name: string) => withModels(new Map())(new Context(name)),
  ),
  withTelemetry({serviceName: 'my-api'}),
  withDeadline(5000), // 5 second timeout
]);

const ctx = wrap(new Context('request'));

Recommended order:

  1. withModels - Base models and memoization
  2. withCache / withOverrides / withTelemetry - Caching, overrides, telemetry
  3. withDeadline - On top to cut off long-running operations

Installation

npm install @ojson/models

Documentation

Development

Setup

npm install

Build

npm run build

Test

npm test

License

ISC