-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseeder.go
120 lines (101 loc) · 4.09 KB
/
seeder.go
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
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"context"
"strconv"
"time"
"github.com/brianvoe/gofakeit/v7"
"github.com/fastschema/fastschema"
"github.com/fastschema/fastschema/db"
"github.com/fastschema/fastschema/entity"
"github.com/fastschema/fastschema/fs"
"github.com/fastschema/fastschema/pkg/utils"
)
// 27 tags
var initTagValues = []string{"javascript", "golang", "webdev", "beginners", "programming", "react", "python", "angular", "csharp", "css", "typescript", "cplusplus",
"ruby", "ai", "devops", "rust", "opensource", "#database", "java", "node", "security", "docker", "api", "algorithms", "cloud", "machinelearning", "career"}
// TagSeeder create tags
func TagSeeder(ctx context.Context, app *fastschema.App) {
for _, v := range initTagValues {
// Create tag using schema name
_ = utils.Must(db.Builder[Tag](app.DB(), "tag").Create(ctx, fs.Map{
"name": v,
"description": gofakeit.SentenceSimple(),
}))
// fmt.Printf("> Create tag using system schema: %+s\n\n", spew.Sdump(tag))
}
}
// UserSeeder create 3 users
func UserSeeder(ctx context.Context, app *fastschema.App) {
for i := 101; i < 104; i++ {
// Create user using schema name
_ = utils.Must(db.Builder[fs.User](app.DB()).Create(ctx, fs.Map{
"username": "test" + strconv.Itoa(i),
"email": "test" + strconv.Itoa(i) + "@example.com",
"provider": "local",
"password": utils.Must(utils.GenerateHash("123456")), // 123456
"active": true,
"roles": []*entity.Entity{},
}))
// fmt.Printf("> Create tag using system schema: %+s\n\n", spew.Sdump(tag))
}
}
// PostSeeder create quant posts
func PostSeeder(ctx context.Context, app *fastschema.App, quant int) {
for i := 1; i < quant+1; i++ {
var tagNames []any
// Query tags
tagNames = append(tagNames, gofakeit.RandomString(initTagValues[:9]), gofakeit.RandomString(initTagValues[9:18]), gofakeit.RandomString(initTagValues[18:27]))
// fmt.Printf("> Query blog with tags: %+v\n\n", spew.Sdump(tagNames))
// Query tags
postTags := utils.Must(db.Builder[*Tag](app.DB()).
// Where(db.In("name", tagNames)).
Where(db.Or(
db.EQ("name", tagNames[0]),
db.EQ("name", tagNames[1]),
db.EQ("name", tagNames[2]),
)).
Get(ctx))
// fmt.Printf("> blogTags: %s\n\n", spew.Sdump(blogTags))
// Create blog with tags
ca1 := gofakeit.DateRange(time.Now().AddDate(-2, 0, 0), time.Now().AddDate(0, 0, -2))
published := true
published_at := gofakeit.Date()
if i%8 == 0 {
published = false
published_at = time.Now()
}
_ = utils.Must(db.Builder[Post](app.DB()).Create(ctx, fs.Map{
"title": gofakeit.SentenceSimple(),
"cover_image": "https://fakeimg.pl/?text=Hello%20World&font=noto", // https://fakeimg.pl/350x200/?text=World&font=noto
"content": gofakeit.Paragraph(3, 10, 20, "\n\n"),
"published": published,
"published_at": published_at,
"view_count": gofakeit.Number(10, 100),
"share_count": gofakeit.Number(10, 100),
"time_to_read": gofakeit.Number(1, 6),
"user_id": gofakeit.RandomInt([]int{1, 2, 3}),
"user_interaction": gofakeit.Bool(),
"mode": gofakeit.RandomInt([]int{0, 1, 2}),
"tags": []*entity.Entity{
entity.New(postTags[0].ID),
entity.New(postTags[1].ID),
entity.New(postTags[2].ID),
},
"created_at": ca1,
"updated_at": ca1.AddDate(0, 0, gofakeit.RandomInt([]int{1, 2, 3, 4, 5, 6, 7})),
}))
// fmt.Printf("> Create blog with tags: %s\n\n", spew.Sdump(blog))
}
}
// InitDB
func InitDB(ctx context.Context, app *fastschema.App) {
// Delete all relations
utils.Must(db.Exec(ctx, app.DB(), "DELETE FROM posts_tags"))
utils.Must(db.Exec(ctx, app.DB(), "DELETE FROM likes"))
utils.Must(db.Exec(ctx, app.DB(), "DELETE FROM follows"))
utils.Must(db.Exec(ctx, app.DB(), "DELETE FROM readings"))
// Delete all posts
utils.Must(db.Exec(ctx, app.DB(), "DELETE FROM tags; DELETE FROM SQLITE_SEQUENCE WHERE name='tags';"))
utils.Must(db.Exec(ctx, app.DB(), "DELETE FROM posts; DELETE FROM SQLITE_SEQUENCE WHERE name='posts';"))
utils.Must(db.Exec(ctx, app.DB(), "DELETE FROM profiles; DELETE FROM SQLITE_SEQUENCE WHERE name='profiles';"))
}