levelgraph / levelgraph-jsonld

The Object Document Mapper for LevelGraph based on JSON-LD
113 stars 16 forks source link

Whats the correct way to query #61

Closed KunalSaini closed 7 years ago

KunalSaini commented 7 years ago

Hi, I am trying to search on a jsonld representation using below code

var building = require('./db/dbfile.jsonld');
var levelgraph = require('levelgraph'),
    levelup = require("levelup"),
    db = levelup('/does/not/matter', { db: require('memdown') });
var levelgraphJSONLD = require('levelgraph-jsonld'),
db = levelgraphJSONLD(levelgraph(db));

db.jsonld.put(building, function(){
db.search([{
    subject: db.v('x'),
    predicate: 'http://buildsys.org/ontologies/BrickFrame#isRelatedTo',
    object: db.v('y'),
  }], function(err, data) {
    console.log(data, err);
  });
});

This works , however i want to not specify predicate as full namespace instead i want to leverage the context information when searching. something like

db.search([{
    subject: db.v('x'),
    predicate: 'rel:isRelatedTo',
    object: db.v('y'),
  }],....

following is the context object as specified in my /db/dbfile.jsonld file

"@context": {
        "feeds": {
            "@id": "rel:feeds",
            "@type": "@id"
        },
        "id": "@id",
        "isRelatedTo": {
            "@id": "rel:isPointOf",
            "@type": "@id"
        },
        "rel": "http://buildsys.org/ontologies/BrickFrame#",
        "site": "http://my.org/ontology/site#",
        "tagset": "http://buildsys.org/ontologies/Brick#",
        "type": "@type"
    },
    "@graph": [
        {
            "id": "site:id0",
            "isRelatedTo": "site:id7",
            "type": "tagset:Point"
        },
        {
            "id": "site:id1",
            "isRelatedTo": "site:id8",
            "type": "tagset:Point"
        },....
BigBlueHat commented 7 years ago

levelgraph-jsonld stores the fully qualified identifiers, so that you can use various prefixes for data coming in and out of the database.

If you want to write queries using your prefixes, you can simply define a small prefix mapping function like so:

// `context` is assumed to be your global @context's value--customize to your liking ^_^
function unprefix(id) {
  var bits = id.split(':');
  var base = context[bits[0]];
  return base + bits[1];
}

Which would allow you to do the following:

db.search([{
    subject: db.v('x'),
    predicate: unprefix('rel:isRelatedTo'),
    object: db.v('y'),
  }]

However, this is a minimal effort sort of mapping and may not (i.e. likely won't) scale to more complex contexts--in which case using something like jsonld.js might be best.

This is a common problem/need, but it's not one levelgraph-jsonld is likely to solve directly.

Thoughts?

KunalSaini commented 7 years ago

thanks, I did wrote simple prifixer/unprefix function as you suggested for now. my scenario is fairly simple so i can live with this mapper.