Skip to content

Commit 380d27d

Browse files
author
Benjamin Forster
committed
add sparql query interface
1 parent ba521e2 commit 380d27d

17 files changed

Lines changed: 359 additions & 4 deletions

index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const stream = require('readable-stream')
33
const pump = require('pump')
44
const inherits = require('inherits')
55
const events = require('events')
6+
const SparqlIterator = require('sparql-iterator')
67

78
const utils = require('./lib/utils')
89
const Variable = require('./lib/Variable')
@@ -122,6 +123,14 @@ Graph.prototype.search = function (query, options, callback) {
122123
utils.collect(this.searchStream(query, options), callback)
123124
}
124125

126+
Graph.prototype.queryStream = function (query) {
127+
return new SparqlIterator(query, { hypergraph: this })
128+
}
129+
130+
Graph.prototype.query = function (query, callback) {
131+
utils.collect(this.queryStream(query), callback)
132+
}
133+
125134
Graph.prototype.generateBatch = utils.generateBatch
126135

127136
Graph.prototype.close = function (callback) {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"hyperdb": "^2.0.0",
1515
"lru": "^3.1.0",
1616
"pump": "^2.0.0",
17-
"readable-stream": "^2.3.3"
17+
"readable-stream": "^2.3.3",
18+
"sparql-iterator": "^2.0.5"
1819
},
1920
"devDependencies": {
2021
"chai": "^4.1.2",

readme.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,21 @@ Allowed options:
9595
}
9696
```
9797

98-
#### `db.search(queries, [options], callback)`
98+
#### `db.query(query, callback)`
9999

100-
Allows for Basic Graph Patterns searches where all queries must match.
101-
Expects queries to be an array of triple options of the form:
100+
Allows for querying the graph with [SPARQL](https://www.w3.org/TR/sparql11-protocol/) queries.
101+
Returns all entries that match the query.
102+
103+
SPARQL queries are implemented using [sparql-iterator](https://github.com/e-e-e/sparql-iterator) - a fork of [Linked Data Fragments Client](https://github.com/LinkedDataFragments/Client.js).
104+
105+
#### `var stream = db.queryStream(query)`
106+
107+
Returns a stream of results from the SPARQL query.
108+
109+
#### `db.search(patterns, [options], callback)`
110+
111+
Allows for Basic Graph Patterns searches where all patterns must match.
112+
Expects patterns to be an array of triple options of the form:
102113

103114
```js
104115
{

test/data/simplefoaf.ttl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
2+
3+
_:a foaf:givenname "Alice" .
4+
_:a foaf:family_name "Hacker" .
5+
6+
_:b foaf:firstname "Bob" .
7+
_:b foaf:surname "Hacker" .

test/data/sparqlIn11Minutes.ttl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
@prefix vcard: <http://www.w3.org/2006/vcard/ns#> .
2+
@prefix sn: <http://www.snee.com/hr/> .
3+
4+
sn:emp1 vcard:given-name "Heidi" .
5+
sn:emp1 vcard:family-name "Smith" .
6+
sn:emp1 vcard:title "CEO" .
7+
sn:emp1 sn:hireDate "2015-01-13" .
8+
sn:emp1 sn:completedOrientation "2015-01-30" .
9+
10+
sn:emp2 vcard:given-name "John" .
11+
sn:emp2 vcard:family-name "Smith" .
12+
sn:emp2 sn:hireDate "2015-01-28" .
13+
sn:emp2 vcard:title "Engineer" .
14+
sn:emp2 sn:completedOrientation "2015-01-30" .
15+
sn:emp2 sn:completedOrientation "2015-03-15" .
16+
17+
sn:emp3 vcard:given-name "Francis" .
18+
sn:emp3 vcard:family-name "Jones" .
19+
sn:emp3 sn:hireDate "2015-02-13" .
20+
sn:emp3 vcard:title "Vice President" .
21+
22+
sn:emp4 vcard:given-name "Jane" .
23+
sn:emp4 vcard:family-name "Berger" .
24+
sn:emp4 sn:hireDate "1000-03-10" .
25+
sn:emp4 vcard:title "Sales" .

test/queries/sparqlIn11Minutes1.rq

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
PREFIX vcard: <http://www.w3.org/2006/vcard/ns#>
2+
3+
SELECT ?person
4+
WHERE
5+
{
6+
?person vcard:family-name "Smith" .
7+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
PREFIX vcard: <http://www.w3.org/2006/vcard/ns#>
2+
PREFIX sn: <http://www.snee.com/hr/>
3+
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
4+
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
5+
6+
CONSTRUCT {
7+
?person rdf:type foaf:Person .
8+
?person foaf:givenName ?givenName .
9+
?person foaf:familyName ?familyName .
10+
?person foaf:name ?fullName .
11+
}
12+
WHERE {
13+
?person vcard:given-name ?givenName .
14+
?person vcard:family-name ?familyName .
15+
BIND(concat(?givenName," ",?familyName) AS ?fullName)
16+
}

test/queries/sparqlIn11Minutes2.rq

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
PREFIX vcard: <http://www.w3.org/2006/vcard/ns#>
2+
3+
SELECT ?person ?givenName
4+
WHERE
5+
{
6+
?person vcard:family-name "Smith" .
7+
?person vcard:given-name ?givenName .
8+
}

test/queries/sparqlIn11Minutes3.rq

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
PREFIX vcard: <http://www.w3.org/2006/vcard/ns#>
2+
PREFIX sn: <http://www.snee.com/hr/>
3+
4+
SELECT ?givenName ?familyName ?hireDate
5+
WHERE
6+
{
7+
?person vcard:given-name ?givenName .
8+
?person vcard:family-name ?familyName .
9+
?person sn:hireDate ?hireDate .
10+
}

test/queries/sparqlIn11Minutes4.rq

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
PREFIX vcard: <http://www.w3.org/2006/vcard/ns#>
2+
PREFIX sn: <http://www.snee.com/hr/>
3+
4+
SELECT ?givenName ?familyName ?hireDate
5+
WHERE
6+
{
7+
?person vcard:given-name ?givenName .
8+
?person vcard:family-name ?familyName .
9+
?person sn:hireDate ?hireDate .
10+
FILTER(?hireDate < "2015-03-01")
11+
}

0 commit comments

Comments
 (0)