Skip to content

Commit dfa9c25

Browse files
committed
feat: 更新数据请求和响应类型,改为使用输入类型以增强类型安全
1 parent 7321f6b commit dfa9c25

3 files changed

Lines changed: 45 additions & 30 deletions

File tree

packages/objectql/src/engine.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { QueryAST, HookContext, DataEngineRequestSchema } from '@objectstack/spec/data';
1+
import { QueryAST, HookContext } from '@objectstack/spec/data';
22
import {
33
DataEngineQueryOptions,
44
DataEngineInsertOptions,
@@ -454,6 +454,7 @@ export class ObjectQL implements IDataEngine {
454454

455455
async aggregate(object: string, query: DataEngineAggregateOptions): Promise<any[]> {
456456
const driver = this.getDriver(object);
457+
this.logger.debug(`Aggregate on ${object} using ${driver.name}`, query);
457458
// Driver needs support for raw aggregation or mapped aggregation
458459
// For now, if driver supports 'execute', we might pass it down, or we need to add 'aggregate' to DriverInterface
459460
// In this version, we'll assume driver might handle it via special 'find' or throw not implemented

packages/objectql/src/protocol.ts

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { IDataEngine } from '@objectstack/core';
33
import type {
44
BatchUpdateRequest,
55
BatchUpdateResponse,
6-
UpdateManyRequest,
7-
DeleteManyRequest,
6+
UpdateManyDataRequest,
7+
DeleteManyDataRequest,
88
BatchOperationResult
99
} from '@objectstack/spec/api';
1010
import type { MetadataCacheRequest, MetadataCacheResponse } from '@objectstack/spec/api';
@@ -122,7 +122,9 @@ export class ObjectStackProtocolImplementation implements ObjectStackProtocol {
122122
const records = await this.engine.find(request.object, options);
123123
return {
124124
object: request.object,
125-
records
125+
records,
126+
total: records.length,
127+
hasMore: false
126128
};
127129
}
128130

@@ -234,17 +236,16 @@ export class ObjectStackProtocolImplementation implements ObjectStackProtocol {
234236
};
235237
}
236238

237-
async updateManyData(request: UpdateManyRequest): Promise<any> {
238-
return this.engine.update(request.object, request.data, {
239-
filter: request.filter,
240-
multi: true
241-
});
239+
async updateManyData(request: UpdateManyDataRequest): Promise<any> {
240+
// TODO: Implement proper updateMany in DataEngine
241+
throw new Error('updateManyData not implemented');
242242
}
243243

244-
async deleteManyData(request: DeleteManyRequest): Promise<any> {
244+
async deleteManyData(request: DeleteManyDataRequest): Promise<any> {
245+
// This expects deleting by IDs.
245246
return this.engine.delete(request.object, {
246-
filter: request.filter,
247-
multi: true
247+
filter: { _id: { $in: request.ids } },
248+
...request.options
248249
});
249250
}
250251

@@ -259,36 +260,49 @@ export class ObjectStackProtocolImplementation implements ObjectStackProtocol {
259260
...request,
260261
createdAt: new Date().toISOString(),
261262
updatedAt: new Date().toISOString(),
262-
owner: 'system'
263-
};
263+
createdBy: 'system',
264+
updatedBy: 'system'
265+
} as SavedView;
266+
264267
this.viewStorage.set(id, view);
265-
return { success: true, view };
268+
return { success: true, data: view };
266269
}
267270

268271
async getView(request: { id: string }): Promise<ViewResponse> {
269272
const view = this.viewStorage.get(request.id);
270273
if (!view) throw new Error(`View ${request.id} not found`);
271-
return { success: true, view };
274+
return { success: true, data: view };
272275
}
273276

274277
async listViews(request: ListViewsRequest): Promise<ListViewsResponse> {
275278
const views = Array.from(this.viewStorage.values())
276279
.filter(v => !request?.object || v.object === request.object);
277-
return { success: true, views, total: views.length };
280+
281+
return {
282+
success: true,
283+
data: views,
284+
pagination: {
285+
total: views.length,
286+
limit: request.limit || 50,
287+
offset: request.offset || 0,
288+
hasMore: false
289+
}
290+
};
278291
}
279292

280293
async updateView(request: UpdateViewRequest): Promise<ViewResponse> {
281294
const view = this.viewStorage.get(request.id);
282295
if (!view) throw new Error(`View ${request.id} not found`);
283296

284-
const updated = { ...view, ...request.updates, updatedAt: new Date().toISOString() };
297+
const { id, ...updates } = request;
298+
const updated = { ...view, ...updates, updatedAt: new Date().toISOString() };
285299
this.viewStorage.set(request.id, updated);
286-
return { success: true, view: updated };
300+
return { success: true, data: updated };
287301
}
288302

289-
async deleteView(request: { id: string }): Promise<{ success: boolean }> {
303+
async deleteView(request: { id: string }): Promise<{ success: boolean, object: string, id: string }> {
290304
const deleted = this.viewStorage.delete(request.id);
291305
if (!deleted) throw new Error(`View ${request.id} not found`);
292-
return { success: true };
306+
return { success: true, object: 'view', id: request.id };
293307
}
294308
}

packages/spec/src/api/protocol.zod.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -456,24 +456,24 @@ export type GetMetaItemCachedResponse = z.infer<typeof GetMetaItemCachedResponse
456456
export type GetUiViewRequest = z.infer<typeof GetUiViewRequestSchema>;
457457
export type GetUiViewResponse = z.infer<typeof GetUiViewResponseSchema>;
458458

459-
export type FindDataRequest = z.infer<typeof FindDataRequestSchema>;
459+
export type FindDataRequest = z.input<typeof FindDataRequestSchema>;
460460
export type FindDataResponse = z.infer<typeof FindDataResponseSchema>;
461-
export type GetDataRequest = z.infer<typeof GetDataRequestSchema>;
461+
export type GetDataRequest = z.input<typeof GetDataRequestSchema>;
462462
export type GetDataResponse = z.infer<typeof GetDataResponseSchema>;
463-
export type CreateDataRequest = z.infer<typeof CreateDataRequestSchema>;
463+
export type CreateDataRequest = z.input<typeof CreateDataRequestSchema>;
464464
export type CreateDataResponse = z.infer<typeof CreateDataResponseSchema>;
465-
export type UpdateDataRequest = z.infer<typeof UpdateDataRequestSchema>;
465+
export type UpdateDataRequest = z.input<typeof UpdateDataRequestSchema>;
466466
export type UpdateDataResponse = z.infer<typeof UpdateDataResponseSchema>;
467-
export type DeleteDataRequest = z.infer<typeof DeleteDataRequestSchema>;
467+
export type DeleteDataRequest = z.input<typeof DeleteDataRequestSchema>;
468468
export type DeleteDataResponse = z.infer<typeof DeleteDataResponseSchema>;
469469

470-
export type BatchDataRequest = z.infer<typeof BatchDataRequestSchema>;
470+
export type BatchDataRequest = z.input<typeof BatchDataRequestSchema>;
471471
export type BatchDataResponse = z.infer<typeof BatchDataResponseSchema>;
472-
export type CreateManyDataRequest = z.infer<typeof CreateManyDataRequestSchema>;
472+
export type CreateManyDataRequest = z.input<typeof CreateManyDataRequestSchema>;
473473
export type CreateManyDataResponse = z.infer<typeof CreateManyDataResponseSchema>;
474-
export type UpdateManyDataRequest = z.infer<typeof UpdateManyDataRequestSchema>;
474+
export type UpdateManyDataRequest = z.input<typeof UpdateManyDataRequestSchema>;
475475
export type UpdateManyDataResponse = z.infer<typeof UpdateManyDataResponseSchema>;
476-
export type DeleteManyDataRequest = z.infer<typeof DeleteManyDataRequestSchema>;
476+
export type DeleteManyDataRequest = z.input<typeof DeleteManyDataRequestSchema>;
477477
export type DeleteManyDataResponse = z.infer<typeof DeleteManyDataResponseSchema>;
478478

479479
export type GetViewRequest = z.infer<typeof GetViewRequestSchema>;

0 commit comments

Comments
 (0)