|
| 1 | +LevelGraph-JSONLD |
| 2 | +=========== |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | +__LevelGraph-JSONLD__ is a plugin for |
| 7 | +[LevelGraph](http://github.com/mcollina/levelgraph) that adds the |
| 8 | +ability to store, retrieve and delete JSON-LD objects. |
| 9 | +In fact, it is a full-bown Object-Document-Mapper (ODM) for |
| 10 | +__LevelGraph__. |
| 11 | + |
| 12 | +[](https://travis-ci.org/mcollina/levelgraph-jsonld) |
| 14 | + |
| 15 | +## Install on Node.js |
| 16 | + |
| 17 | +``` |
| 18 | +npm install levelgraph levelgraph-jsonld --save |
| 19 | +``` |
| 20 | + |
| 21 | +At the moment it requires node v0.10.x, but the port to node v0.8.x |
| 22 | +should be straighforward. |
| 23 | +If you need it, just open a pull request. |
| 24 | + |
| 25 | +## Install in the Browser |
| 26 | + |
| 27 | +TO BE DONE! |
| 28 | + |
| 29 | +## Usage |
| 30 | + |
| 31 | +Adding support for JSON-LD to LevelGraph is easy: |
| 32 | +``` |
| 33 | +var levelgraph = require("levelgraph") |
| 34 | + , jsonld = require("levelgraph-jsonld") |
| 35 | + , db = jsonld(levelgraph("yourdb")); |
| 36 | +``` |
| 37 | + |
| 38 | +### Put |
| 39 | + |
| 40 | +Storing a JSON-LD file in the database is extremey easy: |
| 41 | +``` |
| 42 | +var manu = { |
| 43 | + "@context": { |
| 44 | + "name": "http://xmlns.com/foaf/0.1/name" |
| 45 | + , "homepage": { |
| 46 | + "@id": "http://xmlns.com/foaf/0.1/homepage" |
| 47 | + , "@type": "@id" |
| 48 | + } |
| 49 | + } |
| 50 | + , "@id": "http://manu.sporny.org#person" |
| 51 | + , "name": "Manu Sporny" |
| 52 | + , "homepage": "http://manu.sporny.org/" |
| 53 | +} |
| 54 | +
|
| 55 | +db.jsonld.put(manu, function(err, obj) { |
| 56 | + // do something after the obj is inserted |
| 57 | +}); |
| 58 | +``` |
| 59 | + |
| 60 | +if the object as no `'@id'` key, one will be generated for you, |
| 61 | +using a UUID and the `'base'` argument, like so: |
| 62 | +``` |
| 63 | +delete manu["@id"]; |
| 64 | +db.jsonld.put(manu, { base: "http://this/is/an/iri" }, function(err, obj) { |
| 65 | + // obj["@id"] will be something like |
| 66 | +http://this/is/an/iri/b1e783b0-eda6-11e2-9540-d7575689f4bc |
| 67 | +}); |
| 68 | +``` |
| 69 | + |
| 70 | +`'base'` can also be specified when you create the db: |
| 71 | +``` |
| 72 | +var levelgraph = require("levelgraph") |
| 73 | + , jsonld = require("levelgraph-jsonld") |
| 74 | + , opts = { base: "http://matteocollina.com/base" } |
| 75 | + , db = jsonld(levelgraph("yourdb"), opts); |
| 76 | +``` |
| 77 | + |
| 78 | +__LevelGraph-JSONLD__ also support nested objects, like so: |
| 79 | +``` |
| 80 | +var nested = { |
| 81 | + "@context": { |
| 82 | + "name": "http://xmlns.com/foaf/0.1/name" |
| 83 | + , "knows": "http://xmlns.com/foaf/0.1/knows" |
| 84 | + } |
| 85 | + , "@id": "http://matteocollina.com" |
| 86 | + , "name": "matteo" |
| 87 | + , "knows": [{ |
| 88 | + "name": "daniele" |
| 89 | + }, { |
| 90 | + "name": "lucio" |
| 91 | + }] |
| 92 | + }; |
| 93 | +
|
| 94 | +db.jsonld.put(nested, function(err, obj) { |
| 95 | + // do something... |
| 96 | +}); |
| 97 | +``` |
| 98 | + |
| 99 | +### Get |
| 100 | + |
| 101 | +Retrieving a JSON-LD document from the store requires its `'@id'`: |
| 102 | +``` |
| 103 | +db.jsonld.get(manu["@id"], { "@context": manu["@context"] }, function(err, obj) { |
| 104 | + // obj will be the very same of the manu object |
| 105 | +}); |
| 106 | +``` |
| 107 | + |
| 108 | +The format of the loaded object is entirely specified by the |
| 109 | +`'@context'`, so have fun :). |
| 110 | + |
| 111 | +As with `'put'` it correctly support nested objects, but it |
| 112 | +inserts `'@id'` properties for them, like so: |
| 113 | +``` |
| 114 | +var nested = { |
| 115 | + "@context": { |
| 116 | + "name": "http://xmlns.com/foaf/0.1/name" |
| 117 | + , "knows": "http://xmlns.com/foaf/0.1/knows" |
| 118 | + } |
| 119 | + , "@id": "http://matteocollina.com" |
| 120 | + , "name": "matteo" |
| 121 | + , "knows": [{ |
| 122 | + "name": "daniele" |
| 123 | + }, { |
| 124 | + "name": "lucio" |
| 125 | + }] |
| 126 | + }; |
| 127 | +
|
| 128 | +db.jsonld.put(nested, function(err, obj) { |
| 129 | + // obj will be |
| 130 | + // { |
| 131 | + // "@context": { |
| 132 | + // "name": "http://xmlns.com/foaf/0.1/name" |
| 133 | + // , "knows": "http://xmlns.com/foaf/0.1/knows" |
| 134 | + // } |
| 135 | + // , "@id": "http://matteocollina.com" |
| 136 | + // , "name": "matteo" |
| 137 | + // , "knows": [{ |
| 138 | + // "name": "daniele" |
| 139 | + // }, { |
| 140 | + // "name": "lucio" |
| 141 | + // }] |
| 142 | + // } |
| 143 | +}); |
| 144 | +``` |
| 145 | + |
| 146 | +### Deleting |
| 147 | + |
| 148 | +In order to delete an object, you can just pass it's `'@id'` to the |
| 149 | +`'@del'` method: |
| 150 | +``` |
| 151 | +db.jsonld.del(manu["@id"], function(err) { |
| 152 | + // do something after it is deleted! |
| 153 | +}); |
| 154 | +``` |
| 155 | + |
| 156 | +### Searching with LevelGraph |
| 157 | + |
| 158 | +__LevelGraph-JSONLD__ does not support searching for objects, because |
| 159 | +that problem is already solved by __LevelGraph__ itself, like these: |
| 160 | +``` |
| 161 | +var nested = { |
| 162 | + "@context": { |
| 163 | + "name": "http://xmlns.com/foaf/0.1/name" |
| 164 | + , "knows": "http://xmlns.com/foaf/0.1/knows" |
| 165 | + } |
| 166 | + , "@id": "http://matteocollina.com" |
| 167 | + , "name": "matteo" |
| 168 | + , "knows": [{ |
| 169 | + "name": "daniele" |
| 170 | + }, { |
| 171 | + "name": "lucio" |
| 172 | + }] |
| 173 | + }; |
| 174 | +
|
| 175 | +db.jsonld.put(nested, function(err) { |
| 176 | + db.join([{ |
| 177 | + subject: db.v("person") |
| 178 | + , predicate: "http://xmlns.com/foaf/0.1/knows" |
| 179 | + , object: db.v("friend") |
| 180 | + }, { |
| 181 | + subject: db.v("friend") |
| 182 | + , predicate: "http://xmlns.com/foaf/0.1/knows" |
| 183 | + , object: "daniele" |
| 184 | + }], function(err, solutions) { |
| 185 | + // The solutions will be: |
| 186 | + // 1. { person: "http://matteocollina.com", friend: "_:abcde" } |
| 187 | + // 1. { person: "http://matteocollina.com", friend: "_:efghi" } |
| 188 | + }); |
| 189 | +}); |
| 190 | +``` |
| 191 | + |
| 192 | +## Contributing to LevelGraph-JSONLD |
| 193 | + |
| 194 | +* Check out the latest master to make sure the feature hasn't been |
| 195 | + implemented or the bug hasn't been fixed yet |
| 196 | +* Check out the issue tracker to make sure someone already hasn't |
| 197 | + requested it and/or contributed it |
| 198 | +* Fork the project |
| 199 | +* Start a feature/bugfix branch |
| 200 | +* Commit and push until you are happy with your contribution |
| 201 | +* Make sure to add tests for it. This is important so I don't break it |
| 202 | + in a future version unintentionally. |
| 203 | +* Please try not to mess with the Makefile and package.json. If you |
| 204 | + want to have your own version, or is otherwise necessary, that is |
| 205 | + fine, but please isolate to its own commit so I can cherry-pick around |
| 206 | + it. |
| 207 | + |
| 208 | +## LICENSE - "MIT License" |
| 209 | + |
| 210 | +Copyright (c) 2013 Matteo Collina (http://matteocollina.com) |
| 211 | + |
| 212 | +Permission is hereby granted, free of charge, to any person |
| 213 | +obtaining a copy of this software and associated documentation |
| 214 | +files (the "Software"), to deal in the Software without |
| 215 | +restriction, including without limitation the rights to use, |
| 216 | +copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 217 | +copies of the Software, and to permit persons to whom the |
| 218 | +Software is furnished to do so, subject to the following |
| 219 | +conditions: |
| 220 | + |
| 221 | +The above copyright notice and this permission notice shall be |
| 222 | +included in all copies or substantial portions of the Software. |
| 223 | + |
| 224 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 225 | +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 226 | +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 227 | +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 228 | +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 229 | +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 230 | +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 231 | +OTHER DEALINGS IN THE SOFTWARE. |
0 commit comments