upsertDocument handles the final upsert operation to Typesense
(ctx context.Context, table string, data map[string]interface{})
| 412 | |
| 413 | // upsertDocument handles the final upsert operation to Typesense |
| 414 | func (t *TypesenseClient) upsertDocument(ctx context.Context, table string, data map[string]interface{}) error { |
| 415 | idField := t.getIDField(table) |
| 416 | |
| 417 | if idField != "" { |
| 418 | if id, ok := data[idField].(string); ok && id != "" { |
| 419 | // Upsert the document in Typesense with the provided ID |
| 420 | data["id"] = id |
| 421 | _, err := t.Client.Collection(table).Documents().Upsert(ctx, data) |
| 422 | if err != nil { |
| 423 | return fmt.Errorf("failed to upsert document in Typesense: %w", err) |
| 424 | } |
| 425 | return nil |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | // For other collections, perform a regular upsert |
| 430 | _, err := t.Client.Collection(table).Documents().Upsert(ctx, data) |
| 431 | if err != nil { |
| 432 | return fmt.Errorf("failed to index document in Typesense: %w", err) |
| 433 | } |
| 434 | |
| 435 | return nil |
| 436 | } |
| 437 | |
| 438 | // MigrateTypeSenseSchema adds new fields and removes old fields from the existing collection schema in Typesense. |
| 439 | // This is useful when the schema has been updated, and fields need to be added or removed. |
no test coverage detected