# HG changeset patch # User Frank Bessou <frank.bessou@logilab.fr> # Date 1613999217 -3600 # Mon Feb 22 14:06:57 2021 +0100 # Node ID 7e81ecb8d2f4b1114f4e45d9538de3c70081563d # Parent 6657f8415dcbc6db67eaf595f9232e585ea948b3 feat: handle sort on getList diff --git a/packages/ra-cubicweb/src/index.ts b/packages/ra-cubicweb/src/index.ts --- a/packages/ra-cubicweb/src/index.ts +++ b/packages/ra-cubicweb/src/index.ts @@ -22,21 +22,28 @@ 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[] = []; + let sortvariable = null; 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( - `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(", ")}`, {}