# @microsoft/overreact-odata

> OData binding generator for overreact

Latest version **0.1.0-alpha.32** (published 2022-03-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install @microsoft/overreact-odata
pnpm add @microsoft/overreact-odata
yarn add @microsoft/overreact-odata
bun add @microsoft/overreact-odata
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0-alpha.32 |
| Published | 2022-03-11 |
| First published | 2021-06-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 61.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Like Zhu |
| Maintainers | likezhmsft, microsoft1es |
| Keywords | odata |

## Links

- npm: https://www.npmjs.com/package/@microsoft/overreact-odata
- Repository: https://github.com/microsoft/overreact-core
- Homepage: https://github.com/microsoft/overreact-core#readme
- Issues: https://github.com/microsoft/overreact-core/issues
- npm.io page: https://npm.io/package/@microsoft/overreact-odata

## Dependencies (2)

- [query-string](https://npm.io/package/query-string.md) ^7.0.1
- [@microsoft/overreact](https://npm.io/package/@microsoft/overreact.md) ^0.1.0-alpha.32

## Recent versions

- 0.1.0-alpha.32 (latest) — 2022-03-11
- 0.1.0-alpha.31 — 2022-02-22
- 0.1.0-alpha.30 — 2022-02-16
- 0.1.0-alpha.29 — 2022-02-08
- 0.1.0-alpha.28 — 2022-01-28
- 0.1.0-alpha.27 — 2021-10-18
- 0.1.0-alpha.26 — 2021-10-15
- 0.1.0-alpha.25 — 2021-09-30
- 0.1.0-alpha.24 — 2021-09-29
- 0.1.0-alpha.23 — 2021-09-27
- 0.1.0-alpha.22 — 2021-09-23
- 0.1.0-alpha.21 — 2021-09-23
- 0.1.0-alpha.20 — 2021-08-30
- 0.1.0-alpha.19 — 2021-08-18
- 0.1.0-alpha.18 — 2021-08-17
- … 10 more at https://npm.io/package/@microsoft/overreact-odata/versions

## README

# `overreact-odata`

A typical usage of overreact within our team is to deal with [OData](https://www.odata.org/) from various service endpoints. Usually these endpoints already have pre-built schema packages available (such as `@bingads-webui/mca-odata-schemas`, and `@bingads-webui/campaign-odata-schemas`), from which we can extract useful information and generate overreact specs without having our developers write from scratch. Given each entity (e.g., `Activity`, `Ad`) have CRUD operations, as well as OData actions/functions attached, this could save a tremendous amount of manual effort.

## The Idea

To generate a spec for each OData model, we'll need to solve these problems:

1. How to create `dataPath` from OData model hierarchy.
2. How to assign proper "Key" values to each level on hierarchy.
3. How to identify from response which property is the "Key".

In overreact, the internal data structure ("store") is constructed from a schema tree, where each node has an associated `dataPath` to describe its location from root. Similarly, OData also organizes data using a tree-like structure, and provides navigation properties to locate specific data in the tree. 

Consider an OData GET request to fetch an `Activity`. The URL would look like this:

> GET https://contoso.com/Customers(123)/Accounts(456)/Activities('789')

We can directly map `dataPath` from EDM hierarchy to `customer:account:activity`.

For OData actions/functions, a call to `https://contoso.com/Customers(123)/Accounts(456)/Default.FooBar()` will map to `customer:account:foo_bar`. Note that we converted the Pascal naming to Snake convention, and discarded the namespace "Default" in this case.

The "Key" values are used to identify which entity to use on each level in the hierarchy. Currently in overreact we have 2 options to select keys:

1. Using `locator.order` in `variables`. For example:
```javascript
const variables = {
    locator: {
        order: ['cid', 'aid', 'activityId'],
        descriptor: { cid: 123, aid: 456, activityId: '789' },
    },
};
```

2. Using `parentKeySelector` in request contract.

Unfortunately `parentKeySelector` only provides "Key" info for current value, as well as its "parent" key values. We'll loose info for levels that are higher than 2, so in our case we'll resort to using `order`.

Finally, we need to identify the "Key" property value from OData responses, as they are used to look up cached items in overreact store. Luckily it is usually specified in `$$ODataExtension.Key` from the OData schemas. It is an array value but for now we'll only leverage the first one:

```javascript
const { $$ODataExtension } = entitySchema;

createResponseContract({
    // ...
    keySelector: r => r[$$ODataExtension.Key[0]],
})
```

## Usage

Due to length limit, please visit [Working with OData](https://microsoft.github.io/overreact-core/blog/odata) for details on usage.

---
_Source: https://npm.io/package/@microsoft/overreact-odata · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
