I'm looking for the best practices on RESTful API design for the following use case:
Table1 Table2
Id1 Id1
Id2 Id2
Id3 Id3
Name Name
Table1Id1(FK to Table1)
Table1Id1(FK to Table1)
Table1Id1(FK to Table1)
Suppose i have endpoints like below for Table1:
/root/table1 (to get list of records)
/root/table2 (to get single record by primary key)
Now here my question is which would be the best way from below two to represent composite key in second url :
/root/e1/Id1/Id2/Id3
or
/root/e1?Id1=1&Id2=2&Id3=3
Suppose i have endpoints like below for Table2:
/root/table1/Table1Id1_Table1Id2_Table1Id1/table2 (to get list of records for table2 by table1).
Now here is my question that is above url valid and appropriate in case of composite key?
Any advice on a good pattern to follow for this use case would be greatly appreciated.
Composite keys in SQL prove to be useful in those cases where you have a requirement of keys that can uniquely identify records for better search purposes, but you do not possess any single unique column. In such cases, you must combine multiple columns to create a unique key.
It is technically possible to join these tables -- you can join on any columns regardless of whether they are key columns or not.
A synthetic primary key says nothing about the entity you are modeling, it's merely a number. A composite key is better since it is tied to the domain.
Any advice on a good pattern to follow for this use case would be greatly appreciated.
Don't couple your resource identifiers to your (current) database schema; that violates encapsulation.
I'm looking for the best practices on RESTful API design for the following use case
REST really doesn't care. As far as REST is concerned, the URI is opaque; any information encoded into it is done at the server's discretion and for its own use.
The relevant concerns are RFC 3986, and your local design conventions.
The path component contains data, usually organized in hierarchical form, that, along with data in the non-hierarchical query component (Section 3.4), serves to identify a resource within the scope of the URI's scheme and naming authority (if any).
Path elements are supposed to be for hierarchical data -- think about the way that relative URIs resolve.
Based on your description here, I wouldn't think that the foreign keys have a natural hierarchy to them; certainly not in the general case. So using the non hierarchical part of the URI (the query) might make more sense.
Another possibility to consider would be matrix parameters; you can combine the foreign keys into a single path segment, thereby avoiding any suggestion of hierarchy among them.
Agree with VoiceOfUnreason
Don't couple your resource identifiers to your (current) database schema; that violates encapsulation.
Dont do that
For your specific usecase
As a general best practice, REST Urls should follow a predictable and hierarchical pattern to identofy resources. This brings much transparency between Client and Server. So suppose you have parent-child relations ship in your entity it should be better be designed as
/{appname}/{version}/parent/{parentId}/child/{childId}
instead of just having
/{appname}/{version}/child/{childId}
In your usecase the non-hierarchical part of URL is Id1/Id2/Id3
Best approach to have a unique identifier for your tables. But if it really can not be done then you should rather go for
/root/e1?Id1=1&Id2=2&Id3=3
This gives a notion "get me the e1 with is having Id "1" and Id "2" and Id "3" which is correct in your context
/root/e1/Id1/Id2/Id3
This is non standard and therefore should be avoided
to get list of records for table2 by table1
Your should have
/root/table1/table2?Table1Id1&Table1Id2&Table1Id1
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