@ldo/subscribable-dataset 0.0.1-alpha.16 → 0.0.1-alpha.17
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/LICENSE.txt +21 -0
- package/Readme.md +298 -0
- package/package.json +4 -4
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Jackson Morgan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/Readme.md
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
A library of RDFJS Datasets that have many uses including subscribing to node changes and making transactions on a dataset.
|
|
2
|
+
|
|
3
|
+
This library follows the [RDFJS spec for a Dataset](https://rdf.js.org/dataset-spec/).
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
```bash
|
|
7
|
+
npm i @ldo/subscribable-dataset
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
## Simple Example
|
|
11
|
+
|
|
12
|
+
```typescript
|
|
13
|
+
import { createSubscribableDataset, DatasetChanges } from "@ldo/subscribable-dataset";
|
|
14
|
+
import { Dataset } from "@rdfjs/types";
|
|
15
|
+
import { quad, namedNode, literal } from "@rdfjs/data-model";
|
|
16
|
+
|
|
17
|
+
const subscribableDataset = createSubscribableDataset([
|
|
18
|
+
quad(
|
|
19
|
+
namedNode("http://example.org/cartoons#Zuko"),
|
|
20
|
+
namedNode("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
|
|
21
|
+
namedNode("http://example.org/cartoons#Firebender")
|
|
22
|
+
),
|
|
23
|
+
]);
|
|
24
|
+
subscribableDataset.on(
|
|
25
|
+
namedNode("http://example.org/cartoons#Zuko"),
|
|
26
|
+
(currentQuads: Dataset, changes: DatasetChanges) => {
|
|
27
|
+
console.log(currentQuads.toString());
|
|
28
|
+
console.log("--------");
|
|
29
|
+
console.log(changes.added?.toString());
|
|
30
|
+
}
|
|
31
|
+
);
|
|
32
|
+
/*
|
|
33
|
+
Prints:
|
|
34
|
+
<http://example.org/cartoons#Zuko> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/cartoons#Firebender> .
|
|
35
|
+
<http://example.org/cartoons#Zuko> <http://example.org/cartoons#name> "Zuko" .
|
|
36
|
+
--------
|
|
37
|
+
<http://example.org/cartoons#Zuko> <http://example.org/cartoons#name> "Zuko" .
|
|
38
|
+
*/
|
|
39
|
+
subscribableDataset.add(
|
|
40
|
+
quad(
|
|
41
|
+
namedNode("http://example.org/cartoons#Zuko"),
|
|
42
|
+
namedNode("http://example.org/cartoons#name"),
|
|
43
|
+
literal("Zuko")
|
|
44
|
+
)
|
|
45
|
+
);
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Loading from Serialized Data
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
import { serializedToDataset, serializedToSubscribableDataset } from "@ldo/subscribable-dataset";
|
|
52
|
+
|
|
53
|
+
async function run(): Promise<void> {
|
|
54
|
+
// Create an ExtendedDataset using Turtle
|
|
55
|
+
const turtleData = `
|
|
56
|
+
@prefix : <#>.
|
|
57
|
+
@prefix elem: <http://purl.org/dc/elements/1.1/>.
|
|
58
|
+
@prefix card: </profile/card#>.
|
|
59
|
+
|
|
60
|
+
:this
|
|
61
|
+
elem:author card:me.
|
|
62
|
+
`;
|
|
63
|
+
const turtleDataset = await serializedToDataset(turtleData, {
|
|
64
|
+
baseIRI:
|
|
65
|
+
"https://jackson.solidcommunity.net/IndividualChats/jackson.solidcommunity.net/index.ttl#",
|
|
66
|
+
// NOTE: the "format" field isn't required because Turtle is the default parser
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// Create a SubcribableDataset using JSON-LD
|
|
70
|
+
const jsonLdData = [
|
|
71
|
+
{
|
|
72
|
+
"@id":
|
|
73
|
+
"https://jackson.solidcommunity.net/IndividualChats/jackson.solidcommunity.net/index.ttl#this",
|
|
74
|
+
"http://purl.org/dc/elements/1.1/author": [
|
|
75
|
+
{
|
|
76
|
+
"@id": "https://jackson.solidcommunity.net/profile/card#me",
|
|
77
|
+
},
|
|
78
|
+
],
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
"@id": "https://jackson.solidcommunity.net/profile/card#me",
|
|
82
|
+
},
|
|
83
|
+
];
|
|
84
|
+
const jsonLdDataset = await serializedToSubscribableDataset(
|
|
85
|
+
JSON.stringify(jsonLdData),
|
|
86
|
+
{
|
|
87
|
+
baseIRI:
|
|
88
|
+
"https://jackson.solidcommunity.net/IndividualChats/jackson.solidcommunity.net/index.ttl#",
|
|
89
|
+
format: "application/json-ld",
|
|
90
|
+
}
|
|
91
|
+
);
|
|
92
|
+
// Returns true because the input data describes the same triple.
|
|
93
|
+
console.log(turtleDataset.equals(jsonLdDataset));
|
|
94
|
+
}
|
|
95
|
+
run();
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## Advanced Example
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
import { createSubscribableDataset, DatasetChanges } from "@ldo/subscribable-dataset";
|
|
102
|
+
import { quad, namedNode, literal } from "@rdfjs/data-model";
|
|
103
|
+
import { Dataset } from "@rdfjs/types";
|
|
104
|
+
|
|
105
|
+
// Create an empty subscribable dataset
|
|
106
|
+
const subscribableDataset = createSubscribableDataset();
|
|
107
|
+
// Add some initial quads
|
|
108
|
+
subscribableDataset.addAll([
|
|
109
|
+
quad(
|
|
110
|
+
namedNode("http://example.org/cartoons#Zuko"),
|
|
111
|
+
namedNode("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
|
|
112
|
+
namedNode("http://example.org/cartoons#Firebender"),
|
|
113
|
+
namedNode("http://example.org/cartoons")
|
|
114
|
+
),
|
|
115
|
+
quad(
|
|
116
|
+
namedNode("http://example.org/cartoons#Zuko"),
|
|
117
|
+
namedNode("http://example.org/cartoons#name"),
|
|
118
|
+
literal("Zuko"),
|
|
119
|
+
namedNode("http://example.org/cartoons")
|
|
120
|
+
),
|
|
121
|
+
]);
|
|
122
|
+
// Set up listeners
|
|
123
|
+
// Listener that will trigger whenever a quad containing the named
|
|
124
|
+
// node "http://example.org/cartoons#Zuko" is added or removed.
|
|
125
|
+
subscribableDataset.on(
|
|
126
|
+
namedNode("http://example.org/cartoons#Zuko"),
|
|
127
|
+
(zukoQuads: Dataset, changes: DatasetChanges) => {
|
|
128
|
+
console.log("ZUKO NODE CHANGED ============");
|
|
129
|
+
console.log(zukoQuads.toString());
|
|
130
|
+
console.log("Added Quads:");
|
|
131
|
+
console.log(changes.added?.toString());
|
|
132
|
+
console.log("Removed Quads:");
|
|
133
|
+
console.log(changes.removed?.toString());
|
|
134
|
+
console.log("\n\n");
|
|
135
|
+
}
|
|
136
|
+
);
|
|
137
|
+
// Listener that will trigger whenever a quad containing the named
|
|
138
|
+
// node "http://example.org/cartoons" is added or removed. This is
|
|
139
|
+
// useful for keeping track of the cartoons graph.
|
|
140
|
+
subscribableDataset.on(
|
|
141
|
+
namedNode("http://example.org/cartoons"),
|
|
142
|
+
(cartoonGraphQuads: Dataset, changes: DatasetChanges) => {
|
|
143
|
+
console.log("CARTOON GRAPH CHANGED ============");
|
|
144
|
+
console.log(cartoonGraphQuads.toString());
|
|
145
|
+
console.log("Added Quads:");
|
|
146
|
+
console.log(changes.added?.toString());
|
|
147
|
+
console.log("Removed Quads:");
|
|
148
|
+
console.log(changes.removed?.toString());
|
|
149
|
+
console.log("\n\n");
|
|
150
|
+
}
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
// Modify the dataset
|
|
154
|
+
/*
|
|
155
|
+
Prints:
|
|
156
|
+
CARTOON GRAPH CHANGED ============
|
|
157
|
+
<http://example.org/cartoons#Zuko> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/cartoons#Firebender> <http://example.org/cartoons> .
|
|
158
|
+
<http://example.org/cartoons#Zuko> <http://example.org/cartoons#name> "Zuko" <http://example.org/cartoons> .
|
|
159
|
+
<http://example.org/cartoons#Katara> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/cartoons#Waterbender> <http://example.org/cartoons> .
|
|
160
|
+
<http://example.org/cartoons#Katara> <http://example.org/cartoons#name> "Katara" <http://example.org/cartoons> .
|
|
161
|
+
|
|
162
|
+
Added Quads:
|
|
163
|
+
<http://example.org/cartoons#Katara> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/cartoons#Waterbender> <http://example.org/cartoons> .
|
|
164
|
+
<http://example.org/cartoons#Katara> <http://example.org/cartoons#name> "Katara" <http://example.org/cartoons> .
|
|
165
|
+
|
|
166
|
+
Removed Quads:
|
|
167
|
+
undefined
|
|
168
|
+
*/
|
|
169
|
+
subscribableDataset.addAll([
|
|
170
|
+
quad(
|
|
171
|
+
namedNode("http://example.org/cartoons#Katara"),
|
|
172
|
+
namedNode("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
|
|
173
|
+
namedNode("http://example.org/cartoons#Waterbender"),
|
|
174
|
+
namedNode("http://example.org/cartoons")
|
|
175
|
+
),
|
|
176
|
+
quad(
|
|
177
|
+
namedNode("http://example.org/cartoons#Katara"),
|
|
178
|
+
namedNode("http://example.org/cartoons#name"),
|
|
179
|
+
literal("Katara"),
|
|
180
|
+
namedNode("http://example.org/cartoons")
|
|
181
|
+
),
|
|
182
|
+
]);
|
|
183
|
+
|
|
184
|
+
/*
|
|
185
|
+
Prints:
|
|
186
|
+
ZUKO NODE CHANGED ============
|
|
187
|
+
<http://example.org/cartoons#Zuko> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/cartoons#Firebender> <http://example.org/cartoons> .
|
|
188
|
+
<http://example.org/cartoons#Zuko> <http://example.org/cartoons#name> "Zuko" <http://example.org/cartoons> .
|
|
189
|
+
<http://example.org/cartoons#Zuko> <http://example.org/cartoons#hasEnemy> <http://example.org/cartoons#Katara> <http://example.org/cartoons> .
|
|
190
|
+
<http://example.org/cartoons#Katara> <http://example.org/cartoons#hasEnemy> <http://example.org/cartoons#Zuko> <http://example.org/cartoons> .
|
|
191
|
+
|
|
192
|
+
Added Quads:
|
|
193
|
+
<http://example.org/cartoons#Katara> <http://example.org/cartoons#hasEnemy> <http://example.org/cartoons#Zuko> <http://example.org/cartoons> .
|
|
194
|
+
<http://example.org/cartoons#Zuko> <http://example.org/cartoons#hasEnemy> <http://example.org/cartoons#Katara> <http://example.org/cartoons> .
|
|
195
|
+
|
|
196
|
+
Removed Quads:
|
|
197
|
+
undefined
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
CARTOON GRAPH CHANGED ============
|
|
201
|
+
<http://example.org/cartoons#Zuko> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/cartoons#Firebender> <http://example.org/cartoons> .
|
|
202
|
+
<http://example.org/cartoons#Zuko> <http://example.org/cartoons#name> "Zuko" <http://example.org/cartoons> .
|
|
203
|
+
<http://example.org/cartoons#Zuko> <http://example.org/cartoons#hasEnemy> <http://example.org/cartoons#Katara> <http://example.org/cartoons> .
|
|
204
|
+
<http://example.org/cartoons#Katara> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/cartoons#Waterbender> <http://example.org/cartoons> .
|
|
205
|
+
<http://example.org/cartoons#Katara> <http://example.org/cartoons#name> "Katara" <http://example.org/cartoons> .
|
|
206
|
+
<http://example.org/cartoons#Katara> <http://example.org/cartoons#hasEnemy> <http://example.org/cartoons#Zuko> <http://example.org/cartoons> .
|
|
207
|
+
|
|
208
|
+
Added Quads:
|
|
209
|
+
<http://example.org/cartoons#Katara> <http://example.org/cartoons#hasEnemy> <http://example.org/cartoons#Zuko> <http://example.org/cartoons> .
|
|
210
|
+
<http://example.org/cartoons#Zuko> <http://example.org/cartoons#hasEnemy> <http://example.org/cartoons#Katara> <http://example.org/cartoons> .
|
|
211
|
+
|
|
212
|
+
Removed Quads:
|
|
213
|
+
undefined
|
|
214
|
+
*/
|
|
215
|
+
subscribableDataset.addAll([
|
|
216
|
+
quad(
|
|
217
|
+
namedNode("http://example.org/cartoons#Katara"),
|
|
218
|
+
namedNode("http://example.org/cartoons#hasEnemy"),
|
|
219
|
+
namedNode("http://example.org/cartoons#Zuko"),
|
|
220
|
+
namedNode("http://example.org/cartoons")
|
|
221
|
+
),
|
|
222
|
+
quad(
|
|
223
|
+
namedNode("http://example.org/cartoons#Zuko"),
|
|
224
|
+
namedNode("http://example.org/cartoons#hasEnemy"),
|
|
225
|
+
namedNode("http://example.org/cartoons#Katara"),
|
|
226
|
+
namedNode("http://example.org/cartoons")
|
|
227
|
+
),
|
|
228
|
+
]);
|
|
229
|
+
|
|
230
|
+
// If there are many operation you want to do at once, use transactions.
|
|
231
|
+
// An update will not be triggered until the transaction is committed.
|
|
232
|
+
const transactionalDataset = subscribableDataset.startTransaction();
|
|
233
|
+
// Delete all triples with a "hasEnemy" predicate
|
|
234
|
+
transactionalDataset.deleteMatches(
|
|
235
|
+
undefined,
|
|
236
|
+
namedNode("http://example.org/cartoons#hasEnemy"),
|
|
237
|
+
undefined,
|
|
238
|
+
undefined
|
|
239
|
+
);
|
|
240
|
+
// Add "hasFrient" predicate
|
|
241
|
+
transactionalDataset.addAll([
|
|
242
|
+
quad(
|
|
243
|
+
namedNode("http://example.org/cartoons#Katara"),
|
|
244
|
+
namedNode("http://example.org/cartoons#hasFriend"),
|
|
245
|
+
namedNode("http://example.org/cartoons#Zuko"),
|
|
246
|
+
namedNode("http://example.org/cartoons")
|
|
247
|
+
),
|
|
248
|
+
quad(
|
|
249
|
+
namedNode("http://example.org/cartoons#Zuko"),
|
|
250
|
+
namedNode("http://example.org/cartoons#hasFriend"),
|
|
251
|
+
namedNode("http://example.org/cartoons#Katara"),
|
|
252
|
+
namedNode("http://example.org/cartoons")
|
|
253
|
+
),
|
|
254
|
+
]);
|
|
255
|
+
/*
|
|
256
|
+
Prints:
|
|
257
|
+
ZUKO NODE CHANGED ============
|
|
258
|
+
<http://example.org/cartoons#Zuko> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/cartoons#Firebender> <http://example.org/cartoons> .
|
|
259
|
+
<http://example.org/cartoons#Zuko> <http://example.org/cartoons#name> "Zuko" <http://example.org/cartoons> .
|
|
260
|
+
<http://example.org/cartoons#Zuko> <http://example.org/cartoons#hasFriend> <http://example.org/cartoons#Katara> <http://example.org/cartoons> .
|
|
261
|
+
<http://example.org/cartoons#Katara> <http://example.org/cartoons#hasFriend> <http://example.org/cartoons#Zuko> <http://example.org/cartoons> .
|
|
262
|
+
|
|
263
|
+
Added Quads:
|
|
264
|
+
<http://example.org/cartoons#Katara> <http://example.org/cartoons#hasFriend> <http://example.org/cartoons#Zuko> <http://example.org/cartoons> .
|
|
265
|
+
<http://example.org/cartoons#Zuko> <http://example.org/cartoons#hasFriend> <http://example.org/cartoons#Katara> <http://example.org/cartoons> .
|
|
266
|
+
|
|
267
|
+
Removed Quads:
|
|
268
|
+
<http://example.org/cartoons#Katara> <http://example.org/cartoons#hasEnemy> <http://example.org/cartoons#Zuko> <http://example.org/cartoons> .
|
|
269
|
+
<http://example.org/cartoons#Zuko> <http://example.org/cartoons#hasEnemy> <http://example.org/cartoons#Katara> <http://example.org/cartoons> .
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
CARTOON GRAPH CHANGED ============
|
|
273
|
+
<http://example.org/cartoons#Zuko> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/cartoons#Firebender> <http://example.org/cartoons> .
|
|
274
|
+
<http://example.org/cartoons#Zuko> <http://example.org/cartoons#name> "Zuko" <http://example.org/cartoons> .
|
|
275
|
+
<http://example.org/cartoons#Zuko> <http://example.org/cartoons#hasFriend> <http://example.org/cartoons#Katara> <http://example.org/cartoons> .
|
|
276
|
+
<http://example.org/cartoons#Katara> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/cartoons#Waterbender> <http://example.org/cartoons> .
|
|
277
|
+
<http://example.org/cartoons#Katara> <http://example.org/cartoons#name> "Katara" <http://example.org/cartoons> .
|
|
278
|
+
<http://example.org/cartoons#Katara> <http://example.org/cartoons#hasFriend> <http://example.org/cartoons#Zuko> <http://example.org/cartoons> .
|
|
279
|
+
|
|
280
|
+
Added Quads:
|
|
281
|
+
<http://example.org/cartoons#Katara> <http://example.org/cartoons#hasFriend> <http://example.org/cartoons#Zuko> <http://example.org/cartoons> .
|
|
282
|
+
<http://example.org/cartoons#Zuko> <http://example.org/cartoons#hasFriend> <http://example.org/cartoons#Katara> <http://example.org/cartoons> .
|
|
283
|
+
|
|
284
|
+
Removed Quads:
|
|
285
|
+
<http://example.org/cartoons#Katara> <http://example.org/cartoons#hasEnemy> <http://example.org/cartoons#Zuko> <http://example.org/cartoons> .
|
|
286
|
+
<http://example.org/cartoons#Zuko> <http://example.org/cartoons#hasEnemy> <http://example.org/cartoons#Katara> <http://example.org/cartoons> .
|
|
287
|
+
*/
|
|
288
|
+
transactionalDataset.commit();
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
## Sponsorship
|
|
292
|
+
This project was made possible by a grant from NGI Zero Entrust via nlnet. Learn more on the [NLnet project page](https://nlnet.nl/project/SolidUsableApps/).
|
|
293
|
+
|
|
294
|
+
[<img src="https://nlnet.nl/logo/banner.png" alt="nlnet foundation logo" width="300" />](https://nlnet.nl/)
|
|
295
|
+
[<img src="https://nlnet.nl/image/logos/NGI0Entrust_tag.svg" alt="NGI Zero Entrust Logo" width="300" />](https://nlnet.nl/)
|
|
296
|
+
|
|
297
|
+
## Liscense
|
|
298
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ldo/subscribable-dataset",
|
|
3
|
-
"version": "0.0.1-alpha.
|
|
3
|
+
"version": "0.0.1-alpha.17",
|
|
4
4
|
"description": "An RDFJS dataset implementation that can be subscribed to for updates",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -33,8 +33,8 @@
|
|
|
33
33
|
"ts-node": "^9.1.1"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@ldo/dataset": "^0.0.1-alpha.
|
|
37
|
-
"@ldo/rdf-utils": "^0.0.1-alpha.
|
|
36
|
+
"@ldo/dataset": "^0.0.1-alpha.17",
|
|
37
|
+
"@ldo/rdf-utils": "^0.0.1-alpha.17"
|
|
38
38
|
},
|
|
39
39
|
"files": [
|
|
40
40
|
"dist",
|
|
@@ -43,5 +43,5 @@
|
|
|
43
43
|
"publishConfig": {
|
|
44
44
|
"access": "public"
|
|
45
45
|
},
|
|
46
|
-
"gitHead": "
|
|
46
|
+
"gitHead": "e4af4f641afe4afa069a9fa411fccea7eacf9e22"
|
|
47
47
|
}
|