Generates random mock data for entities described in the OData metadata document. Based on the code from OpenUI5 Mock Server, but has additional features:
- use faker.js API methods for data generation
- generate specific number of entities for given entity sets
- skip generation of Entitiy Sets you don't need
- provide sets of values, which should be used instead of pure random values
- more meanigful and related data - values from one property can have a specific value based on a value from another property, which helps with building navigations
- force to have only distinct entries within an Entity Set (based on key properties)
npm install omg-odata-mock-generator
import { ODataMockGenerator } from "omg-odata-mock-generator";
// ... prepare metadata and options
const generator = new ODataMockGenerator(metadata, options);
const { ODataMockGenerator } = require("omg-odata-mock-generator/dist/cjs");
// prepare metadata and options
const generator = new ODataMockGenerator(metadata, options);
Use dist/preset-env/bundle.umd.js; jQuery is required. See samples/browser_usage.html
Check the docs
The samples below are based on modules.
import { ODataMockGenerator } from "omg-odata-mock-generator"; //or require("omg-odata-mock-generator")
const generator = new ODataMockGenerator(metadataAsString, options);
const mockData = generator.createMockData();
- metadataAsString is an OData metadata XML document
- options - an optional parameter for fine-tuning the generation process. If not provided, defaults are used.
Options parameter structure is as follows:
{
defaultLengthOfEntitySets: <number>, // default 30
mockDataRootURI: <root uri for entity URIs>, // default ""
rules: {
predefined: { <configuration for predefined values> }, // default empty
skipMockGeneration: ["EntitySet1", "EntitySet2", ...], // default empty
distinctValues: [ "EntitySet1", "EntitySet2", ...], // default empty
variables: { <variables for predefined values> }, // default empty
faker: { configuration for faker.js }, // default empty
lengthOf: { entities size config } // default empty
}
}
Please also refer to docs
Note: assuming metadata is https://services.odata.org/V3/OData/OData.svc/$metadata
const generator = new ODataMockGenerator(metadata);
const mockData = generator.createMockData();
will generate 30 entries for each entity set
{
"Products": [
{
"ID": 9,
"Name": "Name 1"
// rest of properties
}
],
"ProductDetails": [
{
"ProductID": 6671,
"Details": "Details 1"
// rest of properties
}
}
See samples/generatedDataSample.json
defaultLengthOfEntitySets sets the global, default number of generated entries; this can be overwritten for specific entity sets using rules.lengthOf option
const options = {
defaultLengthOfEntitySets: 3,
};
const generator = new ODataMockGenerator(metadata, options);
const mockData = generator.createMockData();
Each entity set will have 3 entries. Setting generation of 2 entries for Products, 12 for Categories:
const options = {
defaultLengthOfEntitySets: 3,
rules: {
lengthOf: {
Products: 2,
Categories: 12,
},
},
};
const options = {
defaultLengthOfEntitySets: 3,
mockDataRootURI: "my/path",
};
const generator = new ODataMockGenerator(metadata, options);
const mockData = generator.createMockData();
URI in __metadata will be prefixed:
"ProductDetails": [{
"ProductID": 6671,
"Details": "Details 1",
"__metadata": { "uri": "my/path/ProductDetails(6671)", "type": "ODataDemo.ProductDetail" },
// ...
{
rules: {
skipMockGeneration: ["EntitySet1", "EntitySet2"];
}
}
For example:
{
rules: {
skipMockGeneration: ["Persons", "Suppliers"];
}
}
Faker.js (v8) API methods can be provided and they will be used
instead of default logic for data generation. Alternatively, Mustache-like string with several values
can be also passed as described in the faker.js docs, for
example {{name.lastName}}, {{name.firstName}} {{name.suffix}}
.
If the string property has *MaxLength" attribute, generated value will be limited accordingly.
{
rules: {
faker: {
Entity: {
Property1: "faker.method",
Property2: "{{faker.method}}, {{faker.method}}"
}
}
}
}
For example:
const options = {
rules: {
faker: {
Product: {
Name: "commerce.productName",
Description: "{{lorem.paragraph}}, {{commerce.productDescription}}",
},
},
},
};
If for some entities values should be randomly selected from a predefined set, it can be configured in the following way:
{
rules: {
predefined: {
Entity: {
Property: [Value1, Value2, Value3];
}
}
}
}
For example:
const options = {
rules: {
predefined: {
Product: {
Rating: [1, 2, 3],
},
},
},
};
const generator = new ODataMockGenerator(metadata, options);
const mockData = generator.createMockData();
Rating will be a random value but from [1,2,3] set
{
"Products": [{
"ID": 9,
"Name": "Name 1",
"Description": "Description 1",
"ReleaseDate": "/Date(967811009000)/",
"DiscontinuedDate": "/Date(1114259009000)/",
"Rating": 1
...
Some values can be dependent on other values. This can be achieved in the following way:
{
rules: {
predefined: {
Entity: {
Property1: [Value1, Value2, Value2],
Property2: {
reference: "Property1",
values: [{
key: Value1,
value: "Description for value 1"
},{
key: Value2,
value: "Description for value 2"
}]
}
}
}
}
}
For example:
const options = {
rules: {
predefined: {
Product: {
Rating: [1, 2, 3],
Description: {
reference: "Rating",
values: [
{
key: 1,
value: "Custom description for rating 1",
},
],
},
},
},
},
};
const generator = new ODataMockGenerator(metadata, options);
const mockData = generator.createMockData();
Now if Rating = 1, Description will by "Custom...", not the generated one. Not all dependent values has to be provided - if it is not found in values array, it will be generated as usual.
It easier to keep predefined values in one place, as they might be used in several places. It can be done with help of special variables property and special $ref:... handling:
{
rules: {
variables: {
myValues: [value1, value2, value3]
},
predefined: {
Entity: {
Property1: "$ref:myValues",
Property2: {
reference: "Property1",
values: [{
key: "value1",
value: "Text1"
}, {
key: "value2",
value: "Text2"
}]
}
}
}
}
}
For example
{
rules: {
variables: {
categoryIds: [1, 2, 3]
},
predefined: {
Category: {
ID: "$ref:categoryIds",
Name: {
reference: "ID",
values: [{
key: 1,
value: "Category1"
}, {
key: 2,
value: "Category2"
}, {
key: 3,
value: "Category3"
}]
}
}
}
}
}
const generator = new ODataMockGenerator(metadata, options)
const mockData = generator.createMockData();
Name will be based on ID, which takes values from categoryIds variable.
Having predefined values for entities and their key properties, duplicated entries will be present,
as the generator always produces the number of entries specified by the mockDataEntitySize.
To have only distinct values (based on all key properties):
{
rules: {
"distinctValues": ["EnitytSet1", "EntitySet2"]
}
}
For example
{
rules: {
distinctValues: ["Categories"],
variables: {
categoryIds: [1, 2, 3]
},
predefined: {
Category: {
ID: "$ref:categoryIds",
Name: {
reference: "ID",
values: [{
key: 1,
value: "Category1"
}, {
key: 2,
value: "Category2"
}, {
key: 3,
value: "Category3"
}]
}
}
}
}
}
const generator = new ODataMockGenerator(metadata, options)
const mockData = generator.createMockData();
There will be max 3 entries for Categories entity set.
Please check the samples/node_usage.mjs to see an example with all options.
See CHANGELOG.md
This plugin is licensed under the MIT license.
Feel free to contact me:
- wozjac@zoho.com | https://jacekw.dev
- Bluesky (https://jacekwoz.bsky.social)
- LinkedIn (https://www.linkedin.com/in/jacek-wznk)