When I'm creating a model in strongloop with the following command:
slc loopback:model
loopback asks me for choose between common model or server.
Common model or server only?
I really don't know what criteria to take into account to choose between one option or the other. I'll be thankful if any of you can help me to understand or give me any insights to take a wise decision.
A LoopBack model is a JavaScript object with both Node and REST APIs that represents data in backend systems such as databases. Models are connected to backend systems via data sources. You use the model APIs to interact with the data source to which it is attached.
A remote method is a method of a model, exposed over a custom REST endpoint. Use a remote method to perform operations not provided by LoopBack's standard model REST API. Note: The easiest way to define a remote method is by using the command-line remote method generator.
LoopBack is a highly-extensible, open-source Node. js framework that enables you to: Create dynamic end-to-end REST APIs with little or no coding. Access data from major relational databases, MongoDB, SOAP and REST APIs. Incorporate model relationships and access controls for complex APIs.
If you want to be able to share models between client and server parts of your app, put your model JSON and JavaScript files in the /common/models directory. If you want to have separate client and server models, then put your model JSON and JavaScript files in the /server/models directory.
Refer this link. https://docs.strongloop.com/display/public/LB/common+directory
Loopback can be run on both the client and the server using isomorphic LoopBack. The same app running in NodeJs can also run in the client browser so instead of coding HTTP requests and responses on the client to CRUD your LoopBack model instances, you can simply call the appropriate loopback method.
In other words, instead of calling a POST to an API end-point to create a new object (POST /api/MyObject) in your database, you can simply call MyObject.create(data) on the client and the LoopBack app in the client will make the call for you. And instead of returning an HTTP response, it will return the created object.
Now, this brings us to the difference between /common and /server models: if you want the model used in the browser to be the same as the model used on the server, create your model in /common. If you need the model to be different, create the model in the /server/models folder (not sure where to create the model for the client)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With