@coderzz/mocker-data-generator 3.0.5 → 3.0.7
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 +43 -0
- package/LICENSE +0 -0
- package/README.md +0 -0
- package/build/main/array-includes.d.ts +0 -0
- package/build/main/array-includes.js +45 -45
- package/build/main/index.d.ts +7 -7
- package/build/main/index.js +12 -12
- package/build/main/index.spec.d.ts +1 -1
- package/build/main/index.spec.js +16 -16
- package/build/main/lib/Generator.d.ts +61 -61
- package/build/main/lib/Generator.js +134 -134
- package/build/main/lib/Mocker.d.ts +22 -22
- package/build/main/lib/Mocker.js +76 -76
- package/build/main/lib/Schema.d.ts +9 -9
- package/build/main/lib/Schema.js +210 -210
- package/build/main/lib/types.d.ts +8 -8
- package/build/main/lib/types.js +2 -2
- package/build/main/lib/utils.d.ts +10 -10
- package/build/main/lib/utils.js +222 -222
- package/build/main/tests/Generator.db.spec.d.ts +1 -1
- package/build/main/tests/Generator.db.spec.js +33 -33
- package/build/main/tests/Generator.eval.spec.d.ts +1 -1
- package/build/main/tests/Generator.eval.spec.js +21 -21
- package/build/main/tests/Generator.function.spec.d.ts +1 -1
- package/build/main/tests/Generator.function.spec.js +48 -48
- package/build/main/tests/Generator.generator.spec.d.ts +1 -1
- package/build/main/tests/Generator.generator.spec.js +187 -187
- package/build/main/tests/Generator.hasMany.spec.d.ts +1 -1
- package/build/main/tests/Generator.hasMany.spec.js +115 -115
- package/build/main/tests/Generator.hasOne.spec.d.ts +1 -1
- package/build/main/tests/Generator.hasOne.spec.js +146 -146
- package/build/main/tests/Generator.incrementalId.spec.d.ts +1 -1
- package/build/main/tests/Generator.incrementalId.spec.js +26 -26
- package/build/main/tests/Generator.self.spec.d.ts +1 -1
- package/build/main/tests/Generator.self.spec.js +24 -24
- package/build/main/tests/Generator.static.spec.d.ts +1 -1
- package/build/main/tests/Generator.static.spec.js +15 -15
- package/build/main/tests/Generator.values.spec.d.ts +1 -1
- package/build/main/tests/Generator.values.spec.js +16 -16
- package/build/main/tests/Mocker.build.spec.d.ts +1 -1
- package/build/main/tests/Mocker.build.spec.js +196 -196
- package/build/main/tests/Mocker.reset.spec.d.ts +1 -1
- package/build/main/tests/Mocker.reset.spec.js +17 -17
- package/build/main/tests/Mocker.restart.spec.d.ts +1 -1
- package/build/main/tests/Mocker.restart.spec.js +24 -24
- package/build/main/tests/Mocker.schema.spec.d.ts +1 -1
- package/build/main/tests/Mocker.schema.spec.js +18 -18
- package/build/main/tests/Mocker.seed.spec.d.ts +1 -1
- package/build/main/tests/Mocker.seed.spec.js +42 -42
- package/build/main/tests/Schema.Array.spec.d.ts +1 -1
- package/build/main/tests/Schema.Array.spec.js +332 -332
- package/build/main/tests/Schema.BuildSingle.spec.d.ts +1 -1
- package/build/main/tests/Schema.BuildSingle.spec.js +13 -13
- package/build/main/tests/mocker.spec.d.ts +1 -1
- package/build/main/tests/mocker.spec.js +405 -405
- package/build/module/array-includes.js +45 -45
- package/build/module/index.js +7 -7
- package/build/module/lib/Generator.js +131 -131
- package/build/module/lib/Mocker.js +73 -73
- package/build/module/lib/Schema.js +207 -207
- package/build/module/lib/types.js +1 -1
- package/build/module/lib/utils.js +209 -209
- package/package.json +1 -1
@@ -1,22 +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
|
-
}
|
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
|
+
}
|
package/build/main/lib/Mocker.js
CHANGED
@@ -1,76 +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;
|
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;
|
@@ -1,9 +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
|
-
}
|
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
|
+
}
|
package/build/main/lib/Schema.js
CHANGED
@@ -1,210 +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;
|
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;
|
@@ -1,8 +1,8 @@
|
|
1
|
-
export type CustomGeneratorRun = (generator: any, input: any) => any;
|
2
|
-
export type CustomGenerator = {
|
3
|
-
library: any;
|
4
|
-
run: CustomGeneratorRun | undefined;
|
5
|
-
};
|
6
|
-
export type Generators = {
|
7
|
-
[key: string]: CustomGenerator;
|
8
|
-
};
|
1
|
+
export type CustomGeneratorRun = (generator: any, input: any) => any;
|
2
|
+
export type CustomGenerator = {
|
3
|
+
library: any;
|
4
|
+
run: CustomGeneratorRun | undefined;
|
5
|
+
};
|
6
|
+
export type Generators = {
|
7
|
+
[key: string]: CustomGenerator;
|
8
|
+
};
|
package/build/main/lib/types.js
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|