Skip to content
Snippets Groups Projects

feat: handle sort on getList

Merged Frank Bessou requested to merge topic/default/handle-sort into branch/default
1 file
+ 9
2
Compare changes
  • Side-by-side
  • Inline
@@ -22,7 +22,8 @@
const httpClient = new client.CwSimpleHttpClient(endpoint, true);
const rqlClient = new client.CwRqlClient(httpClient);
return {
getList: async (resource: ETypesNames<S>, { pagination }) => {
getList: async (resource: ETypesNames<S>, { pagination, sort }) => {
const sortAttribute = sort.field === "id" ? "eid" : sort.field;
const attributesNames = ["eid", ...Object.keys(schema.etypes[resource])];
const selection: string[] = [];
const restrictions: string[] = [];
@@ -26,7 +27,8 @@
const attributesNames = ["eid", ...Object.keys(schema.etypes[resource])];
const selection: string[] = [];
const restrictions: string[] = [];
let sortvariable = null;
attributesNames.forEach((key, idx) => {
const variable = `X${idx}`;
selection.push(variable);
restrictions.push(`X ${key} ${variable}`);
@@ -29,10 +31,13 @@
attributesNames.forEach((key, idx) => {
const variable = `X${idx}`;
selection.push(variable);
restrictions.push(`X ${key} ${variable}`);
if (key === sortAttribute) {
sortvariable = variable;
}
});
const total = await rqlClient
.queryRows(`Any Count(${selection[0]}) WHERE ${restrictions.join(",")}`)
.then((rows) => rows[0][0]);
return rqlClient
.queryRows(
@@ -33,10 +38,12 @@
});
const total = await rqlClient
.queryRows(`Any Count(${selection[0]}) WHERE ${restrictions.join(",")}`)
.then((rows) => rows[0][0]);
return rqlClient
.queryRows(
`Any ${selection.join(", ")} LIMIT ${pagination.perPage} OFFSET ${
`Any ${selection.join(", ")} ORDERBY ${sortvariable} ${
sort.order
} LIMIT ${pagination.perPage} OFFSET ${
pagination.page * pagination.perPage
} WHERE ${restrictions.join(", ")}`,
{}
Loading