salix/services/loopback/common/models/vn-model.js

124 lines
3.9 KiB
JavaScript

module.exports = function(Self) {
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]);
} */
};
Self.defineScope = function(serverFilter) {
this.remoteMethodCtx('list', {
accepts: [
{
arg: 'filter',
type: 'object',
description: 'Filter defining where'
}
],
returns: {
type: [this.modelName],
root: true
},
http: {
verb: 'get',
path: '/list'
}
});
this.list = function(ctx, clientFilter, cb) {
let clientFields = (clientFilter && clientFilter.fields) ? clientFilter.fields : [];
let serverFields = (serverFilter && serverFilter.fields) ? serverFilter.fields : [];
let fields = clientFields.filter(itemC => {
return serverFields.some(itemS => itemS === itemC);
});
let and = [];
let order;
let limit;
let filter = {order: order, limit: limit};
if (clientFilter && clientFilter.where)
and.push(clientFilter.where);
if (serverFilter && serverFilter.where)
and.push(serverFilter.where);
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;
filter.where = (and.length > 0) && {and: and};
filter.fields = fields;
this.find(filter, function(err, states) {
if (err)
cb(err, null);
else
cb(null, states);
});
};
};
Self.remoteMethodCtx = function(methodName, args) {
var ctx = {
arg: 'context',
type: 'object',
http: function(ctx) {
return ctx;
}
};
if (args.accepts === undefined)
args.accepts = [];
else if (!Array.isArray(args.accepts))
args.accepts = [args.accepts];
args.accepts.unshift(ctx);
this.remoteMethod(methodName, args);
};
Self.getConnection = function(cb) {
this.dataSource.connector.client.getConnection(cb);
};
Self.connectToService = function(ctx, dataSource) {
this.app.dataSources[dataSource].connector.remotes.auth = {
bearer: new Buffer(ctx.req.accessToken.id).toString('base64'),
sendImmediately: true
};
};
Self.disconnectFromService = function(dataSource) {
this.app.dataSources[dataSource].connector.remotes.auth = {
bearer: new Buffer('').toString('base64'),
sendImmediately: true
};
};
require('../methods/vnModel/rawSql')(Self);
require('../methods/vnModel/installMethod')(Self);
require('../methods/vnModel/validateBinded')(Self);
require('../methods/vnModel/installCrudModel')(Self);
};