Skip to content

Commit

Permalink
build(seeder): add methods to Seeder to seed the tables
Browse files Browse the repository at this point in the history
  • Loading branch information
danvergara committed May 1, 2021
1 parent a0706df commit 58d4ae8
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
16 changes: 16 additions & 0 deletions db/seeds/customer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package seeds

import "github.com/bxcodec/faker/v3"

// CustomerSeed seeds the database with customers.
func (s Seed) CustomerSeed() {
for i := 0; i < 100; i++ {
// prepare the statement.
stmt, _ := s.db.Prepare(`INSERT INTO customers(name, email) VALUES ($1, $2)`)
// execute query.
_, err := stmt.Exec(faker.Name(), faker.Email())
if err != nil {
panic(err)
}
}
}
20 changes: 20 additions & 0 deletions db/seeds/product.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package seeds

import (
"math/rand"

"github.com/bxcodec/faker/v3"
)

// ProductSeed seeds product data.
func (s Seed) ProductSeed() {
for i := 0; i < 100; i++ {
// prepare the statement.
stmt, _ := s.db.Prepare(`INSERT INTO products(name, price) VALUES ($1, $2)`)
// execute query.
_, err := stmt.Exec(faker.Word(), rand.Float32())
if err != nil {
panic(err)
}
}
}
19 changes: 19 additions & 0 deletions db/seeds/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package seeds

import (
"github.com/bxcodec/faker/v3"
)

// UserSeed seeds the database with users.
func (s Seed) UserSeed() {
for i := 0; i < 100; i++ {

// prepare the statement.
stmt, _ := s.db.Prepare(`INSERT INTO users(username) VALUES ($1)`)
// execute query.
_, err := stmt.Exec(faker.Name())
if err != nil {
panic(err)
}
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/danvergara/dblab
go 1.16

require (
github.com/bxcodec/faker/v3 v3.6.0 // indirect
github.com/go-sql-driver/mysql v1.6.0
github.com/golang-migrate/migrate/v4 v4.14.1
github.com/hashicorp/errwrap v1.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932/go.mod h1:NOuUCS
github.com/bkaradzic/go-lz4 v1.0.0/go.mod h1:0YdlkowM3VswSROI7qDxhRvJ3sLhlFrRRwjwegp5jy4=
github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
github.com/bxcodec/faker/v3 v3.6.0 h1:Meuh+M6pQJsQJwxVALq6H5wpDzkZ4pStV9pmH7gbKKs=
github.com/bxcodec/faker/v3 v3.6.0/go.mod h1:gF31YgnMSMKgkvl+fyEo1xuSMbEuieyqfeslGYFjneM=
github.com/cenkalti/backoff/v4 v4.0.2/go.mod h1:eEew/i+1Q6OrCDZh3WiXYv3+nJwBASZ8Bog/87DQnVg=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
Expand Down

0 comments on commit 58d4ae8

Please sign in to comment.