NodeJs SDK

See code example here

Main function of SDK - start receives context (ctx below), which describes environment variables, application info, registered subscriptions and operations. It consists of two main parts - environment section and manifest.

ctx.js
var ctx = {
env: {
init_url: process.env.APP_INIT_URL,
....
},
manifest: {
id: APP_ID,
type: 'app'
...
}
};

Register Operation

Let's describe new operation - called "report", we will obtain count of Aidbox default resource - Attribute. Our handle will looks like this

handler.js
const report = (ctx, msg) =>
ctx
.query('select count(*) FROM Attribute')
.then(data => Promise.resolve({ count: data[0].count }));

Add it into manifest in operations section and specify method - http request method, path - array with path request value, handler - link to handler

Register Subscription

We have an ability to subscribe to updates of resources. For example let's subscribe for User resource and print to console new entities.

subscription.js
const userSub =(ctx, msg) => {
console.log('my userSub handler\nctx:', ctx, '\nmsg:', msg);
}

Add register it into manifest in subscriptions section specifies entity and subscription handler

Our context variable with operation and subscription will looks like this

ctx.js
var ctx = {
env: {
...
},
manifest: {
id: APP_ID,
type: 'app',
subscriptions: {
User: {
handler: userSub
}
},
operations: {
report: {
method: 'GET',
path: ["_report"],
handler: report
}
}
}
};

Then pass ctx into start method and have fun!

index.js
const aidbox = require('aidbox-node-sdk');
const ctx = require('./src/ctx');
aidbox.start(ctx)
.then(() => console.log('connected to server and started'))
.catch(err => console.log(err.body));