@ldo/solid-type-index 0.0.1-1.0.0-alpha.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/LICENSE.txt +21 -0
  2. package/README.md +49 -0
  3. package/dist/.ldo/profile.context.d.ts +2 -0
  4. package/dist/.ldo/profile.context.js +16 -0
  5. package/dist/.ldo/profile.context.js.map +1 -0
  6. package/dist/.ldo/profile.schema.d.ts +2 -0
  7. package/dist/.ldo/profile.schema.js +59 -0
  8. package/dist/.ldo/profile.schema.js.map +1 -0
  9. package/dist/.ldo/profile.shapeTypes.d.ts +3 -0
  10. package/dist/.ldo/profile.shapeTypes.js +11 -0
  11. package/dist/.ldo/profile.shapeTypes.js.map +1 -0
  12. package/dist/.ldo/profile.typings.d.ts +11 -0
  13. package/dist/.ldo/profile.typings.js +3 -0
  14. package/dist/.ldo/profile.typings.js.map +1 -0
  15. package/dist/.ldo/typeIndex.context.d.ts +2 -0
  16. package/dist/.ldo/typeIndex.context.js +61 -0
  17. package/dist/.ldo/typeIndex.context.js.map +1 -0
  18. package/dist/.ldo/typeIndex.schema.d.ts +2 -0
  19. package/dist/.ldo/typeIndex.schema.js +141 -0
  20. package/dist/.ldo/typeIndex.schema.js.map +1 -0
  21. package/dist/.ldo/typeIndex.shapeTypes.d.ts +4 -0
  22. package/dist/.ldo/typeIndex.shapeTypes.js +16 -0
  23. package/dist/.ldo/typeIndex.shapeTypes.js.map +1 -0
  24. package/dist/.ldo/typeIndex.typings.d.ts +26 -0
  25. package/dist/.ldo/typeIndex.typings.js +3 -0
  26. package/dist/.ldo/typeIndex.typings.js.map +1 -0
  27. package/dist/constants.d.ts +5 -0
  28. package/dist/constants.js +9 -0
  29. package/dist/constants.js.map +1 -0
  30. package/dist/getTypeIndex.d.ts +9 -0
  31. package/dist/getTypeIndex.js +62 -0
  32. package/dist/getTypeIndex.js.map +1 -0
  33. package/dist/index.d.ts +4 -0
  34. package/dist/index.js +21 -0
  35. package/dist/index.js.map +1 -0
  36. package/dist/react/useInstanceUris.d.ts +2 -0
  37. package/dist/react/useInstanceUris.js +26 -0
  38. package/dist/react/useInstanceUris.js.map +1 -0
  39. package/dist/react/useTypeIndexProfile.d.ts +2 -0
  40. package/dist/react/useTypeIndexProfile.js +13 -0
  41. package/dist/react/useTypeIndexProfile.js.map +1 -0
  42. package/dist/react/util/useSubscribeToUris.d.ts +1 -0
  43. package/dist/react/util/useSubscribeToUris.js +31 -0
  44. package/dist/react/util/useSubscribeToUris.js.map +1 -0
  45. package/dist/setTypeIndex.d.ts +14 -0
  46. package/dist/setTypeIndex.js +122 -0
  47. package/dist/setTypeIndex.js.map +1 -0
  48. package/dist/util/Options.d.ts +9 -0
  49. package/dist/util/Options.js +12 -0
  50. package/dist/util/Options.js.map +1 -0
  51. package/package.json +50 -0
  52. package/src/.ldo/profile.context.ts +19 -0
  53. package/src/.ldo/profile.schema.ts +64 -0
  54. package/src/.ldo/profile.shapeTypes.ts +19 -0
  55. package/src/.ldo/profile.typings.ts +27 -0
  56. package/src/.ldo/typeIndex.context.ts +64 -0
  57. package/src/.ldo/typeIndex.schema.ts +144 -0
  58. package/src/.ldo/typeIndex.shapeTypes.ts +28 -0
  59. package/src/.ldo/typeIndex.typings.ts +58 -0
  60. package/src/.shapes/profile.shex +10 -0
  61. package/src/.shapes/typeIndex.shex +23 -0
  62. package/src/constants.ts +7 -0
  63. package/src/getTypeIndex.ts +93 -0
  64. package/src/index.ts +4 -0
  65. package/src/react/useInstanceUris.ts +45 -0
  66. package/src/react/useTypeIndexProfile.ts +10 -0
  67. package/src/react/util/useSubscribeToUris.ts +35 -0
  68. package/src/setTypeIndex.ts +219 -0
  69. package/src/util/Options.ts +14 -0
@@ -0,0 +1,28 @@
1
+ import { ShapeType } from "@ldo/ldo";
2
+ import { typeIndexSchema } from "./typeIndex.schema";
3
+ import { typeIndexContext } from "./typeIndex.context";
4
+ import { TypeIndexDocument, TypeRegistration } from "./typeIndex.typings";
5
+
6
+ /**
7
+ * =============================================================================
8
+ * LDO ShapeTypes typeIndex
9
+ * =============================================================================
10
+ */
11
+
12
+ /**
13
+ * TypeIndexDocument ShapeType
14
+ */
15
+ export const TypeIndexDocumentShapeType: ShapeType<TypeIndexDocument> = {
16
+ schema: typeIndexSchema,
17
+ shape: "https://shaperepo.com/schemas/solidProfile#TypeIndexDocument",
18
+ context: typeIndexContext,
19
+ };
20
+
21
+ /**
22
+ * TypeRegistration ShapeType
23
+ */
24
+ export const TypeRegistrationShapeType: ShapeType<TypeRegistration> = {
25
+ schema: typeIndexSchema,
26
+ shape: "https://shaperepo.com/schemas/solidProfile#TypeRegistration",
27
+ context: typeIndexContext,
28
+ };
@@ -0,0 +1,58 @@
1
+ import { LdoJsonldContext, LdSet } from "@ldo/ldo";
2
+
3
+ /**
4
+ * =============================================================================
5
+ * Typescript Typings for typeIndex
6
+ * =============================================================================
7
+ */
8
+
9
+ /**
10
+ * TypeIndexDocument Type
11
+ */
12
+ export interface TypeIndexDocument {
13
+ "@id"?: string;
14
+ "@context"?: LdoJsonldContext;
15
+ /**
16
+ * Defines the node as a TypeIndex | Defines the node as a Listed Document
17
+ */
18
+ type: LdSet<
19
+ | {
20
+ "@id": "TypeIndex";
21
+ }
22
+ | {
23
+ "@id": "ListedDocument";
24
+ }
25
+ >;
26
+ }
27
+
28
+ /**
29
+ * TypeRegistration Type
30
+ */
31
+ export interface TypeRegistration {
32
+ "@id"?: string;
33
+ "@context"?: LdoJsonldContext;
34
+ /**
35
+ * Defines this node as a Type Registration
36
+ */
37
+ type: {
38
+ "@id": "TypeRegistration";
39
+ };
40
+ /**
41
+ * The class of object at this type.
42
+ */
43
+ forClass: {
44
+ "@id": string;
45
+ };
46
+ /**
47
+ * A specific resource that contains the class.
48
+ */
49
+ instance?: LdSet<{
50
+ "@id": string;
51
+ }>;
52
+ /**
53
+ * Containers that contain resources with the class.
54
+ */
55
+ instanceContainer?: LdSet<{
56
+ "@id": string;
57
+ }>;
58
+ }
@@ -0,0 +1,10 @@
1
+ PREFIX srs: <https://shaperepo.com/schemas/solidProfile#>
2
+ PREFIX solid: <http://www.w3.org/ns/solid/terms#>
3
+ PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
4
+
5
+ srs:TypeIndexProfile {
6
+ solid:privateTypeIndex IRI *
7
+ // rdfs:comment "A registry of all types used on the user's Pod (for private access only)" ;
8
+ solid:publicTypeIndex IRI *
9
+ // rdfs:comment "A registry of all types used on the user's Pod (for public access)" ;
10
+ }
@@ -0,0 +1,23 @@
1
+ PREFIX srs: <https://shaperepo.com/schemas/solidProfile#>
2
+ PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
3
+ PREFIX solid: <http://www.w3.org/ns/solid/terms#>
4
+ PREFIX vcard: <http://www.w3.org/2006/vcard/ns#>
5
+ PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
6
+
7
+ srs:TypeIndexDocument EXTRA a {
8
+ a [ solid:TypeIndex ]
9
+ // rdfs:comment "Defines the node as a TypeIndex" ;
10
+ a [ solid:ListedDocument ]
11
+ // rdfs:comment "Defines the node as a Listed Document" ;
12
+ }
13
+
14
+ srs:TypeRegistration EXTRA a {
15
+ a [ solid:TypeRegistration ]
16
+ // rdfs:comment "Defines this node as a Type Registration" ;
17
+ solid:forClass IRI
18
+ // rdfs:comment "The class of object at this type." ;
19
+ solid:instance IRI *
20
+ // rdfs:comment "A specific resource that contains the class." ;
21
+ solid:instanceContainer IRI *
22
+ // rdfs:comment "Containers that contain resources with the class." ;
23
+ }
@@ -0,0 +1,7 @@
1
+ export const RDF_TYPE = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type";
2
+ export const TYPE_REGISTRATION =
3
+ "http://www.w3.org/ns/solid/terms#TypeRegistration";
4
+ export const FOR_CLASS = "http://www.w3.org/ns/solid/terms#forClass";
5
+ export const INSTANCE = "http://www.w3.org/ns/solid/terms#instance";
6
+ export const INSTANCE_CONTAINER =
7
+ "http://www.w3.org/ns/solid/terms#instanceContainer";
@@ -0,0 +1,93 @@
1
+ import type { ContainerUri, LeafUri } from "@ldo/solid";
2
+ import type { TypeRegistration } from "./.ldo/typeIndex.typings";
3
+ import type { TypeIndexProfile } from "./.ldo/profile.typings";
4
+ import { TypeIndexProfileShapeType } from "./.ldo/profile.shapeTypes";
5
+ import { TypeRegistrationShapeType } from "./.ldo/typeIndex.shapeTypes";
6
+ import { RDF_TYPE, TYPE_REGISTRATION } from "./constants";
7
+ import type { Options } from "./util/Options";
8
+ import { guaranteeOptions } from "./util/Options";
9
+ import type { LdSet } from "@ldo/ldo";
10
+
11
+ export async function getTypeRegistrations(
12
+ webId: string,
13
+ options?: Options,
14
+ ): Promise<LdSet<TypeRegistration>> {
15
+ const { dataset } = guaranteeOptions(options);
16
+
17
+ // Get Profile
18
+ const profile = await getProfile(webId, options);
19
+
20
+ // Get Type Indexes
21
+ const typeIndexUris = getTypeIndexesUrisFromProfile(profile);
22
+
23
+ // Fetch the type Indexes
24
+ await Promise.all(
25
+ typeIndexUris.map(async (typeIndexUri) => {
26
+ const typeIndexResource = dataset.getResource(typeIndexUri);
27
+ const readResult = await typeIndexResource.readIfUnfetched();
28
+ if (readResult.isError) throw readResult;
29
+ }),
30
+ );
31
+
32
+ // Get Type Registrations
33
+ return dataset
34
+ .usingType(TypeRegistrationShapeType)
35
+ .matchSubject(RDF_TYPE, TYPE_REGISTRATION);
36
+ }
37
+
38
+ export async function getProfile(
39
+ webId: string,
40
+ options?: Options,
41
+ ): Promise<TypeIndexProfile> {
42
+ const { dataset } = guaranteeOptions(options);
43
+ const profileResource = dataset.getResource(webId);
44
+ const readResult = await profileResource.readIfUnfetched();
45
+ if (readResult.isError) throw readResult;
46
+ return dataset.usingType(TypeIndexProfileShapeType).fromSubject(webId);
47
+ }
48
+
49
+ export function getTypeIndexesUrisFromProfile(
50
+ profile: TypeIndexProfile,
51
+ ): LeafUri[] {
52
+ const uris: LeafUri[] = [];
53
+ profile.privateTypeIndex?.forEach((indexNode) => {
54
+ uris.push(indexNode["@id"] as LeafUri);
55
+ });
56
+ profile.publicTypeIndex?.forEach((indexNode) => {
57
+ uris.push(indexNode["@id"] as LeafUri);
58
+ });
59
+ return uris;
60
+ }
61
+
62
+ export async function getInstanceUris(
63
+ classUri: string,
64
+ typeRegistrations: TypeRegistration[],
65
+ options?: Options,
66
+ ): Promise<LeafUri[]> {
67
+ const { dataset } = guaranteeOptions(options);
68
+
69
+ const leafUris = new Set<LeafUri>();
70
+ await Promise.all(
71
+ typeRegistrations.map(async (registration) => {
72
+ if (registration.forClass["@id"] === classUri) {
73
+ // Individual registrations
74
+ registration.instance?.forEach((instance) =>
75
+ leafUris.add(instance["@id"] as LeafUri),
76
+ );
77
+ // Container registrations
78
+ await Promise.all(
79
+ registration.instanceContainer?.map(async (instanceContainer) => {
80
+ const containerResource = dataset.getResource(
81
+ instanceContainer["@id"] as ContainerUri,
82
+ );
83
+ await containerResource.readIfUnfetched();
84
+ containerResource.children().forEach((child) => {
85
+ if (child.type === "leaf") leafUris.add(child.uri);
86
+ });
87
+ }) ?? [],
88
+ );
89
+ }
90
+ }),
91
+ );
92
+ return Array.from(leafUris);
93
+ }
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from "./getTypeIndex";
2
+ export * from "./setTypeIndex";
3
+ export * from "./react/useInstanceUris";
4
+ export * from "./react/useTypeIndexProfile";
@@ -0,0 +1,45 @@
1
+ import type { LeafUri } from "@ldo/solid";
2
+ import { useTypeIndexProfile } from "./useTypeIndexProfile";
3
+ import { useEffect, useMemo, useState } from "react";
4
+ import { useSubscribeToUris } from "./util/useSubscribeToUris";
5
+ import { useLdo, useMatchSubject } from "@ldo/solid-react";
6
+ import { TypeRegistrationShapeType } from "../.ldo/typeIndex.shapeTypes";
7
+ import { RDF_TYPE, TYPE_REGISTRATION } from "../constants";
8
+ import {
9
+ getInstanceUris,
10
+ getTypeIndexesUrisFromProfile,
11
+ } from "../getTypeIndex";
12
+
13
+ /**
14
+ * Provides the LeafUris of everything in a type node for a specific class uri
15
+ *
16
+ * @param classUri - the class uri
17
+ * @returns - URIs of all resources registered with this node
18
+ */
19
+ export function useInstanceUris(classUri: string): LeafUri[] {
20
+ const { dataset } = useLdo();
21
+ const profile = useTypeIndexProfile();
22
+
23
+ const typeIndexUris: string[] = useMemo(
24
+ () => (profile ? getTypeIndexesUrisFromProfile(profile) : []),
25
+ [profile],
26
+ );
27
+
28
+ useSubscribeToUris(typeIndexUris);
29
+
30
+ const [leafUris, setLeafUris] = useState<LeafUri[]>([]);
31
+
32
+ const typeRegistrations = useMatchSubject(
33
+ TypeRegistrationShapeType,
34
+ RDF_TYPE,
35
+ TYPE_REGISTRATION,
36
+ );
37
+
38
+ useEffect(() => {
39
+ getInstanceUris(classUri, typeRegistrations.toArray(), {
40
+ solidLdoDataset: dataset,
41
+ }).then(setLeafUris);
42
+ }, [typeRegistrations]);
43
+
44
+ return leafUris;
45
+ }
@@ -0,0 +1,10 @@
1
+ import { useResource, useSolidAuth, useSubject } from "@ldo/solid-react";
2
+ import type { TypeIndexProfile } from "../.ldo/profile.typings";
3
+ import { TypeIndexProfileShapeType } from "../.ldo/profile.shapeTypes";
4
+
5
+ export function useTypeIndexProfile(): TypeIndexProfile | undefined {
6
+ const { session } = useSolidAuth();
7
+ useResource(session.webId, { subscribe: true });
8
+ const profile = useSubject(TypeIndexProfileShapeType, session.webId);
9
+ return profile;
10
+ }
@@ -0,0 +1,35 @@
1
+ import { useLdo } from "@ldo/solid-react";
2
+ import { useEffect, useRef } from "react";
3
+
4
+ export function useSubscribeToUris(uris: string[]) {
5
+ const { dataset } = useLdo();
6
+ const currentlySubscribed = useRef<Record<string, string>>({});
7
+ useEffect(() => {
8
+ const resources = uris.map((uri) => dataset.getResource(uri));
9
+ const previousSubscriptions = { ...currentlySubscribed.current };
10
+ Promise.all<void>(
11
+ resources.map(async (resource) => {
12
+ if (!previousSubscriptions[resource.uri]) {
13
+ // Read and subscribe
14
+ await resource.readIfUnfetched();
15
+ currentlySubscribed.current[resource.uri] =
16
+ await resource.subscribeToNotifications();
17
+ } else {
18
+ delete previousSubscriptions[resource.uri];
19
+ }
20
+ }),
21
+ ).then(async () => {
22
+ // Unsubscribe from all remaining previous subscriptions
23
+ await Promise.all(
24
+ Object.entries(previousSubscriptions).map(
25
+ async ([resourceUri, subscriptionId]) => {
26
+ // Unsubscribe
27
+ delete currentlySubscribed.current[resourceUri];
28
+ const resource = dataset.getResource(resourceUri);
29
+ await resource.unsubscribeFromNotifications(subscriptionId);
30
+ },
31
+ ),
32
+ );
33
+ });
34
+ }, [uris]);
35
+ }
@@ -0,0 +1,219 @@
1
+ import { v4 } from "uuid";
2
+ import {
3
+ TypeIndexDocumentShapeType,
4
+ TypeRegistrationShapeType,
5
+ } from "./.ldo/typeIndex.shapeTypes";
6
+ import { FOR_CLASS, RDF_TYPE, TYPE_REGISTRATION } from "./constants";
7
+ import { guaranteeOptions, type Options } from "./util/Options";
8
+ import { namedNode, quad } from "@rdfjs/data-model";
9
+ import type { TypeRegistration } from "./.ldo/typeIndex.typings";
10
+ import { getProfile } from "./getTypeIndex";
11
+ import { TypeIndexProfileShapeType } from "./.ldo/profile.shapeTypes";
12
+ import type { Container } from "@ldo/solid";
13
+ import type { ISolidLdoDataset } from "@ldo/solid";
14
+ import type { NamedNode } from "@rdfjs/types";
15
+ import { set } from "@ldo/ldo";
16
+
17
+ /**
18
+ * =============================================================================
19
+ * INITIALIZERS
20
+ * =============================================================================
21
+ */
22
+ export async function initTypeIndex(
23
+ webId: string,
24
+ options?: Options,
25
+ ): Promise<void> {
26
+ const { dataset } = guaranteeOptions(options);
27
+ const profile = await getProfile(webId, options);
28
+ if (!profile.privateTypeIndex?.size || !profile.publicTypeIndex?.size) {
29
+ const profileFolder = await dataset.getResource(webId).getParentContainer();
30
+ if (profileFolder?.isError) throw profileFolder;
31
+ if (!profileFolder)
32
+ throw new Error("No folder to save the type indexes to.");
33
+ if (!profile.privateTypeIndex?.size) {
34
+ await createIndex(webId, profileFolder, dataset, true);
35
+ }
36
+ if (!profile.publicTypeIndex?.size) {
37
+ await createIndex(webId, profileFolder, dataset, false);
38
+ }
39
+ }
40
+ }
41
+
42
+ /**
43
+ * @internal
44
+ * @param webId
45
+ * @param profileFolder
46
+ * @param dataset
47
+ */
48
+ export async function createIndex(
49
+ webId,
50
+ profileFolder: Container,
51
+ dataset: ISolidLdoDataset,
52
+ isPrivate: boolean,
53
+ ) {
54
+ // Create a private type index
55
+ const createResult = await profileFolder.createChildAndOverwrite(
56
+ `${isPrivate ? "private" : "public"}_index_${v4()}`,
57
+ );
58
+ if (createResult.isError) throw createResult;
59
+ const indexResource = createResult.resource;
60
+ const wacResult = await indexResource.setWac({
61
+ agent: {
62
+ [webId]: { read: true, write: true, append: true, control: true },
63
+ },
64
+ public: {
65
+ read: isPrivate ? false : true,
66
+ write: true,
67
+ append: true,
68
+ control: true,
69
+ },
70
+ authenticated: {
71
+ read: isPrivate ? false : true,
72
+ write: true,
73
+ append: true,
74
+ control: true,
75
+ },
76
+ });
77
+ if (wacResult.isError) throw wacResult;
78
+ const transaction = dataset.startTransaction();
79
+ const cProfile = transaction
80
+ .usingType(TypeIndexProfileShapeType)
81
+ .write(dataset.getResource(webId).uri)
82
+ .fromSubject(webId);
83
+ if (isPrivate) {
84
+ cProfile.privateTypeIndex?.add({ "@id": indexResource.uri });
85
+ } else {
86
+ cProfile.publicTypeIndex?.add({ "@id": indexResource.uri });
87
+ }
88
+ const cTypeIndex = transaction
89
+ .usingType(TypeIndexDocumentShapeType)
90
+ .write(indexResource.uri)
91
+ .fromSubject(indexResource.uri);
92
+
93
+ cTypeIndex.type = set({ "@id": "ListedDocument" }, { "@id": "TypeIndex" });
94
+ const commitResult = await transaction.commitToPod();
95
+ if (commitResult.isError) throw commitResult;
96
+ }
97
+
98
+ /**
99
+ * =============================================================================
100
+ * DATASET MODIFIERS
101
+ * =============================================================================
102
+ */
103
+ interface Instances {
104
+ instance?: string[];
105
+ instanceContainer?: string[];
106
+ }
107
+
108
+ /**
109
+ * Adds an instance to a TypeRegistration
110
+ * @param indexUri The URI of the TypeIndex
111
+ * @param classUri The URI for the class the instance should be added to
112
+ * @param instances Objects representing the instances to add
113
+ * @param options Options
114
+ */
115
+ export function addRegistration(
116
+ indexUri: string,
117
+ classUri: string,
118
+ instances: Instances,
119
+ options?: Options,
120
+ ): void {
121
+ // Check to see if its already in the index
122
+ const typeRegistration = findAppropriateTypeRegistration(
123
+ indexUri,
124
+ classUri,
125
+ options,
126
+ );
127
+
128
+ // Add instances to type registration
129
+ instances.instance?.forEach((instance) => {
130
+ typeRegistration.instance?.add({ "@id": instance });
131
+ });
132
+ instances.instanceContainer?.forEach((instanceContainer) => {
133
+ typeRegistration.instanceContainer?.add({ "@id": instanceContainer });
134
+ });
135
+ }
136
+
137
+ /**
138
+ * Removes instances from a TypeRegistration
139
+ * @param indexUri The URI of the TypeIndex
140
+ * @param classUri The URI for the class the instance should be removed from
141
+ * @param instances Objects representing the instances to remove
142
+ * @param options Options
143
+ */
144
+ export async function removeRegistration(
145
+ indexUri: string,
146
+ classUri: string,
147
+ instances: Instances,
148
+ options?: Options,
149
+ ) {
150
+ // Check to see if its already in the index
151
+ const typeRegistration = findAppropriateTypeRegistration(
152
+ indexUri,
153
+ classUri,
154
+ options,
155
+ );
156
+
157
+ // Add instances to type registration
158
+ instances.instance?.forEach((instanceUri) => {
159
+ typeRegistration.instance?.delete({ "@id": instanceUri });
160
+ });
161
+ instances.instanceContainer?.forEach((instanceContainerUri) => {
162
+ typeRegistration.instanceContainer?.delete({ "@id": instanceContainerUri });
163
+ });
164
+ }
165
+
166
+ /**
167
+ * Finds a TypeRegistration inside of a type index. If it doesn't exist, it
168
+ * creates one.
169
+ * @param indexUri The URI of the typeIndex
170
+ * @param classUri The URI of the class in question for the TypeRegistration
171
+ * @param options
172
+ * @returns The Type Index
173
+ */
174
+ export function findAppropriateTypeRegistration(
175
+ indexUri: string,
176
+ classUri: string,
177
+ options?: Options,
178
+ ): TypeRegistration {
179
+ const { dataset } = guaranteeOptions(options);
180
+ // Check to see if its already in the index
181
+ const existingRegistrationsUris: NamedNode[] = dataset
182
+ .match(
183
+ null,
184
+ namedNode(RDF_TYPE),
185
+ namedNode(TYPE_REGISTRATION),
186
+ namedNode(indexUri),
187
+ )
188
+ .toArray()
189
+ .map((quad) => quad.subject) as NamedNode[];
190
+
191
+ const existingRegistrationForClassUri = existingRegistrationsUris.find(
192
+ (registrationUri) => {
193
+ return dataset.has(
194
+ quad(
195
+ registrationUri,
196
+ namedNode(FOR_CLASS),
197
+ namedNode(classUri),
198
+ namedNode(indexUri),
199
+ ),
200
+ );
201
+ },
202
+ )?.value;
203
+
204
+ let typeRegistration: TypeRegistration;
205
+ if (existingRegistrationForClassUri) {
206
+ typeRegistration = dataset
207
+ .usingType(TypeRegistrationShapeType)
208
+ .write(indexUri)
209
+ .fromSubject(existingRegistrationForClassUri);
210
+ } else {
211
+ typeRegistration = dataset
212
+ .usingType(TypeRegistrationShapeType)
213
+ .write(indexUri)
214
+ .fromSubject(`${indexUri}#${v4()}`);
215
+ typeRegistration.type = { "@id": "TypeRegistration" };
216
+ typeRegistration.forClass = { "@id": classUri };
217
+ }
218
+ return typeRegistration;
219
+ }
@@ -0,0 +1,14 @@
1
+ import { createSolidLdoDataset } from "@ldo/solid";
2
+ import type { ISolidLdoDataset } from "@ldo/solid";
3
+ import { guaranteeFetch } from "@ldo/solid/dist/util/guaranteeFetch";
4
+
5
+ export interface Options {
6
+ solidLdoDataset?: ISolidLdoDataset;
7
+ fetch?: typeof fetch;
8
+ }
9
+
10
+ export function guaranteeOptions(options?: Options) {
11
+ const fetch = guaranteeFetch(options?.fetch);
12
+ const dataset = options?.solidLdoDataset ?? createSolidLdoDataset({ fetch });
13
+ return { fetch, dataset };
14
+ }