2017-02-28 17:10:11 +00:00
|
|
|
module.exports = function(self) {
|
2017-06-13 06:44:40 +00:00
|
|
|
|
2017-02-28 17:10:11 +00:00
|
|
|
self.setup = function() {
|
|
|
|
self.super_.setup.call(this);
|
|
|
|
|
|
|
|
let disableMethods = {
|
|
|
|
'create': true,
|
|
|
|
'replaceOrCreate': true,
|
|
|
|
'patchOrCreate': true,
|
|
|
|
'upsert': true,
|
|
|
|
'updateOrCreate': true,
|
|
|
|
'exists': true,
|
|
|
|
'find': true,
|
|
|
|
'findOne': true,
|
|
|
|
'findById': true,
|
|
|
|
'deleteById': true,
|
|
|
|
'replaceById': true,
|
|
|
|
'updateAttributes': false,
|
|
|
|
'createChangeStream': true,
|
|
|
|
'updateAll': true,
|
|
|
|
'upsertWithWhere': true,
|
|
|
|
'count': true
|
|
|
|
};
|
|
|
|
|
|
|
|
for(let method in disableMethods) {
|
|
|
|
//this.disableRemoteMethod(method, disableMethods[method]);
|
|
|
|
}
|
2017-06-21 05:28:03 +00:00
|
|
|
};
|
2017-06-20 11:06:13 +00:00
|
|
|
|
2017-06-21 05:28:03 +00:00
|
|
|
self.defineScope = function(serverFilter) {
|
2017-06-20 11:06:13 +00:00
|
|
|
this.remoteMethod('list', {
|
|
|
|
accepts: [
|
|
|
|
{
|
|
|
|
arg: 'filter',
|
|
|
|
type: 'object',
|
|
|
|
description: 'Filter defining where'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
returns: {
|
|
|
|
type: [this.modelName],
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
verb: 'get',
|
|
|
|
path: '/list'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-06-21 05:28:03 +00:00
|
|
|
this.list = function(clientFilter, cb) {
|
|
|
|
var where = {"and": []};
|
|
|
|
(clientFilter) ? where.and.push(clientFilter.where) : undefined;
|
|
|
|
(serverFilter) ? where.and.push(serverFilter.where) : undefined;
|
|
|
|
|
|
|
|
var order = (clientFilter && clientFilter.order) ? clientFilter.order : (serverFilter && serverFilter.order) ? serverFilter.order : undefined;
|
|
|
|
var limit = (serverFilter && serverFilter.limit) ? serverFilter.limit : (clientFilter && clientFilter.limit) ? clientFilter.limit : undefined;
|
|
|
|
|
|
|
|
var filter = {"where": where, "order": order, "limit": limit};
|
|
|
|
filter = removeEmpty(filter);
|
|
|
|
|
|
|
|
this.find(filter, function(err, states) {
|
|
|
|
(!err) ? cb(null, states) : cb(err, null);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
2017-06-20 11:06:13 +00:00
|
|
|
|
2017-06-15 09:02:50 +00:00
|
|
|
self.rawSql = function(query, params, cb) {
|
|
|
|
this.dataSource.connector.execute(query, params, function(error, response) {
|
|
|
|
cb(error, response);
|
|
|
|
});
|
|
|
|
};
|
2017-06-14 06:54:45 +00:00
|
|
|
|
2017-06-15 09:02:50 +00:00
|
|
|
self.remoteMethodCtx = function(methodName, args) {
|
|
|
|
if(args.accepts !== undefined && Array.isArray(args.accepts)) {
|
|
|
|
var ctx = {
|
|
|
|
arg: 'context',
|
|
|
|
type: 'object',
|
|
|
|
http: function(ctx) {
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
args.accepts.unshift(ctx);
|
|
|
|
}
|
|
|
|
this.remoteMethod(methodName, args);
|
2017-06-14 06:54:45 +00:00
|
|
|
};
|
2017-06-13 06:44:40 +00:00
|
|
|
|
2017-06-15 09:02:50 +00:00
|
|
|
self.connectToService = function(ctx, dataSource) {
|
|
|
|
this.app.dataSources[dataSource].connector.remotes.auth = {
|
|
|
|
bearer: new Buffer(ctx.req.accessToken.id).toString('base64'),
|
|
|
|
sendImmediately: true
|
|
|
|
};
|
2017-06-13 06:44:40 +00:00
|
|
|
};
|
|
|
|
|
2017-06-15 09:02:50 +00:00
|
|
|
self.disconnectFromService = function(dataSource) {
|
|
|
|
this.app.dataSources[dataSource].connector.remotes.auth = {
|
|
|
|
bearer: new Buffer("").toString('base64'),
|
|
|
|
sendImmediately: true
|
|
|
|
};
|
2017-06-13 06:44:40 +00:00
|
|
|
};
|
2017-02-28 17:10:11 +00:00
|
|
|
|
|
|
|
self.installMethod = function(methodName, filterCb) {
|
|
|
|
this.remoteMethod(methodName, {
|
|
|
|
description: 'List items using a filter',
|
|
|
|
accessType: 'READ',
|
|
|
|
accepts: [
|
|
|
|
{
|
|
|
|
arg: 'filter',
|
|
|
|
type: 'object',
|
|
|
|
required: true,
|
|
|
|
description: 'Filter defining where',
|
|
|
|
http: function(ctx) {
|
|
|
|
return ctx.req.query;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
returns: {
|
|
|
|
arg: 'data',
|
|
|
|
type: [this.modelName],
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
verb: 'get',
|
|
|
|
path: `/${methodName}`
|
|
|
|
}
|
|
|
|
});
|
2017-05-18 11:40:42 +00:00
|
|
|
|
2017-05-31 08:58:48 +00:00
|
|
|
this[methodName] = (params, cb) => {
|
|
|
|
let filter = removeEmpty(filterCb(params));
|
|
|
|
var response = {}
|
|
|
|
|
|
|
|
function returnValues(){
|
|
|
|
if(response.instances !== undefined && response.count !== undefined)
|
|
|
|
cb(null, response);
|
|
|
|
}
|
|
|
|
|
|
|
|
function error(){
|
|
|
|
cb(null, response);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.find(filter, function(err, instances) {
|
|
|
|
if(!err){
|
|
|
|
response.instances = instances;
|
|
|
|
returnValues();
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
error();
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
this.count(filter.where, function(err, totalCount){
|
|
|
|
if(!err){
|
|
|
|
response.count = totalCount;
|
|
|
|
returnValues();
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
error();
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
};
|
2017-05-18 11:40:42 +00:00
|
|
|
|
2017-05-18 11:59:02 +00:00
|
|
|
};
|
2017-02-28 17:10:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function removeEmpty(o) {
|
|
|
|
if(Array.isArray(o)) {
|
|
|
|
let array = [];
|
|
|
|
for(let item of o) {
|
|
|
|
let i = removeEmpty(item);
|
|
|
|
if(!isEmpty(item))
|
|
|
|
array.push(item);
|
|
|
|
};
|
|
|
|
if(array.length > 0)
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
else if (typeof o === 'object') {
|
|
|
|
let object = {};
|
|
|
|
for(let key in o) {
|
|
|
|
let i = removeEmpty(o[key]);
|
|
|
|
if(!isEmpty(i))
|
|
|
|
object[key] = i;
|
|
|
|
}
|
|
|
|
if(Object.keys(object).length > 0)
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
else if (!isEmpty(o))
|
|
|
|
return o;
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isEmpty(value) {
|
|
|
|
return value === undefined || value === "";
|
|
|
|
}
|
2017-05-17 12:58:23 +00:00
|
|
|
|