0.1.0 • Published 6 years ago

jsonld-patch v0.1.0

Weekly downloads
5
License
-
Repository
github
Last release
6 years ago

JSON-LD Patch

This library provides an API for applying JSON patches to JSON-LD documents.

JSON patches may be represented in JSON-LD by using hte will be interpreted as JSON-LD using the JSON-LD Patch @context.

The API

  • api.applyPatch(document, patch)

Quick Examples

Installation

npm install jsonld-patch

Simple patch

const jldp = require('jsonld-patch');

const document = {
  "@context": "http://schema.org/",
  "@type": "Person",
  "name": "Alice"
};

const patch = [
  {op: 'add', path: '/email', value: 'pdoe@example.com'}
];

const {newDocument} = jldp.applyPatch({document, patch});

/*
newDocument is:
{
  "@context": "http://schema.org/",
  "@type": "Person",
  "name": "Alice",
  "email": "alice@example.com"
}
*/

Contextual patch

const document = {
  "@type": "http://schema.org/Person",
  "http://schema.org/name": "Alice"
};

const patch = [
  {op: 'add', path: '/email', value: 'alice@example.com'}
];

const context = {
  "@context": "http://schema.org/"
};

// document will be compacted to the new context, then patched
const {newDocument} = jldp.applyPatch({document, patch, context});

/*
newDocument is:
{
  "@context": "http://schema.org/",
  "@type": "Person",
  "name": "Alice",
  "email": "alice@example.com"
}
*/

Frame and patch

const document = {
  "@context": "http://schema.org/",
  "@id": "http://example.com/alice",
  "@type": "Person",
  "name": "Alice",
  "knows": {
    "@id": "http://example.com/bob",
    "@type": "Person",
    "name": "Bob",
    "knows": "http://example.com/alice"
  }
};

const patch = [
  {op: 'add', path: '/email', value: 'bob@example.com'}
];

const frame = {
  "@context": "http://schema.org/",
  "@id": "http://example.com/bob"
}

const {newDocument} = jldp.applyPatch({document, patch, frame});

/*
newDocument is:
{
  "@context": "http://schema.org/",
  "@type": "Person",
  "@id": "http://example.com/bob",
  "name": "Bob",
  "email": "bob@example.com",
  "knows": {
    "@id": "http://example.com/alice",
    "@type": "Person",
    "name": "Alice",
    "knows": "http://example.com/bob"
  }
}
*/

// Note: newDocument could be reframed back to alice at its root using jsonld:

const aliceFrame = {
  "@context": "http://schema.org/",
  "@id": "http://example.com/alice"
}

const aliceAtRoot = jsonld.frame(newDocument, aliceFrame);



/*
const jsonpatch = require('fast-json-patch');

const document = {
  "@context": "http://schema.org/",
  "@type": "Person",
  "name": "Alice",
};

const observer = jsonpatch.observe(document);

document.email = 'alice@example.com';
const patch = jsonpatch.generate(observer);
jsonpatch.unobserve(document, observer);

console.log('patch', patch);
*/

API Documentation

Apply a JSON patch

Generate a JSON patch

A JSON patch can be generated by observing changes to a document.