1
1
import pool from "config/database" ;
2
2
import bcrypt from "bcrypt" ;
3
3
import { initializeDatabase } from "./setup" ;
4
+ import { RegisterPayload } from "auth/auth.validation" ;
5
+ import { CreateProductPayload } from "products/products.validation" ;
6
+ import { faker } from "@faker-js/faker" ;
4
7
5
- const seedData = [
6
- {
7
- username : "Mateo" ,
8
-
9
- password : "123456"
10
- }
11
- ] ;
8
+ const users : RegisterPayload [ ] = Array . from ( { length : 2 } , ( ) => ( {
9
+ username : faker . internet . displayName ( ) ,
10
+ email : faker . internet . email ( ) ,
11
+ password : "123456"
12
+ } ) ) ;
13
+
14
+ const products : CreateProductPayload [ ] = Array . from ( { length : 2 } , ( ) => ( {
15
+ name : faker . commerce . productName ( ) ,
16
+ description : faker . commerce . productDescription ( ) ,
17
+ price : + faker . commerce . price ( { min : 100 , max : 10000 , dec : 0 } )
18
+ } ) ) ;
12
19
13
20
export async function seedDatabase ( ) {
14
21
await initializeDatabase ( ) ;
@@ -19,15 +26,24 @@ export async function seedDatabase() {
19
26
20
27
// Clear the users table
21
28
await client . query ( "TRUNCATE TABLE users RESTART IDENTITY CASCADE" ) ;
29
+ await client . query ( "TRUNCATE TABLE products RESTART IDENTITY CASCADE" ) ;
22
30
23
- for ( const { username, email, password } of seedData ) {
31
+ for ( const { username, email, password } of users ) {
24
32
const hashedPassword = await bcrypt . hash ( password , 10 ) ;
25
33
await client . query ( `INSERT INTO users (username, email, password) VALUES ($1, $2, $3)` , [
26
34
username ,
27
35
email ,
28
36
hashedPassword
29
37
] ) ;
30
38
}
39
+
40
+ for ( const { name, description, price } of products ) {
41
+ await client . query ( `INSERT INTO products (name, description, price) VALUES ($1, $2, $3)` , [
42
+ name ,
43
+ description ,
44
+ price
45
+ ] ) ;
46
+ }
31
47
await client . query ( "COMMIT" ) ;
32
48
} catch ( error ) {
33
49
await client . query ( "ROLLBACK" ) ;
0 commit comments