Skip to content

Commit 4de9f22

Browse files
committed
feat: allow counting with filter
1 parent 2436280 commit 4de9f22

File tree

4 files changed

+10
-6
lines changed

4 files changed

+10
-6
lines changed

storage/dynamodb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ func (s *DynamoDBAdapter) Search(dest any, sortKey string, query string, limit i
257257
})
258258
}
259259

260-
func (s *DynamoDBAdapter) Count(dest any) (int64, error) {
260+
func (s *DynamoDBAdapter) Count(dest any, filter map[string]any) (int64, error) {
261261
// TODO Implement
262262
var total int64
263263
return total, nil

storage/memory.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ func (m *MemoryAdapter) Search(dest any, sortKey string, query string, limit int
100100
return m.DB.Search(dest, sortKey, query, limit, cursor)
101101
}
102102

103-
func (s *MemoryAdapter) Count(dest any) (int64, error) {
104-
//TODO: Implement
103+
func (m *MemoryAdapter) Count(dest any, filter map[string]any) (int64, error) {
105104
var total int64
106-
return total, nil
105+
total, err := m.DB.Count(dest, filter)
106+
return total, err
107107
}
108108

109109
func (m *MemoryAdapter) Query(dest any, statement string, limit int, cursor string) (string, error) {

storage/sql.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,13 @@ func (s *SQLAdapter) Search(dest any, sortKey string, query string, limit int, c
288288
})
289289
}
290290

291-
func (s *SQLAdapter) Count(dest any) (int64, error) {
291+
func (s *SQLAdapter) Count(dest any, filter map[string]any) (int64, error) {
292292
q := s.DB.Model(dest)
293293

294+
if len(filter) > 0 {
295+
q = q.Where(s.buildQuery(filter), filter)
296+
}
297+
294298
var total int64
295299
if err := q.Count(&total).Error; err != nil {
296300
slog.Error("Error finding count")

storage/storage.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type StorageAdapter interface {
2424
Delete(item any, filter map[string]any) error
2525
List(dest any, sortKey string, filter map[string]any, limit int, cursor string) (string, error)
2626
Search(dest any, sortKey string, query string, limit int, cursor string) (string, error)
27-
Count(dest any) (int64, error)
27+
Count(dest any, filter map[string]any) (int64, error)
2828
Query(dest any, statement string, limit int, cursor string) (string, error)
2929
}
3030

0 commit comments

Comments
 (0)