@coderzz/mocker-data-generator 3.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +561 -0
- package/LICENSE +21 -0
- package/README.md +550 -0
- package/build/main/array-includes.d.ts +0 -0
- package/build/main/array-includes.js +45 -0
- package/build/main/index.d.ts +7 -0
- package/build/main/index.js +12 -0
- package/build/main/index.spec.d.ts +1 -0
- package/build/main/index.spec.js +16 -0
- package/build/main/lib/Generator.d.ts +61 -0
- package/build/main/lib/Generator.js +134 -0
- package/build/main/lib/Mocker.d.ts +22 -0
- package/build/main/lib/Mocker.js +76 -0
- package/build/main/lib/Schema.d.ts +9 -0
- package/build/main/lib/Schema.js +210 -0
- package/build/main/lib/types.d.ts +8 -0
- package/build/main/lib/types.js +2 -0
- package/build/main/lib/utils.d.ts +10 -0
- package/build/main/lib/utils.js +222 -0
- package/build/main/tests/Generator.db.spec.d.ts +1 -0
- package/build/main/tests/Generator.db.spec.js +33 -0
- package/build/main/tests/Generator.eval.spec.d.ts +1 -0
- package/build/main/tests/Generator.eval.spec.js +21 -0
- package/build/main/tests/Generator.function.spec.d.ts +1 -0
- package/build/main/tests/Generator.function.spec.js +48 -0
- package/build/main/tests/Generator.generator.spec.d.ts +1 -0
- package/build/main/tests/Generator.generator.spec.js +187 -0
- package/build/main/tests/Generator.hasMany.spec.d.ts +1 -0
- package/build/main/tests/Generator.hasMany.spec.js +115 -0
- package/build/main/tests/Generator.hasOne.spec.d.ts +1 -0
- package/build/main/tests/Generator.hasOne.spec.js +146 -0
- package/build/main/tests/Generator.incrementalId.spec.d.ts +1 -0
- package/build/main/tests/Generator.incrementalId.spec.js +26 -0
- package/build/main/tests/Generator.self.spec.d.ts +1 -0
- package/build/main/tests/Generator.self.spec.js +24 -0
- package/build/main/tests/Generator.static.spec.d.ts +1 -0
- package/build/main/tests/Generator.static.spec.js +15 -0
- package/build/main/tests/Generator.values.spec.d.ts +1 -0
- package/build/main/tests/Generator.values.spec.js +16 -0
- package/build/main/tests/Mocker.build.spec.d.ts +1 -0
- package/build/main/tests/Mocker.build.spec.js +196 -0
- package/build/main/tests/Mocker.reset.spec.d.ts +1 -0
- package/build/main/tests/Mocker.reset.spec.js +17 -0
- package/build/main/tests/Mocker.restart.spec.d.ts +1 -0
- package/build/main/tests/Mocker.restart.spec.js +24 -0
- package/build/main/tests/Mocker.schema.spec.d.ts +1 -0
- package/build/main/tests/Mocker.schema.spec.js +18 -0
- package/build/main/tests/Mocker.seed.spec.d.ts +1 -0
- package/build/main/tests/Mocker.seed.spec.js +42 -0
- package/build/main/tests/Schema.Array.spec.d.ts +1 -0
- package/build/main/tests/Schema.Array.spec.js +332 -0
- package/build/main/tests/Schema.BuildSingle.spec.d.ts +1 -0
- package/build/main/tests/Schema.BuildSingle.spec.js +13 -0
- package/build/main/tests/mocker.spec.d.ts +1 -0
- package/build/main/tests/mocker.spec.js +405 -0
- package/build/module/array-includes.js +45 -0
- package/build/module/index.js +7 -0
- package/build/module/lib/Generator.js +131 -0
- package/build/module/lib/Mocker.js +73 -0
- package/build/module/lib/Schema.js +207 -0
- package/build/module/lib/types.js +1 -0
- package/build/module/lib/utils.js +209 -0
- package/package.json +130 -0
@@ -0,0 +1,61 @@
|
|
1
|
+
import { CustomGeneratorRun } from './types';
|
2
|
+
export declare class Generator<T> {
|
3
|
+
name: string;
|
4
|
+
DB: {};
|
5
|
+
object: T;
|
6
|
+
schema: {
|
7
|
+
values: string[];
|
8
|
+
};
|
9
|
+
options: {
|
10
|
+
uniqueField: string;
|
11
|
+
max: number;
|
12
|
+
min: number;
|
13
|
+
};
|
14
|
+
virtualPaths: string[];
|
15
|
+
generators: {};
|
16
|
+
self(cfg: {
|
17
|
+
self: any;
|
18
|
+
eval?: boolean;
|
19
|
+
}): any;
|
20
|
+
db(cfg: {
|
21
|
+
eval?: boolean;
|
22
|
+
db: any;
|
23
|
+
}): any;
|
24
|
+
custom(cfg: {
|
25
|
+
generator: any;
|
26
|
+
input: string | RegExp;
|
27
|
+
run?: CustomGeneratorRun;
|
28
|
+
adapter?: (generator: any, input: string | RegExp) => any;
|
29
|
+
eval?: boolean;
|
30
|
+
}): any;
|
31
|
+
eval(cfg: {
|
32
|
+
eval: string;
|
33
|
+
}): any;
|
34
|
+
values(cfg: {
|
35
|
+
values: any[];
|
36
|
+
}): any;
|
37
|
+
function(cfg: {
|
38
|
+
function: any;
|
39
|
+
}, ...args: any[]): any;
|
40
|
+
static(cfg: {
|
41
|
+
static: any;
|
42
|
+
}): any;
|
43
|
+
incrementalId(cfg: {
|
44
|
+
incrementalId: number | true | string;
|
45
|
+
}): number;
|
46
|
+
hasOne(cfg: {
|
47
|
+
hasOne: string;
|
48
|
+
get?: string;
|
49
|
+
eval?: boolean;
|
50
|
+
uniqueDB?: any[];
|
51
|
+
}): null;
|
52
|
+
hasMany(cfg: {
|
53
|
+
min?: number;
|
54
|
+
max?: number;
|
55
|
+
hasMany: string;
|
56
|
+
amount?: number;
|
57
|
+
get?: string;
|
58
|
+
eval?: boolean;
|
59
|
+
unique?: boolean;
|
60
|
+
}): any;
|
61
|
+
}
|
@@ -0,0 +1,134 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.Generator = void 0;
|
4
|
+
var tslib_1 = require("tslib");
|
5
|
+
var utils_1 = require("./utils");
|
6
|
+
var Generator = /** @class */ (function () {
|
7
|
+
function Generator() {
|
8
|
+
}
|
9
|
+
Generator.prototype.self = function (cfg) {
|
10
|
+
var object = this.object;
|
11
|
+
return cfg.eval
|
12
|
+
? eval('object.' + cfg.self)
|
13
|
+
: (0, utils_1.loopInside)(this.object, cfg.self);
|
14
|
+
};
|
15
|
+
Generator.prototype.db = function (cfg) {
|
16
|
+
var db = this.DB;
|
17
|
+
if (cfg.eval) {
|
18
|
+
return eval('db.' + cfg.db);
|
19
|
+
}
|
20
|
+
else {
|
21
|
+
return (0, utils_1.loopInside)(this.DB, cfg.db);
|
22
|
+
}
|
23
|
+
};
|
24
|
+
Generator.prototype.custom = function (cfg) {
|
25
|
+
var db = this.DB;
|
26
|
+
var object = this.object;
|
27
|
+
var re;
|
28
|
+
var matches;
|
29
|
+
var strFn;
|
30
|
+
var generator = cfg.generator;
|
31
|
+
if (cfg.run) {
|
32
|
+
return cfg.run(generator, cfg.input);
|
33
|
+
}
|
34
|
+
else if (cfg.eval) {
|
35
|
+
return eval('generator.' + cfg.input);
|
36
|
+
}
|
37
|
+
else {
|
38
|
+
return utils_1.stringToPathOrCall.call(generator, 'generator', generator, cfg.input);
|
39
|
+
}
|
40
|
+
};
|
41
|
+
Generator.prototype.eval = function (cfg) {
|
42
|
+
var db = this.DB;
|
43
|
+
var object = this.object;
|
44
|
+
var generators = this.generators;
|
45
|
+
return eval(cfg.eval);
|
46
|
+
};
|
47
|
+
Generator.prototype.values = function (cfg) {
|
48
|
+
var i = Math.floor(cfg.values.length * Math.random());
|
49
|
+
return cfg.values[i];
|
50
|
+
};
|
51
|
+
Generator.prototype.function = function (cfg) {
|
52
|
+
var _a;
|
53
|
+
var args = [];
|
54
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
55
|
+
args[_i - 1] = arguments[_i];
|
56
|
+
}
|
57
|
+
var object = this.object;
|
58
|
+
var db = this.DB;
|
59
|
+
var generators = this.generators;
|
60
|
+
return (_a = cfg.function).call.apply(_a, tslib_1.__spreadArray([{ object: object, db: db, generators: generators }], args, false));
|
61
|
+
};
|
62
|
+
Generator.prototype.static = function (cfg) {
|
63
|
+
return cfg.static;
|
64
|
+
};
|
65
|
+
Generator.prototype.incrementalId = function (cfg) {
|
66
|
+
var n = 0;
|
67
|
+
var db = this.DB;
|
68
|
+
if (db[this.name] && db[this.name].length) {
|
69
|
+
n = db[this.name].length;
|
70
|
+
}
|
71
|
+
if (cfg.incrementalId === true) {
|
72
|
+
cfg.incrementalId = '0';
|
73
|
+
}
|
74
|
+
return n + parseInt(cfg.incrementalId, 10);
|
75
|
+
};
|
76
|
+
Generator.prototype.hasOne = function (cfg) {
|
77
|
+
var db = this.DB;
|
78
|
+
var entity = null;
|
79
|
+
if (cfg.uniqueDB) {
|
80
|
+
var dbString = JSON.stringify(cfg.uniqueDB);
|
81
|
+
for (var i = 0; i < db[cfg.hasOne].length; i++) {
|
82
|
+
var element = db[cfg.hasOne][i];
|
83
|
+
element = cfg.get
|
84
|
+
? cfg.eval
|
85
|
+
? eval('element.' + cfg.get)
|
86
|
+
: (0, utils_1.loopInside)(element, cfg.get)
|
87
|
+
: element;
|
88
|
+
if (cfg.uniqueDB.length === 0 ||
|
89
|
+
dbString.indexOf(JSON.stringify(element)) < 0) {
|
90
|
+
entity = element;
|
91
|
+
break;
|
92
|
+
}
|
93
|
+
}
|
94
|
+
if (entity === null) {
|
95
|
+
throw "Can\u00B4t get unique data. Source \"".concat(cfg.hasOne, "\" has not enough data");
|
96
|
+
}
|
97
|
+
}
|
98
|
+
else {
|
99
|
+
var i = Math.floor(db[cfg.hasOne].length * Math.random());
|
100
|
+
entity = db[cfg.hasOne][i];
|
101
|
+
entity = cfg.get
|
102
|
+
? cfg.eval
|
103
|
+
? eval('entity.' + cfg.get)
|
104
|
+
: (0, utils_1.loopInside)(entity, cfg.get)
|
105
|
+
: entity;
|
106
|
+
}
|
107
|
+
return entity;
|
108
|
+
};
|
109
|
+
Generator.prototype.hasMany = function (cfg) {
|
110
|
+
var _this = this;
|
111
|
+
var amount = 1;
|
112
|
+
var db = this.DB;
|
113
|
+
var min = cfg.min || cfg.min === 0 ? cfg.min : 1;
|
114
|
+
var max = cfg.max ? cfg.max : cfg.hasMany ? db[cfg.hasMany].length : 1;
|
115
|
+
if (cfg.amount) {
|
116
|
+
amount = cfg.amount;
|
117
|
+
}
|
118
|
+
else {
|
119
|
+
amount = Math.floor(Math.random() * (max - min + 1)) + min;
|
120
|
+
}
|
121
|
+
var newCfg = {
|
122
|
+
hasOne: cfg.hasMany,
|
123
|
+
get: cfg.get ? cfg.get : undefined,
|
124
|
+
eval: cfg.eval ? true : false
|
125
|
+
};
|
126
|
+
return cfg.unique
|
127
|
+
? Array.from(new Array(amount)).reduce(function (acc, val) { return tslib_1.__spreadArray(tslib_1.__spreadArray([], acc, true), [
|
128
|
+
_this.hasOne(tslib_1.__assign(tslib_1.__assign({}, newCfg), { uniqueDB: acc }))
|
129
|
+
], false); }, [])
|
130
|
+
: Array.from(new Array(amount)).map(function () { return _this.hasOne(newCfg); });
|
131
|
+
};
|
132
|
+
return Generator;
|
133
|
+
}());
|
134
|
+
exports.Generator = Generator;
|
@@ -0,0 +1,22 @@
|
|
1
|
+
import { Schema } from './Schema';
|
2
|
+
import { CustomGeneratorRun, Generators } from './types';
|
3
|
+
export type PromiseCb = Promise<any> | void;
|
4
|
+
export type IDB = {
|
5
|
+
[key: string]: any[];
|
6
|
+
};
|
7
|
+
export declare class Mocker {
|
8
|
+
schemas: Schema[];
|
9
|
+
DB: IDB;
|
10
|
+
options: {};
|
11
|
+
generators: Generators;
|
12
|
+
constructor(options?: {});
|
13
|
+
seed(name: string, data: any[]): Mocker;
|
14
|
+
addGenerator(name: string, library: any, run?: CustomGeneratorRun): Mocker;
|
15
|
+
schema(name: string, schema: {}, options?: {}): Mocker;
|
16
|
+
reset(): Mocker;
|
17
|
+
restart(): Mocker;
|
18
|
+
private _buildSync;
|
19
|
+
buildSync(): IDB;
|
20
|
+
build(cb?: (error: Error | null, _?: any) => void): Promise<any>;
|
21
|
+
build(cb?: (error: Error | null, _?: any) => void): void;
|
22
|
+
}
|
@@ -0,0 +1,76 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.Mocker = void 0;
|
4
|
+
var Schema_1 = require("./Schema");
|
5
|
+
var utils_1 = require("./utils");
|
6
|
+
var Mocker = /** @class */ (function () {
|
7
|
+
function Mocker(options) {
|
8
|
+
if (options === void 0) { options = {}; }
|
9
|
+
this.schemas = [];
|
10
|
+
this.DB = {};
|
11
|
+
this.options = {};
|
12
|
+
this.generators = {};
|
13
|
+
this.options = options;
|
14
|
+
this.DB = {};
|
15
|
+
}
|
16
|
+
Mocker.prototype.seed = function (name, data) {
|
17
|
+
this.DB[name] = data;
|
18
|
+
return this;
|
19
|
+
};
|
20
|
+
Mocker.prototype.addGenerator = function (name, library, run) {
|
21
|
+
this.generators[name] = { library: library, run: run };
|
22
|
+
return this;
|
23
|
+
};
|
24
|
+
Mocker.prototype.schema = function (name, schema, options) {
|
25
|
+
this.schemas.push(new Schema_1.Schema(name, schema, options));
|
26
|
+
return this;
|
27
|
+
};
|
28
|
+
Mocker.prototype.reset = function () {
|
29
|
+
this.DB = {};
|
30
|
+
return this;
|
31
|
+
};
|
32
|
+
Mocker.prototype.restart = function () {
|
33
|
+
this.DB = {};
|
34
|
+
this.schemas = [];
|
35
|
+
return this;
|
36
|
+
};
|
37
|
+
Mocker.prototype._buildSync = function () {
|
38
|
+
var _this = this;
|
39
|
+
this.schemas.reduce(function (acc, schema) {
|
40
|
+
var instances;
|
41
|
+
try {
|
42
|
+
instances = schema.build(_this.generators, acc);
|
43
|
+
}
|
44
|
+
catch (e) {
|
45
|
+
throw new Error('Schema: "' + schema.name + '" ' + e);
|
46
|
+
}
|
47
|
+
// Clean virtuals
|
48
|
+
if (schema.virtualPaths.length > 0) {
|
49
|
+
instances.forEach(function (x) {
|
50
|
+
return (0, utils_1.cleanVirtuals)(schema.virtualPaths, x, {
|
51
|
+
strict: true,
|
52
|
+
symbol: ','
|
53
|
+
});
|
54
|
+
});
|
55
|
+
}
|
56
|
+
// Add to db
|
57
|
+
acc[schema.name] = instances;
|
58
|
+
return acc;
|
59
|
+
}, this.DB);
|
60
|
+
};
|
61
|
+
Mocker.prototype.buildSync = function () {
|
62
|
+
this._buildSync();
|
63
|
+
return this.DB;
|
64
|
+
};
|
65
|
+
Mocker.prototype.build = function (cb) {
|
66
|
+
try {
|
67
|
+
this._buildSync();
|
68
|
+
}
|
69
|
+
catch (e) {
|
70
|
+
return cb ? cb(e) : Promise.reject(e);
|
71
|
+
}
|
72
|
+
return cb ? cb(null, this.DB) : Promise.resolve(this.DB);
|
73
|
+
};
|
74
|
+
return Mocker;
|
75
|
+
}());
|
76
|
+
exports.Mocker = Mocker;
|
@@ -0,0 +1,9 @@
|
|
1
|
+
import { Generator } from './Generator';
|
2
|
+
import { Generators } from './types';
|
3
|
+
export declare class Schema extends Generator<any> {
|
4
|
+
constructor(name: string, cfg: any, options: any, generators?: Generators);
|
5
|
+
proccessLeaf(field: any, fieldName?: any): any;
|
6
|
+
generateField(fieldName: any, cfg: any, ...args: any[]): {};
|
7
|
+
buildSingle(schema: any): void;
|
8
|
+
build(generators?: Generators, db?: {}): any;
|
9
|
+
}
|
@@ -0,0 +1,210 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.Schema = void 0;
|
4
|
+
var tslib_1 = require("tslib");
|
5
|
+
var Generator_1 = require("./Generator");
|
6
|
+
var utils_1 = require("./utils");
|
7
|
+
var iterate = function (obj, res, currentPath) {
|
8
|
+
var _this = this;
|
9
|
+
if (!currentPath) {
|
10
|
+
currentPath = [];
|
11
|
+
}
|
12
|
+
Object.keys(obj).map(function (k) {
|
13
|
+
var value = obj[k];
|
14
|
+
var path = currentPath.slice(0);
|
15
|
+
path.push(k);
|
16
|
+
if ((0, utils_1.iamLastParent)(value)) {
|
17
|
+
if (path) {
|
18
|
+
if ((0, utils_1.isArray)(value)) {
|
19
|
+
if (value[0] && value[0].virtual) {
|
20
|
+
_this.virtualPaths.push(path.toString());
|
21
|
+
}
|
22
|
+
}
|
23
|
+
else {
|
24
|
+
if (value.virtual) {
|
25
|
+
_this.virtualPaths.push(path.toString());
|
26
|
+
}
|
27
|
+
}
|
28
|
+
}
|
29
|
+
var key = '';
|
30
|
+
if (!(0, utils_1.isConditional)(k)) {
|
31
|
+
key = k;
|
32
|
+
}
|
33
|
+
else {
|
34
|
+
var keykey = k.split(',');
|
35
|
+
if ((0, utils_1.evalWithContextData)(keykey[0], _this.object, _this.DB, _this.generators)) {
|
36
|
+
key = keykey[1];
|
37
|
+
}
|
38
|
+
}
|
39
|
+
if (key !== '') {
|
40
|
+
res[key] = _this.proccessLeaf(value, key);
|
41
|
+
}
|
42
|
+
}
|
43
|
+
else {
|
44
|
+
res[k] = {};
|
45
|
+
iterate.call(_this, value, res[k], path);
|
46
|
+
}
|
47
|
+
});
|
48
|
+
};
|
49
|
+
var Schema = /** @class */ (function (_super) {
|
50
|
+
tslib_1.__extends(Schema, _super);
|
51
|
+
function Schema(name, cfg, options, generators) {
|
52
|
+
if (generators === void 0) { generators = {}; }
|
53
|
+
var _this = _super.call(this) || this;
|
54
|
+
_this.schema = cfg;
|
55
|
+
_this.name = name;
|
56
|
+
_this.options = options;
|
57
|
+
_this.generators = generators;
|
58
|
+
// Temp fields
|
59
|
+
_this.DB = {};
|
60
|
+
_this.object = {};
|
61
|
+
_this.virtualPaths = [];
|
62
|
+
return _this;
|
63
|
+
}
|
64
|
+
Schema.prototype.proccessLeaf = function (field, fieldName) {
|
65
|
+
var _this = this;
|
66
|
+
if ((0, utils_1.isArray)(field)) {
|
67
|
+
var fieldConfig_1 = field[0];
|
68
|
+
if (field.length > 1) {
|
69
|
+
fieldConfig_1 = { values: field };
|
70
|
+
}
|
71
|
+
var na = Array();
|
72
|
+
if (fieldConfig_1.concat) {
|
73
|
+
na = (0, utils_1.evalWithContextData)(fieldConfig_1.concat, this.object, this.DB, this.generators);
|
74
|
+
// Strict Mode
|
75
|
+
na = fieldConfig_1.concatStrict
|
76
|
+
? tslib_1.__spreadArray([], Array.from(new Set(na)), true) : na;
|
77
|
+
}
|
78
|
+
var length_1 = (0, utils_1.fieldArrayCalcLength)(field.length > 1 ? fieldConfig_1.values : fieldConfig_1, na.length, this);
|
79
|
+
var array = Array.from(new Array(length_1)).reduce(function (acc, el, index) {
|
80
|
+
var self = acc.slice(0);
|
81
|
+
acc.push(_this.generateField(fieldName, fieldConfig_1, index, length_1, self));
|
82
|
+
return acc;
|
83
|
+
}, []);
|
84
|
+
return array.concat(na);
|
85
|
+
}
|
86
|
+
else {
|
87
|
+
return this.generateField(fieldName, field);
|
88
|
+
}
|
89
|
+
};
|
90
|
+
Schema.prototype.generateField = function (fieldName, cfg) {
|
91
|
+
var _a, _b;
|
92
|
+
var args = [];
|
93
|
+
for (var _i = 2; _i < arguments.length; _i++) {
|
94
|
+
args[_i - 2] = arguments[_i];
|
95
|
+
}
|
96
|
+
var result = {};
|
97
|
+
var customGenerators = Object.keys(this.generators);
|
98
|
+
var ownedByMocker = [
|
99
|
+
'self',
|
100
|
+
'db',
|
101
|
+
'hasOne',
|
102
|
+
'hasMany',
|
103
|
+
'static',
|
104
|
+
'function',
|
105
|
+
'values',
|
106
|
+
'incrementalId'
|
107
|
+
];
|
108
|
+
var generators = tslib_1.__spreadArray(tslib_1.__spreadArray([], ownedByMocker, true), customGenerators, true);
|
109
|
+
var keys = Object.keys(cfg);
|
110
|
+
var key = keys.reduce(function (acc, val) {
|
111
|
+
if (generators.includes(val)) {
|
112
|
+
acc = val;
|
113
|
+
}
|
114
|
+
return acc;
|
115
|
+
}, 'noKey');
|
116
|
+
if (key === 'noKey' && !keys.includes('eval')) {
|
117
|
+
throw "Error: Invalid or missing generator".concat(fieldName !== 'root' ? " on field ".concat(fieldName) : '', ". Please use one of this generators [").concat(generators.join(','), "], note that if your generator doesnt appear in the list maybe you forgot to add it.");
|
118
|
+
}
|
119
|
+
if (keys.includes('eval') && keys.length === 1) {
|
120
|
+
key = 'eval';
|
121
|
+
}
|
122
|
+
try {
|
123
|
+
result = customGenerators.includes(key)
|
124
|
+
? this.custom({
|
125
|
+
generator: (_a = this.generators[key]) === null || _a === void 0 ? void 0 : _a.library,
|
126
|
+
run: (_b = this.generators[key]) === null || _b === void 0 ? void 0 : _b.run,
|
127
|
+
input: cfg[key],
|
128
|
+
eval: cfg.eval
|
129
|
+
})
|
130
|
+
: this[key].apply(this, tslib_1.__spreadArray([tslib_1.__assign(tslib_1.__assign({}, cfg), { generators: this.generators })], args, false));
|
131
|
+
}
|
132
|
+
catch (e) {
|
133
|
+
throw 'Error: "' + key + '" ' + e;
|
134
|
+
}
|
135
|
+
return result;
|
136
|
+
};
|
137
|
+
Schema.prototype.buildSingle = function (schema) {
|
138
|
+
if ((0, utils_1.iamLastParent)(schema)) {
|
139
|
+
this.object = this.proccessLeaf(schema, 'root');
|
140
|
+
}
|
141
|
+
else {
|
142
|
+
iterate.call(this, schema, this.object);
|
143
|
+
}
|
144
|
+
};
|
145
|
+
Schema.prototype.build = function (generators, db) {
|
146
|
+
var _this = this;
|
147
|
+
if (generators === void 0) { generators = {}; }
|
148
|
+
if (db === void 0) { db = {}; }
|
149
|
+
this.generators = generators;
|
150
|
+
this.object = {};
|
151
|
+
this.DB = db ? db : {};
|
152
|
+
this.DB[this.name] = this.DB[this.name] ? this.DB[this.name] : [];
|
153
|
+
if (Number.isInteger(this.options)) {
|
154
|
+
Array.from(new Array(this.options)).map(function () {
|
155
|
+
_this.buildSingle(_this.schema);
|
156
|
+
_this.DB[_this.name].push(_this.object);
|
157
|
+
_this.object = {};
|
158
|
+
});
|
159
|
+
}
|
160
|
+
else if ((0, utils_1.isObject)(this.options) && this.options.max) {
|
161
|
+
var max = this.options.max;
|
162
|
+
var min = this.options.min ? this.options.min : 0;
|
163
|
+
var length = Math.floor(Math.random() * (max - min + 1) + min);
|
164
|
+
Array.from(new Array(length)).map(function () {
|
165
|
+
_this.buildSingle(_this.schema);
|
166
|
+
_this.DB[_this.name].push(_this.object);
|
167
|
+
_this.object = {};
|
168
|
+
});
|
169
|
+
}
|
170
|
+
else if ((0, utils_1.isObject)(this.options) && this.options.uniqueField) {
|
171
|
+
var f_1 = this.options.uniqueField;
|
172
|
+
var entityConfig_1 = this.schema;
|
173
|
+
var possibleValues = void 0;
|
174
|
+
if (f_1 === '.') {
|
175
|
+
possibleValues = this.schema.values;
|
176
|
+
}
|
177
|
+
else {
|
178
|
+
if (this.schema[f_1]) {
|
179
|
+
if ((0, utils_1.isArray)(this.schema[f_1].values)) {
|
180
|
+
possibleValues = this.schema[f_1].values;
|
181
|
+
}
|
182
|
+
else {
|
183
|
+
possibleValues = this.schema[f_1];
|
184
|
+
}
|
185
|
+
}
|
186
|
+
else {
|
187
|
+
throw "The field \"".concat(f_1, "\" not exists.");
|
188
|
+
}
|
189
|
+
}
|
190
|
+
if (!(0, utils_1.isArray)(possibleValues)) {
|
191
|
+
throw "The posible values value is not an Array";
|
192
|
+
}
|
193
|
+
possibleValues.map(function (value) {
|
194
|
+
if (f_1 === '.') {
|
195
|
+
return;
|
196
|
+
}
|
197
|
+
entityConfig_1[f_1] = { static: value };
|
198
|
+
_this.buildSingle(entityConfig_1);
|
199
|
+
_this.DB[_this.name].push(_this.object);
|
200
|
+
_this.object = {};
|
201
|
+
});
|
202
|
+
}
|
203
|
+
else {
|
204
|
+
throw "An string \"".concat(this.options, "\" is not recognized as a parameter.");
|
205
|
+
}
|
206
|
+
return this.DB[this.name];
|
207
|
+
};
|
208
|
+
return Schema;
|
209
|
+
}(Generator_1.Generator));
|
210
|
+
exports.Schema = Schema;
|
@@ -0,0 +1,10 @@
|
|
1
|
+
export declare const isArray: (arg: any) => boolean;
|
2
|
+
export declare const isObject: (arg: any) => boolean;
|
3
|
+
export declare const evalWithContextData: (key: string, object: {}, db: {}, generators: {}) => any;
|
4
|
+
export declare const fieldArrayCalcLength: (config: any, fixedArrayLength: any, schema: any) => any;
|
5
|
+
export declare const iamLastChild: (parent: any, k: any) => boolean;
|
6
|
+
export declare const iamLastParent: (obj: any) => boolean;
|
7
|
+
export declare const isConditional: (str: any) => boolean;
|
8
|
+
export declare const cleanVirtuals: (paths: any, object: any, options: any) => any;
|
9
|
+
export declare const stringToPathOrCall: (name: any, fn: any, cfg: any) => any;
|
10
|
+
export declare const loopInside: (object: any, path: string) => any;
|