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 CHANGED
@@ -1,49 +1,46 @@
1
- # Data Faker
1
+ # Getting Started
2
2
 
3
- 注意下面的仅仅是演示案例,具体详情请参见
4
- [DataFaker 国外官网](https://df-docs-lgxj441cg-bloom-lmh.vercel.app/)
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
- `DataFaker` 是一个数据生成器,底层依赖 `faker.js`,并在之上扩展了模板语法,能够帮助你快速生成各类的数据,包括引用数据和递归数据,满足你在不同场景下各类数据的生成需求。它特别适用于以下场景:
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
- [致敬 faker.js](https://faker.nodejs.cn/guide/)
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
- - 无侵入:`DataFaker` 对`faker.js`只做增强不做修改,你仍然可以像以前那样使用 `faker.js`
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
- `DataFaker`使用起来十分的简单,你只需要:
32
+ 1. Define a data model
33
+ 2. Generate data
34
34
 
35
- 1. 定义数据模型
36
- 2. 生成数据
35
+ ## Defining a Model - defineModel
37
36
 
38
- ### 定义模型-defineModel
37
+ The `defineModel` method is used to define a data model and accepts two parameters:
39
38
 
40
- `defineModel`方法用于定义数据模型,它接受两个参数:
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
- ### 生成数据-fakeData
51
+ ## Generating Data - fakeData
55
52
 
56
- 使用`fakeData`函数并传入数据模型就能生成模型模板对象的数据,如下所示:
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`通过模板来定义数据结构,就像定义一个数据库表那样,每一个数据结构就是一个`schema`。
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
- secondeName: 'person.lastName',
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.secondeName });
89
+ return faker.internet.email({ firstName: ctx.firstName, lastName: ctx.secondName });
93
90
  },
94
91
  address: addressModel,
95
92
  children: {
96
- // 引用自身,此时必须使用模型别名'user'而不能使用userModel
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
- 使用`cloneModel`函数我们能够克隆一个模型,其需要提供两个参数
105
+ The `cloneModel` function allows you to clone a model, which requires two parameters:
109
106
 
110
- - 参数 1】:克隆的新的模型别名
111
- - 参数 2】:要克隆的模型对象
107
+ - Parameter [1]: The alias of the new cloned model
108
+ - Parameter [2]: The model object to be cloned
112
109
 
113
- 比如我们以`userModel`模型为原型,克隆出了一个学生模型,并将其别名命名为`studentModel`
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
- 钩子函数就是在数据生成前后或数据生成中执行的函数,它能让你在数据生成前后或数据生成中操作数据项和模板,改变数据生成方式,`DataFaker`提供了四类钩子函数:
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
- - 数据生成前操作模板-`beforeAllCbs`
169
- - 数据生成后操作数据-`afterAllCbs`
170
- - 数据项生成前设置模板-`beforeEachCbs`
171
- - 数据项生成后操作数据-`afterEachCbs`
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
- > 比如我希望为所有引用数据添加 id`属性
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
- // 对所有引用类型添加id
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
- // 第一个address数据
259
+ // First address data
263
260
  {
264
261
  "country": "Democratic Republic of the Congo",
265
262
  "city": "Elisefurt",
266
- // 递归1
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
- // 第二个address数据
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
- // 第一层children
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
- // 第二层children
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
- `DataFaker`为了更好的支持`ts`,引入了装饰器语法,装饰器语法本质上就是`defineModel`的语法糖,它设计的初衷就是为了保持现有类和模型的共通性。
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
- 为了将这两个类利用起来,而不是重新使用`defineModel`来定义数据模型,我们可以使用装饰器语法来将现有的类型类定义为`User`和`Address`数据模型。
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
- - 使用`@DataModel`装饰器定义数据模型,它接受一个模型别名作为参数
396
- - 使用`@DataField`装饰器来定义字段,与[模板语法](/模板语法)中定义字段是一致的
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
- 装饰器语法可以更加方便的实现模型继承,只需要像原生继承那样,无需做任何改动,如下所示,`User` 类从 `Person` 类的数据模型中继承了 `email` `children` 字段:
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
- // 全局定义beforeAllCbs回调函数
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引用数据生成一个,然后其address.childern自引用数据生成一个
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
- // 自引用递归深度为1,且只生成一个,address引用属性同上
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,