You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> **💡 Note:**`stores.All()` returns a `map[string]func() any` — a map of **factory functions**, not active instances. Use `stores.GetStore(name)` for convenient access.
> **⚠️ Naming Convention:** The registry (`stores/all.go`) automatically appends `"Store"` when building constructor calls. To avoid compilation errors, always name your interface as `<Name>Store` (e.g., `UserStore`) and the generated constructor will be `New<Name>Store()`.
167
+
153
168
### Models
154
169
155
170
**Generate a new model:**
@@ -185,10 +200,10 @@ models:
185
200
- `delete`- DELETE queries
186
201
187
202
**Return Types:**
188
-
- `single`- Returns a single model instance
189
-
- `multiple`- Returns a slice of models
190
-
- `count`- Returns `int64` count
191
-
- `custom`- Returns `interface{}`
203
+
- `single`- Returns `(*Model, error)`
204
+
- `multiple`- Returns `([]Model, error)`
205
+
- `count`- Returns `(int64, error)`
206
+
- `custom`- Returns `(any, error)`
192
207
193
208
**Example Query:**
194
209
```yaml
@@ -206,21 +221,23 @@ queries:
206
221
207
222
### Multiple Stores
208
223
224
+
You can define multiple stores in a single YAML file. Each store gets its own directory and the registry (`stores/all.go`) tracks all of them.
GetUserByID(ctx *gofr.Context, id int64) (User, error)
266
+
type UserStore interface {
267
+
GetUserByID(ctx *gofr.Context, id int64) (*User, error)
245
268
GetAllUsers(ctx *gofr.Context) ([]User, error)
246
269
}
247
270
```
@@ -253,16 +276,18 @@ package user
253
276
254
277
type userStore struct{}
255
278
256
-
func NewUser() User {
279
+
func NewUserStore() UserStore {
257
280
return &userStore{}
258
281
}
259
282
260
-
func (s *userStore) GetUserByID(ctx *gofr.Context, id int64) (User, error) {
283
+
func (s *userStore) GetUserByID(ctx *gofr.Context, id int64) (*User, error) {
261
284
// TODO: Implement query using ctx.SQL()
262
-
return User{}, nil
285
+
return &User{}, nil
263
286
}
264
287
```
265
288
289
+
> **💡 Note:** The generator creates method **signatures and boilerplate only**. You must implement the actual SQL execution in the `// TODO` sections using `ctx.SQL()` methods.
290
+
266
291
### Model
267
292
```go
268
293
// Code generated by gofr.dev/cli/gofr. DO NOT EDIT.
@@ -277,4 +302,13 @@ type User struct {
277
302
func (User) TableName() string {
278
303
return "user"
279
304
}
280
-
```
305
+
```
306
+
307
+
## Best Practices
308
+
309
+
1.**Never Edit Generated Files**: Files marked `DO NOT EDIT` (`interface.go`, `all.go`) are overwritten on every generation. Keep custom SQL logic in your implementation file (e.g., `userStore.go`).
310
+
2.**Use `<Name>Store` Interface Names**: This ensures the registry and constructor align correctly.
311
+
3.**Commit your YAML**: Treat `store.yaml` as source of truth. Re-run `gofr store generate` after every change.
312
+
4.**Reference Existing Models**: If you already have model structs, use the `path` + `package` fields to avoid duplication.
313
+
314
+
For a complete working example, see [`store/example.yaml`](./example.yaml).
0 commit comments