@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
Maintainers
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, forcreateCrudServer({ extensions }).validate(defaultfalse): the internal references a write introduces must refer to existing Resources (REL_VALIDATE).maxDepth(default3): how many relationships an Expand path may cross, the first one included;1turns nested expansion off (REL_EXPAND_NESTED).maxExpanded(default10000): 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)andwithoutData(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
Relvalues and returned in the same form, single or several, in the same order, withoutdataunless expanded. A plain JSON object shaped like a reference stays an object. dataon writes (REL_WRITE_REFERENCE): every reference in a Resource to write, at any depth, loses thedataof 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_PATCHfrom a patch), withdata.field. References stored before are not checked again, and neither are those under_meta, which Metadata handles. References inside$setand$mapvalues 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; otherwiseINVALID_INPUT(INVALID_PATCH) withdata.fieldanddata.id, nothing written. Resources created by the samecreatedo 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):
expandis 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 getsdata: the referenced Resource as stored,nullwhen it does not exist (withREFERENCE_NOT_FOUND,fieldandid), a list in the order ofids. External references are left as they are and reported withEXPAND_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.companyexpandscompanyinside each expanded author; a relationship beyondmaxDepth, or a nested level beyondmaxExpanded, is left as it is and reported withEXPAND_DEPTH_LIMITEDand itsfield. - 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 agetof the Client (authorizeofcreateCrudServer), before anything about it is looked up; a refusal fails the operation withACCESS_DENIED. Each referenced Resource is read once per operation.
