From b941385a6ee90c1f1beaa065d456b157427f5596 Mon Sep 17 00:00:00 2001 From: jaxoncreed Date: Thu, 15 Feb 2024 13:40:59 -0500 Subject: [PATCH] Before remove transactionDatasetFactory --- packages/ldo/src/index.ts | 1 + packages/solid/src/SolidLdoDataset.ts | 237 +++++++++--------- .../solid/src/SolidLdoTransactionDataset.ts | 8 + packages/solid/src/types.ts | 11 + 4 files changed, 143 insertions(+), 114 deletions(-) create mode 100644 packages/solid/src/SolidLdoTransactionDataset.ts create mode 100644 packages/solid/src/types.ts diff --git a/packages/ldo/src/index.ts b/packages/ldo/src/index.ts index a612422..77f5250 100644 --- a/packages/ldo/src/index.ts +++ b/packages/ldo/src/index.ts @@ -2,6 +2,7 @@ export * from "./parseRdf"; export * from "./ShapeType"; export * from "./methods"; export * from "./LdoDataset"; +export * from "./LdoTransactionDataset"; export * from "./LdoBuilder"; export * from "./createLdoDataset"; import type { LdoBase as LdoBaseImport } from "./util"; diff --git a/packages/solid/src/SolidLdoDataset.ts b/packages/solid/src/SolidLdoDataset.ts index 53c2e02..c655f00 100644 --- a/packages/solid/src/SolidLdoDataset.ts +++ b/packages/solid/src/SolidLdoDataset.ts @@ -1,4 +1,4 @@ -import type { LdoBase, ShapeType } from "@ldo/ldo"; +import type { LdoBase, LdoTransactionDataset, ShapeType } from "@ldo/ldo"; import { LdoDataset, startTransaction } from "@ldo/ldo"; import type { DatasetChanges, GraphNode, SubjectNode } from "@ldo/rdf-utils"; import type { Dataset, DatasetFactory, Quad } from "@rdfjs/types"; @@ -22,6 +22,7 @@ import { splitChangesByGraph } from "./util/splitChangesByGraph"; import type { ContainerUri, LeafUri } from "./util/uriTypes"; import { isContainerUri } from "./util/uriTypes"; import type { Resource } from "./resource/Resource"; +import { SolidLdoTransactionDataset } from "./SolidLdoTransactionDataset"; /** * A SolidLdoDataset has all the functionality of an LdoDataset with the added @@ -61,7 +62,7 @@ export class SolidLdoDataset extends LdoDataset { */ constructor( context: SolidLdoDatasetContext, - datasetFactory: DatasetFactory, + datasetFactory: DatasetFactory, initialDataset?: Dataset, ) { super(datasetFactory, initialDataset); @@ -92,120 +93,128 @@ export class SolidLdoDataset extends LdoDataset { return this.context.resourceStore.get(uri, options); } - /** - * Given dataset changes, commit all changes made to the proper place - * on Solid Pods. - * - * @param changes - A set of changes that should be applied to Solid Pods - * - * @returns an AggregateSuccess if successful and an AggregateError if not - * - * @example - * ```typescript - * const result = await solidLdoDataset.commitChangesToPod({ - * added: createDataset([ - * quad(namedNode("a"), namedNode("b"), namedNode("d")); - * ]), - * removed: createDataset([ - * quad(namedNode("a"), namedNode("b"), namedNode("c")); - * ]) - * }); - * if (result.isError()) { - * // handle error - * } - * ``` - */ - async commitChangesToPod( - changes: DatasetChanges, - ): Promise< - | AggregateSuccess< - ResourceResult - > - | AggregateError - > { - // Optimistically add changes to the datastore - // this.bulk(changes); - const changesByGraph = splitChangesByGraph(changes); - - // Iterate through all changes by graph in - const results: [ - GraphNode, - DatasetChanges, - UpdateResult | InvalidUriError | UpdateDefaultGraphSuccess, - ][] = await Promise.all( - Array.from(changesByGraph.entries()).map( - async ([graph, datasetChanges]) => { - if (graph.termType === "DefaultGraph") { - // Undefined means that this is the default graph - this.bulk(datasetChanges); - return [ - graph, - datasetChanges, - { - type: "updateDefaultGraphSuccess", - isError: false, - } as UpdateDefaultGraphSuccess, - ]; - } - if (isContainerUri(graph.value)) { - return [ - graph, - datasetChanges, - new InvalidUriError( - graph.value, - `Container URIs are not allowed for custom data.`, - ), - ]; - } - const resource = this.getResource(graph.value as LeafUri); - return [graph, datasetChanges, await resource.update(datasetChanges)]; - }, - ), + public startTransaction(): SolidLdoTransactionDataset { + return new SolidLdoTransactionDataset( + this, + this.datasetFactory, + this.transactionDatasetFactory, ); + } - // If one has errored, return error - const errors = results.filter((result) => result[2].isError); + // /** + // * Given dataset changes, commit all changes made to the proper place + // * on Solid Pods. + // * + // * @param changes - A set of changes that should be applied to Solid Pods + // * + // * @returns an AggregateSuccess if successful and an AggregateError if not + // * + // * @example + // * ```typescript + // * const result = await solidLdoDataset.commitChangesToPod({ + // * added: createDataset([ + // * quad(namedNode("a"), namedNode("b"), namedNode("d")); + // * ]), + // * removed: createDataset([ + // * quad(namedNode("a"), namedNode("b"), namedNode("c")); + // * ]) + // * }); + // * if (result.isError()) { + // * // handle error + // * } + // * ``` + // */ + // async commitChangesToPod( + // changes: DatasetChanges, + // ): Promise< + // | AggregateSuccess< + // ResourceResult + // > + // | AggregateError + // > { + // // Optimistically add changes to the datastore + // // this.bulk(changes); + // const changesByGraph = splitChangesByGraph(changes); - if (errors.length > 0) { - return new AggregateError( - errors.map( - (result) => result[2] as UpdateResultError | InvalidUriError, - ), - ); - } - return { - isError: false, - type: "aggregateSuccess", - results: results - .map((result) => result[2]) - .filter( - (result): result is ResourceResult => - result.type === "updateSuccess" || - result.type === "updateDefaultGraphSuccess", - ), - }; - } + // // Iterate through all changes by graph in + // const results: [ + // GraphNode, + // DatasetChanges, + // UpdateResult | InvalidUriError | UpdateDefaultGraphSuccess, + // ][] = await Promise.all( + // Array.from(changesByGraph.entries()).map( + // async ([graph, datasetChanges]) => { + // if (graph.termType === "DefaultGraph") { + // // Undefined means that this is the default graph + // this.bulk(datasetChanges); + // return [ + // graph, + // datasetChanges, + // { + // type: "updateDefaultGraphSuccess", + // isError: false, + // } as UpdateDefaultGraphSuccess, + // ]; + // } + // if (isContainerUri(graph.value)) { + // return [ + // graph, + // datasetChanges, + // new InvalidUriError( + // graph.value, + // `Container URIs are not allowed for custom data.`, + // ), + // ]; + // } + // const resource = this.getResource(graph.value as LeafUri); + // return [graph, datasetChanges, await resource.update(datasetChanges) ]; + // }, + // ), + // ); - /** - * Shorthand for solidLdoDataset - * .usingType(shapeType) - * .write(...resources.map((r) => r.uri)) - * .fromSubject(subject); - * @param shapeType - The shapetype to represent the data - * @param subject - A subject URI - * @param resources - The resources changes to should written to - */ - createData( - shapeType: ShapeType, - subject: string | SubjectNode, - resource: Resource, - ...additionalResources: Resource[] - ): Type { - const resources = [resource, ...additionalResources]; - const linkedDataObject = this.usingType(shapeType) - .write(...resources.map((r) => r.uri)) - .fromSubject(subject); - startTransaction(linkedDataObject); - return linkedDataObject; - } + // // If one has errored, return error + // const errors = results.filter((result) => result[2].isError); + + // if (errors.length > 0) { + // return new AggregateError( + // errors.map( + // (result) => result[2] as UpdateResultError | InvalidUriError, + // ), + // ); + // } + // return { + // isError: false, + // type: "aggregateSuccess", + // results: results + // .map((result) => result[2]) + // .filter( + // (result): result is ResourceResult => + // result.type === "updateSuccess" || + // result.type === "updateDefaultGraphSuccess", + // ), + // }; + // } + + // /** + // * Shorthand for solidLdoDataset + // * .usingType(shapeType) + // * .write(...resources.map((r) => r.uri)) + // * .fromSubject(subject); + // * @param shapeType - The shapetype to represent the data + // * @param subject - A subject URI + // * @param resources - The resources changes to should written to + // */ + // createData( + // shapeType: ShapeType, + // subject: string | SubjectNode, + // resource: Resource, + // ...additionalResources: Resource[] + // ): Type { + // const resources = [resource, ...additionalResources]; + // const linkedDataObject = this.usingType(shapeType) + // .write(...resources.map((r) => r.uri)) + // .fromSubject(subject); + // startTransaction(linkedDataObject); + // return linkedDataObject; + // } } diff --git a/packages/solid/src/SolidLdoTransactionDataset.ts b/packages/solid/src/SolidLdoTransactionDataset.ts new file mode 100644 index 0000000..4e31b9c --- /dev/null +++ b/packages/solid/src/SolidLdoTransactionDataset.ts @@ -0,0 +1,8 @@ +import { LdoTransactionDataset } from "@ldo/ldo"; +import type { ISolidLdoDataset } from "./types"; + +export class SolidLdoTransactionDataset + extends LdoTransactionDataset + implements ISolidLdoDataset { + + } diff --git a/packages/solid/src/types.ts b/packages/solid/src/types.ts new file mode 100644 index 0000000..45bd75a --- /dev/null +++ b/packages/solid/src/types.ts @@ -0,0 +1,11 @@ +import type { ResourceGetterOptions } from "./ResourceStore"; +import type { Container } from "./resource/Container"; +import type { Leaf } from "./resource/Leaf"; +import type { ContainerUri, LeafUri } from "./util/uriTypes"; + +export interface ISolidLdoDataset { + getResource(uri: ContainerUri, options?: ResourceGetterOptions): Container; + getResource(uri: LeafUri, options?: ResourceGetterOptions): Leaf; + getResource(uri: string, options?: ResourceGetterOptions): Leaf | Container; + getResource(uri: string, options?: ResourceGetterOptions): Leaf | Container; +}