csvan
csvan

Reputation: 9454

Loopback: How to isolate a users queries to a specific datasource?

Let's say I have two users, A and B, with IDs 1 and 2 (respectively). Further, let's assume I have two datasources configured: X and Y.

How could I isolate ALL queries issued by user A to datasource X, and all by B to Y for some given remote method? For example, say that A wants to run 'find' for some model via the API - how could I make sure that the only results A will get are those which are accessible through datasource X?

Upvotes: 4

Views: 87

Answers (1)

Jordan Kasper
Jordan Kasper

Reputation: 13273

I'm not sure I entirely understand why you would decide a datasource based on the current user, but in any case, I'm not sure you can do that with LoopBack ... at least, not easily. LoopBack is a model-driven framework - everything derives from the model. As such, all API endpoints go through a model (although you can set up custom routes). And each model is connected to a single datasource.

So, if I hit /api/Widget/13 there is no way to make that findById() call switch between two datasources, it will always hit whatever datasource the model is connected to.

Okay, that all said, the solutions I see are to:

  1. Create a "dispatcher" and have that model do the appropriate thing.
  2. Create a custom remote method on your existing model and do the decision making there and the find, etc on the correct datasource.

In either case, it's not straightforward, and not built-in. FYI, if you need to get the datasource you can access it from the LoopBack application object: MyModel.app.datasources.ds1

Upvotes: 1

Related Questions