@@ -11,24 +11,26 @@ import (
11
11
type Database [T any ] struct {
12
12
auth model.Auth
13
13
conf model.DatabaseConfig
14
+ col string
14
15
}
15
16
16
17
// Collection returns a ready to use Database to perform operations on a specific type
17
- func Collection [T any ](auth model.Auth , base model.DatabaseConfig ) Database [T ] {
18
+ func Collection [T any ](auth model.Auth , base model.DatabaseConfig , col string ) Database [T ] {
18
19
return Database [T ]{
19
20
auth : auth ,
20
21
conf : base ,
22
+ col : col ,
21
23
}
22
24
}
23
25
24
26
// Create creates a record in the collection/repository
25
- func (d Database [T ]) Create (col string , data T ) (inserted T , err error ) {
27
+ func (d Database [T ]) Create (data T ) (inserted T , err error ) {
26
28
doc , err := toDoc (data )
27
29
if err != nil {
28
30
return
29
31
}
30
32
31
- doc , err = DB .CreateDocument (d .auth , d .conf .Name , col , doc )
33
+ doc , err = DB .CreateDocument (d .auth , d .conf .Name , d . col , doc )
32
34
if err != nil {
33
35
return
34
36
}
@@ -37,7 +39,7 @@ func (d Database[T]) Create(col string, data T) (inserted T, err error) {
37
39
}
38
40
39
41
// BulkCreate creates multiple records in the collection/repository
40
- func (d Database [T ]) BulkCreate (col string , entities []T ) error {
42
+ func (d Database [T ]) BulkCreate (entities []T ) error {
41
43
docs := make ([]interface {}, 0 )
42
44
43
45
for _ , doc := range entities {
@@ -48,7 +50,7 @@ func (d Database[T]) BulkCreate(col string, entities []T) error {
48
50
49
51
docs = append (docs , x )
50
52
}
51
- return DB .BulkCreateDocument (d .auth , d .conf .Name , col , docs )
53
+ return DB .BulkCreateDocument (d .auth , d .conf .Name , d . col , docs )
52
54
}
53
55
54
56
// PageResult wraps a slice of type T with paging information
@@ -60,8 +62,8 @@ type PagedResult[T any] struct {
60
62
}
61
63
62
64
// List returns records from a collection/repository using paging/sorting params
63
- func (d Database [T ]) List (col string , lp model.ListParams ) (res PagedResult [T ], err error ) {
64
- r , err := DB .ListDocuments (d .auth , d .conf .Name , col , lp )
65
+ func (d Database [T ]) List (lp model.ListParams ) (res PagedResult [T ], err error ) {
66
+ r , err := DB .ListDocuments (d .auth , d .conf .Name , d . col , lp )
65
67
if err != nil {
66
68
return
67
69
}
@@ -83,13 +85,13 @@ func (d Database[T]) List(col string, lp model.ListParams) (res PagedResult[T],
83
85
}
84
86
85
87
// Query returns records that match with the provided filters.
86
- func (d Database [T ]) Query (col string , filters [][]any , lp model.ListParams ) (res PagedResult [T ], err error ) {
88
+ func (d Database [T ]) Query (filters [][]any , lp model.ListParams ) (res PagedResult [T ], err error ) {
87
89
clauses , err := DB .ParseQuery (filters )
88
90
if err != nil {
89
91
return
90
92
}
91
93
92
- r , err := DB .QueryDocuments (d .auth , d .conf .Name , col , clauses , lp )
94
+ r , err := DB .QueryDocuments (d .auth , d .conf .Name , d . col , clauses , lp )
93
95
if err != nil {
94
96
return
95
97
}
@@ -111,8 +113,8 @@ func (d Database[T]) Query(col string, filters [][]any, lp model.ListParams) (re
111
113
}
112
114
113
115
// GetByID returns a specific record from a collection/repository
114
- func (d Database [T ]) GetByID (col , id string ) (entity T , err error ) {
115
- doc , err := DB .GetDocumentByID (d .auth , d .conf .Name , col , id )
116
+ func (d Database [T ]) GetByID (id string ) (entity T , err error ) {
117
+ doc , err := DB .GetDocumentByID (d .auth , d .conf .Name , d . col , id )
116
118
if err != nil {
117
119
return
118
120
}
@@ -122,13 +124,13 @@ func (d Database[T]) GetByID(col, id string) (entity T, err error) {
122
124
}
123
125
124
126
// Update updates some fields of a record
125
- func (d Database [T ]) Update (col , id string , v any ) (entity T , err error ) {
127
+ func (d Database [T ]) Update (id string , v any ) (entity T , err error ) {
126
128
doc , err := toDoc (v )
127
129
if err != nil {
128
130
return
129
131
}
130
132
131
- x , err := DB .UpdateDocument (d .auth , d .conf .Name , col , id , doc )
133
+ x , err := DB .UpdateDocument (d .auth , d .conf .Name , d . col , id , doc )
132
134
if err != nil {
133
135
return
134
136
}
@@ -138,7 +140,7 @@ func (d Database[T]) Update(col, id string, v any) (entity T, err error) {
138
140
}
139
141
140
142
// UpdateMany updates multiple records matching filters
141
- func (d Database [T ]) UpdateMany (col string , filters [][]any , v any ) (int64 , error ) {
143
+ func (d Database [T ]) UpdateMany (filters [][]any , v any ) (int64 , error ) {
142
144
clauses , err := DB .ParseQuery (filters )
143
145
if err != nil {
144
146
return 0 , err
@@ -148,17 +150,17 @@ func (d Database[T]) UpdateMany(col string, filters [][]any, v any) (int64, erro
148
150
if err != nil {
149
151
return 0 , err
150
152
}
151
- return DB .UpdateDocuments (d .auth , d .conf .Name , col , clauses , doc )
153
+ return DB .UpdateDocuments (d .auth , d .conf .Name , d . col , clauses , doc )
152
154
}
153
155
154
156
// IncrementValue increments or decrements a specifc field from a collection/repository
155
- func (d Database [T ]) IncrementValue (col , id , field string , n int ) error {
156
- return DB .IncrementValue (d .auth , d .conf .Name , col , id , field , n )
157
+ func (d Database [T ]) IncrementValue (id , field string , n int ) error {
158
+ return DB .IncrementValue (d .auth , d .conf .Name , d . col , id , field , n )
157
159
}
158
160
159
161
// Delete removes a record from a collection
160
- func (d Database [T ]) Delete (col , id string ) (int64 , error ) {
161
- return DB .DeleteDocument (d .auth , d .conf .Name , col , id )
162
+ func (d Database [T ]) Delete (id string ) (int64 , error ) {
163
+ return DB .DeleteDocument (d .auth , d .conf .Name , d . col , id )
162
164
}
163
165
164
166
func toDoc (v any ) (doc map [string ]any , err error ) {
0 commit comments