Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 52 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,35 +159,65 @@ db.jsonld.del(manu["@id"], function(err) {
### Searching with LevelGraph

__LevelGraph-JSONLD__ does not support searching for objects, because
that problem is already solved by __LevelGraph__ itself, like these:
that problem is already solved by __LevelGraph__ itself. This example
search finds friends living near Paris:
```javascript
var nested = {
var manu = {
"@context": {
"name": "http://xmlns.com/foaf/0.1/name"
, "knows": "http://xmlns.com/foaf/0.1/knows"
"@vocab": "http://xmlns.com/foaf/0.1/"
, "homepage": { "@type": "@id" }
, "knows": { "@type": "@id" }
, "based_near": { "@type": "@id" }
}
, "@id": "http://matteocollina.com"
, "name": "matteo"
, "knows": [{
"name": "daniele"
, "@id": "http://manu.sporny.org#person"
, "name": "Manu Sporny"
, "homepage": "http://manu.sporny.org/"
, "knows": [
{
"@id": "https://my-profile.eu/people/deiu/card#me",
"name": "Andrei Vlad Sambra",
"based_near": "http://dbpedia.org/resource/Paris"
}, {
"name": "lucio"
}]
};
"@id": "http://melvincarvalho.com/#me",
"name": "Melvin Carvalho",
"based_near": "http://dbpedia.org/resource/Honolulu"
}, {
"@id": "http://bblfish.net/people/henry/card#me",
"name": "Henry Story",
"based_near": "http://dbpedia.org/resource/Paris"
}, {
"@id": "http://presbrey.mit.edu/foaf#presbrey",
"name": "Joe Presbrey",
"based_near": "http://dbpedia.org/resource/Cambridge"
}
]
}

db.jsonld.put(nested, function(err) {
var paris = "http://dbpedia.org/resource/Paris";

db.jsonld.put(manu, function(){
db.join([{
subject: db.v("person")
, predicate: "http://xmlns.com/foaf/0.1/knows"
, object: db.v("friend")
subject: manu["@id"],
predicate: "http://xmlns.com/foaf/0.1/knows",
object: db.v("webid")
}, {
subject: db.v("webid"),
predicate: "http://xmlns.com/foaf/0.1/based_near",
object: paris
}, {
subject: db.v("friend")
, predicate: "http://xmlns.com/foaf/0.1/knows"
, object: "daniele"
}], function(err, solutions) {
// The solutions will be:
// 1. { person: "http://matteocollina.com", friend: "_:abcde" }
// 1. { person: "http://matteocollina.com", friend: "_:efghi" }
subject: db.v("webid"),
predicate: "http://xmlns.com/foaf/0.1/name",
object: db.v("name")
}
], function(err, solution) {
// solution contains
// [{
// webid: "http://bblfish.net/people/henry/card#me",
// name: "Henry Story"
// }, {
// webid: "https://my-profile.eu/people/deiu/card#me",
// name: "Andrei Vlad Sambra"
// }]
});
});
```
Expand Down
7 changes: 2 additions & 5 deletions test/fixture/manu.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
{
"@context": {
"name": "http://xmlns.com/foaf/0.1/name"
, "homepage": {
"@id": "http://xmlns.com/foaf/0.1/homepage"
, "@type": "@id"
}
"@vocab": "http://xmlns.com/foaf/0.1/"
, "homepage": { "@type": "@id" }
}
, "@id": "http://manu.sporny.org#person"
, "name": "Manu Sporny"
Expand Down
71 changes: 71 additions & 0 deletions test/search_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@

var level = require("level-test")()
, graph = require("levelgraph")
, jsonld = require("../");

describe("jsonld.put", function() {

var db, gang, manu;

beforeEach(function() {
db = jsonld(graph(level()));
manu = fixture("manu.json");
manu["@context"]["knows"] = { "@type": "@id" };
manu["@context"]["based_near"] = { "@type": "@id" };
manu["knows"] = [
{
"@id": "https://my-profile.eu/people/deiu/card#me",
"name": "Andrei Vlad Sambra",
"based_near": "http://dbpedia.org/resource/Paris"
}, {
"@id": "http://melvincarvalho.com/#me",
"name": "Melvin Carvalho",
"based_near": "http://dbpedia.org/resource/Honolulu"
}, {
"@id": "http://bblfish.net/people/henry/card#me",
"name": "Henry Story",
"based_near": "http://dbpedia.org/resource/Paris"
}, {
"@id": "http://presbrey.mit.edu/foaf#presbrey",
"name": "Joe Presbrey",
"based_near": "http://dbpedia.org/resource/Cambridge"
}
];
});

afterEach(function(done) {
db.close(done);
});

it("should find homies in Paris", function(done) {
var paris = "http://dbpedia.org/resource/Paris";
var parisians = [{
webid: "http://bblfish.net/people/henry/card#me",
name: "Henry Story"
}, {
webid: "https://my-profile.eu/people/deiu/card#me",
name: "Andrei Vlad Sambra"
}];

db.jsonld.put(manu, function(){
db.join([{
subject: manu["@id"],
predicate: "http://xmlns.com/foaf/0.1/knows",
object: db.v("webid")
}, {
subject: db.v("webid"),
predicate: "http://xmlns.com/foaf/0.1/based_near",
object: paris
}, {
subject: db.v("webid"),
predicate: "http://xmlns.com/foaf/0.1/name",
object: db.v("name")
}
], function(err, solution) {
expect(solution).to.eql(parisians);
done();
});
});
});

});