loopback icon indicating copy to clipboard operation
loopback copied to clipboard

A way to override remote methods

Open offlinehacker opened this issue 12 years ago • 34 comments

One such example would be deleteById, where instead of deleting, i would set only deleted property.

offlinehacker avatar Aug 06 '14 14:08 offlinehacker

+1

meng-zhang avatar Aug 06 '14 14:08 meng-zhang

For example, if you want to override it for customer model in 2.x, add a customer.js to common/models:

module.exports = function(Customer) {
  Customer.deleteById = function(id, cb) {
    Customer.update({id: id}, {deleted: true}, cb);
  }
}

raymondfeng avatar Aug 06 '14 16:08 raymondfeng

Nice, thanks

meng-zhang avatar Aug 06 '14 21:08 meng-zhang

I have created a dynamic model "Item" (item.json). I want to override the "find" method. I wrote the following in item.js

module.exports = function(Item) { Item.find = function(cb) { cb(null, 'This is a overridden method'); } }

But I don't get the message passed in the callback function when I access the method from explorer. Is there anything else I need to do here.

alffrey avatar Aug 27 '14 13:08 alffrey

@alffrey I think your find method signature is wrong (it should take a query argument too). Other than that, it should work. I have a project where I'm doing this, and it works for me.

fabien avatar Aug 27 '14 13:08 fabien

@fabien I changed my method signature to "Item.find = function (params, cb)" but still no luck.

alffrey avatar Aug 27 '14 13:08 alffrey

@alffrey perhaps try cb(new Error('Override')) to see if the method is being called, perhaps it's tripping over the string return value you're giving.

fabien avatar Aug 27 '14 13:08 fabien

@fabien The overridden method is not getting called at all. I even tried putting in a console.log statement inside the method and it is not getting executed. From the explorer I am hitting GET /Items method.

alffrey avatar Aug 27 '14 13:08 alffrey

@alffrey I just tried this in a /models/*.js specifically with a find method like yours, and it works. I'd need more details in order to help you solve this.

fabien avatar Aug 28 '14 13:08 fabien

@alffrey Maybe this is what you want.


Model.once('attached',function(){
        Model.find = function(filter,cb){
            cb(null,'This is a overridden method')
        }
    })

veekungx avatar Sep 09 '14 08:09 veekungx

@veekungx Thank you so much. Your solution works.

alffrey avatar Sep 26 '14 10:09 alffrey

I have similar issue. https://github.com/strongloop/loopback/issues/1745

mike-aungsan avatar Oct 20 '15 00:10 mike-aungsan

@veekungx's solution works for me, @raymondfeng's didn't.

CarlosCuevas avatar Dec 03 '15 19:12 CarlosCuevas

I was trying @veekungx's solution but server got really slow, at the end it didn't complete any request. I found out debugging that Model.on('attached'.... was being called constantly again and again. Am I doing something wrong?

@raymondfeng's didn't work for me

Thanks

Shyri avatar Dec 09 '15 08:12 Shyri

Edit: Ok never mind is an issue with promises inside Model.on('attached'

Shyri avatar Dec 10 '15 14:12 Shyri

The @veekungx example doesn't work as it is for me (2.22.0). this works :

User.on('attached',function(app){
    User.find = function(filter, empty, cb){
        cb(null,['This is a overridden method'])
    }
})

EDIT:

Since LB uses find() to login, you have to send a User instance to the callback. Otherwise :

Error in POST /Users/login: TypeError: user.hasPassword is not a function

EDIT:

Surfer.__unlink__friends = function (id, cb) {
  console.log(, id, cb)
}

This new code above doesn't trigger.

strong-remoting:shared-method - __unlink__friends - invoke with +1ms [ 1, [Function: callback] ]
loopback:connector:postgresql SQL: DELETE FROM "couch-surfing"."surfer-friend" WHERE "surferid"=$1 AND "friendid"=$2

amaurybrisou avatar Dec 25 '15 18:12 amaurybrisou

I found overriding the find method also affected findOne etc which was undesirable.

My workaround was:

// Override Project.find to use our bespoke Project.list
Project.on('attached', () => {
  const origFind = Project.find.bind(Project);
  Project.find = (query, options, cb) => {
    if (typeof options === 'function' && options.name === 'callback') {
      return Project.list(query, options);
    }
    return origFind(query, options, cb);
  };
});

// This method is written as it would be as a standard remote method
Project.list = (filter, cb) => {
  ...
}

PaddyMann avatar Apr 22 '16 08:04 PaddyMann

I tried this for updateAttributes with no success.

Workaround: disable method and create my own

mamartins avatar May 31 '16 11:05 mamartins

Important: I no longer use or recommend the solution below - see my next comment

My solution required a little tweaking for findById, and the same is probably true for updateAttributes @mamartins

I now have a module exposing an overideRemoteMethod function:

function overideRemoteMethod (model, methodName, newFunc) {
 const origFunc = model[methodName];
 model[methodName] = (a, b, c, d) => {
   // We rely on the remote method call having a different signature to
   // the standard call.
   switch (methodName) {
     case 'findById':
       if (typeof c === 'function' && c.name === 'callback') {
         return newFunc(a, b, {}, c);
       }
       return origFunc.call(model, a, b, c, d);
     case 'find':
     case 'count':
     default:
       if (typeof b === 'function' && b.name === 'callback') {
         return newFunc(a, {}, b);
       }
       return origFunc.call(model, a, b, c);
   }
 };
}

(You'll probably need to add case 'updateById' below case 'findById')

I then call it in my model:

module.exports = (Workspace) => {
  Workspace.on('attached', () => {
    // Overide default methods to only return workspaces that the given user has
    // access to
    overideRemoteMethod(Workspace, 'find', Workspace.remoteFind);
    overideRemoteMethod(Workspace, 'count', Workspace.remoteCount);
    overideRemoteMethod(Workspace, 'findById', Workspace.remoteFindById);
    ...
  }

  Workspace.remoteFind = function (filter, options) {
    ...
  }

PaddyMann avatar May 31 '16 11:05 PaddyMann

That's a good one.

It would be good to have a default way of doing this for all methods

mamartins avatar May 31 '16 13:05 mamartins