|
1 | 1 | import { z } from 'zod'; |
| 2 | +import { FilterConditionSchema } from '../data/filter.zod'; |
| 3 | +import { SortNodeSchema } from '../data/query.zod'; |
2 | 4 |
|
3 | 5 | /** |
4 | 6 | * Data Engine Protocol |
5 | 7 | * |
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. |
9 | 12 | * |
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. |
12 | 14 | */ |
13 | 15 |
|
| 16 | +// ========================================================================== |
| 17 | +// 1. Shared Definitions |
| 18 | +// ========================================================================== |
| 19 | + |
14 | 20 | /** |
15 | 21 | * 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) |
17 | 23 | */ |
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'); |
19 | 28 |
|
20 | 29 | /** |
21 | | - * Query options for IDataEngine.find() operations |
| 30 | + * Sort order definition |
| 31 | + * Supports: |
| 32 | + * - { name: 'asc' } |
| 33 | + * - { name: 1 } |
| 34 | + * - [{ field: 'name', order: 'asc' }] |
22 | 35 | */ |
| 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 | + |
23 | 46 | export const DataEngineQueryOptionsSchema = z.object({ |
24 | | - /** Filter conditions */ |
| 47 | + /** Filter conditions (WHERE) */ |
25 | 48 | filter: DataEngineFilterSchema.optional(), |
26 | | - /** Fields to select */ |
| 49 | + |
| 50 | + /** Fields to select (SELECT) */ |
27 | 51 | 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(), |
36 | 73 | }).describe('Query options for IDataEngine.find() operations'); |
37 | 74 |
|
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 | +// ========================================================================== |
49 | 166 |
|
50 | | -/** |
51 | | - * TypeScript types derived from schemas |
52 | | - */ |
53 | 167 | export type DataEngineFilter = z.infer<typeof DataEngineFilterSchema>; |
| 168 | +export type DataEngineSort = z.infer<typeof DataEngineSortSchema>; |
54 | 169 | 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>; |
55 | 175 |
|
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