loopback icon indicating copy to clipboard operation
loopback copied to clipboard

dynamic role resolver optimization

Open joeybenenati opened this issue 9 years ago • 4 comments

As I'm implementing dynamic roles resolvers with Role.registerResolver, I find myself often having to query for data that I will need to query again in an after save hook for the same request. Or in some cases needing to make the same request as the REST request itself. If I understand it correctly (which i may not...), it seems like thee are duplicate queries being made and there is room for optimization.

like in the example below, pulled from https://loopback.io/doc/en/lb3/Defining-and-using-roles.html. If the request being authenticated was GET /projects/{id} (findById), the role resolver has to call project.findById. Then is project.findById called again later in the sequence to fulfill the request?

It would be nice if there was a way data fetched in the role resolvers could be stashed somewhere in the context to be used later in the sequence to avoid duplicate queries.

Role.registerResolver('teamMember', function(role, context, cb) {
    // Q: Is the current request accessing a Project?
    if (context.modelName !== 'project') {
      // A: No. This role is only for projects: callback with FALSE
      return process.nextTick(() => cb(null, false));
    }

    //Q: Is the user logged in? (there will be an accessToken with an ID if so)
    var userId = context.accessToken.userId;
    if (!userId) {
      //A: No, user is NOT logged in: callback with FALSE
      return process.nextTick(() => cb(null, false));
    }

    // Q: Is the current logged-in user associated with this Project?
    // Step 1: lookup the requested project
    context.model.findById(context.modelId, function(err, project) {
      // A: The datastore produced an error! Pass error to callback
      if(err) return cb(err);
      // A: There's no project by this ID! Pass error to callback
      if(!project) return cb(new Error("Project not found"));

      // Step 2: check if User is part of the Team associated with this Project
      // (using count() because we only want to know if such a record exists)
      var Team = app.models.Team;
      Team.count({
        ownerId: project.ownerId,
        memberId: userId
      }, function(err, count) {
        // A: The datastore produced an error! Pass error to callback
        if (err) return cb(err);

        if(count > 0){
          // A: YES. At least one Team associated with this User AND Project
          // callback with TRUE, user is role:`teamMember`
          return cb(null, true);
        }

		else{
          // A: NO, User is not in this Project's Team
          // callback with FALSE, user is NOT role:`teamMember`
          return cb(null, false);
        }
      });
    });
  });

joeybenenati avatar May 18 '17 19:05 joeybenenati

@joeybenenati You can try passing in context.options

padelagani avatar May 19 '17 04:05 padelagani

@joeybenenati : do you care about internal optimization only or look also to have access to the data that was already fetched by LoopBack when running the role resolver?

ebarault avatar May 19 '17 08:05 ebarault

@ebarault Both. With the current implementation (3.6.0) it seems like having any role resolver for an instance method would require redundant queries to fulfill the request. in my case, having access to the data that was already fetched by LoopBack when running the role resolver would be a greater optimization. I've tried storing properties in ctx.remotingContext.req in the resolver but they don't seem to make it through to the hooks.

joeybenenati avatar May 19 '17 16:05 joeybenenati

Thanks @padelagani and @ebarault . I took a closer look at context passed into the resolver and found the options object at ctx.remotingContext.args.options which i can add use to store fetched data just as I would in a hook. Also found ctx.remotingContext.instance which solves my issue of of duplicate findById queries for instance methods. I guess I was under the impression that the resolvers ran before instance methods queried for the instance but this is great. closing issue

well, it looks like ctx.remotingContext.instance in resolvers is only available for write, and execute methods and not for remote findById, so still have to duplicate the findById in that case.

joeybenenati avatar May 19 '17 17:05 joeybenenati