Skip to content

Commit 91ea3b0

Browse files
committed
完善数据引擎协议,重构查询、插入、更新、删除和聚合选项的模式定义,增强文档描述
1 parent 4d29ab5 commit 91ea3b0

1 file changed

Lines changed: 152 additions & 34 deletions

File tree

packages/spec/src/system/data-engine.zod.ts

Lines changed: 152 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,175 @@
11
import { z } from 'zod';
2+
import { FilterConditionSchema } from '../data/filter.zod';
3+
import { SortNodeSchema } from '../data/query.zod';
24

35
/**
46
* Data Engine Protocol
57
*
6-
* Defines the standard interface for data persistence engines.
7-
* This allows different data engines (ObjectQL, Prisma, TypeORM, etc.)
8-
* to be used interchangeably through a common interface.
8+
* Defines the standard interface for data persistence engines in ObjectStack.
9+
* This protocol abstracts the underlying storage mechanism (SQL, NoSQL, API, Memory),
10+
* allowing the ObjectQL engine to execute standardized CRUD and Aggregation operations
11+
* regardless of where the data resides.
912
*
10-
* Following the Dependency Inversion Principle - plugins depend on this interface,
11-
* not on concrete database implementations.
13+
* The Data Engine acts as the "Driver" layer in the Hexagonal Architecture.
1214
*/
1315

16+
// ==========================================================================
17+
// 1. Shared Definitions
18+
// ==========================================================================
19+
1420
/**
1521
* Data Engine Query filter conditions
16-
* Simple key-value filter structure for IDataEngine.find() operations
22+
* Supports simple key-value map or complex Logic/Field expressions (DSL)
1723
*/
18-
export const DataEngineFilterSchema = z.record(z.any()).describe('Data Engine query filter conditions');
24+
export const DataEngineFilterSchema = z.union([
25+
z.record(z.any()),
26+
FilterConditionSchema
27+
]).describe('Data Engine query filter conditions');
1928

2029
/**
21-
* Query options for IDataEngine.find() operations
30+
* Sort order definition
31+
* Supports:
32+
* - { name: 'asc' }
33+
* - { name: 1 }
34+
* - [{ field: 'name', order: 'asc' }]
2235
*/
36+
export const DataEngineSortSchema = z.union([
37+
z.record(z.enum(['asc', 'desc'])),
38+
z.record(z.union([z.literal(1), z.literal(-1)])),
39+
z.array(SortNodeSchema)
40+
]).describe('Sort order definition');
41+
42+
// ==========================================================================
43+
// 2. method: FIND
44+
// ==========================================================================
45+
2346
export const DataEngineQueryOptionsSchema = z.object({
24-
/** Filter conditions */
47+
/** Filter conditions (WHERE) */
2548
filter: DataEngineFilterSchema.optional(),
26-
/** Fields to select */
49+
50+
/** Fields to select (SELECT) */
2751
select: z.array(z.string()).optional(),
28-
/** Sort order */
29-
sort: z.record(z.union([z.literal(1), z.literal(-1), z.literal('asc'), z.literal('desc')])).optional(),
30-
/** Limit number of results (alternative name for top, used by some drivers) */
31-
limit: z.number().optional(),
32-
/** Skip number of results (for pagination) */
33-
skip: z.number().optional(),
34-
/** Maximum number of results (OData-style, takes precedence over limit if both specified) */
35-
top: z.number().optional(),
52+
53+
/** Sort order (ORDER BY) */
54+
sort: DataEngineSortSchema.optional(),
55+
56+
/** Limit number of results (LIMIT) */
57+
limit: z.number().int().min(1).optional(),
58+
59+
/** Skip number of results (OFFSET) */
60+
skip: z.number().int().min(0).optional(),
61+
62+
/**
63+
* Maximum number of results (OData style)
64+
* Takes precedence over limit if both specified
65+
*/
66+
top: z.number().int().min(1).optional(),
67+
68+
/**
69+
* Include related records (JOIN/Populate)
70+
* List of relationship field names to expand
71+
*/
72+
populate: z.array(z.string()).optional(),
3673
}).describe('Query options for IDataEngine.find() operations');
3774

38-
/**
39-
* Data Engine Interface Schema
40-
*
41-
* Defines the contract for data engine implementations.
42-
* (Deprecated: Moved to @objectstack/core/contracts/data-engine)
43-
*/
44-
/*
45-
export const DataEngineSchema = z.object({
46-
...
47-
}).describe('Data Engine Interface');
48-
*/
75+
// ==========================================================================
76+
// 3. method: INSERT
77+
// ==========================================================================
78+
79+
export const DataEngineInsertOptionsSchema = z.object({
80+
/**
81+
* Return the inserted record(s)?
82+
* Some drivers support RETURNING clause for efficiency.
83+
* Default: true
84+
*/
85+
returning: z.boolean().default(true).optional(),
86+
}).describe('Options for DataEngine.insert operations');
87+
88+
// ==========================================================================
89+
// 4. method: UPDATE
90+
// ==========================================================================
91+
92+
export const DataEngineUpdateOptionsSchema = z.object({
93+
/** Filter conditions to identify records to update */
94+
filter: DataEngineFilterSchema.optional(),
95+
96+
/**
97+
* Perform an upsert?
98+
* If true, insert if not found.
99+
*/
100+
upsert: z.boolean().default(false).optional(),
101+
102+
/**
103+
* Update multiple records?
104+
* If false, only the first match is updated.
105+
* Default: false
106+
*/
107+
multi: z.boolean().default(false).optional(),
108+
109+
/**
110+
* Return the updated record(s)?
111+
* Default: false (returns update count/status)
112+
*/
113+
returning: z.boolean().default(false).optional(),
114+
}).describe('Options for DataEngine.update operations');
115+
116+
// ==========================================================================
117+
// 5. method: DELETE
118+
// ==========================================================================
119+
120+
export const DataEngineDeleteOptionsSchema = z.object({
121+
/** Filter conditions to identify records to delete */
122+
filter: DataEngineFilterSchema.optional(),
123+
124+
/**
125+
* Delete multiple records?
126+
* If false, only the first match is deleted.
127+
* Default: false
128+
*/
129+
multi: z.boolean().default(false).optional(),
130+
}).describe('Options for DataEngine.delete operations');
131+
132+
// ==========================================================================
133+
// 6. method: AGGREGATE
134+
// ==========================================================================
135+
136+
export const DataEngineAggregateOptionsSchema = z.object({
137+
/** Filter conditions (WHERE) */
138+
filter: DataEngineFilterSchema.optional(),
139+
140+
/** Group By fields */
141+
groupBy: z.array(z.string()).optional(),
142+
143+
/**
144+
* Aggregation definitions
145+
* e.g. [{ field: 'amount', method: 'sum', alias: 'total' }]
146+
*/
147+
aggregations: z.array(z.object({
148+
field: z.string(),
149+
method: z.enum(['count', 'sum', 'avg', 'min', 'max', 'count_distinct']),
150+
alias: z.string().optional()
151+
})).optional(),
152+
}).describe('Options for DataEngine.aggregate operations');
153+
154+
// ==========================================================================
155+
// 7. method: COUNT
156+
// ==========================================================================
157+
158+
export const DataEngineCountOptionsSchema = z.object({
159+
/** Filter conditions */
160+
filter: DataEngineFilterSchema.optional(),
161+
}).describe('Options for DataEngine.count operations');
162+
163+
// ==========================================================================
164+
// 8. Type Exports
165+
// ==========================================================================
49166

50-
/**
51-
* TypeScript types derived from schemas
52-
*/
53167
export type DataEngineFilter = z.infer<typeof DataEngineFilterSchema>;
168+
export type DataEngineSort = z.infer<typeof DataEngineSortSchema>;
54169
export type DataEngineQueryOptions = z.infer<typeof DataEngineQueryOptionsSchema>;
170+
export type DataEngineInsertOptions = z.infer<typeof DataEngineInsertOptionsSchema>;
171+
export type DataEngineUpdateOptions = z.infer<typeof DataEngineUpdateOptionsSchema>;
172+
export type DataEngineDeleteOptions = z.infer<typeof DataEngineDeleteOptionsSchema>;
173+
export type DataEngineAggregateOptions = z.infer<typeof DataEngineAggregateOptionsSchema>;
174+
export type DataEngineCountOptions = z.infer<typeof DataEngineCountOptionsSchema>;
55175

56-
// Moved IDataEngine interface to @objectstack/core to separate runtime contract from data schema
57-
// Moved IDataEngine interface to @objectstack/core to separate runtime contract from data schema

0 commit comments

Comments
 (0)