Skip to content

Instantly share code, notes, and snippets.

@hacksparrow
Created December 2, 2016 08:56
Show Gist options
  • Save hacksparrow/24b3271696ce64c82b298f678105915c to your computer and use it in GitHub Desktop.
Save hacksparrow/24b3271696ce64c82b298f678105915c to your computer and use it in GitHub Desktop.
LoopBack queries
1. How do you change the dataSource of a hypothetical 10000 a models in model-config.json? It can be done quickly with a search-replace, but it would be good to have a default efficient way built-in to do so.
We don't provide that functionality out of the box and to be honest, I don't think it's an important feature to implement. I consider having 10k models in a single application as an anti-pattern in today's microservices-oriented world.
Another option is to allow packaging a group of LoopBack artifacts into a module and use it as a component to the main app.
2. Is there a way to conditionally apply filters? Eg: user can query for a month, but it should not be March.
My recommendation is to write a custom remote method and build the conditional filter programatically on the server side.
MyModel.queryWithMonth = function(month, cb) {
if (month === 'March') return cb(new Error('March is not allowed.'));
this.find({where: {month: month}}, cb);
};
There are other approaches too, one can use beforeRemote hook or "access" Operation Hook to enforce such restriction.
The following should work too:
MyModel.find({where: {and: [{month: month}, {neq: {month: 'March'}}]}}, ...)
3. Is there a way to conditionally include or exclude embedded models?
Use `include` filters - https://docs.strongloop.com/display/public/LB/Include+filter
For embedded relations, there is a physical property such as '_address' and the relation function such as 'address'. You should be able to use 'fields' to exclude/include embedded items.
4. Is "sort-by" filter supported?
Yes, use the `order` filter - https://docs.strongloop.com/display/public/LB/Order+filter
5. Is there a way to conditionally expose the fields of a model? Not hide like the password field, but conditionally exposed when and where required.
Yes, use the `fields` filters - https://docs.strongloop.com/display/public/LB/Fields+filter
6. Does the API endppoints support map-reduce operarions?
No, we don't support map-reduce operations right now. It should be possible to write a custom remote method querying the database directly (using connector API).
7. Access attempt wth invalid token returns 403 (forbidden), it should be 401 (unauthorized).
Weird. A quick search through our codebase shows that we are defaulting to 401 unless the app is configured to return 403.
https://github.com/strongloop/loopback/search?utf8=%E2%9C%93&q=403
8. Does client-side models have offline support? Meaning, store the model changes if offline and apply them, when online.
We have implemented an early version of offline-sync feature, see the following doc page for details:
http://loopback.io/doc/en/lb2/Synchronization.html
Note that the feature is not ready for prime time and unfortunately it's not our priority to finish it.
9. Native getter/setter behavior for Model properties would be great. This gives the developer a chance to do stuff before actually setting or getting the value.
AFAIK, juggler does support getters and setters, although it's not documented yet (see https://github.com/strongloop/loopback/issues/2293).
Use e.g. this StackOverflow thread for more information:
http://stackoverflow.com/questions/24223329/how-do-i-create-getter-and-setter-overrides
10. How do you implement hasOne-belongsToMany relationship? Eg: I have an iPad (hasOne), but it can be used by anyone in the family (belongsToMany).
We don't support belongsToMany. You should use referencesMany or hasManyAndBelongsTo instead. Think about what FK(s) will be used. Either device.userIds or a third model DeviceOwner (deviceId + userId).
11. Want to see some benchmark of the performance of LoopBack
Agreed, we should have benchmarks that are run regularly to show how our performance is changing over time. It's not on our short-term roadmap though.
12. Any best practices/guidelines for migrating from current db (let's say MongoDB) to LoopBack?
LoopBack is designed to support existing database schemas. You can use auto-discovery to create LoopBack models compatible with your existing database schema/data.
13. Is there a SDK for React? How about making the web front-end SDK framework-agnostic?
Agreed, a good vanilla-js client makes a lot of sense.
Our current recommendation is to use Swagger client generators, e.g. https://github.com/wcandillon/swagger-js-codegen
People that are ok to use TypeScript can try out loopback-sdk-builder which is generating a TypeScript client:
https://github.com/mean-expert-official/loopback-sdk-builder
@vishwasrao
Copy link

Thanks for getting back with answers ..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment