npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ms7700/ra-data-odata-server-forked

v2.2.4

Published

OData Server data provider for react-admin

Downloads

6

Readme

OData Data Provider For React-Admin

OData Data Provider for react-admin, the frontend framework for building admin applications in the browser.

Features

  • Parses OData $metadata and creates a list of EntitySets which can be displayed as react-admin <Resources>
  • Transparently renames entity keys to 'id' fields so they can be used by react-admin.
  • Handles scalar keys (Edm.Int32 and Edm.Guid) correctly so you can directly use EntitySets with integer or guid keys.
  • extends the react-admin getManyReference() method to support expanding child relations.

Installation

npm install --save ra-data-odata-server

Usage

Initializing the OData provider requires loading and parsing the OData service's manifest. Since that is an async operation, you need to make sure it is complete before passing the provider to react-admin. An easy way to do that is the React useEffect() hook.

Once the provider is initialized, you can use the getResources() method to get a list of EntitySets that can be used directly by react-admin.

import odataProvider, { OdataDataProvider } from "ra-data-odata-server";

function App() {
  const [dataProvider, setDataProvider] = useState<OdataDataProvider>();
  useEffect(() => {
    odataProvider(
      "https://services.odata.org/v4/Northwind/Northwind.svc/"
    ).then((p) => setDataProvider(p));
    return () => {};
  }, []);

  return dataProvider ? (
    <Admin dataProvider={dataProvider}>
      {dataProvider.getResources().map((r) => (
        <Resource
          key={r}
          name={r}
          list={ListGuesser}
          edit={EditGuesser}
          show={ShowGuesser}
        />
      ))}
    </Admin>
  ) : (
    <Loading></Loading>
  );
}

See the example directory for a complete working react-admin solution that runs against the Northwind odata service.

Child relationships

OData supports hierarchical relationships - e.g. /Customers('ALFKI')/Orders returns all the order entities related to the customer with ID 'ALFKI'. In order to support this type of query in react-admin, the getManyReferences() filter is enhanced with a 'parent' property. You can pass this to the standard <ReferenceManyField> react-admin component. For example, when the current record is a customer, you can display all that customer's orders with this syntax

<ReferenceManyField
  label="Customer Orders"
  reference="Orders"
  target="Orders"
  filter={{ parent: "customers" }}
>
  <SingleFieldList linkType="show">
    <ChipField source="id" />
  </SingleFieldList>
</ReferenceManyField>

This also supports many-to-many relationships without requiring that your service exposes an intermediate join table as a resource.

Authentication hook

To support OData servers that require authentication, you can provide an options callback when creating the data provider. This will get called before each query and must return a Promise<RequestInit>. Usually just adding an Authorization header is sufficient, but you can override any of the Request options. For example, if you have a getAccessToken() function that returns a Promise<string> you would initialize your provider like this.

            odataProvider("https://myodataservice.com/odata", () => {
                return getAccessToken()
                    .then((token) => ({
                        headers: {
                            Authorization: "Bearer " + token,
                        },
                    }));
            }).then((provider) => setDataProvider(provider));

Known Limitations

  • EntitySets with no keys or compound keys are not supported - react-admin only supports string keys
  • EntityType inheritance doesn't work

License

This data provider is licensed under the MIT License, and sponsored by Groopit.