diff --git a/packages/demo/src/App.tsx b/packages/demo/src/App.tsx index 8e3e1997aa98813f263153c0258243e157bfe444_cGFja2FnZXMvZGVtby9zcmMvQXBwLnRzeA==..70c03510bc065bf65711916c16d4fa87b5683af9_cGFja2FnZXMvZGVtby9zcmMvQXBwLnRzeA== 100644 --- a/packages/demo/src/App.tsx +++ b/packages/demo/src/App.tsx @@ -17,6 +17,7 @@ Show, SimpleShowLayout, DataProvider, + ReferenceArrayField, } from "react-admin"; import { client } from "@logilab/cwclientlibjs"; import { createDataProvider } from "ra-cubicweb/dist"; @@ -65,5 +66,5 @@ export const CityList = (props: ListProps) => ( <List {...props}> <Datagrid rowClick="show"> - <NumberField source="zip_code" /> + <TextField source="id" /> <TextField source="name" /> @@ -69,5 +70,5 @@ <TextField source="name" /> - <TextField source="id" /> + <NumberField source="zip_code" /> </Datagrid> </List> ); @@ -75,5 +76,5 @@ export const CityShow = (props: ShowProps) => ( <Show {...props}> <SimpleShowLayout> - <NumberField source="zip_code" /> + <TextField source="id" /> <TextField source="name" /> @@ -79,5 +80,10 @@ <TextField source="name" /> - <TextField source="id" /> + <NumberField source="zip_code" /> + <ReferenceArrayField label="Museum" reference="Museum" source="reverse_is_in"> + <Datagrid rowClick="show"> + <TextField source="name"/> + </Datagrid> + </ReferenceArrayField> </SimpleShowLayout> </Show> ); diff --git a/packages/ra-cubicweb/src/index.ts b/packages/ra-cubicweb/src/index.ts index 8e3e1997aa98813f263153c0258243e157bfe444_cGFja2FnZXMvcmEtY3ViaWN3ZWIvc3JjL2luZGV4LnRz..70c03510bc065bf65711916c16d4fa87b5683af9_cGFja2FnZXMvcmEtY3ViaWN3ZWIvc3JjL2luZGV4LnRz 100644 --- a/packages/ra-cubicweb/src/index.ts +++ b/packages/ra-cubicweb/src/index.ts @@ -87,7 +87,7 @@ { eid: params.id, id: params.id } ); - // Getting relations + // Getting subject relations const subjectRelations = Object.fromEntries( Object.entries(schema.relationships).filter( ([_relationName, definition]: [string, S["relationships"][string]]) => @@ -102,6 +102,22 @@ entity[relationName] = result.map((row) => row[0]); } } + + // Getting object relations + const objectRelations = Object.fromEntries( + Object.entries(schema.relationships).filter( + ([_relationName, definition]: [string, S["relationships"][string]]) => + definition.object.includes(resource) + ) + ); + for (const relationName in objectRelations) { + const result = await rqlClient.queryRows( + `Any TARGET Where TARGET ${relationName} X, X eid ${params.id}` + ); + if (result.length !== 0) { + entity[`reverse_${relationName}`] = result.map((row) => row[0]); + } + } return { data: entity }; }, getMany: async (resource, params) => { @@ -115,5 +131,4 @@ selection.push(variable); restrictions.push(`X ${key} ${variable}`); }); - const result = await rqlClient.queryRows( @@ -119,5 +134,5 @@ const result = await rqlClient.queryRows( - `Any ${selection.join(",")} Where ${restrictions.join(",")}, X eid ${ - params.ids[0] - }` + `Any ${selection.join(",")} Where ${restrictions.join(",")}, X eid IN (${ + params.ids.join(",") + })` ); @@ -123,7 +138,6 @@ ); - const row = result[0]; - const entity = row.reduce( + const entities = result.map((row, rowIdx) => row.reduce( (agg, attributeValue, idx) => ({ [attributesNames[idx]]: attributeValue, ...agg, }), @@ -126,7 +140,7 @@ (agg, attributeValue, idx) => ({ [attributesNames[idx]]: attributeValue, ...agg, }), - { id: params.ids[0] } - ); + { id: params.ids[rowIdx] } + )); @@ -132,5 +146,5 @@ - return { data: [entity] }; + return { data: entities }; }, getManyReference: (_resource, _params) => Promise.reject("Not implemented"), update: async (resource, { data, id }) => {