mocker-data-generator
Version:
A simplified way to generate mock data, builds using a simple schema and with the FakerJs
77 lines (76 loc) • 2.34 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Mocker = void 0;
var Schema_1 = require("./Schema");
var utils_1 = require("./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_1.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 (0, utils_1.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;
}());
exports.Mocker = Mocker;