@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,45 +1,45 @@
|
|
1
|
-
if (!Array.prototype.includes) {
|
2
|
-
Object.defineProperty(Array.prototype, 'includes', {
|
3
|
-
value: function (searchElement, fromIndex) {
|
4
|
-
// 1. Let O be ? ToObject(this value).
|
5
|
-
if (this == null) {
|
6
|
-
throw new TypeError('"this" is null or not defined');
|
7
|
-
}
|
8
|
-
var o = Object(this);
|
9
|
-
// 2. Let len be ? ToLength(? Get(O, "length")).
|
10
|
-
var len = o.length >>> 0;
|
11
|
-
// 3. If len is 0, return false.
|
12
|
-
if (len === 0) {
|
13
|
-
return false;
|
14
|
-
}
|
15
|
-
// 4. Let n be ? ToInteger(fromIndex).
|
16
|
-
// (If fromIndex is undefined, this step produces the value 0.)
|
17
|
-
var n = fromIndex | 0;
|
18
|
-
// 5. If n ≥ 0, then
|
19
|
-
// a. Let k be n.
|
20
|
-
// 6. Else n < 0,
|
21
|
-
// a. Let k be len + n.
|
22
|
-
// b. If k < 0, let k be 0.
|
23
|
-
var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
|
24
|
-
function sameValueZero(x, y) {
|
25
|
-
return (x === y ||
|
26
|
-
(typeof x === 'number' &&
|
27
|
-
typeof y === 'number' &&
|
28
|
-
isNaN(x) &&
|
29
|
-
isNaN(y)));
|
30
|
-
}
|
31
|
-
// 7. Repeat, while k < len
|
32
|
-
while (k < len) {
|
33
|
-
// a. Let elementK be the result of ? Get(O, ! ToString(k)).
|
34
|
-
// b. If SameValueZero(searchElement, elementK) is true, return true.
|
35
|
-
// c. Increase k by 1.
|
36
|
-
if (sameValueZero(o[k], searchElement)) {
|
37
|
-
return true;
|
38
|
-
}
|
39
|
-
k++;
|
40
|
-
}
|
41
|
-
// 8. Return false
|
42
|
-
return false;
|
43
|
-
}
|
44
|
-
});
|
45
|
-
}
|
1
|
+
if (!Array.prototype.includes) {
|
2
|
+
Object.defineProperty(Array.prototype, 'includes', {
|
3
|
+
value: function (searchElement, fromIndex) {
|
4
|
+
// 1. Let O be ? ToObject(this value).
|
5
|
+
if (this == null) {
|
6
|
+
throw new TypeError('"this" is null or not defined');
|
7
|
+
}
|
8
|
+
var o = Object(this);
|
9
|
+
// 2. Let len be ? ToLength(? Get(O, "length")).
|
10
|
+
var len = o.length >>> 0;
|
11
|
+
// 3. If len is 0, return false.
|
12
|
+
if (len === 0) {
|
13
|
+
return false;
|
14
|
+
}
|
15
|
+
// 4. Let n be ? ToInteger(fromIndex).
|
16
|
+
// (If fromIndex is undefined, this step produces the value 0.)
|
17
|
+
var n = fromIndex | 0;
|
18
|
+
// 5. If n ≥ 0, then
|
19
|
+
// a. Let k be n.
|
20
|
+
// 6. Else n < 0,
|
21
|
+
// a. Let k be len + n.
|
22
|
+
// b. If k < 0, let k be 0.
|
23
|
+
var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
|
24
|
+
function sameValueZero(x, y) {
|
25
|
+
return (x === y ||
|
26
|
+
(typeof x === 'number' &&
|
27
|
+
typeof y === 'number' &&
|
28
|
+
isNaN(x) &&
|
29
|
+
isNaN(y)));
|
30
|
+
}
|
31
|
+
// 7. Repeat, while k < len
|
32
|
+
while (k < len) {
|
33
|
+
// a. Let elementK be the result of ? Get(O, ! ToString(k)).
|
34
|
+
// b. If SameValueZero(searchElement, elementK) is true, return true.
|
35
|
+
// c. Increase k by 1.
|
36
|
+
if (sameValueZero(o[k], searchElement)) {
|
37
|
+
return true;
|
38
|
+
}
|
39
|
+
k++;
|
40
|
+
}
|
41
|
+
// 8. Return false
|
42
|
+
return false;
|
43
|
+
}
|
44
|
+
});
|
45
|
+
}
|
package/build/module/index.js
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
import './array-includes';
|
2
|
-
import { Mocker } from './lib/Mocker';
|
3
|
-
export * from './lib/Mocker';
|
4
|
-
export * from './lib/Schema';
|
5
|
-
export * from './lib/Generator';
|
6
|
-
export var mocker = function (opts) { return new Mocker(opts); };
|
7
|
-
export default mocker;
|
1
|
+
import './array-includes';
|
2
|
+
import { Mocker } from './lib/Mocker';
|
3
|
+
export * from './lib/Mocker';
|
4
|
+
export * from './lib/Schema';
|
5
|
+
export * from './lib/Generator';
|
6
|
+
export var mocker = function (opts) { return new Mocker(opts); };
|
7
|
+
export default mocker;
|
@@ -1,131 +1,131 @@
|
|
1
|
-
import { __assign, __spreadArray } from "tslib";
|
2
|
-
import { loopInside, stringToPathOrCall } from './utils';
|
3
|
-
var Generator = /** @class */ (function () {
|
4
|
-
function Generator() {
|
5
|
-
}
|
6
|
-
Generator.prototype.self = function (cfg) {
|
7
|
-
var object = this.object;
|
8
|
-
return cfg.eval
|
9
|
-
? eval('object.' + cfg.self)
|
10
|
-
: loopInside(this.object, cfg.self);
|
11
|
-
};
|
12
|
-
Generator.prototype.db = function (cfg) {
|
13
|
-
var db = this.DB;
|
14
|
-
if (cfg.eval) {
|
15
|
-
return eval('db.' + cfg.db);
|
16
|
-
}
|
17
|
-
else {
|
18
|
-
return loopInside(this.DB, cfg.db);
|
19
|
-
}
|
20
|
-
};
|
21
|
-
Generator.prototype.custom = function (cfg) {
|
22
|
-
var db = this.DB;
|
23
|
-
var object = this.object;
|
24
|
-
var re;
|
25
|
-
var matches;
|
26
|
-
var strFn;
|
27
|
-
var generator = cfg.generator;
|
28
|
-
if (cfg.run) {
|
29
|
-
return cfg.run(generator, cfg.input);
|
30
|
-
}
|
31
|
-
else if (cfg.eval) {
|
32
|
-
return eval('generator.' + cfg.input);
|
33
|
-
}
|
34
|
-
else {
|
35
|
-
return stringToPathOrCall.call(generator, 'generator', generator, cfg.input);
|
36
|
-
}
|
37
|
-
};
|
38
|
-
Generator.prototype.eval = function (cfg) {
|
39
|
-
var db = this.DB;
|
40
|
-
var object = this.object;
|
41
|
-
var generators = this.generators;
|
42
|
-
return eval(cfg.eval);
|
43
|
-
};
|
44
|
-
Generator.prototype.values = function (cfg) {
|
45
|
-
var i = Math.floor(cfg.values.length * Math.random());
|
46
|
-
return cfg.values[i];
|
47
|
-
};
|
48
|
-
Generator.prototype.function = function (cfg) {
|
49
|
-
var _a;
|
50
|
-
var args = [];
|
51
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
52
|
-
args[_i - 1] = arguments[_i];
|
53
|
-
}
|
54
|
-
var object = this.object;
|
55
|
-
var db = this.DB;
|
56
|
-
var generators = this.generators;
|
57
|
-
return (_a = cfg.function).call.apply(_a, __spreadArray([{ object: object, db: db, generators: generators }], args, false));
|
58
|
-
};
|
59
|
-
Generator.prototype.static = function (cfg) {
|
60
|
-
return cfg.static;
|
61
|
-
};
|
62
|
-
Generator.prototype.incrementalId = function (cfg) {
|
63
|
-
var n = 0;
|
64
|
-
var db = this.DB;
|
65
|
-
if (db[this.name] && db[this.name].length) {
|
66
|
-
n = db[this.name].length;
|
67
|
-
}
|
68
|
-
if (cfg.incrementalId === true) {
|
69
|
-
cfg.incrementalId = '0';
|
70
|
-
}
|
71
|
-
return n + parseInt(cfg.incrementalId, 10);
|
72
|
-
};
|
73
|
-
Generator.prototype.hasOne = function (cfg) {
|
74
|
-
var db = this.DB;
|
75
|
-
var entity = null;
|
76
|
-
if (cfg.uniqueDB) {
|
77
|
-
var dbString = JSON.stringify(cfg.uniqueDB);
|
78
|
-
for (var i = 0; i < db[cfg.hasOne].length; i++) {
|
79
|
-
var element = db[cfg.hasOne][i];
|
80
|
-
element = cfg.get
|
81
|
-
? cfg.eval
|
82
|
-
? eval('element.' + cfg.get)
|
83
|
-
: loopInside(element, cfg.get)
|
84
|
-
: element;
|
85
|
-
if (cfg.uniqueDB.length === 0 ||
|
86
|
-
dbString.indexOf(JSON.stringify(element)) < 0) {
|
87
|
-
entity = element;
|
88
|
-
break;
|
89
|
-
}
|
90
|
-
}
|
91
|
-
if (entity === null) {
|
92
|
-
throw "Can\u00B4t get unique data. Source \"".concat(cfg.hasOne, "\" has not enough data");
|
93
|
-
}
|
94
|
-
}
|
95
|
-
else {
|
96
|
-
var i = Math.floor(db[cfg.hasOne].length * Math.random());
|
97
|
-
entity = db[cfg.hasOne][i];
|
98
|
-
entity = cfg.get
|
99
|
-
? cfg.eval
|
100
|
-
? eval('entity.' + cfg.get)
|
101
|
-
: loopInside(entity, cfg.get)
|
102
|
-
: entity;
|
103
|
-
}
|
104
|
-
return entity;
|
105
|
-
};
|
106
|
-
Generator.prototype.hasMany = function (cfg) {
|
107
|
-
var _this = this;
|
108
|
-
var amount = 1;
|
109
|
-
var db = this.DB;
|
110
|
-
var min = cfg.min || cfg.min === 0 ? cfg.min : 1;
|
111
|
-
var max = cfg.max ? cfg.max : cfg.hasMany ? db[cfg.hasMany].length : 1;
|
112
|
-
if (cfg.amount) {
|
113
|
-
amount = cfg.amount;
|
114
|
-
}
|
115
|
-
else {
|
116
|
-
amount = Math.floor(Math.random() * (max - min + 1)) + min;
|
117
|
-
}
|
118
|
-
var newCfg = {
|
119
|
-
hasOne: cfg.hasMany,
|
120
|
-
get: cfg.get ? cfg.get : undefined,
|
121
|
-
eval: cfg.eval ? true : false
|
122
|
-
};
|
123
|
-
return cfg.unique
|
124
|
-
? Array.from(new Array(amount)).reduce(function (acc, val) { return __spreadArray(__spreadArray([], acc, true), [
|
125
|
-
_this.hasOne(__assign(__assign({}, newCfg), { uniqueDB: acc }))
|
126
|
-
], false); }, [])
|
127
|
-
: Array.from(new Array(amount)).map(function () { return _this.hasOne(newCfg); });
|
128
|
-
};
|
129
|
-
return Generator;
|
130
|
-
}());
|
131
|
-
export { Generator };
|
1
|
+
import { __assign, __spreadArray } from "tslib";
|
2
|
+
import { loopInside, stringToPathOrCall } from './utils';
|
3
|
+
var Generator = /** @class */ (function () {
|
4
|
+
function Generator() {
|
5
|
+
}
|
6
|
+
Generator.prototype.self = function (cfg) {
|
7
|
+
var object = this.object;
|
8
|
+
return cfg.eval
|
9
|
+
? eval('object.' + cfg.self)
|
10
|
+
: loopInside(this.object, cfg.self);
|
11
|
+
};
|
12
|
+
Generator.prototype.db = function (cfg) {
|
13
|
+
var db = this.DB;
|
14
|
+
if (cfg.eval) {
|
15
|
+
return eval('db.' + cfg.db);
|
16
|
+
}
|
17
|
+
else {
|
18
|
+
return loopInside(this.DB, cfg.db);
|
19
|
+
}
|
20
|
+
};
|
21
|
+
Generator.prototype.custom = function (cfg) {
|
22
|
+
var db = this.DB;
|
23
|
+
var object = this.object;
|
24
|
+
var re;
|
25
|
+
var matches;
|
26
|
+
var strFn;
|
27
|
+
var generator = cfg.generator;
|
28
|
+
if (cfg.run) {
|
29
|
+
return cfg.run(generator, cfg.input);
|
30
|
+
}
|
31
|
+
else if (cfg.eval) {
|
32
|
+
return eval('generator.' + cfg.input);
|
33
|
+
}
|
34
|
+
else {
|
35
|
+
return stringToPathOrCall.call(generator, 'generator', generator, cfg.input);
|
36
|
+
}
|
37
|
+
};
|
38
|
+
Generator.prototype.eval = function (cfg) {
|
39
|
+
var db = this.DB;
|
40
|
+
var object = this.object;
|
41
|
+
var generators = this.generators;
|
42
|
+
return eval(cfg.eval);
|
43
|
+
};
|
44
|
+
Generator.prototype.values = function (cfg) {
|
45
|
+
var i = Math.floor(cfg.values.length * Math.random());
|
46
|
+
return cfg.values[i];
|
47
|
+
};
|
48
|
+
Generator.prototype.function = function (cfg) {
|
49
|
+
var _a;
|
50
|
+
var args = [];
|
51
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
52
|
+
args[_i - 1] = arguments[_i];
|
53
|
+
}
|
54
|
+
var object = this.object;
|
55
|
+
var db = this.DB;
|
56
|
+
var generators = this.generators;
|
57
|
+
return (_a = cfg.function).call.apply(_a, __spreadArray([{ object: object, db: db, generators: generators }], args, false));
|
58
|
+
};
|
59
|
+
Generator.prototype.static = function (cfg) {
|
60
|
+
return cfg.static;
|
61
|
+
};
|
62
|
+
Generator.prototype.incrementalId = function (cfg) {
|
63
|
+
var n = 0;
|
64
|
+
var db = this.DB;
|
65
|
+
if (db[this.name] && db[this.name].length) {
|
66
|
+
n = db[this.name].length;
|
67
|
+
}
|
68
|
+
if (cfg.incrementalId === true) {
|
69
|
+
cfg.incrementalId = '0';
|
70
|
+
}
|
71
|
+
return n + parseInt(cfg.incrementalId, 10);
|
72
|
+
};
|
73
|
+
Generator.prototype.hasOne = function (cfg) {
|
74
|
+
var db = this.DB;
|
75
|
+
var entity = null;
|
76
|
+
if (cfg.uniqueDB) {
|
77
|
+
var dbString = JSON.stringify(cfg.uniqueDB);
|
78
|
+
for (var i = 0; i < db[cfg.hasOne].length; i++) {
|
79
|
+
var element = db[cfg.hasOne][i];
|
80
|
+
element = cfg.get
|
81
|
+
? cfg.eval
|
82
|
+
? eval('element.' + cfg.get)
|
83
|
+
: loopInside(element, cfg.get)
|
84
|
+
: element;
|
85
|
+
if (cfg.uniqueDB.length === 0 ||
|
86
|
+
dbString.indexOf(JSON.stringify(element)) < 0) {
|
87
|
+
entity = element;
|
88
|
+
break;
|
89
|
+
}
|
90
|
+
}
|
91
|
+
if (entity === null) {
|
92
|
+
throw "Can\u00B4t get unique data. Source \"".concat(cfg.hasOne, "\" has not enough data");
|
93
|
+
}
|
94
|
+
}
|
95
|
+
else {
|
96
|
+
var i = Math.floor(db[cfg.hasOne].length * Math.random());
|
97
|
+
entity = db[cfg.hasOne][i];
|
98
|
+
entity = cfg.get
|
99
|
+
? cfg.eval
|
100
|
+
? eval('entity.' + cfg.get)
|
101
|
+
: loopInside(entity, cfg.get)
|
102
|
+
: entity;
|
103
|
+
}
|
104
|
+
return entity;
|
105
|
+
};
|
106
|
+
Generator.prototype.hasMany = function (cfg) {
|
107
|
+
var _this = this;
|
108
|
+
var amount = 1;
|
109
|
+
var db = this.DB;
|
110
|
+
var min = cfg.min || cfg.min === 0 ? cfg.min : 1;
|
111
|
+
var max = cfg.max ? cfg.max : cfg.hasMany ? db[cfg.hasMany].length : 1;
|
112
|
+
if (cfg.amount) {
|
113
|
+
amount = cfg.amount;
|
114
|
+
}
|
115
|
+
else {
|
116
|
+
amount = Math.floor(Math.random() * (max - min + 1)) + min;
|
117
|
+
}
|
118
|
+
var newCfg = {
|
119
|
+
hasOne: cfg.hasMany,
|
120
|
+
get: cfg.get ? cfg.get : undefined,
|
121
|
+
eval: cfg.eval ? true : false
|
122
|
+
};
|
123
|
+
return cfg.unique
|
124
|
+
? Array.from(new Array(amount)).reduce(function (acc, val) { return __spreadArray(__spreadArray([], acc, true), [
|
125
|
+
_this.hasOne(__assign(__assign({}, newCfg), { uniqueDB: acc }))
|
126
|
+
], false); }, [])
|
127
|
+
: Array.from(new Array(amount)).map(function () { return _this.hasOne(newCfg); });
|
128
|
+
};
|
129
|
+
return Generator;
|
130
|
+
}());
|
131
|
+
export { Generator };
|
@@ -1,73 +1,73 @@
|
|
1
|
-
import { Schema } from './Schema';
|
2
|
-
import { cleanVirtuals } from './utils';
|
3
|
-
var Mocker = /** @class */ (function () {
|
4
|
-
function Mocker(options) {
|
5
|
-
if (options === void 0) { options = {}; }
|
6
|
-
this.schemas = [];
|
7
|
-
this.DB = {};
|
8
|
-
this.options = {};
|
9
|
-
this.generators = {};
|
10
|
-
this.options = options;
|
11
|
-
this.DB = {};
|
12
|
-
}
|
13
|
-
Mocker.prototype.seed = function (name, data) {
|
14
|
-
this.DB[name] = data;
|
15
|
-
return this;
|
16
|
-
};
|
17
|
-
Mocker.prototype.addGenerator = function (name, library, run) {
|
18
|
-
this.generators[name] = { library: library, run: run };
|
19
|
-
return this;
|
20
|
-
};
|
21
|
-
Mocker.prototype.schema = function (name, schema, options) {
|
22
|
-
this.schemas.push(new Schema(name, schema, options));
|
23
|
-
return this;
|
24
|
-
};
|
25
|
-
Mocker.prototype.reset = function () {
|
26
|
-
this.DB = {};
|
27
|
-
return this;
|
28
|
-
};
|
29
|
-
Mocker.prototype.restart = function () {
|
30
|
-
this.DB = {};
|
31
|
-
this.schemas = [];
|
32
|
-
return this;
|
33
|
-
};
|
34
|
-
Mocker.prototype._buildSync = function () {
|
35
|
-
var _this = this;
|
36
|
-
this.schemas.reduce(function (acc, schema) {
|
37
|
-
var instances;
|
38
|
-
try {
|
39
|
-
instances = schema.build(_this.generators, acc);
|
40
|
-
}
|
41
|
-
catch (e) {
|
42
|
-
throw new Error('Schema: "' + schema.name + '" ' + e);
|
43
|
-
}
|
44
|
-
// Clean virtuals
|
45
|
-
if (schema.virtualPaths.length > 0) {
|
46
|
-
instances.forEach(function (x) {
|
47
|
-
return cleanVirtuals(schema.virtualPaths, x, {
|
48
|
-
strict: true,
|
49
|
-
symbol: ','
|
50
|
-
});
|
51
|
-
});
|
52
|
-
}
|
53
|
-
// Add to db
|
54
|
-
acc[schema.name] = instances;
|
55
|
-
return acc;
|
56
|
-
}, this.DB);
|
57
|
-
};
|
58
|
-
Mocker.prototype.buildSync = function () {
|
59
|
-
this._buildSync();
|
60
|
-
return this.DB;
|
61
|
-
};
|
62
|
-
Mocker.prototype.build = function (cb) {
|
63
|
-
try {
|
64
|
-
this._buildSync();
|
65
|
-
}
|
66
|
-
catch (e) {
|
67
|
-
return cb ? cb(e) : Promise.reject(e);
|
68
|
-
}
|
69
|
-
return cb ? cb(null, this.DB) : Promise.resolve(this.DB);
|
70
|
-
};
|
71
|
-
return Mocker;
|
72
|
-
}());
|
73
|
-
export { Mocker };
|
1
|
+
import { Schema } from './Schema';
|
2
|
+
import { cleanVirtuals } from './utils';
|
3
|
+
var Mocker = /** @class */ (function () {
|
4
|
+
function Mocker(options) {
|
5
|
+
if (options === void 0) { options = {}; }
|
6
|
+
this.schemas = [];
|
7
|
+
this.DB = {};
|
8
|
+
this.options = {};
|
9
|
+
this.generators = {};
|
10
|
+
this.options = options;
|
11
|
+
this.DB = {};
|
12
|
+
}
|
13
|
+
Mocker.prototype.seed = function (name, data) {
|
14
|
+
this.DB[name] = data;
|
15
|
+
return this;
|
16
|
+
};
|
17
|
+
Mocker.prototype.addGenerator = function (name, library, run) {
|
18
|
+
this.generators[name] = { library: library, run: run };
|
19
|
+
return this;
|
20
|
+
};
|
21
|
+
Mocker.prototype.schema = function (name, schema, options) {
|
22
|
+
this.schemas.push(new Schema(name, schema, options));
|
23
|
+
return this;
|
24
|
+
};
|
25
|
+
Mocker.prototype.reset = function () {
|
26
|
+
this.DB = {};
|
27
|
+
return this;
|
28
|
+
};
|
29
|
+
Mocker.prototype.restart = function () {
|
30
|
+
this.DB = {};
|
31
|
+
this.schemas = [];
|
32
|
+
return this;
|
33
|
+
};
|
34
|
+
Mocker.prototype._buildSync = function () {
|
35
|
+
var _this = this;
|
36
|
+
this.schemas.reduce(function (acc, schema) {
|
37
|
+
var instances;
|
38
|
+
try {
|
39
|
+
instances = schema.build(_this.generators, acc);
|
40
|
+
}
|
41
|
+
catch (e) {
|
42
|
+
throw new Error('Schema: "' + schema.name + '" ' + e);
|
43
|
+
}
|
44
|
+
// Clean virtuals
|
45
|
+
if (schema.virtualPaths.length > 0) {
|
46
|
+
instances.forEach(function (x) {
|
47
|
+
return cleanVirtuals(schema.virtualPaths, x, {
|
48
|
+
strict: true,
|
49
|
+
symbol: ','
|
50
|
+
});
|
51
|
+
});
|
52
|
+
}
|
53
|
+
// Add to db
|
54
|
+
acc[schema.name] = instances;
|
55
|
+
return acc;
|
56
|
+
}, this.DB);
|
57
|
+
};
|
58
|
+
Mocker.prototype.buildSync = function () {
|
59
|
+
this._buildSync();
|
60
|
+
return this.DB;
|
61
|
+
};
|
62
|
+
Mocker.prototype.build = function (cb) {
|
63
|
+
try {
|
64
|
+
this._buildSync();
|
65
|
+
}
|
66
|
+
catch (e) {
|
67
|
+
return cb ? cb(e) : Promise.reject(e);
|
68
|
+
}
|
69
|
+
return cb ? cb(null, this.DB) : Promise.resolve(this.DB);
|
70
|
+
};
|
71
|
+
return Mocker;
|
72
|
+
}());
|
73
|
+
export { Mocker };
|