Skip to content

Commit b8f2dd9

Browse files
committed
Returning the generated @id when putting.
1 parent 8b3ee7a commit b8f2dd9

2 files changed

Lines changed: 30 additions & 15 deletions

File tree

index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,18 @@ function levelgraphJSONLD(db) {
2525

2626
options.base = options.base || "";
2727

28+
if (!obj["@id"]) {
29+
obj["@id"] = options.base + uuid.v1();
30+
}
31+
2832
jsonld.expand(obj, options, function(err, expanded) {
2933

3034
var stream = graphdb.putStream();
3135

3236
stream.on("error", callback);
33-
stream.on("close", callback);
37+
stream.on("close", function() {
38+
callback(null, obj);
39+
});
3440

3541
expanded.forEach(function(triples) {
3642
var subject = triples["@id"];

test/put_spec.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@ var level = require("level-test")()
55

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

8-
var db
9-
, manu = {
10-
"@context": {
11-
"name": "http://xmlns.com/foaf/0.1/name"
12-
, "homepage": {
13-
"@id": "http://xmlns.com/foaf/0.1/homepage"
14-
, "@type": "@id"
15-
}
16-
}
17-
, "@id": "http://manu.sporny.org#person"
18-
, "name": "Manu Sporny"
19-
, "homepage": "http://manu.sporny.org/"
20-
}
8+
var db, manu;
219

2210

2311
beforeEach(function() {
2412
db = jsonld(graph(level()));
13+
manu = manu = {
14+
"@context": {
15+
"name": "http://xmlns.com/foaf/0.1/name"
16+
, "homepage": {
17+
"@id": "http://xmlns.com/foaf/0.1/homepage"
18+
, "@type": "@id"
19+
}
20+
}
21+
, "@id": "http://manu.sporny.org#person"
22+
, "name": "Manu Sporny"
23+
, "homepage": "http://manu.sporny.org/"
24+
};
2525
});
2626

2727
afterEach(function(done) {
@@ -81,7 +81,7 @@ describe("jsonld.put", function() {
8181
});
8282
});
8383

84-
it("should generate a uuid for unknown objects", function(done) {
84+
it("should generate an @id for unknown objects", function(done) {
8585
delete manu["@id"];
8686

8787
db.jsonld.put(manu, { base: "http://levelgraph.org/tests/" }, function() {
@@ -95,4 +95,13 @@ describe("jsonld.put", function() {
9595
});
9696
});
9797
});
98+
99+
it("should pass the generated @id to callback", function(done) {
100+
delete manu["@id"];
101+
102+
db.jsonld.put(manu, { base: "http://levelgraph.org/tests/" }, function(err, obj) {
103+
expect(obj["@id"].indexOf("http://levelgraph.org/tests/")).to.equal(0);
104+
done();
105+
});
106+
});
98107
});

0 commit comments

Comments
 (0)