-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed.ts
More file actions
55 lines (46 loc) · 1.44 KB
/
seed.ts
File metadata and controls
55 lines (46 loc) · 1.44 KB
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
import { config } from 'dotenv'
import { createSeedClient } from '@snaplet/seed'
import { copycat } from '@snaplet/copycat'
import { createClient } from '@supabase/supabase-js'
const seed = await createSeedClient()
config({ path: './playground/.env' })
async function main() {
await seed.$resetDatabase()
await createUsers(10)
await createData()
process.exit()
}
async function createData() {
await seed.posts(x => x(10, { comments: x => x({ min: 1, max: 5 }) }), { connect: { members: seed.$store.members } })
}
async function createUsers(n: number = 1) {
const supabase = createClient(process.env.NUXT_PUBLIC_SUPABASE_URL!, process.env.NUXT_PUBLIC_SUPABASE_ANON_KEY!)
const PASSWORD = 'password'
const users = [] as { id: string }[]
for (let i = 0; i <= n; i++) {
const index = i + 1
const email = 'user' + index + '@example.com'
const user_name = 'user' + index
const first_name = copycat.firstName(i)
const last_name = copycat.lastName(i)
const { data, error } = await supabase.auth.signUp({
email,
password: PASSWORD,
options: {
data: {
user_name,
first_name,
last_name,
},
},
})
if (error) {
console.error('error', error)
return error
}
if (data?.user) users.push(data.user)
}
await seed.members(x => x(users.length, ({ index }) => ({ username: `user${index + 1}`, user_id: users[index].id })))
return users
}
main()