GetByGroup retrieves all key-value pairs for a specific group with pagination support. Returns a map of keys to values or an error if the group is empty or not found.
(ctx context.Context, params KVParams)
| 238 | // GetByGroup retrieves all key-value pairs for a specific group with pagination support. |
| 239 | // Returns a map of keys to values or an error if the group is empty or not found. |
| 240 | func (kv *KVOperator) GetByGroup(ctx context.Context, params KVParams) (map[string]string, error) { |
| 241 | if params.Group == "" { |
| 242 | return nil, ErrKVGroupEmpty |
| 243 | } |
| 244 | |
| 245 | if params.Page < 1 { |
| 246 | params.Page = 1 |
| 247 | } |
| 248 | if params.PageSize < 1 { |
| 249 | params.PageSize = 10 |
| 250 | } |
| 251 | |
| 252 | query, cleanup := kv.getSession(ctx) |
| 253 | defer cleanup() |
| 254 | |
| 255 | var items []entity.PluginKVStorage |
| 256 | err := query.Where(builder.Eq{"plugin_slug_name": kv.pluginSlugName, "`group`": params.Group}). |
| 257 | Limit(params.PageSize, (params.Page-1)*params.PageSize). |
| 258 | OrderBy("id ASC"). |
| 259 | Find(&items) |
| 260 | if err != nil { |
| 261 | return nil, err |
| 262 | } |
| 263 | |
| 264 | result := make(map[string]string, len(items)) |
| 265 | for _, item := range items { |
| 266 | result[item.Key] = item.Value |
| 267 | } |
| 268 | |
| 269 | return result, nil |
| 270 | } |
| 271 | |
| 272 | // Tx executes a function within a transaction context. If the KVOperator already has a session, |
| 273 | // it will use that session. Otherwise, it creates a new transaction session. |