-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseed.mjs
80 lines (68 loc) · 1.58 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
import { createClient } from '@supabase/supabase-js'
import dotenv from 'dotenv'
import { faker } from '@faker-js/faker'
dotenv.config({ path: '.env.local' })
const supabase = createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL,
process.env.SUPABASE_SERVICE_ROLE
)
const categories = [
'Housing', 'Transport', 'Health', 'Food', 'Education', 'Other'
]
async function seed() {
let transactions = []
for (let i = 0; i < 10; i++) {
const created_at = faker.date.past()
let type, category = null
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,
})
}
const { error } = await supabase.from('transactions')
.insert(transactions)
if (error) {
console.error('Error inserting data')
} else {
console.log('Data inserted')
}
}
seed().catch(console.error)