Skip to content

Commit c2b661a

Browse files
committed
fix(clients): allow chunked requests on WithTransformation methods
1 parent 4be66cf commit c2b661a

File tree

3 files changed

+45
-51
lines changed

3 files changed

+45
-51
lines changed

templates/javascript/clients/algoliasearch/builds/definition.mustache

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ export type Algoliasearch = SearchClient & {
4444
* @param saveObjects.waitForTasks - Whether or not we should wait until every `batch` tasks has been processed, this operation may slow the total execution time of this method but is more reliable.
4545
* @param requestOptions - The requestOptions to send along with the query, they will be forwarded to the `batch` method and merged with the transporter requestOptions.
4646
*/
47-
saveObjectsWithTransformation: (options: SaveObjectsOptions, requestOptions?: RequestOptions | undefined) => Promise<WatchResponse>;
47+
saveObjectsWithTransformation: (
48+
options: SaveObjectsOptions,
49+
requestOptions?: RequestOptions | undefined,
50+
) => Promise<WatchResponse[]>;
4851

4952
/**
5053
* Helper: Similar to the `partialUpdateObjects` method but requires a Push connector (https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/connectors/push/) to be created first, in order to transform records before indexing them to Algolia. The `region` must have been passed to the client instantiation method.
@@ -58,7 +61,10 @@ export type Algoliasearch = SearchClient & {
5861
* @param partialUpdateObjects.waitForTasks - Whether or not we should wait until every `batch` tasks has been processed, this operation may slow the total execution time of this method but is more reliable.
5962
* @param requestOptions - The requestOptions to send along with the query, they will be forwarded to the `getTask` method and merged with the transporter requestOptions.
6063
*/
61-
partialUpdateObjectsWithTransformation: (options: PartialUpdateObjectsOptions, requestOptions?: RequestOptions | undefined) => Promise<WatchResponse>;
64+
partialUpdateObjectsWithTransformation: (
65+
options: PartialUpdateObjectsOptions,
66+
requestOptions?: RequestOptions | undefined,
67+
) => Promise<WatchResponse[]>;
6268

6369
/**
6470
* Helper: Similar to the `replaceAllObjects` method but requires a Push connector (https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/connectors/push/) to be created first, in order to transform records before indexing them to Algolia. The `region` must have been passed to the client instantiation method.
@@ -86,9 +92,13 @@ export type Algoliasearch = SearchClient & {
8692
* @param chunkedPush.action - The `batch` `action` to perform on the given array of `objects`, defaults to `addObject`.
8793
* @param chunkedPush.waitForTasks - Whether or not we should wait until every `batch` tasks has been processed, this operation may slow the total execution time of this method but is more reliable.
8894
* @param chunkedPush.batchSize - The size of the chunk of `objects`. The number of `batch` calls will be equal to `length(objects) / batchSize`. Defaults to 1000.
95+
* @param chunkedPush.referenceIndexName - This is required when targeting an index that does not have a push connector setup (e.g. a tmp index), but you wish to attach another index's transformation to it (e.g. the source index name).
8996
* @param requestOptions - The requestOptions to send along with the query, they will be forwarded to the `getTask` method and merged with the transporter requestOptions.
9097
*/
91-
chunkedPush: (options: ChunkedBatchOptions, requestOptions?: RequestOptions) => Promise<Array<WatchResponse>>;
98+
chunkedPush: (
99+
options: ChunkedBatchOptions & { referenceIndexName?: string },
100+
requestOptions?: RequestOptions,
101+
) => Promise<Array<WatchResponse>>;
92102
};
93103

94104
export type TransformationOptions = {
@@ -127,55 +137,37 @@ export function algoliasearch(
127137
return {
128138
...client,
129139
130-
async saveObjectsWithTransformation({ indexName, objects, waitForTasks }, requestOptions): Promise<WatchResponse> {
131-
if (!ingestionTransporter) {
132-
throw new Error('`transformation.region` must be provided at client instantiation before calling this method.');
133-
}
134-
135-
if (!options?.transformation?.region) {
136-
throw new Error('`region` must be provided when leveraging the transformation pipeline');
137-
}
138-
139-
return ingestionTransporter?.push(
140-
{
141-
indexName,
142-
watch: waitForTasks,
143-
pushTaskPayload: {
144-
action: 'addObject',
145-
records: objects as PushTaskRecords[],
146-
},
147-
},
148-
requestOptions,
149-
);
140+
async saveObjectsWithTransformation(
141+
{ indexName, objects, waitForTasks },
142+
requestOptions,
143+
): Promise<WatchResponse[]> {
144+
return this.chunkedPush({ indexName, objects, action: 'addObject', waitForTasks }, requestOptions);
150145
},
151146

152147
async partialUpdateObjectsWithTransformation(
153148
{ indexName, objects, createIfNotExists, waitForTasks },
154149
requestOptions,
155-
): Promise<WatchResponse> {
156-
if (!ingestionTransporter) {
157-
throw new Error('`transformation.region` must be provided at client instantiation before calling this method.');
158-
}
159-
160-
if (!options?.transformation?.region) {
161-
throw new Error('`region` must be provided when leveraging the transformation pipeline');
162-
}
163-
164-
return ingestionTransporter?.push(
150+
): Promise<WatchResponse[]> {
151+
return this.chunkedPush(
165152
{
166153
indexName,
167-
watch: waitForTasks,
168-
pushTaskPayload: {
169-
action: createIfNotExists ? 'partialUpdateObject' : 'partialUpdateObjectNoCreate',
170-
records: objects as PushTaskRecords[],
171-
},
154+
objects,
155+
action: createIfNotExists ? 'partialUpdateObject' : 'partialUpdateObjectNoCreate',
156+
waitForTasks,
172157
},
173158
requestOptions,
174159
);
175160
},
176161

177162
async chunkedPush(
178-
{ indexName, objects, action = 'addObject', waitForTasks, batchSize = 1000 }: ChunkedBatchOptions,
163+
{
164+
referenceIndexName,
165+
indexName,
166+
objects,
167+
action = 'addObject',
168+
waitForTasks,
169+
batchSize = 1000,
170+
}: ChunkedBatchOptions & { referenceIndexName?: string },
179171
requestOptions?: RequestOptions,
180172
): Promise<Array<WatchResponse>> {
181173
if (!ingestionTransporter) {
@@ -195,7 +187,7 @@ export function algoliasearch(
195187
if (records.length === batchSize || i === objects.length - 1) {
196188
responses.push(
197189
await ingestionTransporter.push(
198-
{ indexName, pushTaskPayload: { action, records }, watch: waitForTasks },
190+
{ indexName, pushTaskPayload: { action, records }, watch: waitForTasks, referenceIndexName },
199191
requestOptions,
200192
),
201193
);
@@ -217,13 +209,15 @@ export function algoliasearch(
217209
throw new Error('received unexpected response from the push endpoint, eventID must not be undefined');
218210
}
219211

220-
return ingestionTransporter.getEvent({ runID: resp.runID, eventID: resp.eventID }).catch((error: ApiError) => {
221-
if (error.status === 404) {
222-
return undefined;
223-
}
212+
return ingestionTransporter
213+
.getEvent({ runID: resp.runID, eventID: resp.eventID })
214+
.catch((error: ApiError) => {
215+
if (error.status === 404) {
216+
return undefined;
217+
}
224218

225-
throw error;
226-
})
219+
throw error;
220+
});
227221
},
228222
validate: (response) => response !== undefined,
229223
aggregator: () => (retryCount += 1),
@@ -272,7 +266,7 @@ export function algoliasearch(
272266
);
273267

274268
const watchResponses = await this.chunkedPush(
275-
{ indexName: tmpIndexName, objects, waitForTasks: true, batchSize },
269+
{ indexName: tmpIndexName, objects, waitForTasks: true, batchSize, referenceIndexName: indexName },
276270
requestOptions,
277271
);
278272

tests/CTS/client/search/partialUpdateObjectsWithTransformation.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@
3535
},
3636
"expected": {
3737
"type": "response",
38-
"match": {
38+
"match": [{
3939
"runID": "b1b7a982-524c-40d2-bb7f-48aab075abda",
4040
"eventID": "113b2068-6337-4c85-b5c2-e7b213d82925",
4141
"message": "OK",
4242
"createdAt": "2022-05-12T06:24:30.049Z"
43-
}
43+
}]
4444
}
4545
}
4646
]

tests/CTS/client/search/saveObjectsWithTransformation.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@
3434
},
3535
"expected": {
3636
"type": "response",
37-
"match": {
37+
"match": [{
3838
"runID": "b1b7a982-524c-40d2-bb7f-48aab075abda",
3939
"eventID": "113b2068-6337-4c85-b5c2-e7b213d82925",
4040
"message": "OK",
4141
"createdAt": "2022-05-12T06:24:30.049Z"
42-
}
42+
}]
4343
}
4444
}
4545
]

0 commit comments

Comments
 (0)