data-faker-plus 0.0.5 → 0.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/README.md +92 -94
- package/dist/index.cjs.js +1 -1
- package/dist/index.esm.js +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/types/core/ModelParser.d.ts +1 -1
- package/dist/types/index.d.ts +1 -1
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -1,49 +1,46 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Getting Started
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
[
|
|
5
|
-
[DataFaker 国内官网](https://ootjp8xe3-datafaker-9j23z0sk.maozi.io/)
|
|
3
|
+
[Official Website](https://df-docs-lgxj441cg-bloom-lmh.vercel.app/)
|
|
4
|
+
[中文官网](https://ootjp8xe3-datafaker-9j23z0sk.maozi.io/)
|
|
6
5
|
|
|
7
|
-
##
|
|
6
|
+
## Introduction
|
|
8
7
|
|
|
9
|
-
|
|
8
|
+
`DataFaker` is a data generator that relies on `faker.js` under the hood while extending it with template syntax. It enables you to quickly generate various types of data, including referenced data and recursive data, meeting your data generation needs across different scenarios. It is particularly suitable for the following use cases:
|
|
10
9
|
|
|
11
|
-
|
|
10
|
+
- Mock data for front-end development
|
|
11
|
+
- Unit testing and integration testing
|
|
12
|
+
- API interface prototyping
|
|
13
|
+
- Database sample data generation
|
|
14
|
+
- Demonstration and teaching examples
|
|
12
15
|
|
|
13
|
-
|
|
14
|
-
- 单元测试和集成测试
|
|
15
|
-
- API 接口原型设计
|
|
16
|
-
- 数据库样本数据生成
|
|
17
|
-
- 演示和教学用例
|
|
16
|
+
[A Tribute to faker.js](https://faker.nodejs.cn/guide/)
|
|
18
17
|
|
|
19
|
-
|
|
18
|
+
## Features
|
|
20
19
|
|
|
21
|
-
|
|
20
|
+
- **Non-intrusive**: `DataFaker` only enhances `faker.js` without modifying it. You can still use `faker.js` as you did before.
|
|
21
|
+
- **Templating**: `DataFaker` defines data structures using templates, similar to defining database table structures.
|
|
22
|
+
- **Model-oriented**: `DataFaker` encapsulates templates into models, using models as basic units and providing a model reuse mechanism to allow your data templates to be reused in multiple places.
|
|
23
|
+
- **Context mechanism**: `DataFaker` adopts a context mechanism to maintain the correlation between data.
|
|
24
|
+
- **Multi-language support**: Leveraging the underlying `faker.js`, `DataFaker` also supports more than 70 language environments.
|
|
25
|
+
- **Multi-data source**: With the help of `faker.js`'s underlying database, `DataFaker` can generate data covering 26 categories such as animals and books.
|
|
26
|
+
- **Configurable**: `DataFaker` supports personalized configuration methods.
|
|
22
27
|
|
|
23
|
-
|
|
24
|
-
- 模板化:`DataFaker`以模板的方式来定义数据结构,就像定义数据库表结构那样
|
|
25
|
-
- 面向模型:`DataFaker`将模板封装为了模型,以模型为基本单元,提供了模型复用机制,让你的数据模板可在多处复用
|
|
26
|
-
- 上下文机制:`DataFaker`采用上下文机制保持数据之间的关联性
|
|
27
|
-
- 多语言:`DataFaker`底层依托`faker.js`,其同样也支持 `70` 多种语言环境
|
|
28
|
-
- 多数据源:`DataFaker` 借助了 `faker.js` 的底层数据库,能够生成涵盖动物、书本等 `26` 类数据
|
|
29
|
-
- 可配置:`DataFaker` 支持个性化配置方式
|
|
28
|
+
# Basic Usage
|
|
30
29
|
|
|
31
|
-
|
|
30
|
+
Using `DataFaker` is very simple; you just need to:
|
|
32
31
|
|
|
33
|
-
|
|
32
|
+
1. Define a data model
|
|
33
|
+
2. Generate data
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
2. 生成数据
|
|
35
|
+
## Defining a Model - defineModel
|
|
37
36
|
|
|
38
|
-
|
|
37
|
+
The `defineModel` method is used to define a data model and accepts two parameters:
|
|
39
38
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
- 模型名称
|
|
43
|
-
- 数据模板
|
|
39
|
+
- Model name
|
|
40
|
+
- Data template
|
|
44
41
|
|
|
45
42
|
```ts
|
|
46
|
-
//
|
|
43
|
+
// Define the model
|
|
47
44
|
const userModel = defineModel('user', {
|
|
48
45
|
id: 'string.uuid',
|
|
49
46
|
name: 'person.fullName',
|
|
@@ -51,17 +48,17 @@ const userModel = defineModel('user', {
|
|
|
51
48
|
});
|
|
52
49
|
```
|
|
53
50
|
|
|
54
|
-
|
|
51
|
+
## Generating Data - fakeData
|
|
55
52
|
|
|
56
|
-
|
|
53
|
+
Use the `fakeData` function and pass in the data model to generate data matching the model template object, as shown below:
|
|
57
54
|
|
|
58
55
|
```ts
|
|
59
|
-
//
|
|
56
|
+
// Generate data
|
|
60
57
|
const data = fakeData(userModel);
|
|
61
58
|
console.log(data);
|
|
62
59
|
```
|
|
63
60
|
|
|
64
|
-
|
|
61
|
+
The generated data is as follows:
|
|
65
62
|
|
|
66
63
|
```json
|
|
67
64
|
{
|
|
@@ -71,11 +68,11 @@ console.log(data);
|
|
|
71
68
|
}
|
|
72
69
|
```
|
|
73
70
|
|
|
74
|
-
|
|
71
|
+
# Core Concepts
|
|
75
72
|
|
|
76
|
-
|
|
73
|
+
## Template Syntax
|
|
77
74
|
|
|
78
|
-
`DataFaker
|
|
75
|
+
`DataFaker` defines data structures through templates, just like defining a database table—each data structure is a `schema`.
|
|
79
76
|
|
|
80
77
|
```ts {15-20}
|
|
81
78
|
const addressModel = defineModel('address', {
|
|
@@ -85,17 +82,17 @@ const addressModel = defineModel('address', {
|
|
|
85
82
|
const userModel = defineModel('user', {
|
|
86
83
|
id: 'number.int',
|
|
87
84
|
firstName: 'person.firstName',
|
|
88
|
-
|
|
85
|
+
secondName: 'person.lastName',
|
|
89
86
|
age: ['number.int', { min: 18, max: 65 }],
|
|
90
|
-
hobby: ['helpers.arrayElements', ['
|
|
87
|
+
hobby: ['helpers.arrayElements', ['Basketball', 'Football', 'Table Tennis', 'Badminton']],
|
|
91
88
|
email: ctx => {
|
|
92
|
-
return faker.internet.email({ firstName: ctx.firstName, lastName: ctx.
|
|
89
|
+
return faker.internet.email({ firstName: ctx.firstName, lastName: ctx.secondName });
|
|
93
90
|
},
|
|
94
91
|
address: addressModel,
|
|
95
92
|
children: {
|
|
96
|
-
//
|
|
93
|
+
// Reference the model itself; in this case, you must use the model alias 'user' instead of userModel
|
|
97
94
|
refModel: 'user',
|
|
98
|
-
//
|
|
95
|
+
// Control the depth of self-referential recursion
|
|
99
96
|
deep: 3,
|
|
100
97
|
},
|
|
101
98
|
});
|
|
@@ -103,17 +100,17 @@ const userDatas = fakeData(userModel);
|
|
|
103
100
|
console.dir(userDatas, { depth: Infinity });
|
|
104
101
|
```
|
|
105
102
|
|
|
106
|
-
|
|
103
|
+
## Model Reuse
|
|
107
104
|
|
|
108
|
-
|
|
105
|
+
The `cloneModel` function allows you to clone a model, which requires two parameters:
|
|
109
106
|
|
|
110
|
-
-
|
|
111
|
-
-
|
|
107
|
+
- Parameter [1]: The alias of the new cloned model
|
|
108
|
+
- Parameter [2]: The model object to be cloned
|
|
112
109
|
|
|
113
|
-
|
|
110
|
+
For example, we take the `userModel` as a prototype to clone a student model and name its alias `studentModel`:
|
|
114
111
|
|
|
115
112
|
```ts {2,17-19}
|
|
116
|
-
//
|
|
113
|
+
// User model
|
|
117
114
|
const userModel = defineModel('user', {
|
|
118
115
|
id: 'number.int',
|
|
119
116
|
firstName: 'person.firstName',
|
|
@@ -124,17 +121,17 @@ const userModel = defineModel('user', {
|
|
|
124
121
|
},
|
|
125
122
|
children: {
|
|
126
123
|
refModel: 'user',
|
|
127
|
-
//
|
|
124
|
+
// Control the depth of self-referential recursion
|
|
128
125
|
deep: 1,
|
|
129
126
|
},
|
|
130
127
|
});
|
|
131
|
-
//
|
|
128
|
+
// Clone the student model
|
|
132
129
|
const studentModel = cloneModel('student', userModel);
|
|
133
130
|
const studentDatas = fakeData(studentModel);
|
|
134
131
|
console.dir(studentDatas, { depth: Infinity });
|
|
135
132
|
```
|
|
136
133
|
|
|
137
|
-
|
|
134
|
+
The generated data is as follows:
|
|
138
135
|
|
|
139
136
|
```json
|
|
140
137
|
{
|
|
@@ -161,16 +158,16 @@ console.dir(studentDatas, { depth: Infinity });
|
|
|
161
158
|
}
|
|
162
159
|
```
|
|
163
160
|
|
|
164
|
-
|
|
161
|
+
## Hook Functions
|
|
165
162
|
|
|
166
|
-
|
|
163
|
+
Hook functions are functions executed before, after, or during data generation. They allow you to manipulate data items and templates before, after, or during data generation, changing the way data is generated. `DataFaker` provides four types of hook functions:
|
|
167
164
|
|
|
168
|
-
-
|
|
169
|
-
-
|
|
170
|
-
-
|
|
171
|
-
-
|
|
165
|
+
- `beforeAllCbs`: Manipulate templates before data generation
|
|
166
|
+
- `afterAllCbs`: Manipulate data after data generation
|
|
167
|
+
- `beforeEachCbs`: Set templates before data item generation
|
|
168
|
+
- `afterEachCbs`: Manipulate data after data item generation
|
|
172
169
|
|
|
173
|
-
>
|
|
170
|
+
> For example, if you want to add an `id` attribute to all referenced data:
|
|
174
171
|
|
|
175
172
|
```ts
|
|
176
173
|
const addressModel = defineModel('address', {
|
|
@@ -180,7 +177,7 @@ const addressModel = defineModel('address', {
|
|
|
180
177
|
refModel: 'address',
|
|
181
178
|
},
|
|
182
179
|
});
|
|
183
|
-
//
|
|
180
|
+
// User model
|
|
184
181
|
const userModel = defineModel('user', {
|
|
185
182
|
firstName: 'person.firstName',
|
|
186
183
|
secondName: 'person.lastName',
|
|
@@ -191,7 +188,7 @@ const userDatas = fakeData(userModel, {
|
|
|
191
188
|
hooks: {
|
|
192
189
|
afterEachCbs: ctx => {
|
|
193
190
|
if (ctx.type === 'object' && ctx.value) {
|
|
194
|
-
//
|
|
191
|
+
// Add an id to all reference types
|
|
195
192
|
ctx.value['id'] = faker.string.uuid();
|
|
196
193
|
}
|
|
197
194
|
return ctx;
|
|
@@ -201,7 +198,7 @@ const userDatas = fakeData(userModel, {
|
|
|
201
198
|
console.dir(userDatas, { depth: Infinity });
|
|
202
199
|
```
|
|
203
200
|
|
|
204
|
-
|
|
201
|
+
The generated data is as follows:
|
|
205
202
|
|
|
206
203
|
```ts
|
|
207
204
|
{
|
|
@@ -222,9 +219,9 @@ console.dir(userDatas, { depth: Infinity });
|
|
|
222
219
|
}
|
|
223
220
|
```
|
|
224
221
|
|
|
225
|
-
|
|
222
|
+
## Referenced Data and Self-Reference
|
|
226
223
|
|
|
227
|
-
`DataFaker
|
|
224
|
+
`DataFaker` supports referenced data and the generation of self-referential recursive data.
|
|
228
225
|
|
|
229
226
|
```ts
|
|
230
227
|
const addressModel = defineModel('address', {
|
|
@@ -236,7 +233,7 @@ const addressModel = defineModel('address', {
|
|
|
236
233
|
deep: 1,
|
|
237
234
|
},
|
|
238
235
|
});
|
|
239
|
-
//
|
|
236
|
+
// User model
|
|
240
237
|
const userModel = defineModel('user', {
|
|
241
238
|
firstName: 'person.firstName',
|
|
242
239
|
secondName: 'person.lastName',
|
|
@@ -251,7 +248,7 @@ const userDatas = fakeData(userModel);
|
|
|
251
248
|
console.dir(userDatas, { depth: Infinity });
|
|
252
249
|
```
|
|
253
250
|
|
|
254
|
-
|
|
251
|
+
The generated data is as follows:
|
|
255
252
|
|
|
256
253
|
```json
|
|
257
254
|
{
|
|
@@ -259,11 +256,11 @@ console.dir(userDatas, { depth: Infinity });
|
|
|
259
256
|
"secondName": "Adams",
|
|
260
257
|
"age": 41,
|
|
261
258
|
"address": [
|
|
262
|
-
//
|
|
259
|
+
// First address data
|
|
263
260
|
{
|
|
264
261
|
"country": "Democratic Republic of the Congo",
|
|
265
262
|
"city": "Elisefurt",
|
|
266
|
-
//
|
|
263
|
+
// 1 level of recursion
|
|
267
264
|
"children": [
|
|
268
265
|
{
|
|
269
266
|
"country": "Nauru",
|
|
@@ -275,7 +272,7 @@ console.dir(userDatas, { depth: Infinity });
|
|
|
275
272
|
}
|
|
276
273
|
]
|
|
277
274
|
},
|
|
278
|
-
//
|
|
275
|
+
// Second address data
|
|
279
276
|
{
|
|
280
277
|
"country": "Ecuador",
|
|
281
278
|
"city": "Hamillworth",
|
|
@@ -291,7 +288,7 @@ console.dir(userDatas, { depth: Infinity });
|
|
|
291
288
|
]
|
|
292
289
|
}
|
|
293
290
|
],
|
|
294
|
-
//
|
|
291
|
+
// First-level children
|
|
295
292
|
"children": {
|
|
296
293
|
"firstName": "Lisette",
|
|
297
294
|
"secondName": "Gutmann",
|
|
@@ -326,7 +323,7 @@ console.dir(userDatas, { depth: Infinity });
|
|
|
326
323
|
]
|
|
327
324
|
}
|
|
328
325
|
],
|
|
329
|
-
//
|
|
326
|
+
// Second-level children
|
|
330
327
|
"children": {
|
|
331
328
|
"firstName": "Rex",
|
|
332
329
|
"secondName": "Farrell",
|
|
@@ -366,19 +363,20 @@ console.dir(userDatas, { depth: Infinity });
|
|
|
366
363
|
}
|
|
367
364
|
```
|
|
368
365
|
|
|
369
|
-
|
|
366
|
+
# Decorator Syntax
|
|
367
|
+
|
|
368
|
+
## Basic Usage
|
|
370
369
|
|
|
371
|
-
|
|
370
|
+
To better support TypeScript, `DataFaker` introduces decorator syntax. Essentially, decorator syntax is syntactic sugar for `defineModel`, designed to maintain compatibility between existing classes and models.
|
|
372
371
|
|
|
373
|
-
`
|
|
374
|
-
比如现在项目中本来就有`User`和`Address`两个类作为 ts 类型
|
|
372
|
+
For example, if your project already has two classes (`User` and `Address`) used as TypeScript types:
|
|
375
373
|
|
|
376
374
|
```ts
|
|
377
375
|
class Address {
|
|
378
376
|
declare city: string;
|
|
379
377
|
declare children: Address[];
|
|
380
378
|
}
|
|
381
|
-
//
|
|
379
|
+
// User class
|
|
382
380
|
class User {
|
|
383
381
|
declare id: string;
|
|
384
382
|
declare firstName: string;
|
|
@@ -390,12 +388,12 @@ class User {
|
|
|
390
388
|
}
|
|
391
389
|
```
|
|
392
390
|
|
|
393
|
-
|
|
391
|
+
To utilize these two classes instead of redefining data models with `defineModel`, you can use decorator syntax to define the existing type classes as `User` and `Address` data models:
|
|
394
392
|
|
|
395
|
-
-
|
|
396
|
-
-
|
|
393
|
+
- Use the `@DataModel` decorator to define a data model, which accepts a model alias as a parameter.
|
|
394
|
+
- Use the `@DataField` decorator to define fields, consistent with field definition in [Template Syntax](/Template-Syntax).
|
|
397
395
|
|
|
398
|
-
|
|
396
|
+
As shown below:
|
|
399
397
|
|
|
400
398
|
```ts
|
|
401
399
|
@DataModel('address')
|
|
@@ -428,7 +426,7 @@ const userDatas = fakeData('user', 2);
|
|
|
428
426
|
console.dir(userDatas, { depth: Infinity });
|
|
429
427
|
```
|
|
430
428
|
|
|
431
|
-
|
|
429
|
+
The data generation result is as follows:
|
|
432
430
|
|
|
433
431
|
```ts
|
|
434
432
|
[
|
|
@@ -467,9 +465,9 @@ console.dir(userDatas, { depth: Infinity });
|
|
|
467
465
|
];
|
|
468
466
|
```
|
|
469
467
|
|
|
470
|
-
|
|
468
|
+
## Data Model Inheritance Based on Native Classes
|
|
471
469
|
|
|
472
|
-
|
|
470
|
+
Decorator syntax enables easier model inheritance—simply follow the native inheritance approach without any additional modifications. As shown below, the `User` class inherits the `email` and `children` fields from the `Person` class data model:
|
|
473
471
|
|
|
474
472
|
```ts
|
|
475
473
|
@DataModel('person')
|
|
@@ -496,7 +494,7 @@ const userDatas = fakeData('user', 2);
|
|
|
496
494
|
console.dir(userDatas, { depth: Infinity });
|
|
497
495
|
```
|
|
498
496
|
|
|
499
|
-
|
|
497
|
+
The generated data is as follows:
|
|
500
498
|
|
|
501
499
|
```ts
|
|
502
500
|
[
|
|
@@ -531,14 +529,14 @@ console.dir(userDatas, { depth: Infinity });
|
|
|
531
529
|
];
|
|
532
530
|
```
|
|
533
531
|
|
|
534
|
-
|
|
532
|
+
# Multiple Configuration Methods
|
|
535
533
|
|
|
536
|
-
|
|
534
|
+
## Global Configuration
|
|
537
535
|
|
|
538
|
-
|
|
536
|
+
The following demonstrates defining data generation hook functions globally:
|
|
539
537
|
|
|
540
538
|
```ts
|
|
541
|
-
//
|
|
539
|
+
// Globally define the beforeAllCbs callback function
|
|
542
540
|
DataFaker.setHooks({
|
|
543
541
|
beforeAllCbs: schema => {
|
|
544
542
|
console.log(schema);
|
|
@@ -547,9 +545,9 @@ DataFaker.setHooks({
|
|
|
547
545
|
});
|
|
548
546
|
```
|
|
549
547
|
|
|
550
|
-
|
|
548
|
+
## Configuration in Templates
|
|
551
549
|
|
|
552
|
-
|
|
550
|
+
The following demonstrates defining the number of generated reference types in a template:
|
|
553
551
|
|
|
554
552
|
```ts
|
|
555
553
|
const userModel = defineModel('user', {
|
|
@@ -560,21 +558,21 @@ const userModel = defineModel('user', {
|
|
|
560
558
|
});
|
|
561
559
|
```
|
|
562
560
|
|
|
563
|
-
|
|
561
|
+
## Runtime Configuration
|
|
564
562
|
|
|
565
|
-
|
|
563
|
+
The following shows configuring the generation method of referenced data at runtime:
|
|
566
564
|
|
|
567
565
|
```ts
|
|
568
566
|
const userDatas = fakeData(userModel, {
|
|
569
567
|
refRules: {
|
|
570
|
-
// address
|
|
568
|
+
// Generate 1 piece of address reference data, and then generate 1 piece of address.children self-referential data
|
|
571
569
|
address: {
|
|
572
570
|
[COUNT]: 1,
|
|
573
571
|
children: {
|
|
574
572
|
[COUNT]: 1,
|
|
575
573
|
},
|
|
576
574
|
},
|
|
577
|
-
//
|
|
575
|
+
// The self-referential recursion depth is 1, and only 1 piece of data is generated; the address reference property is the same as above
|
|
578
576
|
children: {
|
|
579
577
|
[DEEP]: 1,
|
|
580
578
|
[COUNT]: 1,
|