@rdfine/generator
Creates entity types from RDF vocabularies
About
With this package you can easily scaffold JavaScript models based on RDF vocabularies:
-
rdfs:Class
andschema:Class
become JavaScript classes-
rdfs:subClassOf
used to build up class hierarchy - because JavaScript mixins are used, "multiple inheritance" is possible
-
- Properties are discovered by exploring the
rdfs:domain
,rdfs:range
,schema:domainIncludes
andschema:rangeIncludes
relations- domains and ranges defined using
owl:unionOf
andowl:disjointUnionOf
are also considered
- domains and ranges defined using
- Property return types are assigned:
- matching JS built-in type for datatype properties
- resource type for classes within the vocabulary
- if all fails, the property is defined to return RDF/JS Term
TL;DR;
Check the generated vocabularies to see the generation result.
Check the examples to see how they are used.
Usage
npm i --save @rdfine/generator
The package is a command-line interface which consumes standard input of RDF data.
Usage: rdfine-gen [options]
Options:
--format <format> Media type of parser to use
--namespace <namespace> Namespace URI
--prefix <prefix> prefix
--outDir <outDir> Output directory (default: ".")
--verbose
-h, --help display help for command
Generate from a common vocabulary
The easiest way is to pipe the output from @zazuko/rdf-vocabularies CLI
npm i -g @zazuko/vocabularies
rdf-vocab prefix foaf | rdfine-gen \
--format application/n-triples \
--prefix foaf
Generate from the web
If the given vocabulary is dereferencable form the web, curl can be used to fetch it and pipe to the generator
curl http://xmlns.com/foaf/0.1/ -H accept:application/rdf+xml -H | \
rdfine-gen \
--format application/rdf+xml \
--prefix foaf
Options
The process can be further tweaked by adding a rdfine
key to package.json. Here's an example of its usage in @rdfine/schema
package.
{
"rdfine": {
"namespace": "http://schema.org/",
"prefix": "schema",
"types": {
"schema:CssSelectorType": "string",
"schema:URL": "NamedNode",
"schema:XPathType": "string"
},
"exclude": [
"Class",
"DataType",
"DefinedTerm",
"EducationalOccupationalCredential",
"Enumeration",
"GeospatialGeometry",
"MedicalBusiness",
"PhysicalActivityCategory",
"URL",
"VirtualLocation"
]
}
}
namespace
and prefix
can be used in place of the CLI params. The former is optional if the vocabulary is known to @zazuko/rdf-vocabularies
types
maps non-standard property ranges to built-in JS types. The keys are prefixed name of the given datatype or class. The values is one of the following: ( 'string', 'number', 'boolean', 'Date', 'NamedNode' )
.
exclude
causes certain classes to be ignored from being generated and removed where they would be used as property types.
What gets generated
- For every class
Foo
aFoo.ts
module: 2. exportsinterface Foo
3. default-exports a mixin functionFoo
4. exports a default implementation used asnew Foo.Class()
- main
index.ts
module: 3. re-exports allFoo
interfaces 4. default-export all mixin functions as an array -
lib/namespace/ts
is created with a @rdfjs/namespace builder export