@ng-org/orm 0.1.2-alpha.7 → 0.1.2-alpha.9
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.
- package/README.md +74 -92
- package/dist/connector/applyPatches.d.ts +13 -4
- package/dist/connector/applyPatches.d.ts.map +1 -1
- package/dist/connector/applyPatches.js +9 -5
- package/dist/connector/discrete/discreteOrmSubscriptionHandler.d.ts +156 -0
- package/dist/connector/discrete/discreteOrmSubscriptionHandler.d.ts.map +1 -0
- package/dist/connector/discrete/{discreteOrmConnectionHandler.js → discreteOrmSubscriptionHandler.js} +130 -19
- package/dist/connector/getObjects.js +2 -2
- package/dist/connector/initNg.d.ts +35 -0
- package/dist/connector/initNg.d.ts.map +1 -1
- package/dist/connector/initNg.js +35 -0
- package/dist/connector/insertObject.js +2 -2
- package/dist/connector/ormSubscriptionHandler.d.ts +166 -0
- package/dist/connector/ormSubscriptionHandler.d.ts.map +1 -0
- package/dist/connector/{ormConnectionHandler.js → ormSubscriptionHandler.js} +157 -32
- package/dist/frontendAdapters/react/useDiscrete.d.ts +89 -69
- package/dist/frontendAdapters/react/useDiscrete.d.ts.map +1 -1
- package/dist/frontendAdapters/react/useDiscrete.js +103 -81
- package/dist/frontendAdapters/react/useShape.d.ts +55 -55
- package/dist/frontendAdapters/react/useShape.d.ts.map +1 -1
- package/dist/frontendAdapters/react/useShape.js +71 -73
- package/dist/frontendAdapters/svelte/useDiscrete.svelte.d.ts +80 -71
- package/dist/frontendAdapters/svelte/useDiscrete.svelte.d.ts.map +1 -1
- package/dist/frontendAdapters/svelte/useDiscrete.svelte.js +102 -91
- package/dist/frontendAdapters/svelte/useShape.svelte.d.ts +70 -64
- package/dist/frontendAdapters/svelte/useShape.svelte.d.ts.map +1 -1
- package/dist/frontendAdapters/svelte/useShape.svelte.js +73 -62
- package/dist/frontendAdapters/svelte4/index.d.ts +5 -0
- package/dist/frontendAdapters/svelte4/index.d.ts.map +1 -0
- package/dist/frontendAdapters/svelte4/index.js +12 -0
- package/dist/frontendAdapters/svelte4/useDiscrete.svelte.d.ts +85 -0
- package/dist/frontendAdapters/svelte4/useDiscrete.svelte.d.ts.map +1 -0
- package/dist/frontendAdapters/svelte4/useDiscrete.svelte.js +124 -0
- package/dist/frontendAdapters/svelte4/useShape.svelte.d.ts +76 -0
- package/dist/frontendAdapters/svelte4/useShape.svelte.d.ts.map +1 -0
- package/dist/frontendAdapters/svelte4/useShape.svelte.js +84 -0
- package/dist/frontendAdapters/vue/useDiscrete.d.ts +87 -80
- package/dist/frontendAdapters/vue/useDiscrete.d.ts.map +1 -1
- package/dist/frontendAdapters/vue/useDiscrete.js +96 -84
- package/dist/frontendAdapters/vue/useShape.d.ts +57 -63
- package/dist/frontendAdapters/vue/useShape.d.ts.map +1 -1
- package/dist/frontendAdapters/vue/useShape.js +59 -64
- package/dist/index.d.ts +6 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -3
- package/dist/types.d.ts +17 -7
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +11 -2
- package/package.json +7 -3
- package/dist/connector/discrete/discreteOrmConnectionHandler.d.ts +0 -45
- package/dist/connector/discrete/discreteOrmConnectionHandler.d.ts.map +0 -1
- package/dist/connector/ormConnectionHandler.d.ts +0 -48
- package/dist/connector/ormConnectionHandler.d.ts.map +0 -1
|
@@ -8,77 +8,88 @@
|
|
|
8
8
|
// according to those terms.
|
|
9
9
|
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
|
10
10
|
import { onDestroy } from "svelte";
|
|
11
|
-
import { useDeepSignal
|
|
12
|
-
import {
|
|
11
|
+
import { useDeepSignal } from "@ng-org/alien-deepsignals/svelte";
|
|
12
|
+
import { OrmSubscription } from "../../connector/ormSubscriptionHandler.js";
|
|
13
13
|
/**
|
|
14
|
-
*
|
|
14
|
+
* Svelte 5 hook to subscribe to RDF data in the graph database using a shape, see {@link ShapeType}.
|
|
15
15
|
*
|
|
16
|
-
* Returns a {@link DeepSignalSet}
|
|
16
|
+
* Returns a {@link DeepSignalSet} that contain the objects matching the shape and that are within the scope.
|
|
17
17
|
* Establishes a 2-way binding: Modifications to the object are immediately committed,
|
|
18
|
-
* changes coming from the
|
|
18
|
+
* changes coming from the engine (or other components) cause an immediate rerender.
|
|
19
19
|
*
|
|
20
|
-
* @param shape The {@link ShapeType} the objects should have (generated by the shex-orm tool).
|
|
20
|
+
* @param shape The {@link ShapeType} the objects should have (generated by the `@ng-org/shex-orm` tool).
|
|
21
21
|
* @param scope The {@link Scope} in which the objects should be.
|
|
22
22
|
* @returns A {@link DeepSignalSet} containing the objects matching the shape type and scope.
|
|
23
23
|
*
|
|
24
24
|
* @example
|
|
25
|
-
```svelte
|
|
26
|
-
<script lang="ts">
|
|
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
|
-
|
|
25
|
+
* ```svelte
|
|
26
|
+
* <script lang="ts">
|
|
27
|
+
* // Gets all expense objects with `@id` <s1 IRI> or <s2 IRI> and `@graph` <g1 IRI> or <g2 IRI>
|
|
28
|
+
* const expenses: DeepSignalSet<Expense> = useShape(ExpenseShapeType,
|
|
29
|
+
* {graphs: ["<g1 IRI>", "<g2 IRI>"],
|
|
30
|
+
* subjects: ["<s1 IRI>", "<s2 IRI>"]});
|
|
31
|
+
*
|
|
32
|
+
* const expensesSorted = computed(() => expenses.sort((a, b) =>
|
|
33
|
+
* a.dateOfPurchase.localeCompare(b.dateOfPurchase)
|
|
34
|
+
* ));
|
|
35
|
+
*
|
|
36
|
+
* const createExpense = () => {
|
|
37
|
+
* expenses.add({
|
|
38
|
+
* "@graph": `<graph IRI>`,
|
|
39
|
+
* "@type": "http://example.org/Expense",
|
|
40
|
+
* "@id": "", // Assigns id automatically, if set to "".
|
|
41
|
+
* title: "New expense",
|
|
42
|
+
* dateOfPurchase: obj.dateOfPurchase ?? new Date().toISOString(),
|
|
43
|
+
* });
|
|
44
|
+
* };
|
|
45
|
+
*
|
|
46
|
+
*
|
|
47
|
+
* // Note that if you use `@id` (the subject IRI) as key, you need to ensure that it is unique within your scope.
|
|
48
|
+
* // If it is not, use the combination of `@graph` and `@id`.
|
|
49
|
+
* </script>
|
|
50
|
+
*
|
|
51
|
+
* <section>
|
|
52
|
+
* <div>
|
|
53
|
+
* <button on:click={() => createExpense()}>
|
|
54
|
+
* + Add expense
|
|
55
|
+
* </button>
|
|
56
|
+
*
|
|
57
|
+
* {# if expensesSorted.length === 0}
|
|
58
|
+
* <p>
|
|
59
|
+
* No expense yet.
|
|
60
|
+
* </p>
|
|
61
|
+
* {:else}
|
|
62
|
+
* {#each expensesSorted as expense, index (expense['@id']) }
|
|
63
|
+
* <ExpenseCard
|
|
64
|
+
* expense={expense}
|
|
65
|
+
* />
|
|
66
|
+
* {/each}
|
|
67
|
+
* {/if}
|
|
68
|
+
* </div>
|
|
69
|
+
* </section>
|
|
70
|
+
* ```
|
|
71
|
+
*
|
|
72
|
+
* ---
|
|
73
|
+
* In the ExpenseCard component:
|
|
74
|
+
* ```svelte
|
|
75
|
+
* <script lang="ts">
|
|
76
|
+
* let {
|
|
77
|
+
* expense,
|
|
78
|
+
* }: { expense: DeepSignal<Expense>; } = $props();
|
|
79
|
+
* </script>
|
|
80
|
+
*
|
|
81
|
+
* <div>
|
|
82
|
+
* <input
|
|
83
|
+
* value={expense.title ?? ""}
|
|
84
|
+
* oninput={(event) => {expense.title = event.currentTarget.value}}
|
|
85
|
+
* />
|
|
86
|
+
* </div>
|
|
87
|
+
* ```
|
|
77
88
|
*/
|
|
78
|
-
export function useShape(shape, scope
|
|
79
|
-
const { signalObject: rootSignal, close } =
|
|
89
|
+
export function useShape(shape, scope) {
|
|
90
|
+
const { signalObject: rootSignal, close } = OrmSubscription.getOrCreate(shape, scope);
|
|
80
91
|
onDestroy(close);
|
|
81
|
-
const
|
|
82
|
-
return
|
|
92
|
+
const shapeSignalSet = useDeepSignal(rootSignal);
|
|
93
|
+
return shapeSignalSet;
|
|
83
94
|
}
|
|
84
95
|
export default useShape;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/frontendAdapters/svelte4/index.ts"],"names":[],"mappings":"AAUA,OAAO,QAAQ,MAAM,sBAAsB,CAAC;AAC5C,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AACtD,OAAO,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAC3D,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,KAAK,mBAAmB,EAAE,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Copyright (c) 2025 Laurin Weger, Par le Peuple, NextGraph.org developers
|
|
2
|
+
// All rights reserved.
|
|
3
|
+
// Licensed under the Apache License, Version 2.0
|
|
4
|
+
// <LICENSE-APACHE2 or http://www.apache.org/licenses/LICENSE-2.0>
|
|
5
|
+
// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
|
|
6
|
+
// at your option. All files in the project carrying such
|
|
7
|
+
// notice may not be copied, modified, or distributed except
|
|
8
|
+
// according to those terms.
|
|
9
|
+
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
|
10
|
+
import useShape from "./useShape.svelte.js";
|
|
11
|
+
import { useDiscrete } from "./useDiscrete.svelte.js";
|
|
12
|
+
export { useShape, useDiscrete };
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { UseDeepSignalResult } from "@ng-org/alien-deepsignals/svelte4";
|
|
2
|
+
import { DiscreteRootArray, DiscreteRootObject } from "../../types.ts";
|
|
3
|
+
/**
|
|
4
|
+
* Svelte 3/4 hook to subscribe to discrete (JSON) CRDT documents.
|
|
5
|
+
* You can modify the returned object like any other JSON object. Changes are immediately
|
|
6
|
+
* reflected in the CRDT.
|
|
7
|
+
*
|
|
8
|
+
* Establishes a 2-way binding: Modifications to the object are immediately committed,
|
|
9
|
+
* changes coming from the backend (or other components) cause an immediate rerender.
|
|
10
|
+
*
|
|
11
|
+
* In comparison to {@link svelte4UseShape}, discrete CRDTs are untyped.
|
|
12
|
+
* You can put any JSON data inside and need to validate the schema yourself.
|
|
13
|
+
*
|
|
14
|
+
* @param documentIdOrPromise The IRI of the CRDT document or a promise to that.
|
|
15
|
+
* @returns The reactive JSON object of the CRDT document.
|
|
16
|
+
*
|
|
17
|
+
*@example
|
|
18
|
+
* ```svelte
|
|
19
|
+
* <script lang="ts">
|
|
20
|
+
*
|
|
21
|
+
* // We assume you have created a CRDT document already, as below.
|
|
22
|
+
* // const documentId = await ng.doc_create(
|
|
23
|
+
* // session_id,
|
|
24
|
+
* // crdt, // "Automerge" | "YMap" | "YArray"
|
|
25
|
+
* // crdt === "Automerge" ? "data:json" : crdt === "YMap ? "data:map" : "data:array",
|
|
26
|
+
* // "store",
|
|
27
|
+
* // undefined
|
|
28
|
+
*
|
|
29
|
+
* const doc = useDiscrete(documentIdPromise);
|
|
30
|
+
*
|
|
31
|
+
* // If the CRDT document is still empty, we need to initialize it.
|
|
32
|
+
* $: if (doc && !doc.expenses) {
|
|
33
|
+
* doc.expenses = [];
|
|
34
|
+
* }
|
|
35
|
+
*
|
|
36
|
+
* // Call doc.expenses.push({title: "Example title"}), to add new elements.
|
|
37
|
+
*
|
|
38
|
+
*
|
|
39
|
+
* // Note that we use expense["@id"] as a key in the expense list.
|
|
40
|
+
* // Every object added to a CRDT array gets a stable `@id` property assigned
|
|
41
|
+
* // which you can use for referencing objects in arrays even as
|
|
42
|
+
* // objects are removed from the array. The ID is an IRI with the schema `<documentId>:d:<object-specific id>`.
|
|
43
|
+
* // Since the `@id` is generated in the backend, the object is preliminarily
|
|
44
|
+
* // given a mock id which will be replaced immediately
|
|
45
|
+
* </script>
|
|
46
|
+
*
|
|
47
|
+
* <section>
|
|
48
|
+
* <div>
|
|
49
|
+
* {#if !doc}
|
|
50
|
+
* Loading...
|
|
51
|
+
* {:else if doc.expenses.length === 0}
|
|
52
|
+
* <p>
|
|
53
|
+
* Nothing tracked yet - log your first purchase to kick things off.
|
|
54
|
+
* </p>
|
|
55
|
+
* {:else}
|
|
56
|
+
* {#each doc.expenses as expense, index (expense['@id']) }
|
|
57
|
+
* <ExpenseCard
|
|
58
|
+
* expense={expense}
|
|
59
|
+
* />
|
|
60
|
+
* {/each}
|
|
61
|
+
* {/if}
|
|
62
|
+
* </div>
|
|
63
|
+
* </section>
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* ---
|
|
67
|
+
* In the ExpenseCard component:
|
|
68
|
+
* ```svelte
|
|
69
|
+
* let {
|
|
70
|
+
* expense = $bindable(),
|
|
71
|
+
* }: { expense: Expense; } = $props();
|
|
72
|
+
* </script>
|
|
73
|
+
*
|
|
74
|
+
* <div>
|
|
75
|
+
* <input
|
|
76
|
+
* value={expense.title ?? ""}
|
|
77
|
+
* oninput={(event) => {expense.title = event.currentTarget?.value ?? ""}}
|
|
78
|
+
* placeholder="Expense title"
|
|
79
|
+
* />
|
|
80
|
+
* </div>
|
|
81
|
+
* ```
|
|
82
|
+
*
|
|
83
|
+
*/
|
|
84
|
+
export declare function useDiscrete(documentIdOrPromise: string | Promise<string>): UseDeepSignalResult<DiscreteRootArray | DiscreteRootObject | undefined>;
|
|
85
|
+
//# sourceMappingURL=useDiscrete.svelte.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useDiscrete.svelte.d.ts","sourceRoot":"","sources":["../../../src/frontendAdapters/svelte4/useDiscrete.svelte.ts"],"names":[],"mappings":"AAWA,OAAO,EAEH,mBAAmB,EACtB,MAAM,mCAAmC,CAAC;AAE3C,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAEvE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgFG;AACH,wBAAgB,WAAW,CACvB,mBAAmB,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,GAC9C,mBAAmB,CAAC,iBAAiB,GAAG,kBAAkB,GAAG,SAAS,CAAC,CAgCzE"}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// Copyright (c) 2026 Laurin Weger, Par le Peuple, NextGraph.org developers
|
|
2
|
+
// All rights reserved.
|
|
3
|
+
// Licensed under the Apache License, Version 2.0
|
|
4
|
+
// <LICENSE-APACHE2 or http://www.apache.org/licenses/LICENSE-2.0>
|
|
5
|
+
// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
|
|
6
|
+
// at your option. All files in the project carrying such
|
|
7
|
+
// notice may not be copied, modified, or distributed except
|
|
8
|
+
// according to those terms.
|
|
9
|
+
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
|
10
|
+
import { onDestroy } from "svelte";
|
|
11
|
+
import { useDeepSignal, } from "@ng-org/alien-deepsignals/svelte4";
|
|
12
|
+
import { DiscreteOrmSubscription } from "../../connector/discrete/discreteOrmSubscriptionHandler.js";
|
|
13
|
+
/**
|
|
14
|
+
* Svelte 3/4 hook to subscribe to discrete (JSON) CRDT documents.
|
|
15
|
+
* You can modify the returned object like any other JSON object. Changes are immediately
|
|
16
|
+
* reflected in the CRDT.
|
|
17
|
+
*
|
|
18
|
+
* Establishes a 2-way binding: Modifications to the object are immediately committed,
|
|
19
|
+
* changes coming from the backend (or other components) cause an immediate rerender.
|
|
20
|
+
*
|
|
21
|
+
* In comparison to {@link svelte4UseShape}, discrete CRDTs are untyped.
|
|
22
|
+
* You can put any JSON data inside and need to validate the schema yourself.
|
|
23
|
+
*
|
|
24
|
+
* @param documentIdOrPromise The IRI of the CRDT document or a promise to that.
|
|
25
|
+
* @returns The reactive JSON object of the CRDT document.
|
|
26
|
+
*
|
|
27
|
+
*@example
|
|
28
|
+
* ```svelte
|
|
29
|
+
* <script lang="ts">
|
|
30
|
+
*
|
|
31
|
+
* // We assume you have created a CRDT document already, as below.
|
|
32
|
+
* // const documentId = await ng.doc_create(
|
|
33
|
+
* // session_id,
|
|
34
|
+
* // crdt, // "Automerge" | "YMap" | "YArray"
|
|
35
|
+
* // crdt === "Automerge" ? "data:json" : crdt === "YMap ? "data:map" : "data:array",
|
|
36
|
+
* // "store",
|
|
37
|
+
* // undefined
|
|
38
|
+
*
|
|
39
|
+
* const doc = useDiscrete(documentIdPromise);
|
|
40
|
+
*
|
|
41
|
+
* // If the CRDT document is still empty, we need to initialize it.
|
|
42
|
+
* $: if (doc && !doc.expenses) {
|
|
43
|
+
* doc.expenses = [];
|
|
44
|
+
* }
|
|
45
|
+
*
|
|
46
|
+
* // Call doc.expenses.push({title: "Example title"}), to add new elements.
|
|
47
|
+
*
|
|
48
|
+
*
|
|
49
|
+
* // Note that we use expense["@id"] as a key in the expense list.
|
|
50
|
+
* // Every object added to a CRDT array gets a stable `@id` property assigned
|
|
51
|
+
* // which you can use for referencing objects in arrays even as
|
|
52
|
+
* // objects are removed from the array. The ID is an IRI with the schema `<documentId>:d:<object-specific id>`.
|
|
53
|
+
* // Since the `@id` is generated in the backend, the object is preliminarily
|
|
54
|
+
* // given a mock id which will be replaced immediately
|
|
55
|
+
* </script>
|
|
56
|
+
*
|
|
57
|
+
* <section>
|
|
58
|
+
* <div>
|
|
59
|
+
* {#if !doc}
|
|
60
|
+
* Loading...
|
|
61
|
+
* {:else if doc.expenses.length === 0}
|
|
62
|
+
* <p>
|
|
63
|
+
* Nothing tracked yet - log your first purchase to kick things off.
|
|
64
|
+
* </p>
|
|
65
|
+
* {:else}
|
|
66
|
+
* {#each doc.expenses as expense, index (expense['@id']) }
|
|
67
|
+
* <ExpenseCard
|
|
68
|
+
* expense={expense}
|
|
69
|
+
* />
|
|
70
|
+
* {/each}
|
|
71
|
+
* {/if}
|
|
72
|
+
* </div>
|
|
73
|
+
* </section>
|
|
74
|
+
* ```
|
|
75
|
+
*
|
|
76
|
+
* ---
|
|
77
|
+
* In the ExpenseCard component:
|
|
78
|
+
* ```svelte
|
|
79
|
+
* let {
|
|
80
|
+
* expense = $bindable(),
|
|
81
|
+
* }: { expense: Expense; } = $props();
|
|
82
|
+
* </script>
|
|
83
|
+
*
|
|
84
|
+
* <div>
|
|
85
|
+
* <input
|
|
86
|
+
* value={expense.title ?? ""}
|
|
87
|
+
* oninput={(event) => {expense.title = event.currentTarget?.value ?? ""}}
|
|
88
|
+
* placeholder="Expense title"
|
|
89
|
+
* />
|
|
90
|
+
* </div>
|
|
91
|
+
* ```
|
|
92
|
+
*
|
|
93
|
+
*/
|
|
94
|
+
export function useDiscrete(documentIdOrPromise) {
|
|
95
|
+
let connection;
|
|
96
|
+
let isDestroyed = false;
|
|
97
|
+
const objectPromise = new Promise((resolve) => {
|
|
98
|
+
const init = (docId) => {
|
|
99
|
+
if (isDestroyed)
|
|
100
|
+
return;
|
|
101
|
+
connection = DiscreteOrmSubscription.getOrCreate(docId);
|
|
102
|
+
connection.readyPromise.then(() => {
|
|
103
|
+
if (isDestroyed) {
|
|
104
|
+
connection?.close();
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
resolve(connection.signalObject);
|
|
108
|
+
});
|
|
109
|
+
};
|
|
110
|
+
if (typeof documentIdOrPromise === "string") {
|
|
111
|
+
init(documentIdOrPromise);
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
documentIdOrPromise.then(init);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
onDestroy(() => {
|
|
118
|
+
isDestroyed = true;
|
|
119
|
+
if (connection) {
|
|
120
|
+
connection.close();
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
return useDeepSignal(objectPromise);
|
|
124
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { type Scope } from "../../types.ts";
|
|
2
|
+
import type { BaseType, ShapeType } from "@ng-org/shex-orm";
|
|
3
|
+
import { type UseDeepSignalResult } from "@ng-org/alien-deepsignals/svelte4";
|
|
4
|
+
export type { UseDeepSignalResult } from "@ng-org/alien-deepsignals/svelte4";
|
|
5
|
+
/** Extended result including the originating root signal wrapper from shape logic. */
|
|
6
|
+
export interface UseShapeRuneResult<T extends object> extends UseDeepSignalResult<T> {
|
|
7
|
+
root: any;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Svelte 3/4 hook to subscribe to RDF data in the graph database using a shape, see {@link ShapeType}.
|
|
11
|
+
*
|
|
12
|
+
* Returns a {@link DeepSignalSet} store containing the objects matching the shape and that are within the scope.
|
|
13
|
+
* Establishes a 2-way binding: Modifications to the object are immediately committed,
|
|
14
|
+
* changes coming from the backend (or other components) cause an immediate rerender.
|
|
15
|
+
*
|
|
16
|
+
* @param shape The {@link ShapeType} the objects should have (generated by the shex-orm tool).
|
|
17
|
+
* @param scope The {@link Scope} in which the objects should be.
|
|
18
|
+
* @returns A {@link DeepSignalSet} containing the objects matching the shape type and scope.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```svelte
|
|
22
|
+
* <script lang="ts">
|
|
23
|
+
* // Gets all expense objects with `@id` <s1 IRI> or <s2 IRI> and `@graph` <g1 IRI> or <g2 IRI>
|
|
24
|
+
* const expenses: DeepSignalSet<Expense> = useShape(ExpenseShape,
|
|
25
|
+
* {graphs: ["<g1 IRI>", "<g2 IRI>"],
|
|
26
|
+
* subjects: ["<s1 IRI>", "<s2 IRI>"]});
|
|
27
|
+
*
|
|
28
|
+
* const expensesSorted = computed(() => expenses.sort((a, b) =>
|
|
29
|
+
* a.dateOfPurchase.localeCompare(b.dateOfPurchase)
|
|
30
|
+
* ));
|
|
31
|
+
*
|
|
32
|
+
* // Call expenses.add({"@graph": "<g1 or g2 IRI>", "@id": "", title: "Example title"}), to add new elements.
|
|
33
|
+
* // Leave `@id` an empty string to auto-generate a subject IRI (adjust your scope accordingly).
|
|
34
|
+
*
|
|
35
|
+
* // Not that if you use `@id` (the subject IRI) as key, you need to ensure that it is unique within your scope.
|
|
36
|
+
* // If it is not, use the combination of `@graph` and `@id`.
|
|
37
|
+
* </script>
|
|
38
|
+
*
|
|
39
|
+
* <section>
|
|
40
|
+
* <div>
|
|
41
|
+
* {# if expensesSorted.length === 0}
|
|
42
|
+
* <p>
|
|
43
|
+
* No expense yet.
|
|
44
|
+
* </p>
|
|
45
|
+
* {:else}
|
|
46
|
+
* {#each expensesSorted as expense, index (expense['@id']) }
|
|
47
|
+
* <ExpenseCard
|
|
48
|
+
* expense={expense}
|
|
49
|
+
* />
|
|
50
|
+
* {/each}
|
|
51
|
+
* {/if}
|
|
52
|
+
* </div>
|
|
53
|
+
* </section>
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* ---
|
|
57
|
+
* In the ExpenseCard component:
|
|
58
|
+
* ```svelte
|
|
59
|
+
*
|
|
60
|
+
* let {
|
|
61
|
+
* expense = $bindable(),
|
|
62
|
+
* }: { expense: Expense; } = $props();
|
|
63
|
+
* </script>
|
|
64
|
+
*
|
|
65
|
+
* <div>
|
|
66
|
+
* <input
|
|
67
|
+
* value={expense.title ?? ""}
|
|
68
|
+
* oninput={(event) => {expense.title = event.currentTarget!.value}}
|
|
69
|
+
* placeholder="Expense title"
|
|
70
|
+
* />
|
|
71
|
+
* </div>
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
export declare function useShape<T extends BaseType>(shape: ShapeType<T>, scope: Scope): UseShapeRuneResult<Set<T>>;
|
|
75
|
+
export default useShape;
|
|
76
|
+
//# sourceMappingURL=useShape.svelte.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useShape.svelte.d.ts","sourceRoot":"","sources":["../../../src/frontendAdapters/svelte4/useShape.svelte.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,KAAK,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAE5C,OAAO,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC5D,OAAO,EAEH,KAAK,mBAAmB,EAC3B,MAAM,mCAAmC,CAAC;AAG3C,YAAY,EAAE,mBAAmB,EAAE,MAAM,mCAAmC,CAAC;AAE7E,sFAAsF;AACtF,MAAM,WAAW,kBAAkB,CAAC,CAAC,SAAS,MAAM,CAChD,SAAQ,mBAAmB,CAAC,CAAC,CAAC;IAC9B,IAAI,EAAE,GAAG,CAAC;CACb;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgEG;AACH,wBAAgB,QAAQ,CAAC,CAAC,SAAS,QAAQ,EACvC,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC,EACnB,KAAK,EAAE,KAAK,GACb,kBAAkB,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAU5B;AAED,eAAe,QAAQ,CAAC"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// Copyright (c) 2025 Laurin Weger, Par le Peuple, NextGraph.org developers
|
|
2
|
+
// All rights reserved.
|
|
3
|
+
// Licensed under the Apache License, Version 2.0
|
|
4
|
+
// <LICENSE-APACHE2 or http://www.apache.org/licenses/LICENSE-2.0>
|
|
5
|
+
// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
|
|
6
|
+
// at your option. All files in the project carrying such
|
|
7
|
+
// notice may not be copied, modified, or distributed except
|
|
8
|
+
// according to those terms.
|
|
9
|
+
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
|
10
|
+
import { onDestroy } from "svelte";
|
|
11
|
+
import { useDeepSignal, } from "@ng-org/alien-deepsignals/svelte4";
|
|
12
|
+
import { OrmSubscription } from "../../connector/ormSubscriptionHandler.js";
|
|
13
|
+
/**
|
|
14
|
+
* Svelte 3/4 hook to subscribe to RDF data in the graph database using a shape, see {@link ShapeType}.
|
|
15
|
+
*
|
|
16
|
+
* Returns a {@link DeepSignalSet} store containing the objects matching the shape and that are within the scope.
|
|
17
|
+
* Establishes a 2-way binding: Modifications to the object are immediately committed,
|
|
18
|
+
* changes coming from the backend (or other components) cause an immediate rerender.
|
|
19
|
+
*
|
|
20
|
+
* @param shape The {@link ShapeType} the objects should have (generated by the shex-orm tool).
|
|
21
|
+
* @param scope The {@link Scope} in which the objects should be.
|
|
22
|
+
* @returns A {@link DeepSignalSet} containing the objects matching the shape type and scope.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```svelte
|
|
26
|
+
* <script lang="ts">
|
|
27
|
+
* // Gets all expense objects with `@id` <s1 IRI> or <s2 IRI> and `@graph` <g1 IRI> or <g2 IRI>
|
|
28
|
+
* const expenses: DeepSignalSet<Expense> = useShape(ExpenseShape,
|
|
29
|
+
* {graphs: ["<g1 IRI>", "<g2 IRI>"],
|
|
30
|
+
* subjects: ["<s1 IRI>", "<s2 IRI>"]});
|
|
31
|
+
*
|
|
32
|
+
* const expensesSorted = computed(() => expenses.sort((a, b) =>
|
|
33
|
+
* a.dateOfPurchase.localeCompare(b.dateOfPurchase)
|
|
34
|
+
* ));
|
|
35
|
+
*
|
|
36
|
+
* // Call expenses.add({"@graph": "<g1 or g2 IRI>", "@id": "", title: "Example title"}), to add new elements.
|
|
37
|
+
* // Leave `@id` an empty string to auto-generate a subject IRI (adjust your scope accordingly).
|
|
38
|
+
*
|
|
39
|
+
* // Not that if you use `@id` (the subject IRI) as key, you need to ensure that it is unique within your scope.
|
|
40
|
+
* // If it is not, use the combination of `@graph` and `@id`.
|
|
41
|
+
* </script>
|
|
42
|
+
*
|
|
43
|
+
* <section>
|
|
44
|
+
* <div>
|
|
45
|
+
* {# if expensesSorted.length === 0}
|
|
46
|
+
* <p>
|
|
47
|
+
* No expense yet.
|
|
48
|
+
* </p>
|
|
49
|
+
* {:else}
|
|
50
|
+
* {#each expensesSorted as expense, index (expense['@id']) }
|
|
51
|
+
* <ExpenseCard
|
|
52
|
+
* expense={expense}
|
|
53
|
+
* />
|
|
54
|
+
* {/each}
|
|
55
|
+
* {/if}
|
|
56
|
+
* </div>
|
|
57
|
+
* </section>
|
|
58
|
+
* ```
|
|
59
|
+
*
|
|
60
|
+
* ---
|
|
61
|
+
* In the ExpenseCard component:
|
|
62
|
+
* ```svelte
|
|
63
|
+
*
|
|
64
|
+
* let {
|
|
65
|
+
* expense = $bindable(),
|
|
66
|
+
* }: { expense: Expense; } = $props();
|
|
67
|
+
* </script>
|
|
68
|
+
*
|
|
69
|
+
* <div>
|
|
70
|
+
* <input
|
|
71
|
+
* value={expense.title ?? ""}
|
|
72
|
+
* oninput={(event) => {expense.title = event.currentTarget!.value}}
|
|
73
|
+
* placeholder="Expense title"
|
|
74
|
+
* />
|
|
75
|
+
* </div>
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export function useShape(shape, scope) {
|
|
79
|
+
const { signalObject: rootSignal, close } = OrmSubscription.getOrCreate(shape, scope);
|
|
80
|
+
onDestroy(close);
|
|
81
|
+
const ds = useDeepSignal(rootSignal);
|
|
82
|
+
return { root: rootSignal, ...ds };
|
|
83
|
+
}
|
|
84
|
+
export default useShape;
|