diff --git a/services/client/common/models/Client.js b/services/client/common/models/Client.js index c8331f42d..64f7fa60f 100644 --- a/services/client/common/models/Client.js +++ b/services/client/common/models/Client.js @@ -1,4 +1,3 @@ -let installMethod = require('../util.js'); module.exports = function(Client) { // Validations @@ -76,7 +75,7 @@ module.exports = function(Client) { // Basic filter - installMethod(Client, 'filter', filterClients); + Client.installMethod('filter', filterClients); function filterClients(p) { return { where: { diff --git a/services/client/common/models/Client.json b/services/client/common/models/Client.json index e561dd88b..38d8effee 100644 --- a/services/client/common/models/Client.json +++ b/services/client/common/models/Client.json @@ -1,6 +1,6 @@ { "name": "Client", - "base": "PersistedModel", + "base": "MyModel", "validateUpsert": true, "properties": { "id": { diff --git a/services/client/common/models/MyModel.js b/services/client/common/models/MyModel.js new file mode 100644 index 000000000..6bf440d1a --- /dev/null +++ b/services/client/common/models/MyModel.js @@ -0,0 +1,96 @@ + +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.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}` + } + }); + + this.filter = (params, cb) => { + let filter = removeEmpty(filterCb(params)); + this.find(filter, function(err, instances) { + if(!err) { + cb(null, instances); + } + }) + }; + }; +} + +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 === ""; +} diff --git a/services/client/common/models/MyModel.json b/services/client/common/models/MyModel.json new file mode 100644 index 000000000..7a0a975bb --- /dev/null +++ b/services/client/common/models/MyModel.json @@ -0,0 +1,4 @@ +{ + "name": "MyModel", + "base": "PersistedModel" +} diff --git a/services/client/common/util.js b/services/client/common/util.js deleted file mode 100644 index aab36597c..000000000 --- a/services/client/common/util.js +++ /dev/null @@ -1,69 +0,0 @@ - -module.exports = installMethod; - -function installMethod(model, methodName, filterCb) { - model.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: [model.modelName], - root: true - }, - http: { - verb: 'get', - path: `/${methodName}` - } - }); - - model.filter = function(params, cb) { - let filter = removeEmpty(filterCb(params)); - model.find(filter, function(err, instances) { - if(!err) { - cb(null, instances); - } - }) - }; -} - -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 === ""; -}