-
-
Notifications
You must be signed in to change notification settings - Fork 11.4k
Description
As mentioned in #5463, we want to add a new order top-level parameter to API browse requests.
The order parameter allows a user to specify which order they'd like their results to be returned in.
The default orders across the 3 key resources are currently:
- Posts:
"posts"."status" ASC, "posts"."published_at" DESC, "posts"."updated_at" DESC, "posts"."id" DESC - Users:
"users"."last_login" DESC, "users"."name" ASC, "users"."created_at" DESC - Tags: none (database default, usually by ID)
The order parameter should accept any valid SQL order by clause, which does not contain quote marks, e.g. users.last_login DESC, users.name ASC, users.created_at DESC. The comma-space separator between rules, and the space separator between property and direction are required.
This means that a valid value for the order parameter can be validated to contain only alphanumeric characters, dots, underscores and spaces, with the property being any string with those characters (except the space), and the direction is always either ASC or DESC (upper or lower) It should be possible to design a regex to match this pattern.
The order parameter should also validate that the provided order properties are valid attributes for the model. Any field with doesn't specify which model it belongs to should default to whichever model is being queried, and this should be set explicitly.
It should be possible to translate this format into a JSON format that matches the one returned from orderDefaultOptions using a split on the comma and space. The model must be included for each property (this will be added to models soon).
E.g. for posts, the default order would be translated to the following JSON objet:
{
'post.status': 'ASC',
'post.published_at': 'DESC',
'post.updated_at': 'DESC',
'post.id': 'DESC'
};
That object can then be used to apply the order, much as is already done, by reading options.order, and using the value of orderDefaultOptions if it doesn't exist: https://github.com/TryGhost/Ghost/blob/master/core/server/models/base/index.js#L293