npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

simplerdf

v0.2.14

Published

RDF as easy as js objects

Downloads

176

Readme

SimpleRDF

The future simplest RDF library ever

--

--

TL;DR example

RDF should be as easy as playing with JSON objects.

Read the original blog post: Towards the future RDF Library

var simple = require('simplerdf')
var context = { 'name': 'http://xmlns.com/foaf/0.1/name' }
var me = simple(context)
// You can now edit the foaf name simply with
me.name = 'Nicola'
// as well as using the full predicate
me['http://xmlns.com/foaf/0.1/name'] = 'Nicola'

Install

As a Node.js library

npm install --save simplerdf

To generate a browser-ready version:

$ git clone https://github.com/nicola/simplerdf
$ cd simplerdf
$ npm install
$ npm run build-browser
# This will generate `simplerdf.js` that you can load in your web application

Quick tutorial

1) Create a SimpleRDF object (easy peasy)

// The constructor takes 4 optional parameters:
// `context` is a JSON-LD context, useful if you want to
//           map predicates to nice property names
// `uri`     is the name of the resource
// `graph`   if you want to load an `rdf-ext` graph
// `store`   see the FAQ!
var simple = require('simplerdf')

var me = simple({
  'name': 'http://xmlns.com/foaf/0.1/name'
})

// You can directly set properties defined in your context
me.name = 'Nicola'
console.log(me.name)
// 'Nicola'

// Or you can set the property with a particular type pointer
me['http://xmlns.com/foaf/0.1/name'] = 'Nicola'
console.log(me['http://xmlns.com/foaf/0.1/name'])
// 'Nicola'

// These are interchangable
me['http://xmlns.com/foaf/0.1/name'] = 'Nicola'
console.log(me.name)

2) Using arrays (simple pimple)

You want to enforce an property to be always an array? Pass '@container': '@set' in the schema description!

me.context({
  'name': 'http://xmlns.com/foaf/0.1/name',
  'knows': {
    '@id': 'http://xmlns.com/foaf/0.1/knows',
    '@type': '@id',
    '@container': '@set'
  }
})
// now we can use me.knows (this will be an array!)

me.knows = ['http://nicola.io/#me']
me.knows.at(0)
me.knows.map(function (object) { return 'hello' + object })
me.knows.length

Note: You just want to do me.knows[0] ? We are implementing this at the moment, stay tuned!

3) Use CRUD features .get and .save (crifty crafty)

SimpleRDF supports some simple .get/.save. These respects the Linked Data Platform standard (have a look at LDP)

// This gets the iri specified in the constructor
simple(context, iri).get(function(err, g) {
  console.log(g.name)
})

// This saves the graph in the iri specified in the constructor
var g = SimpleRDF(context, iri)
g.name = "Nicola"
g.save()

// Hint for the brave:
// You can also do this
SimpleRDF(context).get(iri1, function (err, g) {
  g.save(iri2, ..)
})

4) Understanding .child (master jedi)

Imagine the you set the iri of your graph to be http://amazing-libraries.org and you now would like to change the subject to let's say http://amazing-libraries.org/simplerdf, you can use .child(iri). This will create a new SimpleRDF object.

var amazinglibraries = simple(context, 'http://amazing-libraries.org')
var simplerdf = amazinglibraries.child('http://amazing-libraries.org/simplerdf')
// done!

5) Understanding .graph(), .iri() (wicky wacky)

Both these functions are setters if passed with a parameter and getters if no parameter is found. .graph() will give you access to an rdf-ext graph, .graph(newGraph) will replace the current graph with the new graph. .iri() will give you the iri that is the subject of your simple object. Finally .iri(newIri) will replace your subject

Bonus: Full working example

See other working examples

var context = {
  'name': 'http://xmlns.com/foaf/0.1/name',
  'homepage': {
    '@id': 'http://xmlns.com/foaf/0.1/homepage',
    '@type': '@id'
  }
}
var me = SimpleRDF(context, 'https://nicolagreco.com')
me.name = 'Nicola'
me.homepage = 'http://nicolagreco.com'

console.log(me.name)
console.log(me['http://xmlns.com/foaf/0.1/name'])
console.log(me.toString()) // this returns turtle!

Limitations

  • Only subject-centric queries (e.g. graph.match(you_know_this[, predicate, object]))
  • Schemas must be typed

Note: If you want to use any of these two properties, then you want a proper low-level library like rdf-ext or rdflib, or send a PR!

FAQ

Can I use it without CRUD, parsers and serializers?

Of course, when you require it, use the lite version

var simple = require('simplerdf/lite')

Can I customize the .get and .set?

Of course, there are plenty of rdf-ext stores availabe, here are some:

Now, when you create a SimpleRDF object you can do the following:

var store = require('rdf-store-fs')
var graph = SimpleRDF(context, iri, graph, store)

How can I do queries that are not subject-centric?

You mean..

var simpleObject = SimpleRDF()
// ... //
simpleObject.graph().match(subj, pred, obj)

Roadmap

Coming soon

  • [ ] Support of ES6 Proxy
    • No need to specify context
    • Use uri as context
    • Simple array support (e.g. me.friends[0])
  • [ ] Simple query language
    • me.friends[0].get()

Contributors

License

MIT