/
store.go
269 lines (213 loc) · 7.39 KB
/
store.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package gormstore
import (
"context"
"errors"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"github.com/jkaveri/goflexstore/converter"
gormopscope "github.com/jkaveri/goflexstore/gorm/opscope"
gormquery "github.com/jkaveri/goflexstore/gorm/query"
gormutils "github.com/jkaveri/goflexstore/gorm/utils"
"github.com/jkaveri/goflexstore/query"
"github.com/jkaveri/goflexstore/store"
)
// New initializes a new Store instance for handling CRUD operations on entities.
// It accepts an operation scope and a variable number of options to customize the store behavior.
// The function returns a pointer to the initialized Store.
//
// Entity and DTO are types that must implement the store.Entity interface.
// ID is the type of the identifier for the entities.
func New[Entity store.Entity[ID], DTO store.Entity[ID], ID comparable](
opScope *gormopscope.TransactionScope,
options ...Option[Entity, DTO, ID],
) *Store[Entity, DTO, ID] {
s := &Store[Entity, DTO, ID]{
OpScope: opScope,
BatchSize: 50,
}
for _, option := range options {
option(s)
}
if s.Converter == nil {
s.Converter = converter.NewReflect[Entity, DTO, ID](nil)
}
if s.ScopeBuilder == nil {
s.ScopeBuilder = gormquery.NewBuilder(
gormquery.WithFieldToColMap(
gormutils.FieldToColMap(*new(DTO)),
),
)
}
return s
}
// Store represents a storage mechanism using GORM for database operations.
// It supports CRUD operations and is designed to be generic for any Entity and DTO types.
//
// Entity: The domain model type.
// DTO: The data transfer object type, representing the database model.
// ID: The type of the unique identifier for the entity.
type Store[Entity store.Entity[ID], DTO store.Entity[ID], ID comparable] struct {
OpScope *gormopscope.TransactionScope
Converter converter.Converter[Entity, DTO, ID]
ScopeBuilder *gormquery.ScopeBuilder
BatchSize int
}
// Get retrieves a single entity based on provided query parameters.
// It returns the entity if found, otherwise an error.
func (s *Store[Entity, DTO, ID]) Get(ctx context.Context, params ...query.Param) (Entity, error) {
var (
dto DTO
scopes = s.ScopeBuilder.Build(query.NewParams(params...))
)
tx := s.getTx(ctx).Scopes(scopes...)
if tx.Error != nil {
return *new(Entity), tx.Error
}
if err := tx.
First(&dto).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return *new(Entity), store.ErrorNotFound
}
return *new(Entity), err
}
return s.Converter.ToEntity(dto), nil
}
// List retrieves a list of entities matching the provided query parameters.
// Returns a slice of entities and an error if the operation fails.
func (s *Store[Entity, DTO, ID]) List(ctx context.Context, params ...query.Param) ([]Entity, error) {
var (
dtos []DTO
scopes = s.ScopeBuilder.Build(query.NewParams(params...))
)
tx := s.getTx(ctx).Scopes(scopes...)
if tx.Error != nil {
return nil, tx.Error
}
if err := tx.Find(&dtos).Error; err != nil {
return nil, err
}
return converter.ToMany(dtos, s.Converter.ToEntity), nil
}
// Count returns the number of entities that satisfy the provided query parameters.
// The count is returned along with an error if the operation fails.
func (s *Store[Entity, DTO, ID]) Count(ctx context.Context, params ...query.Param) (int64, error) {
var (
count int64
scopes = s.ScopeBuilder.Build(query.NewParams(params...))
)
tx := s.getTx(ctx).Scopes(scopes...)
if tx.Error != nil {
return 0, tx.Error
}
if err := tx.Count(&count).Error; err != nil {
return 0, err
}
return count, nil
}
// Exists checks for the existence of at least one entity that matches the query parameters.
// Returns true if such an entity exists, false otherwise.
func (s *Store[Entity, DTO, ID]) Exists(ctx context.Context, params ...query.Param) (bool, error) {
var (
count int64
scopes = s.ScopeBuilder.Build(query.NewParams(params...))
)
tx := s.getTx(ctx).Scopes(scopes...)
if tx.Error != nil {
return false, tx.Error
}
if err := tx.
Limit(1).
Count(&count).Error; err != nil {
return false, err
}
return count > 0, nil
}
// Create adds a new entity to the store and returns its ID.
// Returns an error if the creation fails.
func (s *Store[Entity, DTO, ID]) Create(ctx context.Context, entity Entity) (ID, error) {
dto := s.Converter.ToDTO(entity)
if err := s.getTx(ctx).Create(&dto).Error; err != nil {
return *new(ID), err
}
return dto.GetID(), nil
}
// CreateMany performs batch creation of entities.
// The BatchSize field of the store determines the number of entities in each batch.
// Returns an error if the operation fails.
func (s *Store[Entity, DTO, ID]) CreateMany(ctx context.Context, entities []Entity) error {
dtos := converter.ToMany(entities, s.Converter.ToDTO)
batchSize := defaultValue(s.BatchSize, 50)
return s.getTx(ctx).CreateInBatches(dtos, batchSize).Error
}
// Update modifies an existing entity in the store, including fields with zero values.
// Returns an error if the update operation fails.
func (s *Store[Entity, DTO, ID]) Update(ctx context.Context, entity Entity, params ...query.Param) error {
dto := s.Converter.ToDTO(entity)
id := dto.GetID()
if id == *new(ID) && len(params) == 0 {
return errors.New("id is required")
}
tx := s.getTx(ctx)
if len(params) > 0 {
scopes := s.ScopeBuilder.Build(query.NewParams(params...))
tx = tx.Scopes(scopes...)
if tx.Error != nil {
return tx.Error
}
}
return tx.Select("*").Updates(&dto).Error
}
// PartialUpdate updates specific fields of an existing entity in the store.
// Only non-zero fields of the entity are updated.
// Returns an error if the operation fails.
func (s *Store[Entity, DTO, ID]) PartialUpdate(ctx context.Context, entity Entity, params ...query.Param) error {
dto := s.Converter.ToDTO(entity)
scopes := s.ScopeBuilder.Build(query.NewParams(params...))
tx := s.getTx(ctx).Scopes(scopes...)
if tx.Error != nil {
return tx.Error
}
return tx.Updates(dto).Error
}
// Delete removes entities from the store based on the provided query parameters.
// Returns an error if the deletion operation fails.
func (s *Store[Entity, DTO, ID]) Delete(ctx context.Context, params ...query.Param) error {
var (
dto DTO
scopes = s.ScopeBuilder.Build(query.NewParams(params...))
)
tx := s.getTx(ctx).Scopes(scopes...)
if tx.Error != nil {
return tx.Error
}
if err := tx.Delete(&dto).Error; err != nil {
return err
}
return nil
}
// Upsert either creates a new entity or updates an existing one based on the provided conflict resolution strategy.
// Returns the ID of the affected entity and an error if the operation fails.
func (s *Store[Entity, DTO, ID]) Upsert(ctx context.Context, entity Entity, onConflict store.OnConflict) (ID, error) {
dto := s.Converter.ToDTO(entity)
c := clause.OnConflict{
Columns: []clause.Column{},
OnConstraint: onConflict.OnConstraint,
DoNothing: onConflict.DoNothing,
UpdateAll: onConflict.UpdateAll,
}
for _, col := range onConflict.Columns {
c.Columns = append(c.Columns, clause.Column{Name: col})
}
if len(onConflict.Updates) > 0 {
c.DoUpdates = clause.Assignments(onConflict.Updates)
} else if len(onConflict.UpdateColumns) > 0 {
c.DoUpdates = clause.AssignmentColumns(onConflict.UpdateColumns)
}
if err := s.getTx(ctx).Clauses(c).Create(&dto).Error; err != nil {
return *new(ID), err
}
return dto.GetID(), nil
}
func (s *Store[Entity, DTO, ID]) getTx(ctx context.Context) *gorm.DB {
return s.OpScope.Tx(ctx).WithContext(ctx).Model(new(DTO))
}