I'm using faker to generate multiple schemas and want to combine the resolved results into one file called ./db.json not multiple files so I can use that file in json-server.
I have it currently setup to run and print out multiple files based on the amount of schemas are in the schema folder.
const path = require("path");
const fs = require("fs");
const { resolve, extend } = require("json-schema-faker");
extend("faker", () => require("@faker-js/faker"));
var schemasPath = path.join(__dirname, "./schemas");
fs.readdirSync(schemasPath).forEach((file) => {
const name = path.parse(file).name;
const schemas = require(`${schemasPath}/${file}`);
resolve(schemas).then((data) => {
fs.writeFile(`${__dirname}/${name}.json`, JSON.stringify(data), (err) => {
if (err) {
console.log("error = ", err.message);
} else {
console.log("Mock API data generated.");
}
});
});
});
schema example:
const schema = {
type: "object",
properties: {
users: {
type: "array",
minItems: 10,
maxItems: 10,
uniqueItems: true,
items: {
type: "object",
properties: {
id: {
type: "integer",
initialOffset: 1,
autoIncrement: true,
},
name: {
type: "string",
faker: "name.findName",
},
},
required: ["id", "name"],
},
},
},
required: ["users"],
};