Docs‎ > ‎Live API‎ > ‎Node SDK‎ > ‎

Create A Resource & Logic

Using Node.js to Create a Database connection, create a Resource, and Add Reactive Logic (Metadata API)

Most project API endpoints follow a simple structure: {projectUrl}/{databasePrefix}:{tableName} to retrieve data from your service, a full endpoint might look like this:
https://eval.espressologic.com/rest/livedemo/demo/v1/demo:customer
Though the meta API used to start and manage the creation of other projects, database connections, resources, and rules uses the servername and password sent to you when you created a new appliance or cloud account:
var espressologic = require('./EspressoLogicSDK');
var serverName = 'http://{myservername}/rest/abl/admin/v2';
var userName = 'admin';
var password = 'mypassword';
var api = espressologic.connect(serverName, userName, password);
We can look at our current projects via the AllProjects endpoint (sampled below), though when creating rules and resources, we are exclusively interested in the meta API project.
var projects = api.endpoint('AllProjects?pagesize=100');
projects.get().then(function (data) {
    for(var idx in data){
        console.log('ProjectName: '+data[idx].name + " Project ident "+data[idx].ident);
    }
});
In all probability, the meta API project is 1001, unless it had been manually changed.
var project_ident = 1001;
var params = {};

//    dbasetype_ident
//    1    MySQL            MySQL Database
//    2    Oracle            Oracle (Thin driver)
//    3    SQLServer(jTDS)    Microsoft SQL Server(jTDS - obsolete)
//    5    SQLServer        Microsoft SQL Server
//    6    AzureSQL        Windows Azure SQL
//    7    NuoDB (beta)    NuoDB - beta
//    8    PostgreSQL        Postgres (beta)

//create a new connection to our project
var dbschema = api.endpoint('/DbSchemas');
var json = {"name":"My New database","url":"jdbc:mysql://<hostname>/<db-name>","user_name":"jdoe","active":false,"project_ident":project_ident,"dbasetype_ident":1}
dbschema.post(json, params).then(function (txSummary) {
    console.log(txSummary); //an array of objects updated
});

//create a new resource
var resource = api.endpoint('/AllResources');
var json = {"resource_type_ident":1,"apiversion_ident":project_ident,"name":"MyNewCustomerResource","prefix":"demo","table_name":"customer","is_collection":"Y"};
resource.post(json, params).then(function (txSummary) {
    console.log(txSummary); //an array of objects updated
});
//add child resource - using root_ident from prior txSummary - remember to add join as childColumn = [parentColumn]
var subresource = api.endpoint('/AllResources');
var json ={"apiversion_ident":project_ident,"resource_type_ident":1,"name":"NewChildResource","prefix":"demo","table_name":"orders", "join_condition": "customerID=[customerId]","is_collection":"Y","root_ident":1086,"container_ident":1086};
subresource.post(json, params).then(function (txSummary) {
    console.log(txSummary); //an array of objects updated
});

//    ruletype_ident
//    1    sum                sum child attr into parent
//    2    count            count child attr into parent
//    3    formula            formula using table & parent attributes
//    4    parent copy        non-cascaded parent value
//    5    validation        multi-attribute validation
//    6    commit validation    multi-attribute validation after all rows
//    7    event            extensible event
//    8    early event        extensible event before rule execution
//    9    commit event    extensible event after all rows
//    11    minimum            replicate the minimum value from a child attribute
//    12    maximum            replicate the maximum value from a child attribute
//    13    managed parent    create a parent if it does not exist//declare validation and derivation rules.

//add SUM on Customer
var  sumrule  = api.endpoint('/AllRules');
var json = {"ruletype_ident":1,"entity_name":"demo:customer","project_ident":project_ident,"attribute_name":"balance","rule_text1":"purchaseorderList","rule_text3":"amount_total","active":true};
sumrule.post(json, params).then(function (txSummary) {
    console.log(txSummary); //an array of objects updated
});
//Add Validation Check on Credit Limit
var validationRule = api.endpoint('/AllRules');
var json = {"ruletype_ident":5,"entity_name":"demo:customer","attribute_name": null,"project_ident":project_ident,"prop4": "javascript","rule_text1": "return row.balance <= row.credit_limit;","rule_text2": "Customer {name}'s balance: {balance|#,##0.00} exceeds their credit limit: {credit_limit|#,##0.00}","active":true};
validationRule.post(json, params).then(function (txSummary) {
    console.log(txSummary); //an array of objects updated
});
Comments