Skip to content

Commit ba7b27b

Browse files
author
☮ elf Pavlik ☮
committed
Merge pull request #9 from elf-pavlik/bugfix/search-example
search example from README doesn't work
2 parents 94bec5f + 062b754 commit ba7b27b

3 files changed

Lines changed: 125 additions & 27 deletions

File tree

README.md

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -159,35 +159,65 @@ db.jsonld.del(manu["@id"], function(err) {
159159
### Searching with LevelGraph
160160

161161
__LevelGraph-JSONLD__ does not support searching for objects, because
162-
that problem is already solved by __LevelGraph__ itself, like these:
162+
that problem is already solved by __LevelGraph__ itself. This example
163+
search finds friends living near Paris:
163164
```javascript
164-
var nested = {
165+
var manu = {
165166
"@context": {
166-
"name": "http://xmlns.com/foaf/0.1/name"
167-
, "knows": "http://xmlns.com/foaf/0.1/knows"
167+
"@vocab": "http://xmlns.com/foaf/0.1/"
168+
, "homepage": { "@type": "@id" }
169+
, "knows": { "@type": "@id" }
170+
, "based_near": { "@type": "@id" }
168171
}
169-
, "@id": "http://matteocollina.com"
170-
, "name": "matteo"
171-
, "knows": [{
172-
"name": "daniele"
172+
, "@id": "http://manu.sporny.org#person"
173+
, "name": "Manu Sporny"
174+
, "homepage": "http://manu.sporny.org/"
175+
, "knows": [
176+
{
177+
"@id": "https://my-profile.eu/people/deiu/card#me",
178+
"name": "Andrei Vlad Sambra",
179+
"based_near": "http://dbpedia.org/resource/Paris"
173180
}, {
174-
"name": "lucio"
175-
}]
176-
};
181+
"@id": "http://melvincarvalho.com/#me",
182+
"name": "Melvin Carvalho",
183+
"based_near": "http://dbpedia.org/resource/Honolulu"
184+
}, {
185+
"@id": "http://bblfish.net/people/henry/card#me",
186+
"name": "Henry Story",
187+
"based_near": "http://dbpedia.org/resource/Paris"
188+
}, {
189+
"@id": "http://presbrey.mit.edu/foaf#presbrey",
190+
"name": "Joe Presbrey",
191+
"based_near": "http://dbpedia.org/resource/Cambridge"
192+
}
193+
]
194+
}
177195

178-
db.jsonld.put(nested, function(err) {
196+
var paris = "http://dbpedia.org/resource/Paris";
197+
198+
db.jsonld.put(manu, function(){
179199
db.join([{
180-
subject: db.v("person")
181-
, predicate: "http://xmlns.com/foaf/0.1/knows"
182-
, object: db.v("friend")
200+
subject: manu["@id"],
201+
predicate: "http://xmlns.com/foaf/0.1/knows",
202+
object: db.v("webid")
203+
}, {
204+
subject: db.v("webid"),
205+
predicate: "http://xmlns.com/foaf/0.1/based_near",
206+
object: paris
183207
}, {
184-
subject: db.v("friend")
185-
, predicate: "http://xmlns.com/foaf/0.1/knows"
186-
, object: "daniele"
187-
}], function(err, solutions) {
188-
// The solutions will be:
189-
// 1. { person: "http://matteocollina.com", friend: "_:abcde" }
190-
// 1. { person: "http://matteocollina.com", friend: "_:efghi" }
208+
subject: db.v("webid"),
209+
predicate: "http://xmlns.com/foaf/0.1/name",
210+
object: db.v("name")
211+
}
212+
], function(err, solution) {
213+
// solution contains
214+
// [{
215+
// webid: "http://bblfish.net/people/henry/card#me",
216+
// name: "Henry Story"
217+
// }, {
218+
// webid: "https://my-profile.eu/people/deiu/card#me",
219+
// name: "Andrei Vlad Sambra"
220+
// }]
191221
});
192222
});
193223
```

test/fixture/manu.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
{
22
"@context": {
3-
"name": "http://xmlns.com/foaf/0.1/name"
4-
, "homepage": {
5-
"@id": "http://xmlns.com/foaf/0.1/homepage"
6-
, "@type": "@id"
7-
}
3+
"@vocab": "http://xmlns.com/foaf/0.1/"
4+
, "homepage": { "@type": "@id" }
85
}
96
, "@id": "http://manu.sporny.org#person"
107
, "name": "Manu Sporny"

test/search_spec.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
var level = require("level-test")()
3+
, graph = require("levelgraph")
4+
, jsonld = require("../");
5+
6+
describe("jsonld.put", function() {
7+
8+
var db, gang, manu;
9+
10+
beforeEach(function() {
11+
db = jsonld(graph(level()));
12+
manu = fixture("manu.json");
13+
manu["@context"]["knows"] = { "@type": "@id" };
14+
manu["@context"]["based_near"] = { "@type": "@id" };
15+
manu["knows"] = [
16+
{
17+
"@id": "https://my-profile.eu/people/deiu/card#me",
18+
"name": "Andrei Vlad Sambra",
19+
"based_near": "http://dbpedia.org/resource/Paris"
20+
}, {
21+
"@id": "http://melvincarvalho.com/#me",
22+
"name": "Melvin Carvalho",
23+
"based_near": "http://dbpedia.org/resource/Honolulu"
24+
}, {
25+
"@id": "http://bblfish.net/people/henry/card#me",
26+
"name": "Henry Story",
27+
"based_near": "http://dbpedia.org/resource/Paris"
28+
}, {
29+
"@id": "http://presbrey.mit.edu/foaf#presbrey",
30+
"name": "Joe Presbrey",
31+
"based_near": "http://dbpedia.org/resource/Cambridge"
32+
}
33+
];
34+
});
35+
36+
afterEach(function(done) {
37+
db.close(done);
38+
});
39+
40+
it("should find homies in Paris", function(done) {
41+
var paris = "http://dbpedia.org/resource/Paris";
42+
var parisians = [{
43+
webid: "http://bblfish.net/people/henry/card#me",
44+
name: "Henry Story"
45+
}, {
46+
webid: "https://my-profile.eu/people/deiu/card#me",
47+
name: "Andrei Vlad Sambra"
48+
}];
49+
50+
db.jsonld.put(manu, function(){
51+
db.join([{
52+
subject: manu["@id"],
53+
predicate: "http://xmlns.com/foaf/0.1/knows",
54+
object: db.v("webid")
55+
}, {
56+
subject: db.v("webid"),
57+
predicate: "http://xmlns.com/foaf/0.1/based_near",
58+
object: paris
59+
}, {
60+
subject: db.v("webid"),
61+
predicate: "http://xmlns.com/foaf/0.1/name",
62+
object: db.v("name")
63+
}
64+
], function(err, solution) {
65+
expect(solution).to.eql(parisians);
66+
done();
67+
});
68+
});
69+
});
70+
71+
});

0 commit comments

Comments
 (0)