Created
September 11, 2017 07:42
-
-
Save JonJam/3ba268215891fa026301ecae4ccf1dde to your computer and use it in GitHub Desktop.
Creating a fake REST API with json-server - generateData
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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