Skip to content

Commit 44f2e0b

Browse files
docs: add saveObjects, deleteObjects and partialUpdateObjects to helpers
algolia/api-clients-automation#3256 Co-authored-by: Clément Vannicatte <[email protected]>
1 parent e7c3b07 commit 44f2e0b

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

Sources/Search/Extra/SearchClientExtension.swift

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,71 @@ public extension SearchClient {
456456
return responses
457457
}
458458

459+
/// Helper: Saves the given array of objects in the given index. The `chunkedBatch` helper is used under the hood,
460+
/// which creates a `batch` requests with at most 1000 objects in it.
461+
/// - parameter indexName: The name of the index where to save the objects
462+
/// - parameter objects: The new objects
463+
/// - parameter requestOptions: The request options
464+
/// - returns: [BatchResponse]
465+
func saveObjects(
466+
indexName: String,
467+
objects: [some Encodable],
468+
requestOptions: RequestOptions? = nil
469+
) async throws -> [BatchResponse] {
470+
try await self.chunkedBatch(
471+
indexName: indexName,
472+
objects: objects,
473+
action: .addObject,
474+
waitForTasks: false,
475+
batchSize: 1000,
476+
requestOptions: requestOptions
477+
)
478+
}
479+
480+
/// Helper: Deletes every records for the given objectIDs. The `chunkedBatch` helper is used under the hood, which
481+
/// creates a `batch` requests with at most 1000 objectIDs in it.
482+
/// - parameter indexName: The name of the index to delete objectIDs from
483+
/// - parameter objectIDs: The objectIDs to delete
484+
/// - parameter requestOptions: The request options
485+
/// - returns: [BatchResponse]
486+
func deleteObjects(
487+
indexName: String,
488+
objectIDs: [String],
489+
requestOptions: RequestOptions? = nil
490+
) async throws -> [BatchResponse] {
491+
try await self.chunkedBatch(
492+
indexName: indexName,
493+
objects: objectIDs.map { AnyCodable(["objectID": $0]) },
494+
action: .deleteObject,
495+
waitForTasks: false,
496+
batchSize: 1000,
497+
requestOptions: requestOptions
498+
)
499+
}
500+
501+
/// Helper: Replaces object content of all the given objects according to their respective `objectID` field. The
502+
/// `chunkedBatch` helper is used under the hood, which creates a `batch` requests with at most 1000 objects in it.
503+
/// - parameter indexName: The name of the index where to update the objects
504+
/// - parameter objects: The objects to update
505+
/// - parameter createIfNotExist: To be provided if non-existing objects are passed, otherwise, the call will fail..
506+
/// - parameter requestOptions: The request options
507+
/// - returns: [BatchResponse]
508+
func partialUpdateObjects(
509+
indexName: String,
510+
objects: [some Encodable],
511+
createIfNotExist: Bool = false,
512+
requestOptions: RequestOptions? = nil
513+
) async throws -> [BatchResponse] {
514+
try await self.chunkedBatch(
515+
indexName: indexName,
516+
objects: objects,
517+
action: createIfNotExist ? .partialUpdateObject : .partialUpdateObjectNoCreate,
518+
waitForTasks: false,
519+
batchSize: 1000,
520+
requestOptions: requestOptions
521+
)
522+
}
523+
459524
/// Replace all objects in an index
460525
///
461526
/// See https://api-clients-automation.netlify.app/docs/contributing/add-new-api-client#5-helpers for implementation

0 commit comments

Comments
 (0)