@strato-admin/faker-ecommerce 0.1.0
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/LICENSE +21 -0
- package/README.md +47 -0
- package/dist/dataProvider.d.ts +2 -0
- package/dist/dataProvider.js +6 -0
- package/dist/generate.d.ts +8 -0
- package/dist/generate.js +148 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/types.d.ts +69 -0
- package/dist/types.js +1 -0
- package/package.json +53 -0
- package/src/dataProvider.ts +11 -0
- package/src/generate.test.ts +44 -0
- package/src/generate.ts +164 -0
- package/src/index.ts +3 -0
- package/src/types.ts +75 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Vadim Gubergrits
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# @strato-admin/faker-ecommerce
|
|
2
|
+
|
|
3
|
+
This package provides mock data for an E-commerce domain using `@faker-js/faker`.
|
|
4
|
+
|
|
5
|
+
## Resources
|
|
6
|
+
|
|
7
|
+
- `categories`
|
|
8
|
+
- `customers`
|
|
9
|
+
- `products`
|
|
10
|
+
- `reviews`
|
|
11
|
+
- `orders` (including `basket` as `OrderItem[]`)
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
### As a Data Provider
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { Admin, Resource } from 'react-admin';
|
|
19
|
+
import { dataProvider } from '@strato-admin/faker-ecommerce';
|
|
20
|
+
|
|
21
|
+
const App = () => (
|
|
22
|
+
<Admin dataProvider={dataProvider}>
|
|
23
|
+
<Resource name="products" list={ProductList} />
|
|
24
|
+
<Resource name="customers" list={CustomerList} />
|
|
25
|
+
</Admin>
|
|
26
|
+
);
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### Raw Data Generation
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
import { generateEcommerceData } from '@strato-admin/faker-ecommerce';
|
|
33
|
+
|
|
34
|
+
const data = generateEcommerceData();
|
|
35
|
+
console.log(data.products.length); // 50
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Types
|
|
39
|
+
|
|
40
|
+
The package exports interfaces for all resources:
|
|
41
|
+
|
|
42
|
+
- `Category`
|
|
43
|
+
- `Customer`
|
|
44
|
+
- `Product`
|
|
45
|
+
- `Review`
|
|
46
|
+
- `Order`
|
|
47
|
+
- `OrderItem`
|
package/dist/generate.js
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { faker } from '@faker-js/faker';
|
|
2
|
+
export const generateEcommerceData = () => {
|
|
3
|
+
faker.seed(123);
|
|
4
|
+
// Constants
|
|
5
|
+
const CATEGORIES = [
|
|
6
|
+
'animals',
|
|
7
|
+
'beard',
|
|
8
|
+
'business',
|
|
9
|
+
'cars',
|
|
10
|
+
'city',
|
|
11
|
+
'flowers',
|
|
12
|
+
'food',
|
|
13
|
+
'nature',
|
|
14
|
+
'sports',
|
|
15
|
+
'tech',
|
|
16
|
+
'travel',
|
|
17
|
+
'water',
|
|
18
|
+
];
|
|
19
|
+
// 1. Generate Categories
|
|
20
|
+
const categories = CATEGORIES.map((name, id) => ({
|
|
21
|
+
id,
|
|
22
|
+
name,
|
|
23
|
+
}));
|
|
24
|
+
// 2. Generate Customers
|
|
25
|
+
const customers = Array.from({ length: 100 }, (_, id) => {
|
|
26
|
+
const first_name = faker.person.firstName();
|
|
27
|
+
const last_name = faker.person.lastName();
|
|
28
|
+
const email = faker.internet.email({ firstName: first_name, lastName: last_name });
|
|
29
|
+
const birthday = faker.date.birthdate({ min: 18, max: 80, mode: 'age' });
|
|
30
|
+
const first_seen = faker.date.past({ years: 2 });
|
|
31
|
+
const last_seen = faker.date.between({ from: first_seen, to: new Date() });
|
|
32
|
+
const has_ordered = faker.datatype.boolean({ probability: 0.8 });
|
|
33
|
+
const has_newsletter = faker.datatype.boolean({ probability: 0.3 });
|
|
34
|
+
const groups = faker.helpers.arrayElements(['collector', 'ordered_once', 'regular', 'reviewer'], { min: 0, max: 2 });
|
|
35
|
+
return {
|
|
36
|
+
id,
|
|
37
|
+
first_name,
|
|
38
|
+
last_name,
|
|
39
|
+
email,
|
|
40
|
+
address: faker.location.streetAddress(),
|
|
41
|
+
city: faker.location.city(),
|
|
42
|
+
zipcode: faker.location.zipCode(),
|
|
43
|
+
avatar: faker.image.avatar(),
|
|
44
|
+
birthday,
|
|
45
|
+
first_seen,
|
|
46
|
+
last_seen,
|
|
47
|
+
has_ordered,
|
|
48
|
+
has_newsletter,
|
|
49
|
+
groups,
|
|
50
|
+
nb_commands: 0,
|
|
51
|
+
total_spent: 0,
|
|
52
|
+
};
|
|
53
|
+
});
|
|
54
|
+
// 3. Generate Products
|
|
55
|
+
const products = Array.from({ length: 50 }, (_, id) => {
|
|
56
|
+
const category = faker.helpers.arrayElement(categories);
|
|
57
|
+
const width = faker.number.float({ min: 10, max: 100, fractionDigits: 2 });
|
|
58
|
+
const height = faker.number.float({ min: 10, max: 100, fractionDigits: 2 });
|
|
59
|
+
return {
|
|
60
|
+
id,
|
|
61
|
+
category_id: category.id,
|
|
62
|
+
reference: faker.commerce.isbn(),
|
|
63
|
+
name: faker.commerce.productName(),
|
|
64
|
+
width,
|
|
65
|
+
height,
|
|
66
|
+
price: faker.number.float({ min: 1, max: 500, fractionDigits: 2 }),
|
|
67
|
+
thumbnail: faker.image.urlLoremFlickr({ category: category.name, width: 120, height: 120 }),
|
|
68
|
+
image: faker.image.urlLoremFlickr({ category: category.name, width: 800, height: 600 }),
|
|
69
|
+
description: faker.commerce.productDescription(),
|
|
70
|
+
stock: faker.number.int({ min: 0, max: 250 }),
|
|
71
|
+
sales: 0,
|
|
72
|
+
rating: faker.number.float({ min: 1, max: 5, fractionDigits: 1 }),
|
|
73
|
+
};
|
|
74
|
+
});
|
|
75
|
+
// 4. Generate Reviews
|
|
76
|
+
const reviews = Array.from({ length: 200 }, (_, id) => {
|
|
77
|
+
const customer = faker.helpers.arrayElement(customers);
|
|
78
|
+
const product = faker.helpers.arrayElement(products);
|
|
79
|
+
const date = faker.date.past({ years: 1 });
|
|
80
|
+
const status = faker.helpers.arrayElement(['accepted', 'pending', 'rejected']);
|
|
81
|
+
return {
|
|
82
|
+
id,
|
|
83
|
+
date,
|
|
84
|
+
status: status,
|
|
85
|
+
customer_id: customer.id,
|
|
86
|
+
product_id: product.id,
|
|
87
|
+
rating: faker.number.int({ min: 1, max: 5 }),
|
|
88
|
+
comment: faker.lorem.paragraph(),
|
|
89
|
+
};
|
|
90
|
+
});
|
|
91
|
+
// 5. Generate Orders
|
|
92
|
+
const orders = Array.from({ length: 300 }, (_, id) => {
|
|
93
|
+
const customer = faker.helpers.arrayElement(customers.filter(c => c.has_ordered));
|
|
94
|
+
const date = faker.date.between({ from: customer.first_seen, to: customer.last_seen });
|
|
95
|
+
const nbItems = faker.number.int({ min: 1, max: 5 });
|
|
96
|
+
const basket = Array.from({ length: nbItems }, (_, itemId) => {
|
|
97
|
+
const product = faker.helpers.arrayElement(products);
|
|
98
|
+
return {
|
|
99
|
+
id: id * 10 + itemId,
|
|
100
|
+
order_id: id,
|
|
101
|
+
product_id: product.id,
|
|
102
|
+
quantity: faker.number.int({ min: 1, max: 10 }),
|
|
103
|
+
unit_price: product.price,
|
|
104
|
+
};
|
|
105
|
+
});
|
|
106
|
+
const total_ex_taxes = basket.reduce((sum, item) => sum + item.quantity * item.unit_price, 0);
|
|
107
|
+
const delivery_fees = faker.number.float({ min: 0, max: 20, fractionDigits: 2 });
|
|
108
|
+
const tax_rate = 0.2;
|
|
109
|
+
const taxes = (total_ex_taxes + delivery_fees) * tax_rate;
|
|
110
|
+
const total = total_ex_taxes + delivery_fees + taxes;
|
|
111
|
+
const status = faker.helpers.arrayElement(['ordered', 'delivered', 'cancelled', 'unknown']);
|
|
112
|
+
const returned = status === 'delivered' && faker.datatype.boolean({ probability: 0.1 });
|
|
113
|
+
// Update customer stats
|
|
114
|
+
customer.nb_commands++;
|
|
115
|
+
customer.total_spent += total;
|
|
116
|
+
if (!customer.latest_purchase || date > customer.latest_purchase) {
|
|
117
|
+
customer.latest_purchase = date;
|
|
118
|
+
}
|
|
119
|
+
// Update product stats
|
|
120
|
+
basket.forEach(item => {
|
|
121
|
+
const product = products.find(p => p.id === item.product_id);
|
|
122
|
+
if (product) {
|
|
123
|
+
product.sales += item.quantity;
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
return {
|
|
127
|
+
id,
|
|
128
|
+
reference: faker.string.alphanumeric({ length: 6, casing: 'upper' }),
|
|
129
|
+
date,
|
|
130
|
+
customer_id: customer.id,
|
|
131
|
+
basket,
|
|
132
|
+
total_ex_taxes,
|
|
133
|
+
delivery_fees,
|
|
134
|
+
tax_rate,
|
|
135
|
+
taxes,
|
|
136
|
+
total,
|
|
137
|
+
status,
|
|
138
|
+
returned,
|
|
139
|
+
};
|
|
140
|
+
});
|
|
141
|
+
return {
|
|
142
|
+
categories,
|
|
143
|
+
customers,
|
|
144
|
+
products,
|
|
145
|
+
reviews,
|
|
146
|
+
orders,
|
|
147
|
+
};
|
|
148
|
+
};
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
export interface Customer {
|
|
2
|
+
id: number;
|
|
3
|
+
first_name: string;
|
|
4
|
+
last_name: string;
|
|
5
|
+
email: string;
|
|
6
|
+
address: string;
|
|
7
|
+
city: string;
|
|
8
|
+
zipcode: string;
|
|
9
|
+
avatar: string;
|
|
10
|
+
birthday: Date;
|
|
11
|
+
first_seen: Date;
|
|
12
|
+
last_seen: Date;
|
|
13
|
+
has_ordered: boolean;
|
|
14
|
+
latest_purchase?: Date;
|
|
15
|
+
has_newsletter: boolean;
|
|
16
|
+
groups: string[];
|
|
17
|
+
nb_commands: number;
|
|
18
|
+
total_spent: number;
|
|
19
|
+
}
|
|
20
|
+
export interface Category {
|
|
21
|
+
id: number;
|
|
22
|
+
name: string;
|
|
23
|
+
}
|
|
24
|
+
export interface Product {
|
|
25
|
+
id: number;
|
|
26
|
+
category_id: number;
|
|
27
|
+
reference: string;
|
|
28
|
+
name: string;
|
|
29
|
+
width: number;
|
|
30
|
+
height: number;
|
|
31
|
+
price: number;
|
|
32
|
+
thumbnail: string;
|
|
33
|
+
image: string;
|
|
34
|
+
description: string;
|
|
35
|
+
stock: number;
|
|
36
|
+
sales: number;
|
|
37
|
+
rating: number;
|
|
38
|
+
}
|
|
39
|
+
export interface OrderItem {
|
|
40
|
+
id: number;
|
|
41
|
+
order_id: number;
|
|
42
|
+
product_id: number;
|
|
43
|
+
quantity: number;
|
|
44
|
+
unit_price: number;
|
|
45
|
+
}
|
|
46
|
+
export type OrderStatus = 'ordered' | 'delivered' | 'cancelled' | 'unknown';
|
|
47
|
+
export interface Order {
|
|
48
|
+
id: number;
|
|
49
|
+
reference: string;
|
|
50
|
+
date: Date;
|
|
51
|
+
customer_id: number;
|
|
52
|
+
basket: OrderItem[];
|
|
53
|
+
total_ex_taxes: number;
|
|
54
|
+
delivery_fees: number;
|
|
55
|
+
tax_rate: number;
|
|
56
|
+
taxes: number;
|
|
57
|
+
total: number;
|
|
58
|
+
status: OrderStatus;
|
|
59
|
+
returned: boolean;
|
|
60
|
+
}
|
|
61
|
+
export interface Review {
|
|
62
|
+
id: number;
|
|
63
|
+
date: Date;
|
|
64
|
+
status: 'accepted' | 'pending' | 'rejected';
|
|
65
|
+
customer_id: number;
|
|
66
|
+
product_id: number;
|
|
67
|
+
rating: number;
|
|
68
|
+
comment: string;
|
|
69
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@strato-admin/faker-ecommerce",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Strato Admin Faker Ecommerce - Mock data for E-commerce domain using faker",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"default": "dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist",
|
|
15
|
+
"src",
|
|
16
|
+
"README.md",
|
|
17
|
+
"LICENSE"
|
|
18
|
+
],
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"strato",
|
|
24
|
+
"admin",
|
|
25
|
+
"react-admin",
|
|
26
|
+
"faker",
|
|
27
|
+
"ecommerce"
|
|
28
|
+
],
|
|
29
|
+
"author": "Vadim Gubergrits <vadim.gubergrits@gmail.com>",
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "https://github.com/vgrits/strato-admin.git",
|
|
34
|
+
"directory": "packages/strato-faker-ecommerce"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@faker-js/faker": "^9.5.0",
|
|
38
|
+
"ra-data-fakerest": "^5.14.3"
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"@strato-admin/ra-core": "0.1.0"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@types/node": "^22.13.10",
|
|
45
|
+
"typescript": "^5.1.3",
|
|
46
|
+
"vitest": "^4.0.18",
|
|
47
|
+
"@strato-admin/ra-core": "0.1.0"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"build": "tsc -p tsconfig.build.json",
|
|
51
|
+
"test": "vitest run"
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import fakeRestDataProvider from 'ra-data-fakerest';
|
|
2
|
+
import { DataProvider } from '@strato-admin/ra-core';
|
|
3
|
+
import { generateEcommerceData } from './generate';
|
|
4
|
+
|
|
5
|
+
const data = generateEcommerceData();
|
|
6
|
+
|
|
7
|
+
export const dataProvider: DataProvider = fakeRestDataProvider(
|
|
8
|
+
data,
|
|
9
|
+
true, // logging
|
|
10
|
+
500 // simulate network delay
|
|
11
|
+
);
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { generateEcommerceData } from './generate';
|
|
3
|
+
|
|
4
|
+
describe('generateEcommerceData', () => {
|
|
5
|
+
it('should generate ecommerce data with correct structure', () => {
|
|
6
|
+
const data = generateEcommerceData();
|
|
7
|
+
|
|
8
|
+
expect(data).toHaveProperty('categories');
|
|
9
|
+
expect(data).toHaveProperty('customers');
|
|
10
|
+
expect(data).toHaveProperty('products');
|
|
11
|
+
expect(data).toHaveProperty('reviews');
|
|
12
|
+
expect(data).toHaveProperty('orders');
|
|
13
|
+
|
|
14
|
+
expect(data.categories.length).toBeGreaterThan(0);
|
|
15
|
+
expect(data.customers.length).toBe(100);
|
|
16
|
+
expect(data.products.length).toBe(50);
|
|
17
|
+
expect(data.reviews.length).toBe(200);
|
|
18
|
+
expect(data.orders.length).toBe(300);
|
|
19
|
+
|
|
20
|
+
// Check if products have a name
|
|
21
|
+
data.products.forEach(product => {
|
|
22
|
+
expect(product.name).toBeDefined();
|
|
23
|
+
expect(typeof product.name).toBe('string');
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('should have valid relationships', () => {
|
|
28
|
+
const data = generateEcommerceData();
|
|
29
|
+
|
|
30
|
+
// Check orders reference existing customers
|
|
31
|
+
data.orders.forEach(order => {
|
|
32
|
+
const customer = data.customers.find(c => c.id === order.customer_id);
|
|
33
|
+
expect(customer).toBeDefined();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// Check reviews reference existing products and customers
|
|
37
|
+
data.reviews.forEach(review => {
|
|
38
|
+
const product = data.products.find(p => p.id === review.product_id);
|
|
39
|
+
const customer = data.customers.find(c => c.id === review.customer_id);
|
|
40
|
+
expect(product).toBeDefined();
|
|
41
|
+
expect(customer).toBeDefined();
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
});
|
package/src/generate.ts
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { faker } from '@faker-js/faker';
|
|
2
|
+
import { Category, Customer, Order, OrderItem, OrderStatus, Product, Review } from './types';
|
|
3
|
+
|
|
4
|
+
export const generateEcommerceData = () => {
|
|
5
|
+
faker.seed(123);
|
|
6
|
+
// Constants
|
|
7
|
+
const CATEGORIES = [
|
|
8
|
+
'animals',
|
|
9
|
+
'beard',
|
|
10
|
+
'business',
|
|
11
|
+
'cars',
|
|
12
|
+
'city',
|
|
13
|
+
'flowers',
|
|
14
|
+
'food',
|
|
15
|
+
'nature',
|
|
16
|
+
'sports',
|
|
17
|
+
'tech',
|
|
18
|
+
'travel',
|
|
19
|
+
'water',
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
// 1. Generate Categories
|
|
23
|
+
const categories: Category[] = CATEGORIES.map((name, id) => ({
|
|
24
|
+
id,
|
|
25
|
+
name,
|
|
26
|
+
}));
|
|
27
|
+
|
|
28
|
+
// 2. Generate Customers
|
|
29
|
+
const customers: Customer[] = Array.from({ length: 100 }, (_, id) => {
|
|
30
|
+
const first_name = faker.person.firstName();
|
|
31
|
+
const last_name = faker.person.lastName();
|
|
32
|
+
const email = faker.internet.email({ firstName: first_name, lastName: last_name });
|
|
33
|
+
const birthday = faker.date.birthdate({ min: 18, max: 80, mode: 'age' });
|
|
34
|
+
const first_seen = faker.date.past({ years: 2 });
|
|
35
|
+
const last_seen = faker.date.between({ from: first_seen, to: new Date() });
|
|
36
|
+
const has_ordered = faker.datatype.boolean({ probability: 0.8 });
|
|
37
|
+
const has_newsletter = faker.datatype.boolean({ probability: 0.3 });
|
|
38
|
+
const groups = faker.helpers.arrayElements(['collector', 'ordered_once', 'regular', 'reviewer'], { min: 0, max: 2 });
|
|
39
|
+
|
|
40
|
+
return {
|
|
41
|
+
id,
|
|
42
|
+
first_name,
|
|
43
|
+
last_name,
|
|
44
|
+
email,
|
|
45
|
+
address: faker.location.streetAddress(),
|
|
46
|
+
city: faker.location.city(),
|
|
47
|
+
zipcode: faker.location.zipCode(),
|
|
48
|
+
avatar: faker.image.avatar(),
|
|
49
|
+
birthday,
|
|
50
|
+
first_seen,
|
|
51
|
+
last_seen,
|
|
52
|
+
has_ordered,
|
|
53
|
+
has_newsletter,
|
|
54
|
+
groups,
|
|
55
|
+
nb_commands: 0,
|
|
56
|
+
total_spent: 0,
|
|
57
|
+
};
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// 3. Generate Products
|
|
61
|
+
const products: Product[] = Array.from({ length: 50 }, (_, id) => {
|
|
62
|
+
const category = faker.helpers.arrayElement(categories);
|
|
63
|
+
const width = faker.number.float({ min: 10, max: 100, fractionDigits: 2 });
|
|
64
|
+
const height = faker.number.float({ min: 10, max: 100, fractionDigits: 2 });
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
id,
|
|
68
|
+
category_id: category.id,
|
|
69
|
+
reference: faker.commerce.isbn(),
|
|
70
|
+
name: faker.commerce.productName(),
|
|
71
|
+
width,
|
|
72
|
+
height,
|
|
73
|
+
price: faker.number.float({ min: 1, max: 500, fractionDigits: 2 }),
|
|
74
|
+
thumbnail: faker.image.urlLoremFlickr({ category: category.name, width: 120, height: 120 }),
|
|
75
|
+
image: faker.image.urlLoremFlickr({ category: category.name, width: 800, height: 600 }),
|
|
76
|
+
description: faker.commerce.productDescription(),
|
|
77
|
+
stock: faker.number.int({ min: 0, max: 250 }),
|
|
78
|
+
sales: 0,
|
|
79
|
+
rating: faker.number.float({ min: 1, max: 5, fractionDigits: 1 }),
|
|
80
|
+
};
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
// 4. Generate Reviews
|
|
84
|
+
const reviews: Review[] = Array.from({ length: 200 }, (_, id) => {
|
|
85
|
+
const customer = faker.helpers.arrayElement(customers);
|
|
86
|
+
const product = faker.helpers.arrayElement(products);
|
|
87
|
+
const date = faker.date.past({ years: 1 });
|
|
88
|
+
const status = faker.helpers.arrayElement(['accepted', 'pending', 'rejected']);
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
id,
|
|
92
|
+
date,
|
|
93
|
+
status: status as 'accepted' | 'pending' | 'rejected',
|
|
94
|
+
customer_id: customer.id,
|
|
95
|
+
product_id: product.id,
|
|
96
|
+
rating: faker.number.int({ min: 1, max: 5 }),
|
|
97
|
+
comment: faker.lorem.paragraph(),
|
|
98
|
+
};
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// 5. Generate Orders
|
|
102
|
+
const orders: Order[] = Array.from({ length: 300 }, (_, id) => {
|
|
103
|
+
const customer = faker.helpers.arrayElement(customers.filter(c => c.has_ordered));
|
|
104
|
+
const date = faker.date.between({ from: customer.first_seen, to: customer.last_seen });
|
|
105
|
+
const nbItems = faker.number.int({ min: 1, max: 5 });
|
|
106
|
+
|
|
107
|
+
const basket: OrderItem[] = Array.from({ length: nbItems }, (_, itemId) => {
|
|
108
|
+
const product = faker.helpers.arrayElement(products);
|
|
109
|
+
return {
|
|
110
|
+
id: id * 10 + itemId,
|
|
111
|
+
order_id: id,
|
|
112
|
+
product_id: product.id,
|
|
113
|
+
quantity: faker.number.int({ min: 1, max: 10 }),
|
|
114
|
+
unit_price: product.price,
|
|
115
|
+
};
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
const total_ex_taxes = basket.reduce((sum, item) => sum + item.quantity * item.unit_price, 0);
|
|
119
|
+
const delivery_fees = faker.number.float({ min: 0, max: 20, fractionDigits: 2 });
|
|
120
|
+
const tax_rate = 0.2;
|
|
121
|
+
const taxes = (total_ex_taxes + delivery_fees) * tax_rate;
|
|
122
|
+
const total = total_ex_taxes + delivery_fees + taxes;
|
|
123
|
+
const status: OrderStatus = faker.helpers.arrayElement(['ordered', 'delivered', 'cancelled', 'unknown']);
|
|
124
|
+
const returned = status === 'delivered' && faker.datatype.boolean({ probability: 0.1 });
|
|
125
|
+
|
|
126
|
+
// Update customer stats
|
|
127
|
+
customer.nb_commands++;
|
|
128
|
+
customer.total_spent += total;
|
|
129
|
+
if (!customer.latest_purchase || date > customer.latest_purchase) {
|
|
130
|
+
customer.latest_purchase = date;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Update product stats
|
|
134
|
+
basket.forEach(item => {
|
|
135
|
+
const product = products.find(p => p.id === item.product_id);
|
|
136
|
+
if (product) {
|
|
137
|
+
product.sales += item.quantity;
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
return {
|
|
142
|
+
id,
|
|
143
|
+
reference: faker.string.alphanumeric({ length: 6, casing: 'upper' }),
|
|
144
|
+
date,
|
|
145
|
+
customer_id: customer.id,
|
|
146
|
+
basket,
|
|
147
|
+
total_ex_taxes,
|
|
148
|
+
delivery_fees,
|
|
149
|
+
tax_rate,
|
|
150
|
+
taxes,
|
|
151
|
+
total,
|
|
152
|
+
status,
|
|
153
|
+
returned,
|
|
154
|
+
};
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
return {
|
|
158
|
+
categories,
|
|
159
|
+
customers,
|
|
160
|
+
products,
|
|
161
|
+
reviews,
|
|
162
|
+
orders,
|
|
163
|
+
};
|
|
164
|
+
};
|
package/src/index.ts
ADDED
package/src/types.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
export interface Customer {
|
|
2
|
+
id: number;
|
|
3
|
+
first_name: string;
|
|
4
|
+
last_name: string;
|
|
5
|
+
email: string;
|
|
6
|
+
address: string;
|
|
7
|
+
city: string;
|
|
8
|
+
zipcode: string;
|
|
9
|
+
avatar: string;
|
|
10
|
+
birthday: Date;
|
|
11
|
+
first_seen: Date;
|
|
12
|
+
last_seen: Date;
|
|
13
|
+
has_ordered: boolean;
|
|
14
|
+
latest_purchase?: Date;
|
|
15
|
+
has_newsletter: boolean;
|
|
16
|
+
groups: string[];
|
|
17
|
+
nb_commands: number;
|
|
18
|
+
total_spent: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface Category {
|
|
22
|
+
id: number;
|
|
23
|
+
name: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface Product {
|
|
27
|
+
id: number;
|
|
28
|
+
category_id: number;
|
|
29
|
+
reference: string;
|
|
30
|
+
name: string;
|
|
31
|
+
width: number;
|
|
32
|
+
height: number;
|
|
33
|
+
price: number;
|
|
34
|
+
thumbnail: string;
|
|
35
|
+
image: string;
|
|
36
|
+
description: string;
|
|
37
|
+
stock: number;
|
|
38
|
+
sales: number;
|
|
39
|
+
rating: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface OrderItem {
|
|
43
|
+
id: number;
|
|
44
|
+
order_id: number;
|
|
45
|
+
product_id: number;
|
|
46
|
+
quantity: number;
|
|
47
|
+
unit_price: number;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export type OrderStatus = 'ordered' | 'delivered' | 'cancelled' | 'unknown';
|
|
51
|
+
|
|
52
|
+
export interface Order {
|
|
53
|
+
id: number;
|
|
54
|
+
reference: string;
|
|
55
|
+
date: Date;
|
|
56
|
+
customer_id: number;
|
|
57
|
+
basket: OrderItem[];
|
|
58
|
+
total_ex_taxes: number;
|
|
59
|
+
delivery_fees: number;
|
|
60
|
+
tax_rate: number;
|
|
61
|
+
taxes: number;
|
|
62
|
+
total: number;
|
|
63
|
+
status: OrderStatus;
|
|
64
|
+
returned: boolean;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface Review {
|
|
68
|
+
id: number;
|
|
69
|
+
date: Date;
|
|
70
|
+
status: 'accepted' | 'pending' | 'rejected';
|
|
71
|
+
customer_id: number;
|
|
72
|
+
product_id: number;
|
|
73
|
+
rating: number;
|
|
74
|
+
comment: string;
|
|
75
|
+
}
|