SpinalGraph SDK

Introduction:

SpinalGraph is a data schema that models information through nodes and relations. This data schema works on the "context" layer of the pyramid of information.

To get more information about the SpinalGraph data architecture, please read the /wiki/spaces/SIS/pages/174293044 model description.

 This page provides an overview of the SpinalGraph SDK.

Usage:

To use the SpinalGraph SDK, you must :

  • Install :

Installation
npm install --save https://github.com/spinalcom/spinal-model-graph.git#v02
  • Import and instantiate

Import and Instantiate
import {
 SpinalNode,
 SpinalContext,
 SpinalGraph
} from "spinal-model-graph";

let SpinalGraph = new SpinalGraph(); // instantiate
let SpinalContext = new SpinalContext(name); // instantiate
let SpinalNode = new SpinalNode(name, type, element); // instantiate

SDK:

  • SpinalGraph
    • new SpinalGraph(name, type, element)
    • .addContext(context) ⇒ Promise<undefined>
    • .getContext(name) ⇒ Promise<SpinalContext | undefined>
  • SpinalContext
    • new SpinalContext(name, type, element)
    • .getRelationNames() ⇒ Lst<String>
    • .addRelationNames(relationNames) ⇒ Boolean
    • .addChild(child, relationName, relationType) ⇒ Promise<SpinalNode>
    • .addChildInContext(child, relationName, relationType, context) ⇒ Promise<SpinalNode>
  • SpinalNode
    • new SpinalNode(name, type, element)
    • .getId() ⇒ Str
    • .getName() ⇒ Str
    • .getType() ⇒ Str
    • .getElement() ⇒ Promise<anything>
    • .getChildrenIds() ⇒ Array<String>
    • .getNbChildren() ⇒ Number
    • .getContextIds() ⇒ Array<String>
    • .addContextId()
    • .belongsToContext() ⇒ Boolean
    • .hasRelation(relationName, relationType) ⇒ Boolean
    • .hasRelations(relationNames, relationType) ⇒ Boolean
    • .getRelationNames() ⇒ Array<String>
    • .addChild(child, relationName, relationType) ⇒ Promise<SpinalNode>
    • .addChildInContext(child, relationName, relationType, context) ⇒ Promise<SpinalNode>
    • .removeChild(node, relationName, relationType) ⇒ Promise<undefined>
    • .removeFromGraph() ⇒ Promise<undefined>
    • .getChildren() ⇒ Promise<Array<SpinalNode>>
    • .getChildrenInContext() ⇒ Promise<Array<SpinalNode>>
    • .getParent(relationNames) ⇒ Promise<Array<SpinalNode>>
    • .find(relationNames, predicate) ⇒ Promise<Array<SpinalNode>>
    • .findInContext(context, predicate) ⇒ Promise<Array<SpinalNode>>
    • .forEach(relationNames, callback) ⇒ Promise<undefined>
    • .forEachInContext(context, callback) ⇒ Promise<undefined>
    • .map(relationNames, callback) ⇒ Promise<Array<*>>
    • .mapInContext(context, callback) ⇒ Promise<Array<*>>

Coming soon :

  • SpinalNode
    • .removeChildren(relationNames) ⇒ Promise<Array<Boolean>>
    • .removeChild(node, relationName, relationType) ⇒ Promise<Boolean>



SpinalGraph Methods :

new SpinalGraph(name, type, element)

Constructor for the SpinalGraph class.

ParametersTypeDefaultDescription
nameString"undefined"Name of the graph, usually unused

type 

String"SpinalGraph"Type of the graph, usually unused
elementSpinalNode or ModelModelElement of the graph, usually unused


SpinalGraph.addContext(context) ⇒ Promise<undefined>

Adds a context to the graph.

Kind: instance method of SpinalGraph

Returns: An empty promise


ParametersTypeDefaultDescription
contextSpinalContextnoneAdds a context to the graph

SpinalGraph.getContext(name) ⇒ Promise<SpinalContext | undefined>

Searches for a context using its name

Kind: instance method of SpinalGraph

Returns: SpinalContext | undefined - The wanted context or undefined

ParametersTypeDefaultDescription
nameStringnoneName of the context


SpinalContext Methods :

new SpinalContext(name, type, element)

Constructor for the SpinalContext class.

ParametersTypeDefaultDescription
nameString"undefined"Name of the context
typeString"SpinalContext"Type of the context, usually unused
elementSpinalNode or ModelModelElement of the context, usually unused


SpinalContext.getRelationNames() ⇒ Lst<String>

Returns the relation names of the context.

Kind: instance method of SpinalContext

Returns: The relation names that the context knows


SpinalContext.addRelationNames(relationNames) ⇒ Boolean

Adds relation names to the relation names known by the context.

Kind: instance method of SpinalContext

Returns: False if all the relation names are already known

ParametersTypeDefaultDescription
relationNamesString or Array of StringnoneNames of the relations


SpinalContext.addChild(child, relationName, relationType) ⇒ Promise<SpinalNode>

Adds a child with a SpinalRelationLstPtr.

Kind: instance method of SpinalContext

Returns: The child node in a promise

ParametersTypeDefaultDescription
childSpinalNode or ModelnoneNode to add as child
relationNameStringnoneName of the relation
relationTypeStringSPINAL_RELATION_PTR_LST_TYPEThis parameter is here only to properly override the parent method


SpinalContext.addChildInContext(child, relationName, relationType, context) ⇒ Promise<SpinalNode>

Adds a child with a SpinalRelationLstPtr and notices the context if a new relation was created.

Kind: instance method of SpinalContext

Returns: The child node in a promise

ParametersTypeDefaultDescription
childSpinalNode or ModelnoneNode to add as child
relationNameStringnoneName of the relation
relationTypeStringSPINAL_RELATION_PTR_LST_TYPEThis parameter is here only to properly override the parent method
contextSpinalContextthisContext to update, usually unused

SpinalNode Methods :

new SpinalNode(name, type, element)

Constructor for the SpinalNode class.

ParamTypeDefaultDescription
nameString"undefined"Name of the node
typeString"SpinalNode"Type of the node
elementSpinalNode or ModelModelElement of the node

SpinalNode.getId() ⇒ Str

Returns the id.

Kind: instance method of SpinalNode

Returns: Id of the node

SpinalNode.getName() ⇒ Str

Returns the name.

Kind: instance method of SpinalNode

Returns: Name of the node

SpinalNode.getType() ⇒ Str

Returns the Type.

Kind: instance method of SpinalNode

Returns: Type of the node

SpinalNode.getElement() ⇒ Promise<*>

Returns the element.

Kind: instance method of SpinalNode

Returns: A promise where the parameter of the resolve method is the element

SpinalContext.getChildrenIds() ⇒ Array<String>

Returns all the children ids in an array.

Kind: instance method of SpinalContext

Returns: Ids of the children

SpinalNode.getNbChildren() ⇒ Number

Computes and returns the number of children of the node.

Kind: instance method of SpinalNode

Returns: The number of children

SpinalNode.getContextIds() ⇒ Array<String>

Returns a list of the contexts the node is associated to.

Kind: instance method of SpinalNode

Returns: An array of ids of the associated contexts

SpinalNode.addContextId()

Adds an id to the context ids of the node.

Kind: instance method of SpinalNode

ParametersTypeDefaultDescription
idStringnone

Id of the context

SpinalNode.belongsToContext() Boolean

Returns true if the node belongs to the context.

Kind: instance method of SpinalNode

ParametersTypeDefaultDescription
contextSpinalContextnoneThe context that might own the node

SpinalNode.hasRelation(relationName, relationType) ⇒ Boolean

Verify if the node contains the relation name.

Kind: instance method of SpinalNode

Returns: True if the node contains all the relations in relationNames, false otherwise

ParametersTypeDefaultDescription
relationNameStringnoneName of the relation
relationTypeStringnoneType of the relation

SpinalNode.hasRelations(relationNames, relationType) ⇒ Boolean

Verify if the node contains all the relation names.

Kind: instance method of SpinalNode

Returns: True if the node contains all the relations listed in relationNames, false otherwise.

ParametersTypeDefaultDescription
relationNamesArray of StringnoneArray containing all the relation name
relationTypeStringnoneType of the relations

SpinalNode.getRelationNames() ⇒ Array<String>

Returns all the relation names of the node.

Kind: instance method of SpinalNode

Returns: The names of the relations of the node.

SpinalNode.addChild(child, relationName, relationType) ⇒ Promise<SpinalNode>

Adds a child and notices the context if a new relation was created.

Kind: instance method of SpinalNode

Returns: The child node in a promise

ParametersTypeDefaultDescription
childSpinalNode or ModelnoneNode to add as child
relationNameStringnoneName of the relation
relationTypeStringnoneType of the relation

SpinalNode.addChildInContext(child, relationName, relationType, context) ⇒ Promise<SpinalNode>

Adds a child and notices the context if a new relation was created.

Kind: instance method of SpinalNode

Returns: The child node in a promise

ParametersTypeDefaultDescription
childSpinalNode or ModelnoneNode to add as child
relationNameStringnoneName of the relation
relationTypeStringnoneType of the relation
contextSpinalContextnoneContext to update

SpinalNode.removeChild(node, relationName, relationType) Promise<Boolean>

Remove the node from the relation children.

Kind: instance method of SpinalNode

Returns: A promise containing a boolean

ParametersTypeDefaultDescription
nodeSpinalNodenoneNode to remove
relationNameStringnoneName of the relation to which the node belongs
relationTypeStringnoneType of the relation to which the node belongs

SpinalNode.removeChildren(relationNames) Promise<Array<Boolean>>

Removes children with the relation names or all the children if no relation name is passed.

Kind: instance method of SpinalNode

Returns: A promise containing an array of boolean

ParametersTypeDefaultDescription
relationNamesArray of Strings or empty array or Stringempty arrayArray of relation names or empty array or relation name

SpinalNode.removeFromGraph() Promise<undefined>

Remove the node from the graph i.e remove the node from all the parent relation and remove all the children relations. This operation might also delete all the sub-graph under this node. After this operation the node can be deleted without fear.

Kind: instance method of SpinalNode

Returns: An empty promise

SpinalNode.getChildren() ⇒ Promise<Array<SpinalNode>>

Return the children of the node for the relation names.

Kind: instance method of SpinalNode

Returns: The children that were found

ParametersTypeDefaultDescription
relationNamesArray of StringsnoneArray containing the relation names of the desired children

SpinalNode.getChildrenInContext() ⇒ Promise<Array<SpinalNode>>

Return the children of the node that are registered in the context.

Kind: instance method of SpinalNode

Returns: The children that were found

ParametersTypeDefaultDescription
contextSpinalContextnoneContext to use for the search

SpinalNode.getParent(relationNames) ⇒ Promise<Array<SpinalNode>>

Return all parents for the relation names no matter the type of relation

Kind: instance method of SpinalNode

Returns: Promise containing the parents that were found

ParametersTypeDefaultDescription
relationNamesArray of StringsnoneArray containing the relation names of the desired parents

SpinalNode.find(relationNames, predicate) ⇒ Promise<Array<SpinalNode>>

Recursively finds all the children nodes for which the predicate is true.

Kind: instance method of SpinalNode

Returns: The nodes that were found

ParametersTypeDefaultDescription
relationNamesArray of Stringsnone

array containing the relation names to follow

predicatefunctionA function that always returns trueFunction returning true if the node needs to be returned

SpinalNode.findInContext(context, predicate) ⇒ Promise<Array<SpinalNode>>

Recursively finds all the children nodes in the context for which the predicate is true.

Kind: instance method of SpinalNode

Returns: The nodes that were found

ParametersTypeDefaultDescription
contextSpinalContextnoneContext to use for the search
predicatefunctionA function that always returns trueFunction returning true if the node needs to be returned

SpinalNode.forEach(relationNames, callback) ⇒ Promise<undefined>

Recursively applies a function to all the children nodes.

Kind: instance method of SpinalNode

Returns: An empty promise

ParametersTypeDefaultDescription
relationNamesArray of Stringsnone

array containing the relation names to follow

callbackfunctionnoneFunction to apply to the nodes

SpinalNode.forEachInContext(context, callback) ⇒ Promise<undefined>

Recursively applies a function to all the children nodes in the context.

Kind: instance method of SpinalNode

Returns: An empty promise

ParametersTypeDefaultDescription
contextSpinalContextnoneContext to use for the search
callbackfunctionnoneFunction to apply to the nodes

SpinalNode.map(relationNames, callback) ⇒ Promise<Array<*>>

Recursively applies a function to all the children nodes and returns the results in an array.

Kind: instance method of SpinalNode

Returns: The results of the callback for each node

ParametersTypeDefaultDescription
relationNamesArray of Stringsnone

array containing the relation names to follow

callbackfunctionnoneFunction to apply to the nodes

SpinalNode.mapInContext(context, callback) ⇒ Promise<Array<*>>

Recursively applies a function to all the children nodes in the context and returns the results in an array.

Kind: instance method of SpinalNode

Returns: The results of the callback for each node

ParametersTypeDefaultDescription
contextSpinalContextnoneContext to use for the search
callbackfunctionnoneFunction to apply to the nodes