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 :
npm install --save https://github.com/spinalcom/spinal-model-graph.git#v02
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) ⇒ B
oolean
- .hasRelations(relationNames, relationType) ⇒ B
oolean
- .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.
Parameters | Type | Default | Description |
---|---|---|---|
name | String | "undefined" | Name of the graph, usually unused |
type | String | "SpinalGraph" | Type of the graph, usually unused |
element | SpinalNode or Model | Model | Element 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
Parameters | Type | Default | Description |
---|---|---|---|
context | SpinalContext | none | Adds 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
Parameters | Type | Default | Description |
---|---|---|---|
name | String | none | Name of the context |
SpinalContext Methods :
new SpinalContext(name, type, element)
Constructor for the SpinalContext class.
Parameters | Type | Default | Description |
---|---|---|---|
name | String | "undefined" | Name of the context |
type | String | "SpinalContext" | Type of the context, usually unused |
element | SpinalNode or Model | Model | Element 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
Parameters | Type | Default | Description |
---|---|---|---|
relationNames | String or Array of String | none | Names 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
Parameters | Type | Default | Description |
---|---|---|---|
child | SpinalNode or Model | none | Node to add as child |
relationName | String | none | Name of the relation |
relationType | String | SPINAL_RELATION_PTR_LST_TYPE | This 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
Parameters | Type | Default | Description |
---|---|---|---|
child | SpinalNode or Model | none | Node to add as child |
relationName | String | none | Name of the relation |
relationType | String | SPINAL_RELATION_PTR_LST_TYPE | This parameter is here only to properly override the parent method |
context | SpinalContext | this | Context to update, usually unused |
SpinalNode Methods :
new SpinalNode(name, type, element)
Constructor for the SpinalNode class.
Param | Type | Default | Description |
---|---|---|---|
name | String | "undefined" | Name of the node |
type | String | "SpinalNode" | Type of the node |
element | SpinalNode or Model | Model | Element 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
Parameters | Type | Default | Description |
---|---|---|---|
id | String | none | Id of the context |
SpinalNode.belongsToContext() ⇒ Boolean
Returns true if the node belongs to the context.
Kind: instance method of SpinalNode
Parameters | Type | Default | Description |
---|---|---|---|
context | SpinalContext | none | The 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
Parameters | Type | Default | Description |
---|---|---|---|
relationName | String | none | Name of the relation |
relationType | String | none | Type 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.
Parameters | Type | Default | Description |
---|---|---|---|
relationNames | Array of String | none | Array containing all the relation name |
relationType | String | none | Type 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
Parameters | Type | Default | Description |
---|---|---|---|
child | SpinalNode or Model | none | Node to add as child |
relationName | String | none | Name of the relation |
relationType | String | none | Type 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
Parameters | Type | Default | Description |
---|---|---|---|
child | SpinalNode or Model | none | Node to add as child |
relationName | String | none | Name of the relation |
relationType | String | none | Type of the relation |
context | SpinalContext | none | Context 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
Parameters | Type | Default | Description |
---|---|---|---|
node | SpinalNode | none | Node to remove |
relationName | String | none | Name of the relation to which the node belongs |
relationType | String | none | Type 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
Parameters | Type | Default | Description |
---|---|---|---|
relationNames | Array of Strings or empty array or String | empty array | Array 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
Parameters | Type | Default | Description |
---|---|---|---|
relationNames | Array of Strings | none | Array 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
Parameters | Type | Default | Description |
---|---|---|---|
context | SpinalContext | none | Context 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
Parameters | Type | Default | Description |
---|---|---|---|
relationNames | Array of Strings | none | Array 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
Parameters | Type | Default | Description |
---|---|---|---|
relationNames | Array of Strings | none | array containing the relation names to follow |
predicate | function | A function that always returns true | Function 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
Parameters | Type | Default | Description |
---|---|---|---|
context | SpinalContext | none | Context to use for the search |
predicate | function | A function that always returns true | Function 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
Parameters | Type | Default | Description |
---|---|---|---|
relationNames | Array of Strings | none | array containing the relation names to follow |
callback | function | none | Function 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
Parameters | Type | Default | Description |
---|---|---|---|
context | SpinalContext | none | Context to use for the search |
callback | function | none | Function 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
Parameters | Type | Default | Description |
---|---|---|---|
relationNames | Array of Strings | none | array containing the relation names to follow |
callback | function | none | Function 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
Parameters | Type | Default | Description |
---|---|---|---|
context | SpinalContext | none | Context to use for the search |
callback | function | none | Function to apply to the nodes |