GetByIDs returns hosts by IDs in one query. Preserves input order.
(ctx context.Context, ids []string)
| 93 | |
| 94 | // GetByIDs returns hosts by IDs in one query. Preserves input order. |
| 95 | func (s *HostsStore) GetByIDs(ctx context.Context, ids []string) ([]models.Host, error) { |
| 96 | d := s.db.DB(ctx) |
| 97 | if len(ids) == 0 { |
| 98 | return nil, nil |
| 99 | } |
| 100 | rows, err := d.Queries.GetHostsByIDs(ctx, ids) |
| 101 | if err != nil { |
| 102 | return nil, err |
| 103 | } |
| 104 | byID := make(map[string]models.Host) |
| 105 | for _, h := range rows { |
| 106 | byID[h.ID] = *dbHostToModel(h) |
| 107 | } |
| 108 | ordered := make([]models.Host, 0, len(ids)) |
| 109 | for _, id := range ids { |
| 110 | if h, ok := byID[id]; ok { |
| 111 | ordered = append(ordered, h) |
| 112 | } |
| 113 | } |
| 114 | return ordered, nil |
| 115 | } |
| 116 | |
| 117 | // GetByApiID returns a host by api_id. |
| 118 | func (s *HostsStore) GetByApiID(ctx context.Context, apiID string) (*models.Host, error) { |
no test coverage detected