@drichdev/genata 1.0.4 → 1.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/README.md +200 -197
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,17 +2,30 @@
|
|
|
2
2
|
|
|
3
3
|
A lightweight, secure, and TypeScript-first library for generating realistic fake data for testing, development, and documentation.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
* Simple and intuitive API
|
|
10
|
+
* Batch data generation
|
|
11
|
+
* Reproducible data with seed support
|
|
12
|
+
* Full TypeScript support
|
|
13
|
+
* Built-in validation and sanitization
|
|
14
|
+
* No runtime dependencies except `@faker-js/faker`
|
|
15
|
+
* 25+ data generators
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## Why Genata?
|
|
6
20
|
|
|
7
|
-
|
|
8
|
-
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
- **25+ Data Types**: From emails to credit cards
|
|
21
|
+
* Cleaner and simpler API than typical faker usage
|
|
22
|
+
* Built-in batch generation without extra tooling
|
|
23
|
+
* Consistent seeding across all generators
|
|
24
|
+
* Type-safe by default
|
|
25
|
+
|
|
26
|
+
---
|
|
14
27
|
|
|
15
|
-
##
|
|
28
|
+
## Installation
|
|
16
29
|
|
|
17
30
|
```bash
|
|
18
31
|
npm install @drichdev/genata
|
|
@@ -22,19 +35,31 @@ yarn add @drichdev/genata
|
|
|
22
35
|
pnpm add @drichdev/genata
|
|
23
36
|
```
|
|
24
37
|
|
|
25
|
-
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## Import
|
|
41
|
+
|
|
42
|
+
### ES Modules (recommended)
|
|
43
|
+
|
|
44
|
+
```javascript
|
|
45
|
+
import genata from '@drichdev/genata';
|
|
46
|
+
```
|
|
26
47
|
|
|
27
|
-
### CommonJS
|
|
48
|
+
### CommonJS
|
|
28
49
|
|
|
29
50
|
```javascript
|
|
30
51
|
const genata = require('@drichdev/genata');
|
|
52
|
+
```
|
|
31
53
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
54
|
+
### TypeScript
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
import genata, { type FieldDefinition } from '@drichdev/genata';
|
|
35
58
|
```
|
|
36
59
|
|
|
37
|
-
|
|
60
|
+
---
|
|
61
|
+
|
|
62
|
+
## Quick Start
|
|
38
63
|
|
|
39
64
|
```javascript
|
|
40
65
|
import genata from '@drichdev/genata';
|
|
@@ -44,30 +69,23 @@ const firstName = genata.firstName();
|
|
|
44
69
|
const phone = genata.phone();
|
|
45
70
|
```
|
|
46
71
|
|
|
47
|
-
|
|
72
|
+
---
|
|
48
73
|
|
|
49
|
-
|
|
50
|
-
import genata from '@drichdev/genata';
|
|
74
|
+
## Naming Convention
|
|
51
75
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
return (
|
|
58
|
-
<div>
|
|
59
|
-
<p>Email: {email}</p>
|
|
60
|
-
<p>Name: {firstName}</p>
|
|
61
|
-
<p>Phone: {phone}</p>
|
|
62
|
-
</div>
|
|
63
|
-
);
|
|
64
|
-
}
|
|
65
|
-
```
|
|
76
|
+
* Direct API uses camelCase
|
|
77
|
+
`genata.firstName()`
|
|
78
|
+
|
|
79
|
+
* Batch generation uses snake_case
|
|
80
|
+
`type: "first_name"`
|
|
66
81
|
|
|
67
|
-
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
## Generators
|
|
85
|
+
|
|
86
|
+
### Person
|
|
68
87
|
|
|
69
88
|
```javascript
|
|
70
|
-
// Person generators
|
|
71
89
|
genata.person.firstName();
|
|
72
90
|
genata.person.lastName();
|
|
73
91
|
genata.person.fullName();
|
|
@@ -75,46 +93,64 @@ genata.person.email();
|
|
|
75
93
|
genata.person.username();
|
|
76
94
|
genata.person.password();
|
|
77
95
|
genata.person.phone();
|
|
96
|
+
```
|
|
78
97
|
|
|
79
|
-
|
|
98
|
+
### Location
|
|
99
|
+
|
|
100
|
+
```javascript
|
|
80
101
|
genata.location.address();
|
|
81
102
|
genata.location.city();
|
|
82
103
|
genata.location.country();
|
|
83
104
|
genata.location.zipCode();
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Internet
|
|
84
108
|
|
|
85
|
-
|
|
109
|
+
```javascript
|
|
86
110
|
genata.internet.url();
|
|
87
111
|
genata.internet.ipv4();
|
|
88
112
|
genata.internet.ipv6();
|
|
89
113
|
genata.internet.creditCard();
|
|
114
|
+
```
|
|
90
115
|
|
|
91
|
-
|
|
116
|
+
### Company
|
|
117
|
+
|
|
118
|
+
```javascript
|
|
92
119
|
genata.company.company();
|
|
93
120
|
genata.company.jobTitle();
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### Date
|
|
94
124
|
|
|
95
|
-
|
|
125
|
+
```javascript
|
|
96
126
|
genata.date.date();
|
|
97
127
|
genata.date.dateTime();
|
|
98
128
|
genata.date.futureDate();
|
|
129
|
+
```
|
|
99
130
|
|
|
100
|
-
|
|
131
|
+
### Data Types
|
|
132
|
+
|
|
133
|
+
```javascript
|
|
101
134
|
genata.datatype.uuid();
|
|
102
135
|
genata.datatype.boolean();
|
|
103
136
|
genata.datatype.integer({ min: 0, max: 100 });
|
|
104
137
|
genata.datatype.float({ min: 0, max: 100, decimals: 2 });
|
|
105
138
|
genata.datatype.color();
|
|
106
139
|
genata.datatype.hex({ length: 16 });
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Text
|
|
107
143
|
|
|
108
|
-
|
|
144
|
+
```javascript
|
|
109
145
|
genata.text.sentence();
|
|
110
146
|
genata.text.paragraph({ sentences: 5 });
|
|
111
147
|
genata.text.word();
|
|
112
148
|
genata.text.slug();
|
|
113
149
|
```
|
|
114
150
|
|
|
115
|
-
|
|
151
|
+
---
|
|
116
152
|
|
|
117
|
-
|
|
153
|
+
## Batch Generation
|
|
118
154
|
|
|
119
155
|
```javascript
|
|
120
156
|
const fields = [
|
|
@@ -126,44 +162,22 @@ const fields = [
|
|
|
126
162
|
{ name: "createdAt", type: "datetime" },
|
|
127
163
|
];
|
|
128
164
|
|
|
129
|
-
//
|
|
165
|
+
// Default (100 records)
|
|
130
166
|
const users = genata.generateBatch(fields);
|
|
131
167
|
|
|
132
|
-
//
|
|
168
|
+
// Custom count
|
|
133
169
|
const largeDataset = genata.generateBatch(fields, { count: 1000 });
|
|
134
170
|
|
|
135
|
-
// With progress
|
|
171
|
+
// With progress
|
|
136
172
|
const data = genata.generateBatchWithProgress(fields, {
|
|
137
173
|
count: 10000,
|
|
138
174
|
onProgress: (progress) => console.log(`${progress}% complete`),
|
|
139
175
|
});
|
|
140
176
|
```
|
|
141
177
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
```javascript
|
|
145
|
-
// Set a seed for reproducible results
|
|
146
|
-
genata.setSeed(12345);
|
|
147
|
-
|
|
148
|
-
const email1 = genata.email(); // Always the same value
|
|
149
|
-
const email2 = genata.email(); // Always the same value
|
|
150
|
-
|
|
151
|
-
// Reset to random generation
|
|
152
|
-
genata.resetSeed();
|
|
153
|
-
```
|
|
154
|
-
|
|
155
|
-
## 🔒 Security Features
|
|
156
|
-
|
|
157
|
-
- **Input Validation**: All options are validated before use
|
|
158
|
-
- **Sanitization**: User inputs are sanitized to prevent injection
|
|
159
|
-
- **Type Safety**: TypeScript strict mode ensures type correctness
|
|
160
|
-
- **Error Handling**: Proper error messages for invalid inputs
|
|
161
|
-
- **No Shell Execution**: No dynamic code execution
|
|
162
|
-
- **Immutable Defaults**: Safe default values that can't be modified
|
|
163
|
-
|
|
164
|
-
## 📋 Available Field Types
|
|
178
|
+
---
|
|
165
179
|
|
|
166
|
-
|
|
180
|
+
## Supported Field Types
|
|
167
181
|
|
|
168
182
|
```
|
|
169
183
|
Person: first_name, last_name, full_name, email, username, password, phone
|
|
@@ -175,191 +189,180 @@ Data Types: uuid, boolean, int, float, number, zero_one, id_increment, color, he
|
|
|
175
189
|
Text: sentence, paragraph, word, slug
|
|
176
190
|
```
|
|
177
191
|
|
|
178
|
-
|
|
192
|
+
---
|
|
193
|
+
|
|
194
|
+
## Seeding (Reproducible Data)
|
|
195
|
+
|
|
196
|
+
```javascript
|
|
197
|
+
genata.setSeed(12345);
|
|
198
|
+
|
|
199
|
+
const email1 = genata.email();
|
|
200
|
+
const email2 = genata.email();
|
|
201
|
+
|
|
202
|
+
// Reset randomness
|
|
203
|
+
genata.resetSeed();
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
---
|
|
179
207
|
|
|
180
|
-
|
|
208
|
+
## API Reference
|
|
181
209
|
|
|
182
|
-
|
|
210
|
+
### `genata.email()`
|
|
183
211
|
|
|
184
|
-
Generate a random email
|
|
212
|
+
Generate a random email.
|
|
185
213
|
|
|
186
214
|
```javascript
|
|
187
215
|
const email = genata.email();
|
|
188
|
-
// "alice.johnson@example.com"
|
|
189
216
|
```
|
|
190
217
|
|
|
191
|
-
|
|
218
|
+
---
|
|
219
|
+
|
|
220
|
+
### `genata.generateBatch(fields, options?)`
|
|
192
221
|
|
|
193
222
|
Generate multiple records.
|
|
194
223
|
|
|
195
224
|
**Parameters:**
|
|
196
225
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
- `count` (number): Number of records to generate (default: 100)
|
|
200
|
-
- `seed` (number): Seed for reproducible data
|
|
201
|
-
- `locale` (string): Locale for faker
|
|
226
|
+
* `fields`: Array of field definitions
|
|
227
|
+
* `options`:
|
|
202
228
|
|
|
203
|
-
|
|
229
|
+
* `count` (default: 100)
|
|
230
|
+
* `seed`
|
|
231
|
+
* `locale`
|
|
204
232
|
|
|
205
|
-
|
|
233
|
+
**Returns:** Array of objects
|
|
206
234
|
|
|
207
|
-
|
|
235
|
+
---
|
|
208
236
|
|
|
209
|
-
|
|
237
|
+
### `genata.setSeed(seed)`
|
|
210
238
|
|
|
211
|
-
|
|
239
|
+
Set a seed for reproducible results.
|
|
212
240
|
|
|
213
|
-
|
|
241
|
+
---
|
|
214
242
|
|
|
215
|
-
|
|
243
|
+
### `genata.resetSeed()`
|
|
216
244
|
|
|
217
|
-
|
|
245
|
+
Reset random generation.
|
|
218
246
|
|
|
219
|
-
|
|
247
|
+
---
|
|
220
248
|
|
|
221
|
-
|
|
222
|
-
- **types**: TypeScript type definitions
|
|
223
|
-
- **validators**: Input validation and sanitization
|
|
224
|
-
- **utils**: Helper functions (faker initialization)
|
|
249
|
+
## Examples
|
|
225
250
|
|
|
226
|
-
|
|
251
|
+
### Generate Users
|
|
227
252
|
|
|
228
|
-
```
|
|
229
|
-
|
|
253
|
+
```javascript
|
|
254
|
+
const users = genata.generateBatch(
|
|
255
|
+
[
|
|
256
|
+
{ name: "id", type: "id_increment" },
|
|
257
|
+
{ name: "email", type: "email" },
|
|
258
|
+
{ name: "firstName", type: "first_name" },
|
|
259
|
+
{ name: "lastName", type: "last_name" },
|
|
260
|
+
{ name: "phone", type: "phone" },
|
|
261
|
+
{ name: "company", type: "company" },
|
|
262
|
+
{ name: "jobTitle", type: "job_title" },
|
|
263
|
+
{ name: "createdAt", type: "datetime" },
|
|
264
|
+
],
|
|
265
|
+
{ count: 50 }
|
|
266
|
+
);
|
|
230
267
|
```
|
|
231
268
|
|
|
232
|
-
|
|
269
|
+
---
|
|
233
270
|
|
|
234
|
-
|
|
271
|
+
### Generate Products
|
|
235
272
|
|
|
236
|
-
|
|
273
|
+
```javascript
|
|
274
|
+
const products = genata.generateBatch(
|
|
275
|
+
[
|
|
276
|
+
{ name: "id", type: "uuid" },
|
|
277
|
+
{ name: "name", type: "sentence" },
|
|
278
|
+
{ name: "description", type: "paragraph", options: { sentences: 3 } },
|
|
279
|
+
{ name: "price", type: "float", options: { min: 10, max: 1000, decimals: 2 } },
|
|
280
|
+
{ name: "stock", type: "int", options: { min: 0, max: 1000 } },
|
|
281
|
+
{ name: "color", type: "color" },
|
|
282
|
+
],
|
|
283
|
+
{ count: 20 }
|
|
284
|
+
);
|
|
285
|
+
```
|
|
237
286
|
|
|
238
|
-
|
|
287
|
+
---
|
|
239
288
|
|
|
240
|
-
|
|
241
|
-
- Security-first approach
|
|
242
|
-
- Input validation for all public methods
|
|
243
|
-
- Comprehensive error messages
|
|
289
|
+
### Reproducible Tests
|
|
244
290
|
|
|
245
|
-
|
|
291
|
+
```javascript
|
|
292
|
+
beforeEach(() => {
|
|
293
|
+
genata.setSeed(42);
|
|
294
|
+
});
|
|
246
295
|
|
|
247
|
-
|
|
296
|
+
const user1 = { email: genata.email() };
|
|
248
297
|
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
const generateTestUsers = (count = 10) => {
|
|
253
|
-
return genata.generateBatch(
|
|
254
|
-
[
|
|
255
|
-
{ name: "id", type: "id_increment" },
|
|
256
|
-
{ name: "email", type: "email" },
|
|
257
|
-
{ name: "firstName", type: "first_name" },
|
|
258
|
-
{ name: "lastName", type: "last_name" },
|
|
259
|
-
{ name: "phone", type: "phone" },
|
|
260
|
-
{ name: "company", type: "company" },
|
|
261
|
-
{ name: "jobTitle", type: "job_title" },
|
|
262
|
-
{ name: "createdAt", type: "datetime" },
|
|
263
|
-
],
|
|
264
|
-
{ count }
|
|
265
|
-
);
|
|
266
|
-
};
|
|
267
|
-
|
|
268
|
-
const users = generateTestUsers(50);
|
|
269
|
-
console.log(users);
|
|
298
|
+
genata.setSeed(42);
|
|
299
|
+
const user2 = { email: genata.email() };
|
|
270
300
|
```
|
|
271
301
|
|
|
272
|
-
|
|
302
|
+
---
|
|
273
303
|
|
|
274
|
-
|
|
275
|
-
const generateProducts = (count = 20) => {
|
|
276
|
-
return genata.generateBatch(
|
|
277
|
-
[
|
|
278
|
-
{ name: "id", type: "uuid" },
|
|
279
|
-
{ name: "name", type: "sentence" },
|
|
280
|
-
{ name: "description", type: "paragraph", options: { sentences: 3 } },
|
|
281
|
-
{ name: "price", type: "float", options: { min: 10, max: 1000, decimals: 2 } },
|
|
282
|
-
{ name: "stock", type: "int", options: { min: 0, max: 1000 } },
|
|
283
|
-
{ name: "color", type: "color" },
|
|
284
|
-
],
|
|
285
|
-
{ count }
|
|
286
|
-
);
|
|
287
|
-
};
|
|
288
|
-
```
|
|
304
|
+
## Security
|
|
289
305
|
|
|
290
|
-
|
|
306
|
+
* Input validation on all public methods
|
|
307
|
+
* Sanitization of user-provided options
|
|
308
|
+
* No dynamic code execution
|
|
309
|
+
* Safe default values
|
|
291
310
|
|
|
292
|
-
|
|
293
|
-
describe("User Service", () => {
|
|
294
|
-
beforeEach(() => {
|
|
295
|
-
genata.setSeed(42);
|
|
296
|
-
});
|
|
297
|
-
|
|
298
|
-
test("should process user data consistently", () => {
|
|
299
|
-
const user1 = { email: genata.email() };
|
|
300
|
-
|
|
301
|
-
genata.setSeed(42);
|
|
302
|
-
const user2 = { email: genata.email() };
|
|
303
|
-
|
|
304
|
-
expect(user1.email).toBe(user2.email);
|
|
305
|
-
});
|
|
306
|
-
});
|
|
307
|
-
```
|
|
311
|
+
---
|
|
308
312
|
|
|
309
|
-
##
|
|
313
|
+
## Architecture
|
|
310
314
|
|
|
311
|
-
|
|
315
|
+
* `generators`: data generation logic
|
|
316
|
+
* `types`: TypeScript definitions
|
|
317
|
+
* `validators`: input validation
|
|
318
|
+
* `utils`: internal helpers
|
|
312
319
|
|
|
313
|
-
|
|
320
|
+
---
|
|
314
321
|
|
|
315
|
-
|
|
322
|
+
## Testing
|
|
316
323
|
|
|
317
|
-
|
|
324
|
+
```bash
|
|
325
|
+
npm test
|
|
326
|
+
```
|
|
318
327
|
|
|
319
|
-
|
|
328
|
+
---
|
|
320
329
|
|
|
321
|
-
|
|
330
|
+
## Troubleshooting
|
|
322
331
|
|
|
323
|
-
|
|
324
|
-
- Using `generateBatchWithProgress` to show progress
|
|
325
|
-
- Running in a worker thread if in browser
|
|
332
|
+
### Invalid field type
|
|
326
333
|
|
|
327
|
-
|
|
334
|
+
Ensure the field type exists in the supported list.
|
|
328
335
|
|
|
329
|
-
|
|
336
|
+
### Seed not working
|
|
330
337
|
|
|
331
|
-
|
|
338
|
+
Set the seed before generating data and avoid mixing seeded and non-seeded calls.
|
|
332
339
|
|
|
333
|
-
###
|
|
334
|
-
```javascript
|
|
335
|
-
const genata = require('@drichdev/genata');
|
|
336
|
-
genata.email();
|
|
337
|
-
```
|
|
340
|
+
### Performance issues
|
|
338
341
|
|
|
339
|
-
|
|
340
|
-
```javascript
|
|
341
|
-
import genata from '@drichdev/genata';
|
|
342
|
-
genata.email();
|
|
343
|
-
```
|
|
342
|
+
For very large datasets:
|
|
344
343
|
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
```
|
|
344
|
+
* Generate in chunks
|
|
345
|
+
* Use progress tracking
|
|
346
|
+
* Consider worker threads in browser environments
|
|
349
347
|
|
|
350
|
-
|
|
351
|
-
```typescript
|
|
352
|
-
import genata from '@drichdev/genata';
|
|
348
|
+
---
|
|
353
349
|
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
350
|
+
## Contributing
|
|
351
|
+
|
|
352
|
+
* TypeScript only
|
|
353
|
+
* Validate all inputs
|
|
354
|
+
* Maintain strong error handling
|
|
355
|
+
* Follow a security-first approach
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
---
|
|
359
|
+
|
|
360
|
+
## Documentation
|
|
359
361
|
|
|
360
|
-
|
|
362
|
+
See [USAGE.md](./USAGE.md) for advanced usage.
|
|
361
363
|
|
|
362
364
|
---
|
|
363
365
|
|
|
364
|
-
|
|
366
|
+
## Author
|
|
365
367
|
|
|
368
|
+
Created by Drichdev
|