-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseed.mjs
86 lines (73 loc) · 2.38 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
import { createClient } from '@supabase/supabase-js'
import { faker } from '@faker-js/faker'
import 'dotenv/config'
const supabase = createClient(
process.env.SUPABASE_URL,
'INSERT_YOUR_SERVICE_KEY_HERE!', {
auth: { persistSession: false }
})
const categories = ['Food', 'Housing', 'Car', 'Entertainment']
const { data: { users }, error } = await supabase.auth.admin.listUsers()
const userIds = users.map(user => user.id)
async function seedTransactions() {
// Delete existing data
// const { error: deleteError } = await supabase.from('transactions')
// .delete().gte('id', 0)
// if (deleteError) {
// console.error('Error deleting existing data:', deleteError)
// return
// }
let transactions = []
for (const user of userIds) {
for (let year = new Date().getFullYear(); year > new Date().getFullYear() - 2; year--) {
for (let i = 0; i < 10; i++) {
const date = new Date(
year,
faker.number.int({ min: 0, max: 11 }),
faker.number.int({ min: 1, max: 28 })
)
let type, category
const typeBias = Math.random()
if (typeBias < 0.85) {
type = 'Expense'
category = faker.helpers.arrayElement(categories) // Category only for 'Expense'
} else if (typeBias < 0.95) {
type = 'Income'
} else {
type = faker.helpers.arrayElement(['Saving', 'Investment'])
}
let amount
switch (type) {
case 'Income':
amount = faker.number.int({ min: 2000, max: 5000 })
break
case 'Expense':
amount = faker.number.int({ min: 100, max: 1000 })
break
case 'Saving':
case 'Investment':
amount = faker.number.int({ min: 5000, max: 10000 })
break
default:
amount = 0
}
transactions.push({
created_at: date,
amount,
type,
description: faker.lorem.sentence(),
category: type === 'Expense' ? category : null, // Category only for 'Expense'
user_id: user
})
}
}
}
const { error: insertError } = await supabase.from('transactions')
.upsert(transactions)
if (insertError) {
console.error('Error inserting data:', insertError)
} else {
console.log('Data inserted successfully.')
}
}
seedTransactions().catch(console.error)