# extract-cbd-shape

> Extract an entity based on CBD and a SHACL shape

Latest version **0.4.1** (published 2026-09-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install extract-cbd-shape
pnpm add extract-cbd-shape
yarn add extract-cbd-shape
bun add extract-cbd-shape
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.4.1 |
| Published | 2026-09-22 |
| First published | 2023-09-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=22.0.0 |
| Dependencies | 6 |
| Unpacked size | 456.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Pieter Colpaert |
| Maintainers | pietercolpaert |
| Keywords | CBD, RDF, SHACL, RDF-JS |

## Links

- npm: https://www.npmjs.com/package/extract-cbd-shape
- npm.io page: https://npm.io/package/extract-cbd-shape

## Dependencies (6)

- [debug](https://npm.io/package/debug.md) ^4.4.3
- [rdf-stores](https://npm.io/package/rdf-stores.md) ^2.5.0
- [@treecg/types](https://npm.io/package/@treecg/types.md) ^0.4.6
- [rdf-writer-ts](https://npm.io/package/rdf-writer-ts.md) ^0.1.1
- [rdf-dereference](https://npm.io/package/rdf-dereference.md) ^5.0.0
- [rdf-data-factory](https://npm.io/package/rdf-data-factory.md) ^2.0.2

## Recent versions

- 0.4.1 (latest) — 2026-09-22
- 0.3.0-b (0.3.0b) — 2026-07-25
- 0.3.0-alpha (0.3.0a) — 2026-07-25
- 0.4.0 — 2026-09-22
- 0.3.0 — 2026-07-25
- 0.1.15 — 2026-02-05
- 0.1.14 — 2025-05-23
- 0.1.13-updated — 2025-04-30
- 0.1.13 — 2025-04-30
- 0.1.12 — 2025-02-21
- 0.1.11 — 2024-12-04
- 0.1.10 — 2024-12-03
- 0.1.9 — 2024-11-29
- 0.1.8 — 2024-05-16
- 0.1.7 — 2024-05-15
- … 12 more at https://npm.io/package/extract-cbd-shape/versions

## README

# Extract CBD Shape

[![tests](https://github.com/TREEcg/extract-cbd-shape/actions/workflows/tests.yml/badge.svg)](https://github.com/TREEcg/extract-cbd-shape/actions/workflows/tests.yml)
[![npm version](https://img.shields.io/npm/v/extract-cbd-shape.svg)](https://www.npmjs.com/package/extract-cbd-shape)
[![npm downloads](https://img.shields.io/npm/dm/extract-cbd-shape.svg)](https://www.npmjs.com/package/extract-cbd-shape)

Extract one logical RDF member from a page, dataset, or API response that contains many members.

`extract-cbd-shape` is a TypeScript package for RDF-based Web APIs where a response contains a collection page, stream fragment, or batch of records, and a client needs to process one member at a time. Given a focus node, it returns the quads that describe that member.

The package combines two fallback strategies described in the member extraction paper:

- a dataset-aware Concise Bounded Description (CBD) extraction rule;
- optional SHACL-guided extraction, where shapes act as extraction hints.

It works with RDF/JS stores and terms, preserves named graphs, supports blank-node closure, and can extract many members from the same page efficiently.

## Install

```bash
npm install extract-cbd-shape
```

The package is ESM-first and includes TypeScript declarations.

## Quick Start

```ts
import {
  CBDShapeExtractor,
  createGraphIndexedRdfStore,
} from "extract-cbd-shape";
import { DataFactory } from "rdf-data-factory";

const dataStore = createGraphIndexedRdfStore();

// Add RDF/JS quads from your parser of choice.
for (const quad of dataQuads) {
  dataStore.addQuad(quad);
}

const extractor = new CBDShapeExtractor();
const memberQuads = await extractor.extract(
  dataStore,
  DataFactory.namedNode("https://example.org/member/42"),
);
```

With a SHACL shape:

```ts
const shapeStore = createGraphIndexedRdfStore();

for (const quad of shapeQuads) {
  shapeStore.addQuad(quad);
}

const extractor = new CBDShapeExtractor(shapeStore);
const memberQuads = await extractor.extract(
  dataStore,
  DataFactory.namedNode("https://example.org/member/42"),
  DataFactory.namedNode("https://example.org/MemberShape"),
);
```

## What It Extracts

Without a shape, extraction is based on a dataset-aware CBD rule:

1. include quads where the focus node is the subject;
2. recursively include quads for blank node objects;
3. include quads in the named graph whose graph name is the focus node, for every
   focus node reached, so a blank node used as a graph name is followed as well;
4. keep RDF dataset graph names on the extracted quads.

Every blank node is processed at most once per extraction, so cycles and blank
nodes that are both an object and a graph name terminate.

With a SHACL shape, the shape is interpreted as an extraction topology:

1. `sh:property` paths are used as paths to include;
2. `sh:minCount > 0` marks paths as required;
3. `sh:node` links recursively extract related nodes with another node shape;
4. `sh:closed true` restricts extraction to shape-selected paths;
5. open shapes keep the normal CBD result and add shape-guided paths.

This is not SHACL validation. The shape is used to decide which quads to extract. Validate the extracted quads separately if validation matters for your application.

## API

### `new CBDShapeExtractor(shapesGraphStore?, dereferencer?, options?)`

Creates an extractor.

- `shapesGraphStore`: optional RDF/JS store containing SHACL shapes.
- `dereferencer`: optional `rdf-dereference` compatible dereferencer.
- `options.fetch`: optional `fetch` implementation used by dereferencing.
- `options.cbdDefaultGraph`: controls default-graph CBD behavior.
- `options.bulkConcurrency`: concurrency for `bulkExtract()` with asynchronous stores.

### `extract(store, id, shapeId?, graphsToIgnore?)`

Extracts one member.

```ts
const quads = await extractor.extract(store, id, shapeId);
```

- `store`: RDF/JS store containing the current page or dataset.
- `id`: RDF/JS term for the member to extract.
- `shapeId`: optional RDF/JS term identifying the SHACL node shape.
- `graphsToIgnore`: optional list of named graphs to exclude during traversal.
- returns: `Promise<Quad[]>`.

If no local quads can satisfy required shape paths, the extractor may dereference the focus IRI and retry with the retrieved data.

### `bulkExtract(store, ids, shapeId?, graphsToIgnore?, itemExtracted?)`

Extracts several members from the same store.

```ts
const members = await extractor.bulkExtract(
  dataStore,
  memberIds,
  DataFactory.namedNode("https://example.org/MemberShape"),
);
```

Use this when a page contains many members. It avoids repeatedly copying the page, keeps member named graphs isolated from each other, and can process asynchronous stores with bounded concurrency.

The result keeps the input order:

```ts
Array<{
  subject: Term;
  quads: Quad[];
}>
```

### `createGraphIndexedRdfStore()`

Creates an in-memory `rdf-stores` store with indexes tuned for member extraction.

```ts
const store = createGraphIndexedRdfStore();
```

The store includes graph-first indexes for retrieving member named graphs and traversal indexes for CBD and SHACL path lookups across graphs. Prefer it for browser use, tests, and page-scale in-memory extraction.

### `path` and `shape`

The package also exports internal path and shape helpers under:

```ts
import { path, shape } from "extract-cbd-shape";
```

Most consumers only need `CBDShapeExtractor` and `createGraphIndexedRdfStore()`.

## SHACL Support

The extractor uses a focused subset of SHACL Core:

- `sh:NodeShape`
- `sh:property`
- `sh:path`
- `sh:minCount`
- `sh:node`
- `sh:closed`
- `sh:and`
- `sh:or`
- `sh:xone`
- SHACL property paths, including sequence, inverse, alternative, zero-or-more, one-or-more, and zero-or-one paths.

It does not implement full SHACL validation semantics. In particular, it does not use `sh:targetClass` for automatic target selection and does not enforce constraints such as `sh:class`, `sh:pattern`, language constraints, qualified value shapes, SPARQL constraints, or JavaScript constraints.

## Named Graphs

Named graphs are handled conservatively:

- quads in a graph named after a focus node are included, whether that name is an IRI or a blank node;
- `bulkExtract()` avoids leaking one member's named graph into another member;
- other graph names are preserved, but not treated as member boundaries unless they match the extraction rule.

This makes the package useful for collection pages where each member may be represented either by subject-centric quads, by a named graph, or by a combination of both.

## Browser Playground

The repository includes a browser playground in `perf/web`. It demonstrates three common cases:

- closed shape with nested hierarchy;
- open shape over a default graph;
- open shape over named graphs.

Build it with:

```bash
npm run build --prefix perf/web
```

## Development

```bash
npm test
npm run build
```

The tests in `tests/` cover CBD extraction, SHACL-guided extraction, property paths, named graphs, store-agnostic extraction, and Mermaid rendering for shapes.

## Logging

Enable debug logging with:

```bash
DEBUG=extract-cbd-shape npm test
```

---
_Source: https://npm.io/package/extract-cbd-shape · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
