Skip to content

Commit f4bf384

Browse files
Copilothuangyiirene
andcommitted
Address code review feedback: improve hooks dependency array and implement bulk operations
Co-authored-by: huangyiirene <7665279+huangyiirene@users.noreply.github.com>
1 parent eb87499 commit f4bf384

2 files changed

Lines changed: 35 additions & 8 deletions

File tree

packages/data-objectql/src/ObjectQLDataSource.ts

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -333,25 +333,51 @@ export class ObjectQLDataSource<T = any> implements DataSource<T> {
333333
*
334334
* @param resource - Object name
335335
* @param operation - Operation type
336-
* @param data - Bulk data
336+
* @param data - Bulk data or filters for update/delete
337337
* @returns Promise resolving to operation results
338338
*/
339339
async bulk(
340340
resource: string,
341341
operation: 'create' | 'update' | 'delete',
342-
data: Partial<T>[]
342+
data: Partial<T>[] | any
343343
): Promise<T[]> {
344344
try {
345345
if (operation === 'create') {
346346
const response = await this.client.createMany<T>(resource, data);
347347
return response.items || [];
348348
} else if (operation === 'update') {
349-
// For bulk updates, we need to use updateMany with filters
350-
// This is a simplified implementation
351-
throw new Error('Bulk update not yet implemented with SDK');
349+
// For bulk updates with SDK, we need filters and update data
350+
// This is a limitation - the old API accepted array of records
351+
// The new SDK requires filters + data
352+
if (Array.isArray(data)) {
353+
// If array of records is provided, we need to update them individually
354+
// This is less efficient but maintains compatibility
355+
const results: T[] = [];
356+
for (const item of data) {
357+
if ('_id' in item && item._id) {
358+
const updated = await this.client.update<T>(resource, item._id, item);
359+
results.push(updated as T);
360+
}
361+
}
362+
return results;
363+
} else {
364+
throw new Error('Bulk update requires array of records with _id field');
365+
}
352366
} else if (operation === 'delete') {
353-
// For bulk deletes, we need to use deleteMany with filters
354-
throw new Error('Bulk delete not yet implemented with SDK');
367+
// For bulk deletes with SDK, similar approach
368+
if (Array.isArray(data)) {
369+
// Delete each record individually
370+
const results: T[] = [];
371+
for (const item of data) {
372+
if ('_id' in item && item._id) {
373+
await this.client.delete(resource, item._id);
374+
results.push(item as T);
375+
}
376+
}
377+
return results;
378+
} else {
379+
throw new Error('Bulk delete requires array of records with _id field');
380+
}
355381
}
356382

357383
throw new Error(`Unknown bulk operation: ${operation}`);

packages/data-objectql/src/hooks.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ export interface UseObjectQLMutationOptions {
7878
export function useObjectQL(options: UseObjectQLOptions): ObjectQLDataSource {
7979
return useMemo(
8080
() => new ObjectQLDataSource(options.config),
81-
[options.config.baseUrl, options.config.token]
81+
// Serialize the entire config to detect any changes
82+
[JSON.stringify(options.config)]
8283
);
8384
}
8485

0 commit comments

Comments
 (0)