I'm working on an API that consists of several collections that have relations to each other. I want to give users the opportunity to eagerly fetch records from associated collections via a GET url parameter.
For instances
/api/clients/
Would return an array of objects, each representing a client.
But clients have "employees" and "templates". "Templates" also have "revisions." And lastly, "revisions" have "groups"
My strategy for the format of the url parameter is something like this:
/api/clients?expand=[employees][templates[revisions[groups]]]
Which represents:
clients
+ employees
+ templates
+ revisions
+ groups
My question is, what is a good way to go about parsing a string in this format: [employees][templates[revisions[groups]]]
to arrive at an object like this:
{
"employees": {},
"templates": {
"revisions": {
"groups": {}
}
}
}
Or something similar that's easy to work with. I'm working in NodeJS so any answers specific to that environment are a plus. Regex?
Going off @Kovge 's suggestion, I'm going to handle this situation with a JSON string passed in my URL get request. It's a bit verbose, but should be acceptable.
Here's how I'd represent my eager-fetching associated collection records:
[
{
"resource": "employees"
},
{
"resource": "templates",
"extend": [
{
"resource": "revisions",
"extend": [
{
"resource": "groups"
}
]
}
]
}
]
Basically using arrays of objects with "resource" parameters and optional "extend" parameters. My request will end up looking like this: (url encoded)
http://example.com/api/clients?extend=%5B%7B%22resource%22%3A%22employees%22%7D%2C%7B%22resource%22%3A%22templates%22%2C%22extend%22%3A%5B%7B%22resource%22%3A%22revisions%22%2C%22extend%22%3A%5B%7B%22resource%22%3A%22groups%22%7D%5D%7D%5D%7D%5D
This is what my result ended up looking like, still playing with things.
