Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
rename @entity to @dbrepo + support bulk insert
Browse files Browse the repository at this point in the history
  • Loading branch information
imantung committed Apr 21, 2021
1 parent d7f9172 commit 7e7da8c
Show file tree
Hide file tree
Showing 17 changed files with 128 additions and 108 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Pragmatic Golang RESTful Server Implementation. The project using [typical-go](h
- Others
- [x] Database migration and seed tool
- [x] Generate code, `.env` file and `USAGE.md` according the configuration (using `@envconfig` annotation)
- [x] Generate code for repository layer (using `@entity` annotation)
- [x] Generate code for repository layer (using `@dbrepo` annotation)
- [x] Releaser


Expand Down
4 changes: 2 additions & 2 deletions internal/app/entity/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "time"

type (
// Song entity
// @entity (table:"songs" dialect:"mysql" ctor_db:"mysql")
// @dbrepo (table:"songs" dialect:"mysql" ctor_db:"mysql")
Song struct {
ID int64 `column:"id" option:"pk" json:"id"`
Title string `column:"title" json:"title" validate:"required"`
Expand All @@ -13,7 +13,7 @@ type (
CreatedAt time.Time `column:"created_at" option:"now,no_update" json:"created_at"`
}
// Book represented book model
// @entity (table:"books" dialect:"postgres" ctor_db:"pg")
// @dbrepo (table:"books" dialect:"postgres" ctor_db:"pg")
Book struct {
ID int64 `column:"id" option:"pk" json:"id"`
Title string `column:"title" json:"title" validate:"required"`
Expand Down
2 changes: 1 addition & 1 deletion internal/app/service/book_svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (b *BookSvcImpl) Create(ctx context.Context, book *entity.Book) (*entity.Bo
if err := validator.New().Struct(book); err != nil {
return nil, echokit.NewValidErr(err.Error())
}
id, err := b.Repo.Create(ctx, book)
id, err := b.Repo.Insert(ctx, book)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions internal/app/service/book_svc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestBookSvc_Create(t *testing.T) {
expectedErr: "create-error",
bookSvcFn: func(mockRepo *repo_mock.MockBookRepo) {
mockRepo.EXPECT().
Create(gomock.Any(), &entity.Book{Author: "some-author", Title: "some-title"}).
Insert(gomock.Any(), &entity.Book{Author: "some-author", Title: "some-title"}).
Return(int64(-1), errors.New("create-error"))
},
},
Expand All @@ -58,7 +58,7 @@ func TestBookSvc_Create(t *testing.T) {
expectedErr: "find-error",
bookSvcFn: func(mockRepo *repo_mock.MockBookRepo) {
mockRepo.EXPECT().
Create(gomock.Any(), &entity.Book{Author: "some-author", Title: "some-title"}).
Insert(gomock.Any(), &entity.Book{Author: "some-author", Title: "some-title"}).
Return(int64(1), nil)
mockRepo.EXPECT().
Find(gomock.Any(), sqkit.Eq{"id": int64(1)}).
Expand All @@ -73,7 +73,7 @@ func TestBookSvc_Create(t *testing.T) {
expected: &entity.Book{Author: "some-author", Title: "some-title"},
bookSvcFn: func(mockRepo *repo_mock.MockBookRepo) {
mockRepo.EXPECT().
Create(gomock.Any(), &entity.Book{Author: "some-author", Title: "some-title"}).
Insert(gomock.Any(), &entity.Book{Author: "some-author", Title: "some-title"}).
Return(int64(1), nil)
mockRepo.EXPECT().
Find(gomock.Any(), sqkit.Eq{"id": int64(1)}).
Expand Down
2 changes: 1 addition & 1 deletion internal/app/service/song_svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (b *SongSvcImpl) Create(ctx context.Context, book *entity.Song) (*entity.So
if err := validator.New().Struct(book); err != nil {
return nil, echokit.NewValidErr(err.Error())
}
id, err := b.Repo.Create(ctx, book)
id, err := b.Repo.Insert(ctx, book)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions internal/app/service/song_svc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
"github.com/typical-go/typical-rest-server/internal/app/service"
"github.com/typical-go/typical-rest-server/internal/app/entity"
"github.com/typical-go/typical-rest-server/internal/app/service"
"github.com/typical-go/typical-rest-server/internal/generated/entity/app/repo_mock"
"github.com/typical-go/typical-rest-server/pkg/sqkit"
)
Expand Down Expand Up @@ -46,7 +46,7 @@ func TestSongSvc_Create(t *testing.T) {
expectedErr: "create-error",
songSvcFn: func(mockRepo *repo_mock.MockSongRepo) {
mockRepo.EXPECT().
Create(gomock.Any(), &entity.Song{Artist: "some-artist", Title: "some-title"}).
Insert(gomock.Any(), &entity.Song{Artist: "some-artist", Title: "some-title"}).
Return(int64(-1), errors.New("create-error"))
},
},
Expand All @@ -56,7 +56,7 @@ func TestSongSvc_Create(t *testing.T) {
expectedErr: "Find-error",
songSvcFn: func(mockRepo *repo_mock.MockSongRepo) {
mockRepo.EXPECT().
Create(gomock.Any(), &entity.Song{Artist: "some-artist", Title: "some-title"}).
Insert(gomock.Any(), &entity.Song{Artist: "some-artist", Title: "some-title"}).
Return(int64(1), nil)
mockRepo.EXPECT().
Find(gomock.Any(), sqkit.Eq{"id": int64(1)}).
Expand All @@ -71,7 +71,7 @@ func TestSongSvc_Create(t *testing.T) {
expected: &entity.Song{Artist: "some-artist", Title: "some-title"},
songSvcFn: func(mockRepo *repo_mock.MockSongRepo) {
mockRepo.EXPECT().
Create(gomock.Any(), &entity.Song{Artist: "some-artist", Title: "some-title"}).
Insert(gomock.Any(), &entity.Song{Artist: "some-artist", Title: "some-title"}).
Return(int64(1), nil)
mockRepo.EXPECT().
Find(gomock.Any(), sqkit.Eq{"id": int64(1)}).
Expand Down
2 changes: 1 addition & 1 deletion internal/app/service_mock/book_svc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/app/service_mock/song_svc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 16 additions & 13 deletions internal/generated/entity/app/repo/book_repo.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package repo

/* DO NOT EDIT. This file generated due to '@entity' annotation */
/* DO NOT EDIT. This file generated due to '@dbrepo' annotation */

import (
"context"
Expand Down Expand Up @@ -41,7 +41,7 @@ type (
BookRepo interface {
Count(context.Context, ...sqkit.SelectOption) (int64, error)
Find(context.Context, ...sqkit.SelectOption) ([]*entity.Book, error)
Create(context.Context, *entity.Book) (int64, error)
Insert(context.Context, ...*entity.Book) (int64, error)
Delete(context.Context, sqkit.DeleteOption) (int64, error)
Update(context.Context, *entity.Book, sqkit.UpdateOption) (int64, error)
Patch(context.Context, *entity.Book, sqkit.UpdateOption) (int64, error)
Expand Down Expand Up @@ -130,33 +130,36 @@ func (r *BookRepoImpl) Find(ctx context.Context, opts ...sqkit.SelectOption) (li
return
}

// Create books
func (r *BookRepoImpl) Create(ctx context.Context, ent *entity.Book) (int64, error) {
// Insert books
func (r *BookRepoImpl) Insert(ctx context.Context, ents ...*entity.Book) (int64, error) {
txn, err := dbtxn.Use(ctx, r.DB)
if err != nil {
return -1, err
}

scanner := sq.
builder := sq.
Insert(BookTableName).
Columns(
BookTable.Title,
BookTable.Author,
BookTable.UpdatedAt,
BookTable.CreatedAt,
).
Values(
Suffix(
fmt.Sprintf("RETURNING \"%s\"", BookTable.ID),
).
PlaceholderFormat(sq.Dollar)

for _, ent := range ents {
builder = builder.Values(
ent.Title,
ent.Author,
time.Now(),
time.Now(),
).
Suffix(
fmt.Sprintf("RETURNING \"%s\"", BookTable.ID),
).
PlaceholderFormat(sq.Dollar).
RunWith(txn).
QueryRowContext(ctx)
)
}

scanner := builder.RunWith(txn).QueryRowContext(ctx)

var id int64
if err := scanner.Scan(&id); err != nil {
Expand Down
22 changes: 12 additions & 10 deletions internal/generated/entity/app/repo/song_repo.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package repo

/* DO NOT EDIT. This file generated due to '@entity' annotation */
/* DO NOT EDIT. This file generated due to '@dbrepo' annotation */

import (
"context"
Expand Down Expand Up @@ -40,7 +40,7 @@ type (
SongRepo interface {
Count(context.Context, ...sqkit.SelectOption) (int64, error)
Find(context.Context, ...sqkit.SelectOption) ([]*entity.Song, error)
Create(context.Context, *entity.Song) (int64, error)
Insert(context.Context, ...*entity.Song) (int64, error)
Delete(context.Context, sqkit.DeleteOption) (int64, error)
Update(context.Context, *entity.Song, sqkit.UpdateOption) (int64, error)
Patch(context.Context, *entity.Song, sqkit.UpdateOption) (int64, error)
Expand Down Expand Up @@ -128,30 +128,32 @@ func (r *SongRepoImpl) Find(ctx context.Context, opts ...sqkit.SelectOption) (li
return
}

// Create songs
func (r *SongRepoImpl) Create(ctx context.Context, ent *entity.Song) (int64, error) {
// Insert songs
func (r *SongRepoImpl) Insert(ctx context.Context, ents ...*entity.Song) (int64, error) {
txn, err := dbtxn.Use(ctx, r.DB)
if err != nil {
return -1, err
}

res, err := sq.
builder := sq.
Insert(SongTableName).
Columns(
SongTable.Title,
SongTable.Artist,
SongTable.UpdatedAt,
SongTable.CreatedAt,
).
Values(
)

for _, ent := range ents {
builder = builder.Values(
ent.Title,
ent.Artist,
time.Now(),
time.Now(),
).
RunWith(txn).
ExecContext(ctx)
)
}

res, err := builder.RunWith(txn).ExecContext(ctx)
if err != nil {
txn.SetError(err)
return -1, err
Expand Down
35 changes: 20 additions & 15 deletions internal/generated/entity/app/repo_mock/book_repo.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 20 additions & 15 deletions internal/generated/entity/app/repo_mock/song_repo.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7e7da8c

Please sign in to comment.