-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseed.mjs
109 lines (94 loc) · 2.3 KB
/
seed.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { createClient } from '@supabase/supabase-js'
import dotenv from 'dotenv'
import { faker } from '@faker-js/faker'
dotenv.config()
const supabase = createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL,
process.env.SUPABASE_SERVICE_ROLE
)
const categories = [
'Housing', 'Transport', 'Health', 'Food', 'Education', 'Other'
]
async function seedUsers() {
for (let i = 0; i < 20; i++) {
try {
const { error } = await supabase.auth.admin.createUser({
email: faker.internet.email(),
password: 'password',
})
if (error) {
throw new Error(error)
}
console.log(`User added`)
} catch (e) {
console.error(`Error adding user`)
}
}
}
async function seed() {
await seedUsers()
let transactions = []
const { data: { users }, error: listUsersError } = await supabase.auth.admin.listUsers()
if (listUsersError) {
console.error(`Cannot list users, aborting`)
return
}
const userIds = users?.map(user => user.id)
for (let i = 0; i < 100; i++) {
const created_at = faker.date.past()
let type, category = null
const user_id = faker.helpers.arrayElement(userIds)
const typeBias = Math.random()
if (typeBias < 0.80) {
type = 'Expense'
category = faker.helpers.arrayElement(
categories
)
} else if (typeBias < 0.90) {
type = 'Income'
} else {
type = faker.helpers.arrayElement([
'Saving', 'Investment'
])
}
let amount
switch (type) {
case 'Income':
amount = faker.number.int({
min: 2000,
max: 9000
})
break
case 'Expense':
amount = faker.number.int({
min: 10,
max: 1000
})
break
case 'Investment':
case 'Saving':
amount = faker.number.int({
min: 3000,
max: 10000
})
break
}
transactions.push({
created_at,
amount,
type,
description: faker.lorem.sentence(),
category,
user_id
})
}
const { error } = await supabase.from('transactions')
.insert(transactions)
if (error) {
console.error('Error inserting data')
} else {
console.log('Data inserted')
console.log(`${transactions.length} transactions stored`)
}
}
seed().catch(console.error)