By default, LoopBack assumes that each model has id property that uniquely identifies every model instance.
However, LDL allows to customise this:
-
Use different property as the identifier.
loopback.createModel('Customer', {
email: { id: true, type: String }
});
-
Use a composite id - there is a combination of multiple properties that uniquely identifies every model instance.
// I am not sure if this is the right syntax,
// correct me if I am wrong
loopback.createModel('City', {
state: { id: true, type: String },
name: { id: true, type: String }
});
Even when the identifier is not id, the REST API expects a single id parameter in the URL (e.g. GET /customers/:id), even though the JSON documents in requests/responses contain the custom/composite identifier.
This solution brings extra complexity to LB clients.
To begin with, client models need to decide whether they are representing an existing instance or a new object, so that save can call update or create. The current implementations (Android, iOS) are assuming there is always and id property. When this property is set, then the model is an existing instance.
Arbitrary identifiers makes it difficult for front-end developers to use the REST API. For every model, they have to remember what is the identifier and understand when to send it as customId and when as id.
For ids that are not auto-generated (as is the case of the code samples above), we can't decide between update and create just by checking whether the id property was set.
We need a better solution. Thoughts?
/to: @ritch @Schoonology @raymondfeng
By default, LoopBack assumes that each model has
idproperty that uniquely identifies every model instance.However, LDL allows to customise this:
Use different property as the identifier.
Use a composite id - there is a combination of multiple properties that uniquely identifies every model instance.
Even when the identifier is not
id, the REST API expects a singleidparameter in the URL (e.g.GET /customers/:id), even though the JSON documents in requests/responses contain the custom/composite identifier.This solution brings extra complexity to LB clients.
To begin with, client models need to decide whether they are representing an existing instance or a new object, so that
savecan callupdateorcreate. The current implementations (Android, iOS) are assuming there is always andidproperty. When this property is set, then the model is an existing instance.Arbitrary identifiers makes it difficult for front-end developers to use the REST API. For every model, they have to remember what is the identifier and understand when to send it as
customIdand when asid.For ids that are not auto-generated (as is the case of the code samples above), we can't decide between
updateandcreatejust by checking whether the id property was set.We need a better solution. Thoughts?
/to: @ritch @Schoonology @raymondfeng