You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

85 lines
2.6 KiB

import {
type GraphNode,
type ObjectNode,
type PredicateNode,
} from "@ldo/rdf-utils";
import type { RawObject } from "../util/RawObject";
import { addObjectToDataset } from "../util/addObjectToDataset";
import type { ProxyContext } from "../ProxyContext";
import { WildcardSubjectSetProxy } from "./WildcardSubjectSetProxy";
import { _getUnderlyingNode } from "../types";
import { defaultGraph, quad } from "@rdfjs/data-model";
import {
createTransactionDatasetFactory,
TransactionDataset,
} from "@ldo/subscribable-dataset";
import { createDatasetFactory } from "@ldo/dataset";
import { getNodeFromRawObject } from "../util/getNodeFromRaw";
import { nodeToString } from "../util/NodeSet";
export type SubjectSetProxyQuadMatch = [
undefined | null,
PredicateNode,
ObjectNode,
GraphNode | undefined | null,
];
export class SubjectSetProxy<
T extends RawObject,
> extends WildcardSubjectSetProxy<T> {
protected quadMatch: SubjectSetProxyQuadMatch;
constructor(context: ProxyContext, quadMatch: SubjectSetProxyQuadMatch) {
super(context, quadMatch);
this.quadMatch = quadMatch;
}
/**
* Appends a new element with a specified value to the end of the Set.
*/
add(value: T): this {
if (typeof value !== "object") {
throw new Error(
`Cannot add a literal "${value}"(${typeof value}) to a subject-oriented collection.`,
);
}
// Create a test dataset to see if the inputted data is valid
const testDataset = new TransactionDataset(
this.context.dataset,
createDatasetFactory(),
createTransactionDatasetFactory(),
);
addObjectToDataset(
value,
false,
this.context.duplicate({
writeGraphs: [defaultGraph()],
}),
);
const isValidAddition =
testDataset.match(
getNodeFromRawObject(value, this.context.contextUtil),
this.quadMatch[1],
this.quadMatch[2],
).size !== 0;
if (!isValidAddition) {
throw new Error(
`Cannot add value to collection. This must contain a quad that matches (${nodeToString(
this.quadMatch[0],
)}, ${nodeToString(this.quadMatch[1])}, ${nodeToString(
this.quadMatch[2],
)}, ${nodeToString(this.quadMatch[3])})`,
);
}
// Add the object if everything's okay
const added = addObjectToDataset(value as RawObject, false, this.context);
const addedNode = added[_getUnderlyingNode];
this.context.writeGraphs.forEach((graph) => {
this.context.dataset.add(
quad(addedNode, this.quadMatch[1], this.quadMatch[2], graph),
);
});
return this;
}
}