2017-06-07 06:38:48 +00:00
|
|
|
module.exports = function(self) {
|
|
|
|
self.setup = function() {
|
|
|
|
self.super_.setup.call(this);
|
|
|
|
|
|
|
|
let disableMethods = {
|
2017-06-21 11:29:01 +00:00
|
|
|
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
|
2017-06-07 06:38:48 +00:00
|
|
|
};
|
2017-06-21 11:29:01 +00:00
|
|
|
for (let method in disableMethods) {
|
2017-06-07 06:38:48 +00:00
|
|
|
//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-21 10:11:38 +00:00
|
|
|
this.remoteMethodCtx('list', {
|
2017-06-20 11:06:13 +00:00
|
|
|
accepts: [
|
|
|
|
{
|
2017-06-21 11:29:01 +00:00
|
|
|
arg: 'filter',
|
|
|
|
type: 'object',
|
2017-06-20 11:06:13 +00:00
|
|
|
description: 'Filter defining where'
|
|
|
|
}
|
|
|
|
],
|
2017-06-21 11:29:01 +00:00
|
|
|
returns: {
|
2017-06-20 11:06:13 +00:00
|
|
|
type: [this.modelName],
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
verb: 'get',
|
|
|
|
path: '/list'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-06-21 10:11:38 +00:00
|
|
|
this.list = function(ctx, clientFilter, cb) {
|
2017-06-22 12:51:37 +00:00
|
|
|
var clientFields = (clientFilter && clientFilter.fields) ? clientFilter.fields : [];
|
|
|
|
var serverFields = (serverFilter && serverFilter.fields) ? serverFilter.fields : [];
|
|
|
|
var fields = clientFields.filter(itemC => {
|
|
|
|
return serverFields.some(itemS => itemS === itemC);
|
|
|
|
});
|
|
|
|
|
|
|
|
var and = [];
|
|
|
|
(clientFilter && clientFilter.where) && and.push(clientFilter.where);
|
|
|
|
(serverFilter && serverFilter.where) && and.push(serverFilter.where);
|
2017-06-21 05:28:03 +00:00
|
|
|
|
2017-06-22 12:51:37 +00:00
|
|
|
var order;
|
|
|
|
var limit;
|
|
|
|
|
|
|
|
if (clientFilter && clientFilter.order)
|
|
|
|
order = clientFilter.order;
|
|
|
|
else if (serverFilter && serverFilter.order)
|
|
|
|
order = serverFilter.order;
|
|
|
|
|
|
|
|
if (serverFilter && serverFilter.limit)
|
|
|
|
limit = serverFilter.limit;
|
|
|
|
else if (clientFilter && clientFilter.limit)
|
|
|
|
limit = clientFilter.limit;
|
2017-06-21 05:28:03 +00:00
|
|
|
|
2017-06-22 12:51:37 +00:00
|
|
|
var filter = {order: order, limit: limit};
|
|
|
|
filter.where = (and.length > 0) && {and: and};
|
|
|
|
filter.fields = fields;
|
2017-06-21 05:28:03 +00:00
|
|
|
|
|
|
|
this.find(filter, function(err, states) {
|
2017-06-22 12:51:37 +00:00
|
|
|
(err) ? cb(err, null) : cb(null, states);
|
2017-06-21 05:28:03 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
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) {
|
2017-06-21 11:29:01 +00:00
|
|
|
if (args.accepts !== undefined && Array.isArray(args.accepts)) {
|
|
|
|
var ctx = {
|
|
|
|
arg: 'context',
|
|
|
|
type: 'object',
|
|
|
|
http: function(ctx) {
|
|
|
|
return ctx;
|
2017-06-15 09:02:50 +00:00
|
|
|
}
|
2017-06-21 11:29:01 +00:00
|
|
|
};
|
|
|
|
args.accepts.unshift(ctx);
|
2017-06-15 09:02:50 +00:00
|
|
|
}
|
|
|
|
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-06-07 06:38:48 +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: {
|
2017-06-21 11:29:01 +00:00
|
|
|
arg: 'data',
|
2017-06-07 06:38:48 +00:00
|
|
|
type: [this.modelName],
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
verb: 'get',
|
|
|
|
path: `/${methodName}`
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this[methodName] = (params, cb) => {
|
|
|
|
let filter = removeEmpty(filterCb(params));
|
2017-06-21 11:29:01 +00:00
|
|
|
var response = {};
|
2017-06-07 06:38:48 +00:00
|
|
|
|
2017-06-21 11:29:01 +00:00
|
|
|
function returnValues() {
|
|
|
|
if (response.instances !== undefined && response.count !== undefined)
|
2017-06-07 06:38:48 +00:00
|
|
|
cb(null, response);
|
|
|
|
}
|
|
|
|
|
2017-06-21 11:29:01 +00:00
|
|
|
function error() {
|
2017-06-07 06:38:48 +00:00
|
|
|
cb(null, response);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.find(filter, function(err, instances) {
|
2017-06-21 11:29:01 +00:00
|
|
|
if (err) {
|
|
|
|
error();
|
|
|
|
} else {
|
2017-06-07 06:38:48 +00:00
|
|
|
response.instances = instances;
|
|
|
|
returnValues();
|
|
|
|
}
|
2017-06-21 11:29:01 +00:00
|
|
|
});
|
2017-06-07 06:38:48 +00:00
|
|
|
this.count(filter.where, function(err, totalCount){
|
2017-06-21 11:29:01 +00:00
|
|
|
if (err) {
|
|
|
|
error();
|
|
|
|
} else {
|
2017-06-07 06:38:48 +00:00
|
|
|
response.count = totalCount;
|
|
|
|
returnValues();
|
|
|
|
}
|
2017-06-21 11:29:01 +00:00
|
|
|
});
|
2017-06-07 06:38:48 +00:00
|
|
|
};
|
|
|
|
};
|
2017-06-21 11:29:01 +00:00
|
|
|
};
|
2017-06-07 06:38:48 +00:00
|
|
|
function removeEmpty(o) {
|
2017-06-21 11:29:01 +00:00
|
|
|
if (Array.isArray(o)) {
|
2017-06-07 06:38:48 +00:00
|
|
|
let array = [];
|
2017-06-21 11:29:01 +00:00
|
|
|
for (let item of o) {
|
2017-06-07 06:38:48 +00:00
|
|
|
let i = removeEmpty(item);
|
2017-06-21 11:29:01 +00:00
|
|
|
if (!isEmpty(item))
|
2017-06-07 06:38:48 +00:00
|
|
|
array.push(item);
|
2017-06-21 11:29:01 +00:00
|
|
|
}
|
|
|
|
if (array.length > 0)
|
2017-06-07 06:38:48 +00:00
|
|
|
return array;
|
2017-06-21 11:29:01 +00:00
|
|
|
} else if (typeof o === 'object') {
|
2017-06-07 06:38:48 +00:00
|
|
|
let object = {};
|
2017-06-21 11:29:01 +00:00
|
|
|
for (let key in o) {
|
2017-06-07 06:38:48 +00:00
|
|
|
let i = removeEmpty(o[key]);
|
2017-06-21 11:29:01 +00:00
|
|
|
if (!isEmpty(i))
|
|
|
|
object[key] = i;
|
2017-06-07 06:38:48 +00:00
|
|
|
}
|
2017-06-21 11:29:01 +00:00
|
|
|
if (Object.keys(object).length > 0)
|
2017-06-07 06:38:48 +00:00
|
|
|
return object;
|
2017-06-21 11:29:01 +00:00
|
|
|
} else if (!isEmpty(o))
|
2017-06-07 06:38:48 +00:00
|
|
|
return o;
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isEmpty(value) {
|
|
|
|
return value === undefined || value === "";
|
|
|
|
}
|
|
|
|
|