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

@crud-api/relationships

v0.1.3

Published

Relationships extension of the CRUD API reference implementation: $rel references stored as references, integrity checks and expansion of internal relationships

Readme

@crud-api/relationships

The Relationships extension (relationships 0.1.0) of the CRUD API reference implementation: Fields whose value is an ECSON reference ($rel) are stored and returned as references, the internal references a write introduces are checked, and the Expand Parameter puts the referenced Resources in the data member of their references.

ESM only, requires Node 24 or later (also runs on Bun and Deno). Apache-2.0.

Usage

import { createCrudServer } from "@crud-api/core";
import { createMemoryProvider } from "@crud-api/memory";
import { relationships } from "@crud-api/relationships";
import { Rel } from "@ecson/core";

const provider = createMemoryProvider({ collections: { posts: {}, users: {} } });
const server = createCrudServer({ provider, extensions: [relationships({ validate: true })] });

await server.execute({ operation: "create", collection: "users", resources: { _id: "u1", name: "Ana" } });
await server.execute({
  operation: "create",
  collection: "posts",
  resources: { _id: "p1", title: "Hello", author: new Rel({ collection: "users", id: "u1" }) },
});

const read = await server.execute({ operation: "get", collection: "posts", id: "p1", params: { expand: "author" } });
// read.resources.author is a Rel whose value is
// { collection: "users", id: "u1", data: { _id: "u1", name: "Ana" } }

Over HTTP the Expand Parameter is @expand (GET /posts/p1?@expand=author), over MCP the expand argument of crud.get and crud.list; @crud-api/http and @crud-api/mcp turn them on when relationships is among the Server's specifications. References travel as $rel in ECSON and in their plain JSON form (a plain collection object, a URI string) in plain JSON, where a value is never read as a reference: Clients create relationships with ECSON.

API

  • relationships(options?): the extension, for createCrudServer({ extensions }).
    • validate (default false): the internal references a write introduces must refer to existing Resources (REL_VALIDATE).
    • maxDepth (default 3): how many relationships an Expand path may cross, the first one included; 1 turns nested expansion off (REL_EXPAND_NESTED).
    • maxExpanded (default 10000): how many Resources nested expansion (depth 2 and beyond) may place in one Result; a level that would go beyond is not expanded (REL_EXPAND_NESTED).
  • parseExpand(value): the Expand Parameter as field paths; expand(resources, paths, ctx, maxDepth) and withoutData(resource): the two halves of the hooks, for hosts that compose their own.
  • RELATIONSHIPS_VERSION: "0.1.0"; DEFAULT_MAX_DEPTH: 3; DEFAULT_MAX_EXPANDED: 10000.

It declares the Warning codes REFERENCE_NOT_FOUND, EXPAND_IGNORED and EXPAND_DEPTH_LIMITED and the Parameter expand (get, list), and reads other Collections through the hook context (read, D129) of @crud-api/core 0.4.0. The Server's codec must know $rel (the References type set, on in the default codec of @ecson/core 0.2.0); the provider must store $rel natively (REL_FIELD). When metadata() is enabled too, relationships() must come before it, so the content hash covers the references as they are stored, without data; the other order fails when the server is built (Extension.before). schema() of @crud-api/schema, when enabled, comes before both: it converts the values of estype: "rel" Fields to references first.

Behaviour

Decisions D129 to D132 and D137 of docs/decisions.md record each reading; in short:

  • Storage (REL_FIELD, REL_CARDINALITY): references are stored as Rel values and returned in the same form, single or several, in the same order, without data unless expanded. A plain JSON object shaped like a reference stays an object.
  • data on writes (REL_WRITE_REFERENCE): every reference in a Resource to write, at any depth, loses the data of its collection objects, silently. A Resource read with expansion can be written back unchanged.
  • Internal references (REL_INTERNAL): a reference to a Resource the write introduces (a Collection and ID not in the previous state of the Resource, nor in the source of a clone) must name a Collection Slug and a valid string Resource ID; otherwise INVALID_INPUT (INVALID_PATCH from a patch), with data.field. References stored before are not checked again, and neither are those under _meta, which Metadata handles. References inside $set and $map values are handled like any other.
  • Integrity (REL_VALIDATE, off by default): with validate, each introduced internal reference must exist, read in one call per Collection; otherwise INVALID_INPUT (INVALID_PATCH) with data.field and data.id, nothing written. Resources created by the same create do not see each other. Deleting a referenced Resource does nothing to the references to it.
  • External references (REL_EXTERNAL, REL_EXTERNAL_NO_RESOLVE): URIs, Link headers and link objects are stored as they are, never resolved, never checked.
  • Expansion (REL_EXPAND, REL_EXPAND_MISSING, REL_EXPAND_EXTERNAL): expand is a field list without modifiers. Along each path plain objects go one level down and arrays apply the rest of the path to each element; a collection object gets data: the referenced Resource as stored, null when it does not exist (with REFERENCE_NOT_FOUND, field and id), a list in the order of ids. External references are left as they are and reported with EXPAND_IGNORED; in an array with both kinds, the collection objects are expanded. A path that reaches no reference expands nothing. Each Warning is reported once per operation.
  • Nested expansion (REL_EXPAND_NESTED): author.company expands company inside each expanded author; a relationship beyond maxDepth, or a nested level beyond maxExpanded, is left as it is and reported with EXPAND_DEPTH_LIMITED and its field.
  • Projection: the Projection Parameter applies to the outer Resource after the expansion, so it keeps or drops a relationship whole (a path into it, such as author.name, drops it); expanded Resources are not projected. A relationship the Projection drops is neither expanded nor reported.
  • Access: reading a referenced Resource (for expansion or validate) is authorized as a get of the Client (authorize of createCrudServer), before anything about it is looked up; a refusal fails the operation with ACCESS_DENIED. Each referenced Resource is read once per operation.