import {Module} from '../Module.js';
import {events} from './DataSourcesEvents.js';
import {default as PubSub} from 'pubsub-js';
import {Module} from '../Module.js';
import {events} from './DataSourcesEvents.js';
import {default as PubSub} from 'pubsub-js';
depends: Kronicle.Module, Kronicle.DataSourcesEvents, pubsub-js This class is used to implement a CRUD datasource for use in Kronicle. The constructor takes an args object which has the following properties:
export class DataSource extends Module {
constructor(args = {source: undefined, name: ''}) {
let emptySource = {
login: (user, pass, cb) => { cb(); return; },
create: (item, cb) => { cb(); return; },
update: (item, cb) => { cb(); return; },
remove: (item, cb) => { cb(); return; },
get: (item, cb) => { cb(); return; }
};
this.source = args.source || emptySource;
super({name: args.name});
return this;
}
This is impemented in the source if the datasource requires a login. The method takes three arguments
login(user, pass, cb) {
this.source.login(user, pass, (err, data) => {
this._doCbIfExists(cb, err, data);
PubSub.publish(events.OnLogin, user, pass, events.OnLogin);
});
}
This method is implemented in the source to create a new source item. The method takes two arguments
create(item, cb) {
this.source.create(item, (err, data) => {
this._doCbIfExists(cb, err, data);
PubSub.publish(events.OnCreate, item, events.OnCreate);
});
}
This method is implemented in the source to update a source item. The method takes two arguments
update(item, cb) {
this.source.update(item, (err, data) => {
this._doCbIfExists(cb, err, data);
PubSub.publish(events.OnUpdate, item, events.OnUpdate);
});
}
This method is implemented in the source to remove a source item. The method takes two arguments
remove(item, cb) {
this.source.remove(item, (err, data)=> {
this._doCbIfExists(cb, err, data);
PubSub.publish(events.OnRemove, item, events.OnRemove);
});
}
This method is implemented in the source to get a source item. The method takes two arguments
get(item, cb) {
this.source.get(item, (err, data) => {
this._doCbIfExists(cb, err, data);
PubSub.publish(events.OnGet, item, events.OnGet);
});
}
This method is used as a hook into the OnLogin event. It is passed one argument:
onLogin(cb) {
PubSub.subscribe(events.OnLogin, cb);
}
This method is used as a hook into the OnCreate event. It is passed one argument:
onCreate(cb) {
PubSub.subscribe(events.OnCreate, cb);
}
This method is used as a hook into the OnUpdate event. It is passed one argument:
onUpdate(cb) {
PubSub.subscribe(events.OnUpdate, cb);
}
This method is used as a hook into the OnRemove event. It is passed one argument:
onRemove(cb) {
PubSub.subscribe(events.OnRemove, cb);
}
This method is used as a hook into the OnGet event. It is passed one argument:
onGet(cb) {
PubSub.subscribe(events.OnGet, cb);
}
This method is to be used internally and not meant to be used by consuming modules. This method checks if a callback exists and if so, calls it with err and data arguments passed to it. It take three arguments:
_doCbIfExists(cb, err, data) {
if(cb){
cb(err, data);
}
}
}