Skip to content

Instantly share code, notes, and snippets.

@JonJam
Created September 11, 2017 07:42
Show Gist options
  • Save JonJam/3ba268215891fa026301ecae4ccf1dde to your computer and use it in GitHub Desktop.
Save JonJam/3ba268215891fa026301ecae4ccf1dde to your computer and use it in GitHub Desktop.
Creating a fake REST API with json-server - generateData
import { green, red } from "chalk";
import * as fs from "fs";
import * as jsf from "json-schema-faker";
import groupSchema from "./groupSchema";
import IGroup from "../src/models/IGroup";
import ISite from "../src/models/ISite";
import siteSchema from "./siteSchema";
const compiledGroupSchema = jsf(groupSchema);
const groups = compiledGroupSchema.groups;
let sites: ISite[] = [];
groups.forEach((group: IGroup) => {
const compiledSiteSchema = jsf(siteSchema);
sites = [...sites, ...compiledSiteSchema.sites];
group.sites = compiledSiteSchema.sites.map((site: ISite) => site.id);
});
const json = JSON.stringify({
groups,
sites
});
fs.writeFile("./buildScripts/db.json", json, err => {
if (err) {
console.log(red(err.message));
} else {
console.log(green("Mock API data generated."));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment