# odata-v4-schema-extract

> > odata codegen tool

Latest version **1.0.0** (published 2022-02-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install odata-v4-schema-extract
pnpm add odata-v4-schema-extract
yarn add odata-v4-schema-extract
bun add odata-v4-schema-extract
```

Provides the command `odata-v4-schema-extract`.

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2022-02-09 |
| First published | 2022-02-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=14.x |
| Dependencies | 5 |
| Unpacked size | 12.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Barnebys |
| Maintainers | maxwihlborg |

## Links

- npm: https://www.npmjs.com/package/odata-v4-schema-extract
- npm.io page: https://npm.io/package/odata-v4-schema-extract

## Dependencies (5)

- [chalk](https://npm.io/package/chalk.md) ^4.1.2
- [xml2o](https://npm.io/package/xml2o.md) ^0.3.3
- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [minimist](https://npm.io/package/minimist.md) ^1.2.5
- [pluralize](https://npm.io/package/pluralize.md) ^8.0.0

## Recent versions

- 1.0.0 (latest) — 2022-02-09

## README

# odata-v4-schema-extract

> odata codegen tool

Generate json/ts/js from Common Schema Definition Language (CSDL) v4

http://docs.oasis-open.org/odata/odata/v4.0/odata-v4.0-part3-csdl.html

## Install

```shell
yarn add odata-v4-schema-extract
```

## Usage

```shell
$ odata-v4-schema-extract [options] <url>

Options:

  -h, --help               Show this message
  -j, --json               Output the intermediary json format

  -e NAME, --entity=NAME   Entity to extract
  -i FILE, --input=FILE    Input file to read from
  -n NAME, --name=NAME     Override entity name
  -o FILE, --output=FILE   Path to write to
  -t TMPL, --template=TMPL Lodash compatible template

Examples:

  - Write json schema to std output.

    $ odata-v4-schema-extract --json -i ./metadata.xml

  - Generate code with a custom template.

    $ odata-v4-schema-extract -i ./metadata.xml -o ./Entity.js -t ./template.lodash.js

  - Generate code and pipe prettier.

    $ odata-v4-schema-extract -i ./metadata.xml \
      | prettier --stdin-filepath ./Entity.js \
      > ./Entity.js

  - Read from std input.

    $ curl https://odata.api/$metadata | odata-v4-schema-extract -n MyEntity -o ./Entity.js
```

## Custom output

You can override the codegen template with the `-t` option.

```
// Default template (when no `-t` option is set)
import * as csdl from "odata-v4-schema/csdl";

<% entities.map(({ name, modelName, properties }) => { %>
const <%- name %> = csdl.Entity("<%- modelName %>", <%- getCSDLSchema(properties, "csdl") %>);
<% }) %>

<% if (entities.length === 1) { %>
export default <%- model[0].name %>;
<% } else { %>
export { <%- entities.map(({name})=>name).join(", ") %> };
<% } %>
```

## Programatic usage

```js
import { convertStream, buildTemplate } from 'odata-v4-schema-extract'
import got from 'got'

import fs from 'node:fs/promises'
import path from 'node:path'

const template = './scripts/template.lodash.js'

const entities = await convertStream(got.stream(`${process.env.API_PATH}/$metadata`, {
  headers: {
    authorization: `Bearer ${process.env.API_TOKEN}`
  }
}))

await fs.writeFile('./src/models/ProductItem.js', await buildTemplate(entities, {
  entity: 'Products',
  name: 'ProductItem'
  template
}))

await fs.writeFile('./src/models/Customer.js', await buildTemplate(entities, {
  entity: 'Users',
  name: 'Customer'
  template
}))
```

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