All files methods.ts

100% Statements 55/55
100% Branches 7/7
100% Functions 13/13
100% Lines 47/47

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111    2x                   2x 2x             2x 3x 3x 3x     3x 1x             2x 5x 5x 5x 1x   4x                   2x 2x 1x 1x           2x 2x 1x     2x 1x 1x     2x 3x 2x 2x 3x 1x       2x 1x   3x 1x       2x     2x       1x 1x     2x 2x 2x     2x 1x     2x 1x 1x    
import { Dataset } from "@rdfjs/types";
import { JsonLdDocument } from "jsonld";
import {
  getProxyFromObject,
  _getUnderlyingDataset,
  _proxyContext,
  write as writeDependency,
  GraphType,
  InteractOptions,
} from "jsonld-dataset-proxy";
import { Quad, WriterOptions } from "n3";
import { DatasetChanges, SubscribableDataset } from "o-dataset-pack";
import { datasetToString } from "./datasetConverters";
import {
  canDatasetStartTransaction,
  getTransactionalDatasetFromLdo,
  LdoBase,
  normalizeNodeNames,
} from "./util";
 
export {
  graphOf,
  languagesOf,
  setLanguagePreferences,
} from "jsonld-dataset-proxy";
 
export function write(...graphs: (GraphType | string)[]): InteractOptions {
  return writeDependency(...normalizeNodeNames(graphs));
}
 
/**
 * Begins a transaction for the given linked data object
 * @param ldo
 */
export function startTransaction(ldo: LdoBase): void {
  const proxy = getProxyFromObject(ldo);
  const dataset = proxy[_getUnderlyingDataset];
  if (!canDatasetStartTransaction(dataset)) {
    throw new Error("Object is not transactable.");
  }
  proxy[_proxyContext] = proxy[_proxyContext].duplicate({
    dataset: (dataset as SubscribableDataset<Quad>).startTransaction(),
    state: { parentDataset: dataset },
  });
}
 
/**
 * Ends a transaction and commits the
 * @param ldo
 */
export function commitTransaction(ldo: LdoBase): void {
  const [dataset, proxy] = getTransactionalDatasetFromLdo(ldo);
  dataset.commit();
  proxy[_proxyContext] = proxy[_proxyContext].duplicate({
    dataset: proxy[_proxyContext].state
      .parentDataset as SubscribableDataset<Quad>,
  });
}
 
export function transactionChanges(ldo: LdoBase): DatasetChanges {
  const [dataset] = getTransactionalDatasetFromLdo(ldo);
  return dataset.getChanges();
}
 
export function getDataset(ldo: LdoBase): Dataset {
  const proxy = getProxyFromObject(ldo);
  return proxy[_getUnderlyingDataset];
}
 
export async function toSparqlUpdate(ldo: LdoBase): Promise<string> {
  const [dataset] = getTransactionalDatasetFromLdo(ldo);
  const changes = dataset.getChanges();
  let output = "";
  if (changes.removed) {
    output += `DELETE DATA { ${await datasetToString(changes.removed, {
      format: "N-Triples",
    })} }`;
  }
  if (changes.added && changes.removed) {
    output += "; ";
  }
  if (changes.added) {
    output += `INSERT DATA { ${await datasetToString(changes.added, {
      format: "N-Triples",
    })} }`;
  }
  return output.replaceAll("\n", " ");
}
 
export async function serialize(
  ldo: LdoBase,
  options: WriterOptions
): Promise<string> {
  const dataset = getProxyFromObject(ldo)[_getUnderlyingDataset];
  return datasetToString(dataset, options);
}
 
export async function toTurtle(ldo: LdoBase): Promise<string> {
  const dataset = getProxyFromObject(ldo)[_getUnderlyingDataset];
  return datasetToString(dataset, {});
}
 
export async function toJsonLd(_ldo: LdoBase): Promise<JsonLdDocument> {
  throw new Error("Not Implemented");
}
 
export async function toNTriples(ldo: LdoBase): Promise<string> {
  const dataset = getProxyFromObject(ldo)[_getUnderlyingDataset];
  return datasetToString(dataset, { format: "N-Triples" });
}