NodeJS version
Install the package from npm:
npm install https://github.com/spinalcom/spinal-core-connectorjs.git
You can require the module in the standard way:
const spinalCore = require("spinal-core-connectorjs");
const conn = spinalCore.connect("http://644:password@127.0.0.1:8888/__myApp__");
This method establishes a connection with the hub. It returns an object containing the connection information, which will be used in the other API methods that communicate with the hub.
The string parameter has the following format:
protocol://userid:password@host:port/path
Explanation:
- protocol: the communication protocol used to communicate with the hub; currently only supporting "http"
- userid: a user id that has access to the data; beware that the user has the needed rights to manage the data (read more about it here)
- password: the password of the user
- host: address of the hub
- port: port number where the hub is running
- path {optinal}: location in the filesystem of the files that we want to store/load
class MyModel extends Model { constructor() { super(); this.add_attr({ // key: value // attribute1: "sample", // attribute2: 2, // attribute3: ["one", "two"], // attribute4: {one: 1} // ... }); } } spinalCore.register_models([MyModel]);
A Model is a class, which contains a structure of the data we want to synchronize with the hub. There are three required steps to define a class:
- Create a class and extend the class from the base Model class provided by SpinalCore, and call its super() in a constructor().
- Add all the attributes of the class we want to synchronize using the add_attr() method.
- Register the model in the SpinalCore with register_models method, with this method spinalCore can recognize the model.
The value of the attributes should correspond to one of these data types:
- string
- number
- array
- object
- boolean
- any class inherited from Model
const models = require('../path-to-model/models.js'); let myObj = new models.MyModel(); spinalCore.store(conn, myObj, "myObj", callbackSucces, callbackFailure);
A model object is created as an instance of a Model class, and then saved in the hub with the store() method. All the changes made in it will be automatically submited to the hub. The parameters accepted are:
- Connection instance, obtained from the connect() method
- Model object
- The path of the object in your virtual fileSystem
- Callback function executed once the model has been successfully stored
- Callback function executed if there was an error while storing the model (probably another model exists)
spinalCore.load(conn, "myObj", callbackSuccess, callbackFailure);
Once an object has been saved in the hub, this command is used for fetching it and keeping synchronized with its changes. The parameters accepted are:
- Connection instance, obtained from the connect() method
- The path of the object in your virtual fileSystem
- Callback function executed once the model has been successfully loaded; as a parameter it receives the object itself
- Callback function executed if there was an error while loading the model (probably it doesn't exists)
spinalCore.load_type(conn, "className", callbackSuccess, callbackError);
Once some objects have been saved in the hub, this command is used for fetching all the models of a specific class and keeping synchronized with their changes. The parameters accepted are:
- Connection instance, obtained from the connect() method
- Name of the class
- The same function is called for each models that the Connector receive, and the Callback function receives the object itself as a parameter.
- Callback function executed if there was an error while loading the models
let value = myObj.attribute1.get(); myObj.attribute1.set(value);
To obtain or modify an attribute of the model object, the classic get() and set() methods should be applied. Note that if the object has previously been stored with the store() function, the modified data will automatically synchronize with the hub.
class MyProcess extends Process { constructor() { super(model); } onchange() { // code to execute whenever the model changes }; }
A Process is a class prepared to interact with models. It can listen for changes in one or more models and reacts to them. There are three steps to define a process:
- Create a function and call its super(), passing its context and a model or array of models as arguments
- Optionally, you can override the onchange() method that will be executed whenever the given models change
myObj.bind(reactFunction);
The method bind() will execute the reactFunction(), that should be defined by the developer, everytime the data in the models and it's childs are modified.
In SpinalCoreJs, the management of users is done with a set of function of the SpinalUserManager object:
- get_user_id
- new_account
- change_password
- delete_account
- get_admin_id
- change_password_by_admin
- delete_account_by_admin
- change_account_rights_by_admin
get_user_id(options, user_name, password, success_callback, error_callback = null)
- options: String the parameter to connect to the SpinalHub, example "http://127.0.0.1:8888"
- user_name: String containing the username.
- password: String containing the password of the user.
- success_callback: function callback with the response when the function succeed.
- error_callback (optionnal): function callback with the response or status error when the function fail.
SpinalUserManager.get_user_id('http://127.0.0.1:8888', 'test@spinalhub', 'password', function(response) { console.log( 'success : ' + response ) }, function(response) { console.log( 'error : ' + response ) } );
new_account(options, user_name, password, success_callback, error_callback = null)
- options: String the parameter to connect to the SpinalHub, example "http://127.0.0.1:8888"
- user_name: String containing the username.
- password: String containing the password of the user.
- success_callback: function callback with the response when the function succeed.
- error_callback (optionnal): function callback with the response or status error when the function fail.
SpinalUserManager.new_account('http://127.0.0.1:8888', 'test@spinalhub', 'password', function(response) { console.log( 'success : ' + response ) }, function(response) { console.log( 'error : ' + response ) } );
change_password(options, user_id, password, new_password, success_callback, error_callback = null)
- options: String the parameter to connect to the SpinalHub, example "http://127.0.0.1:8888"
- user_id: Contain the user id.
- password: String containing the password of the user.
- new_password: String containing the new_password of the user.
- success_callback: function callback with the response when the function succeed.
- error_callback (optionnal): function callback with the response or status error when the function fail.
SpinalUserManager.change_password('http://127.0.0.1:8888', 777777, 'password', 'new_password', function(response) { console.log( 'success : ' + response ) }, function(response) { console.log( 'error : ' + response ) } );
delete_account(options, user_id, password, success_callback, error_callback = null)
- options: String the parameter to connect to the SpinalHub, example "http://127.0.0.1:8888"
- user_id: Contain the user id.
- password: String containing the password of the user.
- success_callback: function callback with the response when the function succeed.
- error_callback (optionnal): function callback with the response or status error when the function fail.
SpinalUserManager.delete_account('http://127.0.0.1:8888', 777777, 'password', function(response) { console.log( 'success : ' + response ) }, function(response) { console.log( 'error : ' + response ) } );
get_admin_id(options, admin_name, password, success_callback, error_callback = null)
- options: String the parameter to connect to the SpinalHub, example "http://127.0.0.1:8888"
- admin_name: String containing the admin username.
- password: String containing the password of the admin user.
- success_callback: function callback with the response when the function succeed.
- error_callback (optionnal): function callback with the response or status error when the function fail.
SpinalUserManager.get_admin_id('http://127.0.0.1:8888', 'admin name', 'password', function(response) { console.log( 'success : ' + response ) }, function(response) { console.log( 'error : ' + response ) } );
change_password_by_admin(options, user_name, new_password, admin_id, admin_password, success_callback, error_callback = null)
- options: String the parameter to connect to the SpinalHub, example "http://127.0.0.1:8888"
- user_name: String containing the username.
- new_password: String containing the new password of the user.
- admin_id: Contain the id of a admin (default: 644 or 168)
- admin_password: String containing the password of the admin.
- success_callback: function callback with the response when the function succeed.
- error_callback (optionnal): function callback with the response or status error when the function fail.
SpinalUserManager.change_password_by_admin('http://127.0.0.1:8888', 777777, 'new_password', 644,'4YCSeYUzsDG8XSrjqXgkDPrdmJ3fQqHs', function(response) { console.log( 'success : ' + response ) }, function(response) { console.log( 'error : ' + response ) } );
delete_account_by_admin(options, user_name, admin_id, admin_password, success_callback, error_callback = null)
- options: String the parameter to connect to the SpinalHub, example "http://127.0.0.1:8888"
- user_name: String containing the username.
- admin_id: Contain the id of a admin (default: 644 or 168)
- admin_password: String containing the password of the admin.
- success_callback: function callback with the response when the function succeed.
- error_callback (optionnal): function callback with the response or status error when the function fail.
SpinalUserManager.delete_account_by_admin('http://127.0.0.1:8888', 777777, 644,'4YCSeYUzsDG8XSrjqXgkDPrdmJ3fQqHs', function(response) { console.log( 'success : ' + response ) }, function(response) { console.log( 'error : ' + response ) } );
change_account_rights_by_admin(options, user_name, right, admin_id, admin_password, success_callback, error_callback = null)
- options: String the parameter to connect to the SpinalHub, example "http://127.0.0.1:8888"
- user_name: String containing the username.
- right: Contain the right (0 = Read/Write // 1 = Read).
- admin_id: Contain the id of a admin (default: 644 or 168)
- admin_password: String containing the password of the admin.
- success_callback: function callback with the response when the function succeed.
- error_callback (optionnal): function callback with the response or status error when the function fail.
SpinalUserManager.change_account_rights_by_admin('http://127.0.0.1:8888', 'test@spinalhub', 0, '644','4YCSeYUzsDG8XSrjqXgkDPrdmJ3fQqHs', function(response) { console.log( 'success : ' + response ) }, function(response) { console.log( 'error : ' + response ) } );