odata-v4-schema-extract
v1.0.0
Published
> odata codegen tool
Downloads
67
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
yarn add odata-v4-schema-extract
Usage
$ 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
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
}))