UNPKG

mocker-data-generator

Version:

A simplified way to generate mock data, builds using a simple schema and with the FakerJs

74 lines (73 loc) 2.21 kB
import { Schema } from './Schema'; import { cleanVirtuals } from './utils'; var Mocker = /** @class */ (function () { function Mocker(options) { if (options === void 0) { options = {}; } this.schemas = []; this.DB = {}; this.options = {}; this.generators = {}; this.options = options; this.DB = {}; } Mocker.prototype.seed = function (name, data) { this.DB[name] = data; return this; }; Mocker.prototype.addGenerator = function (name, library, run) { this.generators[name] = { library: library, run: run }; return this; }; Mocker.prototype.schema = function (name, schema, options) { this.schemas.push(new Schema(name, schema, options)); return this; }; Mocker.prototype.reset = function () { this.DB = {}; return this; }; Mocker.prototype.restart = function () { this.DB = {}; this.schemas = []; return this; }; Mocker.prototype._buildSync = function () { var _this = this; this.schemas.reduce(function (acc, schema) { var instances; try { instances = schema.build(_this.generators, acc); } catch (e) { throw new Error('Schema: "' + schema.name + '" ' + e); } // Clean virtuals if (schema.virtualPaths.length > 0) { instances.forEach(function (x) { return cleanVirtuals(schema.virtualPaths, x, { strict: true, symbol: ',' }); }); } // Add to db acc[schema.name] = instances; return acc; }, this.DB); }; Mocker.prototype.buildSync = function () { this._buildSync(); return this.DB; }; Mocker.prototype.build = function (cb) { try { this._buildSync(); } catch (e) { return cb ? cb(e) : Promise.reject(e); } return cb ? cb(null, this.DB) : Promise.resolve(this.DB); }; return Mocker; }()); export { Mocker };