Skip to content

Commit 8e3e8d3

Browse files
committed
Merge pull request #10 from elf-pavlik/issue/datatype
v0.3.0-wip
2 parents ba7b27b + 0cec113 commit 8e3e8d3

16 files changed

Lines changed: 879 additions & 426 deletions

README.md

Lines changed: 108 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ LevelGraph-JSONLD
33

44
![Logo](https://github.com/mcollina/node-levelgraph/raw/master/logo.png)
55

6+
[![Build Status](https://travis-ci.org/mcollina/levelgraph-jsonld.png)](https://travis-ci.org/mcollina/levelgraph-jsonld)
7+
[![Coverage Status](https://coveralls.io/repos/mcollina/levelgraph-jsonld/badge.png)](https://coveralls.io/r/mcollina/levelgraph-jsonld)
8+
[![Dependency Status](https://david-dm.org/mcollina/levelgraph-jsonld.png?theme=shields.io)](https://david-dm.org/mcollina/levelgraph-jsonld)
9+
610
__LevelGraph-JSONLD__ is a plugin for
711
[LevelGraph](http://github.com/mcollina/levelgraph) that adds the
812
ability to store, retrieve and delete JSON-LD objects.
913
In fact, it is a full-bown Object-Document-Mapper (ODM) for
1014
__LevelGraph__.
1115

12-
[![Build
13-
Status](https://travis-ci.org/mcollina/levelgraph-jsonld.png)](https://travis-ci.org/mcollina/levelgraph-jsonld)
14-
1516
## Install on Node.js
1617

1718
```shell
@@ -30,28 +31,33 @@ WORK IN PROGRESS! [#3](http://github.com/mcollina/levelgraph-jsonld/issues/3)
3031

3132
Adding support for JSON-LD to LevelGraph is easy:
3233
```javascript
33-
var levelgraph = require("levelgraph")
34-
, jsonld = require("levelgraph-jsonld")
35-
, db = jsonld(levelgraph("yourdb"));
34+
var levelgraph = require('levelgraph'),
35+
jsonld = require('levelgraph-jsonld'),
36+
db = jsonld(levelgraph('yourdb'));
3637
```
3738

3839
### Put
3940

4041
Please keep in mind that LevelGraph-JSONLD __doesn't store the original
41-
JSON-LD document but decomposes it into triples__! Storing triples from JSON-LD document is extremely easy:
42+
JSON-LD document but decomposes it into triples__! It stores literals
43+
double quoted with datatype if other then string. If you use plain
44+
LevelGraph methods, instead trying to match number `42` you need to try
45+
matching `"42"^^<http://www.w3.org/2001/XMLSchema#integer>`
46+
47+
Storing triples from JSON-LD document is extremely easy:
4248
```javascript
4349
var manu = {
44-
"@context": {
45-
"name": "http://xmlns.com/foaf/0.1/name"
46-
, "homepage": {
47-
"@id": "http://xmlns.com/foaf/0.1/homepage"
48-
, "@type": "@id"
49-
}
50+
"@context": {
51+
"name": "http://xmlns.com/foaf/0.1/name",
52+
"homepage": {
53+
"@id": "http://xmlns.com/foaf/0.1/homepage",
54+
"@type": "@id"
5055
}
51-
, "@id": "http://manu.sporny.org#person"
52-
, "name": "Manu Sporny"
53-
, "homepage": "http://manu.sporny.org/"
54-
}
56+
},
57+
"@id": "http://manu.sporny.org#person",
58+
"name": "Manu Sporny",
59+
"homepage": "http://manu.sporny.org/"
60+
};
5561

5662
db.jsonld.put(manu, function(err, obj) {
5763
// do something after the obj is inserted
@@ -61,35 +67,35 @@ db.jsonld.put(manu, function(err, obj) {
6167
if the top level objects have no `'@id'` key, one will be generated for
6268
each, using a UUID and the `'base'` argument, like so:
6369
```javascript
64-
delete manu["@id"];
65-
db.jsonld.put(manu, { base: "http://this/is/an/iri" }, function(err, obj) {
66-
// obj["@id"] will be something like
70+
delete manu['@id'];
71+
db.jsonld.put(manu, { base: 'http://this/is/an/iri' }, function(err, obj) {
72+
// obj['@id'] will be something like
6773
// http://this/is/an/iri/b1e783b0-eda6-11e2-9540-d7575689f4bc
6874
});
6975
```
7076

7177
`'base'` can also be specified when you create the db:
7278
```javascript
73-
var levelgraph = require("levelgraph")
74-
, jsonld = require("levelgraph-jsonld")
75-
, opts = { base: "http://matteocollina.com/base" }
76-
, db = jsonld(levelgraph("yourdb"), opts);
79+
var levelgraph = require('levelgraph'),
80+
jsonld = require('levelgraph-jsonld'),
81+
opts = { base: 'http://matteocollina.com/base' },
82+
db = jsonld(levelgraph('yourdb'), opts);
7783
```
7884

7985
__LevelGraph-JSONLD__ also support nested objects, like so:
8086
```javascript
8187
var nested = {
82-
"@context": {
83-
"name": "http://xmlns.com/foaf/0.1/name"
84-
, "knows": "http://xmlns.com/foaf/0.1/knows"
85-
}
86-
, "@id": "http://matteocollina.com"
87-
, "name": "matteo"
88-
, "knows": [{
89-
"name": "daniele"
90-
}, {
91-
"name": "lucio"
92-
}]
88+
"@context": {
89+
"name": "http://xmlns.com/foaf/0.1/name",
90+
"knows": "http://xmlns.com/foaf/0.1/knows"
91+
},
92+
"@id": "http://matteocollina.com",
93+
"name": "Matteo",
94+
"knows": [{
95+
"name": "Daniele"
96+
}, {
97+
"name": "Lucio"
98+
}]
9399
};
94100

95101
db.jsonld.put(nested, function(err, obj) {
@@ -101,7 +107,7 @@ db.jsonld.put(nested, function(err, obj) {
101107

102108
Retrieving a JSON-LD object from the store requires its `'@id'`:
103109
```javascript
104-
db.jsonld.get(manu["@id"], { "@context": manu["@context"] }, function(err, obj) {
110+
db.jsonld.get(manu['@id'], { '@context': manu['@context'] }, function(err, obj) {
105111
// obj will be the very same of the manu object
106112
});
107113
```
@@ -113,35 +119,35 @@ As with `'put'` it correctly support nested objects. If nested objects didn't or
113119
them as *blank node identifiers*:
114120
```javascript
115121
var nested = {
116-
"@context": {
117-
"name": "http://xmlns.com/foaf/0.1/name"
118-
, "knows": "http://xmlns.com/foaf/0.1/knows"
119-
}
120-
, "@id": "http://matteocollina.com"
121-
, "name": "matteo"
122-
, "knows": [{
123-
"name": "daniele"
124-
}, {
125-
"name": "lucio"
126-
}]
122+
"@context": {
123+
"name": "http://xmlns.com/foaf/0.1/name",
124+
"knows": "http://xmlns.com/foaf/0.1/knows"
125+
},
126+
"@id": "http://matteocollina.com",
127+
"name": "Matteo",
128+
"knows": [{
129+
"name": "Daniele"
130+
}, {
131+
"name": "Lucio"
132+
}]
127133
};
128134

129135
db.jsonld.put(nested, function(err, obj) {
130136
// obj will be
131137
// {
132-
// "@context": {
133-
// "name": "http://xmlns.com/foaf/0.1/name"
134-
// , "knows": "http://xmlns.com/foaf/0.1/knows"
135-
// }
136-
// , "@id": "http://matteocollina.com"
137-
// , "name": "matteo"
138-
// , "knows": [{
139-
// "@id": "_:7053c150-5fea-11e3-a62e-adadc4e3df79"
140-
// , "name": "daniele"
141-
// }, {
142-
// "@id": "_:9d2bb59d-3baf-42ff-ba5d-9f8eab34ada5"
143-
// "name": "lucio"
144-
// }]
138+
// "@context": {
139+
// "name": "http://xmlns.com/foaf/0.1/name",
140+
// "knows": "http://xmlns.com/foaf/0.1/knows"
141+
// },
142+
// "@id": "http://matteocollina.com",
143+
// "name": "Matteo",
144+
// "knows": [{
145+
// "@id": "_:7053c150-5fea-11e3-a62e-adadc4e3df79",
146+
// "name": "Daniele"
147+
// }, {
148+
// "@id": "_:9d2bb59d-3baf-42ff-ba5d-9f8eab34ada5",
149+
// "name": "Lucio"
150+
// }]
145151
// }
146152
});
147153
```
@@ -151,7 +157,7 @@ db.jsonld.put(nested, function(err, obj) {
151157
In order to delete an object, you can just pass it's `'@id'` to the
152158
`'@del'` method:
153159
```javascript
154-
db.jsonld.del(manu["@id"], function(err) {
160+
db.jsonld.del(manu['@id'], function(err) {
155161
// do something after it is deleted!
156162
});
157163
```
@@ -163,60 +169,58 @@ that problem is already solved by __LevelGraph__ itself. This example
163169
search finds friends living near Paris:
164170
```javascript
165171
var manu = {
166-
"@context": {
167-
"@vocab": "http://xmlns.com/foaf/0.1/"
168-
, "homepage": { "@type": "@id" }
169-
, "knows": { "@type": "@id" }
170-
, "based_near": { "@type": "@id" }
171-
}
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"
180-
}, {
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-
}
172+
"@context": {
173+
"@vocab": "http://xmlns.com/foaf/0.1/",
174+
"homepage": { "@type": "@id" },
175+
"knows": { "@type": "@id" },
176+
"based_near": { "@type": "@id" }
177+
},
178+
"@id": "http://manu.sporny.org#person",
179+
"name": "Manu Sporny",
180+
"homepage": "http://manu.sporny.org/",
181+
"knows": [{
182+
"@id": "https://my-profile.eu/people/deiu/card#me",
183+
"name": "Andrei Vlad Sambra",
184+
"based_near": "http://dbpedia.org/resource/Paris"
185+
}, {
186+
"@id": "http://melvincarvalho.com/#me",
187+
"name": "Melvin Carvalho",
188+
"based_near": "http://dbpedia.org/resource/Honolulu"
189+
}, {
190+
"@id": "http://bblfish.net/people/henry/card#me",
191+
"name": "Henry Story",
192+
"based_near": "http://dbpedia.org/resource/Paris"
193+
}, {
194+
"@id": "http://presbrey.mit.edu/foaf#presbrey",
195+
"name": "Joe Presbrey",
196+
"based_near": "http://dbpedia.org/resource/Cambridge"
197+
}]
198+
};
195199

196-
var paris = "http://dbpedia.org/resource/Paris";
200+
var paris = 'http://dbpedia.org/resource/Paris';
197201

198202
db.jsonld.put(manu, function(){
199-
db.join([{
200-
subject: manu["@id"],
201-
predicate: "http://xmlns.com/foaf/0.1/knows",
202-
object: db.v("webid")
203+
db.search([{
204+
subject: manu['@id'],
205+
predicate: 'http://xmlns.com/foaf/0.1/knows',
206+
object: db.v('webid')
203207
}, {
204-
subject: db.v("webid"),
205-
predicate: "http://xmlns.com/foaf/0.1/based_near",
208+
subject: db.v('webid'),
209+
predicate: 'http://xmlns.com/foaf/0.1/based_near',
206210
object: paris
207211
}, {
208-
subject: db.v("webid"),
209-
predicate: "http://xmlns.com/foaf/0.1/name",
210-
object: db.v("name")
212+
subject: db.v('webid'),
213+
predicate: 'http://xmlns.com/foaf/0.1/name',
214+
object: db.v('name')
211215
}
212216
], function(err, solution) {
213217
// solution contains
214218
// [{
215-
// webid: "http://bblfish.net/people/henry/card#me",
216-
// name: "Henry Story"
219+
// webid: 'http://bblfish.net/people/henry/card#me',
220+
// name: '"Henry Story"'
217221
// }, {
218-
// webid: "https://my-profile.eu/people/deiu/card#me",
219-
// name: "Andrei Vlad Sambra"
222+
// webid: 'https://my-profile.eu/people/deiu/card#me',
223+
// name: '"Andrei Vlad Sambra"'
220224
// }]
221225
});
222226
});

0 commit comments

Comments
 (0)