GraphDB Utils

GraphDB utils is an object data modelling tool.

Basics

When create a Model, each property requires specifying data type.

const UserModel = createGraphDBModel({
name: String,
dob: Date,
hobby: [String],
validated: Boolean,
}, schemaOptions);

Available Types

const {Types} = require('../utils/graphdb');
TypeType in RDFExample in JSExample in RDF
Types.NamedIndividualowl:NamedIndividual:primary_contact_1:primary_contact_1
Types.String or String^^xsd:string"sample string""sample string"
Types.Number or Number^^xsd:integer
^^xsd:decimal
^^xsd:double
1
1.3
1.0e6
1
1.3
1.0e6
Types.Date or Date^^xsd:integerDate.now() / new Date()1597676490573
Types.Boolean or Boolean^^xsd:booleantrue
false
true
false
[String] (*see below)^^xsd:string["str1", "str2"]"str1", "str2".

Array Types

The above data types (Types.NamedIndividual, String, Number, ...) can be wrapped by [], i.e. [String], [Number], which represents a list of String or Number.

SchemaOptions

schemaOptionstypedescription
rdfTypesstring[]The list of value in rdf:type.
e.g. [Types.NamedIndividual, ":primary_contact"] => some_instance rdf:type owl:NamedIndividual, :primary_contact.
namestringThe prefix of the created document, e.g. if name="primary_contact", the new created document will have id :primary_contact_1

Internal Key and External Key

By default, has_ is added to the front of the property name as an internal key for SPARQL.
External keys are usually same as the property name.

When creating the following model and create new instance / document / individual for that model:

const UserModel = createGraphDBModel({
name: String,
dob: Date,
hobby: {type: [String], externalKey: 'hobbies'},
validated: Boolean,
}, {
rdfTypes: [Types.NamedIndividual, ":user"],
name: "user"
});
const document = UserModel({
name: "Lester",
dob: Date.now(),
hobbies: ["table tennis", "coding"],
validated: false,
});
await document.save();

The SPARQL query will be:

PREFIX : <http://cmmp#>
INSERT DATA {
:user_1 rdf:type owl:NamedIndividual, :primary_contact;
:has_name "Lester";
:has_dob 1597676490573;
:has_bobby "table tennis", "coding";
:has_validated false;
}

Note that name property renames to has_name in the SPARQL query. In this case name is the external key, has_name is the internal key.

ATTENTION

We defined property hobby as an array of strings. The external key will is be hobbies because we defined it that way, if we don't define the external key manually, the external key will be hobbys, an s is appended to the property key for every array type.

Create a GraphDB Model

const {GraphDB, Types, createGraphDBModel, DeleteType} = require('../utils/graphdb');
const PrimaryContactModel = createGraphDBModel({
first_name: String,
last_name: String,
}, {
rdfTypes: [Types.NamedIndividual, ':primary_contact'], // rdf:types
name: 'primary_contact' // instance id prefix
});

See Full API