Skip to content

Commit a4b2ec3

Browse files
committed
Implemented working del. Reimplemented put with toRDF.
1 parent c70428e commit a4b2ec3

7 files changed

Lines changed: 238 additions & 39 deletions

File tree

index.js

Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ var jsonld = require("jsonld")
33
, uuid = require("uuid")
44
, IRI = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/i
55
, RDFTYPE = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
6-
, async = require("async");
6+
, async = require("async")
7+
, blanksRegexp = /^_:b\d+$/
8+
, genActionStream;
79

810
function levelgraphJSONLD(db, jsonldOpts) {
911

@@ -36,7 +38,9 @@ function levelgraphJSONLD(db, jsonldOpts) {
3638
obj["@id"] = options.base + uuid.v1();
3739
}
3840

39-
jsonld.expand(obj, options, function(err, expanded) {
41+
var blanks = {};
42+
43+
jsonld.toRDF(obj, options, function(err, triples) {
4044

4145
var stream = graphdb.putStream();
4246

@@ -45,48 +49,56 @@ function levelgraphJSONLD(db, jsonldOpts) {
4549
callback(null, obj);
4650
});
4751

48-
var writeTriples = function(triples) {
49-
var subject = triples["@id"];
50-
delete triples["@id"];
51-
52-
if (!subject) {
53-
subject = "_:" + uuid.v1();
54-
}
55-
56-
Object.keys(triples).forEach(function(predicate) {
57-
if (predicate === "@type") {
58-
if (!triples[predicate].forEach) {
59-
triples[predicate] = [triples[predicate]];
52+
triples["@default"].map(function(triple) {
53+
return ["subject", "predicate", "object"].reduce(function(acc, key) {
54+
var value = triple[key].value;
55+
if (value.match(blanksRegexp)) {
56+
if (!blanks[value]) {
57+
blanks[value] = "_:" + uuid.v1();
6058
}
61-
62-
triples[RDFTYPE] = triples[predicate];
63-
delete triples[predicate];
64-
predicate = RDFTYPE;
59+
value = blanks[value];
6560
}
61+
acc[key] = value;
62+
return acc;
63+
}, {});
64+
}).forEach(function(triple) {
65+
stream.write(triple);
66+
});
6667

67-
triples[predicate].forEach(function(object) {
68-
var triple = {
69-
subject: subject
70-
, predicate: predicate
71-
, object: (typeof object === 'string') ? object : object["@id"] || object["@value"]
72-
};
73-
74-
if (!triple.object) {
75-
// the object should be a blank node that points
76-
// to a another triple
77-
writeTriples(object);
78-
triple.object = object["@id"];
79-
}
68+
stream.end();
69+
});
70+
};
8071

81-
stream.write(triple);
82-
});
83-
});
8472

85-
triples["@id"] = subject;
86-
};
73+
graphdb.jsonld.del = function(iri, options, callback) {
74+
if (typeof options === "function") {
75+
callback = options;
76+
options = {};
77+
}
78+
79+
if (typeof iri !=='string') {
80+
iri = iri["@id"];
81+
}
8782

88-
expanded.forEach(writeTriples);
83+
var stream = graphdb.delStream();
84+
stream.on("close", callback);
85+
stream.on("error", callback);
8986

87+
(function delAllTriples(iri, done) {
88+
graphdb.get({ subject: iri }, function(err, triples) {
89+
async.each(triples, function(triple, cb) {
90+
stream.write(triple);
91+
if (triple.object.indexOf("_:") === 0) {
92+
delAllTriples(triple.object, cb);
93+
} else {
94+
cb();
95+
}
96+
}, done);
97+
});
98+
})(iri, function(err) {
99+
if (err) {
100+
return callback(err);
101+
}
90102
stream.end();
91103
});
92104
};
@@ -156,3 +168,7 @@ function levelgraphJSONLD(db, jsonldOpts) {
156168
}
157169

158170
module.exports = levelgraphJSONLD;
171+
172+
genActionStream = function(graphdb, type) {
173+
return
174+
};

test/del_spec.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
var level = require("level-test")()
2+
, graph = require("levelgraph")
3+
, jsonld = require("../")
4+
, JSONLD = require("jsonld");
5+
6+
describe("jsonld.del", function() {
7+
8+
var db, manu, tesla;
9+
10+
beforeEach(function() {
11+
db = jsonld(graph(level()), { base: "http://levelgraph.io/" });
12+
manu = fixture("manu.json");
13+
tesla = fixture("tesla.json");
14+
});
15+
16+
afterEach(function(done) {
17+
db.close(done);
18+
});
19+
20+
it("should accept a done callback", function(done) {
21+
db.jsonld.put(manu, done);
22+
});
23+
24+
it("should del a basic object", function(done) {
25+
db.jsonld.put(manu, function() {
26+
db.jsonld.del(manu, function() {
27+
db.get({}, function(err, triples) {
28+
// getting the full db
29+
expect(triples).to.have.property("length", 0);
30+
done();
31+
});
32+
});
33+
});
34+
});
35+
36+
it("should del a complex object", function(done) {
37+
db.jsonld.put(tesla, function() {
38+
db.jsonld.del(tesla, function() {
39+
db.get({}, function(err, triples) {
40+
// getting the full db
41+
expect(triples).to.have.property("length", 0);
42+
done();
43+
});
44+
});
45+
});
46+
});
47+
});

test/fixture/john.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"@context": "http://json-ld.org/contexts/person.jsonld",
3+
"@id": "http://dbpedia.org/resource/John_Lennon",
4+
"name": "John Lennon",
5+
"born": "1940-10-09",
6+
"spouse": "http://dbpedia.org/resource/Cynthia_Lennon"
7+
}

test/fixture/manu.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"@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+
}
8+
}
9+
, "@id": "http://manu.sporny.org#person"
10+
, "name": "Manu Sporny"
11+
, "homepage": "http://manu.sporny.org/"
12+
}

test/fixture/person.json

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
{
2+
"@context":
3+
{
4+
"Person": "http://xmlns.com/foaf/0.1/Person",
5+
"xsd": "http://www.w3.org/2001/XMLSchema#",
6+
"name": "http://xmlns.com/foaf/0.1/name",
7+
"nickname": "http://xmlns.com/foaf/0.1/nick",
8+
"affiliation": "http://schema.org/affiliation",
9+
"depiction":
10+
{
11+
"@id": "http://xmlns.com/foaf/0.1/depiction",
12+
"@type": "@id"
13+
},
14+
"image":
15+
{
16+
"@id": "http://xmlns.com/foaf/0.1/img",
17+
"@type": "@id"
18+
},
19+
"born":
20+
{
21+
"@id": "http://schema.org/birthDate",
22+
"@type": "xsd:dateTime"
23+
},
24+
"child":
25+
{
26+
"@id": "http://schema.org/children",
27+
"@type": "@id"
28+
},
29+
"colleague":
30+
{
31+
"@id": "http://schema.org/colleagues",
32+
"@type": "@id"
33+
},
34+
"knows":
35+
{
36+
"@id": "http://xmlns.com/foaf/0.1/knows",
37+
"@type": "@id"
38+
},
39+
"died":
40+
{
41+
"@id": "http://schema.org/deathDate",
42+
"@type": "xsd:dateTime"
43+
},
44+
"email":
45+
{
46+
"@id": "http://xmlns.com/foaf/0.1/mbox",
47+
"@type": "@id"
48+
},
49+
"familyName": "http://xmlns.com/foaf/0.1/familyName",
50+
"givenName": "http://xmlns.com/foaf/0.1/givenName",
51+
"gender": "http://schema.org/gender",
52+
"homepage":
53+
{
54+
"@id": "http://xmlns.com/foaf/0.1/homepage",
55+
"@type": "@id"
56+
},
57+
"honorificPrefix": "http://schema.org/honorificPrefix",
58+
"honorificSuffix": "http://schema.org/honorificSuffix",
59+
"jobTitle": "http://xmlns.com/foaf/0.1/title",
60+
"nationality": "http://schema.org/nationality",
61+
"parent":
62+
{
63+
"@id": "http://schema.org/parent",
64+
"@type": "@id"
65+
},
66+
"sibling":
67+
{
68+
"@id": "http://schema.org/sibling",
69+
"@type": "@id"
70+
},
71+
"spouse":
72+
{
73+
"@id": "http://schema.org/spouse",
74+
"@type": "@id"
75+
},
76+
"telephone": "http://schema.org/telephone",
77+
"Address": "http://www.w3.org/2006/vcard/ns#Address",
78+
"address": "http://www.w3.org/2006/vcard/ns#address",
79+
"street": "http://www.w3.org/2006/vcard/ns#street-address",
80+
"locality": "http://www.w3.org/2006/vcard/ns#locality",
81+
"region": "http://www.w3.org/2006/vcard/ns#region",
82+
"country": "http://www.w3.org/2006/vcard/ns#country",
83+
"postalCode": "http://www.w3.org/2006/vcard/ns#postal-code"
84+
}
85+
}
86+

test/fixture/tesla.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"@context": {
3+
"gr": "http://purl.org/goodrelations/v1#"
4+
, "pto": "http://www.productontology.org/id/"
5+
, "foaf": "http://xmlns.com/foaf/0.1/"
6+
, "xsd": "http://www.w3.org/2001/XMLSchema#"
7+
, "foaf:page": {
8+
"@type": "@id"
9+
}
10+
, "gr:acceptedPaymentMethods": {
11+
"@type": "@id"
12+
}
13+
, "gr:hasBusinessFunction": {
14+
"@type": "@id"
15+
}
16+
}
17+
, "@id": "http://example.org/cars/for-sale#tesla"
18+
, "@type": "gr:Offering"
19+
, "gr:name": "Used Tesla Roadster"
20+
, "gr:description": "Need to sell fast and furiously"
21+
, "gr:hasBusinessFunction": "gr:Sell"
22+
, "gr:acceptedPaymentMethods": "gr:Cash"
23+
, "gr:hasPriceSpecification": {
24+
"gr:hasCurrencyValue": "85000"
25+
, "gr:hasCurrency": "USD"
26+
}
27+
, "gr:includes": {
28+
"@type": ["gr:Individual", "pto:Vehicle"]
29+
, "gr:name": "Tesla Roadster"
30+
, "foaf:page": "http://www.teslamotors.com/roadster"
31+
}
32+
}

test/get_spec.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
var level = require("level-test")()
33
, graph = require("levelgraph")
44
, jsonld = require("../")
5-
, fs = require("fs")
6-
, JSONLD = require("jsonld");
5+
, fs = require("fs");
76

87
describe("jsonld.get", function() {
98

0 commit comments

Comments
 (0)