Say, I have a nested structure that is not arbitrary depth. E.g. employees and departments.
The most frequent use I want to support is to display a list of departments and its employees, with paging (X
employees/page, not department). Something like this:
- Department A
- Person 1
- Person 2
- ...
- Department B
- ...
How should I store them? And, how should I query them?
My solution so far:
It seems the structure will work nicely with document-based stores (or maybe key-value stores). But, I don't know how to do paging in this scenario.
If I use a relational database, the best I can think of is to fetch X
number of employees, and order by the department column in SQL; when displaying, we open a new section for that department if the current person is in a different department than the previous.
Update with an example for a flask solution:
This is what I can do with a flat table or with a join; Jinja's groupby
does internal sorting, and applies the logic I stated above.
Let employees be a list of X
employees.
<ul>
{% for department in employees|groupby('department') %}
<!-- this is the department name -->
<li>{{ group.grouper }}
<!-- list of employees -->
<ul>
{% for employee in group.list %}
<li>{{ person.department_name }} {{ person.name }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
This is what Jinja do in groupby
: sorted(map(_GroupTuple, groupby(sorted(value, key=expr), expr)))
Update 2:
My only question is How do I display a one-level nested relationship like above (see the department/person tree)? The storage and querying aspects are the details I want to know about (and to emphasize that the problem is flexible about these options). I initially asked about how it can be extended further to multiple level of nesting, and it is indeed unnecessarily confusing, so that is removed.
Neil has commented hat the data model should be the same "whether or not you are paginating" and whether or not you have "one level or multiple levels of department nesting." Fair enough; if it can be done nicely with the same model, I am happy. For comment #4, if I sort in SQL (and not in python) I can get rid of the inner sorted
in the above Jinja code, which may or may not have some benefits.
For comment #2, although "pagination with tree-like GUI widgets" may not be often seen, I do still want it; the fact it is not popular doesn't really change my problem.