diff --git a/packages/ra-cubicweb/src/index.ts b/packages/ra-cubicweb/src/index.ts index 6657f8415dcbc6db67eaf595f9232e585ea948b3_cGFja2FnZXMvcmEtY3ViaWN3ZWIvc3JjL2luZGV4LnRz..7e81ecb8d2f4b1114f4e45d9538de3c70081563d_cGFja2FnZXMvcmEtY3ViaWN3ZWIvc3JjL2luZGV4LnRz 100644 --- a/packages/ra-cubicweb/src/index.ts +++ b/packages/ra-cubicweb/src/index.ts @@ -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(", ")}`, {}