Wiredcraft / test-fullstack

6 stars 43 forks source link

Questions for loopback from MaYi #6

Closed flyingant closed 8 years ago

flyingant commented 8 years ago

Hi there,

Hope you kindly guys could help me to discuss some the questions about loopback.

For Nodejs and loopback, so far I had read the documentation and realized the basic concept and how it works. Now I am trying to build a backend server based on loopback but still got some confuses. Then wish you can lend me a hand to make me understand easier. Thanks so much!

  1. Is that possible to override or rewrite the predefine method without change the path? (e.g. I have a Publisher model which contain username and password properties. Before create a new Publisher instance I would like to check the username if unique. But as I know the predefine method will directly create a instance after post a request.)
  2. Could I disable all the predefine remote methods but only expose customized remote method? (e.g. In some of my models, for security reasons or simpliy the code I would like to disbale all the predefine mehod and only expose a few customized remote method. I know I could set the model's public property to false but it will lead to all APIs are not available to viste. And the disableRemoteMethod method looks won't work fine when models have relations.)
  3. If there's better way to handle the model's relations? (e.g. I got two models called Publisher and Talk, The Publisher hasMany Talk and the foreign key called publisherId. When I try to create a new Talk, I need check the publisherId (ensure the publisher is existed) first before saved the instance. And then I am thinking about that what if the Talk have other foreign keys? YES! The callback hell come out!)
  4. Any ideas to reformat the response to client? (e.g. When I query the Talk model data and it responsed {title:'ttt', description: 'dddd'}, I would like to add extra property which not belong to theTalk and the response shall look like {title:'ttt', description: 'dddd', voteCount: '1234'}. )
  5. Any suggestions on organizing the code structure?(e.g. like controller, service folder, In my opinion it's not wisely to put all the code in the model.)

Still thanks for taking your precious time to read. I am really grateful.

MaYi

makara commented 8 years ago

@ChopperLee2011 What do you think?

ChopperLee2011 commented 8 years ago

ok, i can have some quick answers here:

  1. Yes.https://docs.strongloop.com/display/public/LB/Customizing+models
  2. Yes.https://github.com/strongloop/loopback/issues/651
  3. Use loopback build inrelationship method https://docs.strongloop.com/display/public/LB/HasOne+relations
  4. Define a response model for doing this, not the PersistedModel.
  5. Organize which part? u can have middleware or utils folder under /server
flyingant commented 8 years ago

@ChopperLee2011 Wow, Thanks all for these. HELPful!