-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for large array modifications (#67)
- Loading branch information
1 parent
9c42634
commit e5bd229
Showing
14 changed files
with
1,046 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
# Working with arrays | ||
|
||
The LDkit library provides a simple and intuitive way to work with arrays in | ||
linked data contexts. This section focuses on the manipulation of array | ||
elements, including adding and removing elements. | ||
|
||
## Initializing and Defining Models | ||
|
||
Before working with arrays, initialize your data source and define your data | ||
schema. Examples below make use of a Director schema, having a director name and | ||
a list of movies: | ||
|
||
```typescript | ||
import { type Context, createLens, createNamespace } from "ldkit"; | ||
|
||
// Create a custom namespace | ||
const ns = createNamespace( | ||
{ | ||
"iri": "http://ns/", | ||
"prefix": "ns", | ||
"terms": [ | ||
"name", | ||
"movie", | ||
], | ||
} as const, | ||
); | ||
|
||
// Create a schema | ||
const DirectorSchema = { | ||
name: ns.name, | ||
movies: { | ||
"@id": ns.movie, | ||
"@array": true, | ||
}, | ||
} as const; | ||
|
||
const Directors = createLens(DirectorSchema); | ||
|
||
// Add a director with a an empty list of movies | ||
await Directors.insert({ | ||
$id: "https://Quentin_Tarantino", | ||
name: "Quentin Tarantino", | ||
movies: [], | ||
}); | ||
``` | ||
|
||
## Updating arrays | ||
|
||
To modify array elements, use the `Lens.update` method. This method supports | ||
different operations. | ||
|
||
1. **Setting an Array**: Replace the entine array with a new set of elements | ||
|
||
```typescript | ||
await Directors.update({ | ||
$id: "https://Quentin_Tarantino", | ||
movies: { | ||
$set: ["Pulp Fiction", "Reservoir Dogs"], | ||
}, | ||
}); | ||
``` | ||
|
||
2. **Adding Elements**: Append new elements to the array | ||
|
||
```typescript | ||
await Directors.update({ | ||
$id: "https://Quentin_Tarantino", | ||
movies: { | ||
$add: ["Kill Bill", "Kill Bill 2"], | ||
}, | ||
}); // The `movies` is now ["Pulp Fiction", "Reservoir Dogs", "Kill Bill", "Kill Bill 2"] | ||
``` | ||
|
||
3. **Removing Elements**: Remove specific elements from the array | ||
|
||
```typescript | ||
await Directors.update({ | ||
$id: "https://Quentin_Tarantino", | ||
movies: { | ||
$remove: ["Reservoir Dogs"], | ||
}, | ||
}); // The `movies` is now ["Pulp Fiction", "Kill Bill", "Kill Bill 2"] | ||
``` | ||
|
||
4. **Setting an Empty Array**: Clear all elements from the array | ||
|
||
```typescript | ||
await Directors.update({ | ||
$id: "https://Quentin_Tarantino", | ||
movies: { | ||
$set: [], // Remove all movies | ||
}, | ||
}); | ||
``` | ||
|
||
## Working with Multiple Entitites | ||
|
||
You can also perform array updates on multiple entities simultaneously using a | ||
single SPARQL query. | ||
|
||
```typescript | ||
await Directors.insert({ | ||
$id: "https://David_Fincher", | ||
name: "David Fincher", | ||
movies: ["Fight Club", "The Social Network"], | ||
}); | ||
|
||
await Directors.update({ | ||
$id: "https://Quentin_Tarantino", | ||
movies: { | ||
$set: ["Inglorious Basterds"], | ||
}, | ||
}, { | ||
$id: "https://David_Fincher", | ||
movies: { | ||
$add: ["The Curious Case of Benjamin Button"], | ||
}, | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { type Context, createLens, createNamespace } from "ldkit"; | ||
import { DataFactory, N3 } from "ldkit/rdf"; | ||
import { QueryEngine as Comunica } from "npm:@comunica/[email protected]"; | ||
|
||
// Create a custom namespace | ||
const ns = createNamespace( | ||
{ | ||
"iri": "http://ns/", | ||
"prefix": "ns", | ||
"terms": [ | ||
"name", | ||
"movie", | ||
], | ||
} as const, | ||
); | ||
|
||
// Create a schema | ||
const DirectorSchema = { | ||
name: ns.name, | ||
movies: { | ||
"@id": ns.movie, | ||
"@array": true, | ||
}, | ||
} as const; | ||
|
||
// Create in memory data store and context for query engine | ||
const store = new N3.Store(undefined, { | ||
factory: new DataFactory(), | ||
}); | ||
const context: Context = { | ||
sources: [store], | ||
}; | ||
const engine = new Comunica(); | ||
|
||
// Create a resource using the data schema and context above | ||
const Directors = createLens(DirectorSchema, context, engine); | ||
|
||
// Add a director with a list of some movies | ||
await Directors.insert({ | ||
$id: "https://Quentin_Tarantino", | ||
name: "Quentin Tarantino", | ||
movies: ["Pulp Fiction", "Reservoir Dogs"], | ||
}); | ||
|
||
// Add a movie to the list of movies | ||
await Directors.update({ | ||
$id: "https://Quentin_Tarantino", | ||
movies: { | ||
$add: ["Kill Bill", "Kill Bill 2"], | ||
}, | ||
}); | ||
|
||
// Remove a movie from the list of movies | ||
await Directors.update({ | ||
$id: "https://Quentin_Tarantino", | ||
movies: { | ||
$remove: ["Reservoir Dogs"], | ||
}, | ||
}); | ||
|
||
// Print the list of movies | ||
const tarantino = await Directors.findByIri("https://Quentin_Tarantino"); | ||
console.log("Tarantino movies", tarantino?.movies); | ||
|
||
// Add another director with a list of some movies | ||
await Directors.insert({ | ||
$id: "https://David_Fincher", | ||
name: "David Fincher", | ||
movies: ["Fight Club", "The Social Network"], | ||
}); | ||
|
||
// Modify the list of movies for both directors | ||
await Directors.update({ | ||
$id: "https://Quentin_Tarantino", | ||
movies: { | ||
$set: [], // Remove all movies | ||
}, | ||
}, { | ||
$id: "https://David_Fincher", | ||
movies: { | ||
$add: ["The Curious Case of Benjamin Button"], | ||
}, | ||
}); | ||
|
||
// Print the list of movies of the other director | ||
const fincher = await Directors.findByIri("https://David_Fincher"); | ||
console.log("Fincher movies", fincher?.movies); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
{ | ||
"importMap": "../import_map.json", | ||
"tasks": { | ||
"main": "deno run --allow-net --allow-env ./main.ts" | ||
"main": "deno run --allow-net --allow-env ./main.ts", | ||
"arrays": "deno run -A ./arrays.ts" | ||
}, | ||
"lock": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.