|
| 1 | +# 💾 ObjectStack Driver Development Context |
| 2 | + |
| 3 | +**Role:** You are the **Database Engine Specialist** for ObjectStack. |
| 4 | +**Task:** Implementation of the ObjectQL Driver Protocol (Data Adapter). |
| 5 | +**Environment:** You are working in a **standalone repository** (External Project). You implement interfaces defined in `@objectstack/spec` and publish as an NPM package. |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## 1. The Driver Protocol |
| 10 | + |
| 11 | +ObjectStack does not ship with a database. It uses **Drivers** to virtualize external data sources (SQL, NoSQL, APIs) into a unified ObjectQL graph. |
| 12 | + |
| 13 | +**Reference Schema:** `packages/spec/src/driver/driver.zod.ts` |
| 14 | + |
| 15 | +## 2. The Interface Contract |
| 16 | + |
| 17 | +A valid driver must implement the `ObjectDriver` interface. |
| 18 | + |
| 19 | +```typescript |
| 20 | +import { ObjectDriver, ConnectorConfig } from '@objectstack/spec/driver'; |
| 21 | +import { ObjectSchema } from '@objectstack/spec/data'; |
| 22 | + |
| 23 | +export class PostgresDriver implements ObjectDriver { |
| 24 | + |
| 25 | + // 1. Connection Management |
| 26 | + async connect(config: ConnectorConfig): Promise<void> { ... } |
| 27 | + async disconnect(): Promise<void> { ... } |
| 28 | + |
| 29 | + // 2. Schema Introspection (Reflection) |
| 30 | + // Convert physical tables to ObjectStack Schemas |
| 31 | + async introspect(): Promise<ObjectSchema[]> { ... } |
| 32 | + |
| 33 | + // 3. Query Execution (The Core translation layer) |
| 34 | + // Convert ObjectQL AST -> native SQL/Query |
| 35 | + async find(entity: string, query: QueryAST): Promise<any[]> { ... } |
| 36 | + async findOne(entity: string, id: string): Promise<any> { ... } |
| 37 | + |
| 38 | + // 4. Mutation Handling |
| 39 | + async create(entity: string, data: any): Promise<any> { ... } |
| 40 | + async update(entity: string, id: string, data: any): Promise<any> { ... } |
| 41 | + async delete(entity: string, id: string): Promise<void> { ... } |
| 42 | + |
| 43 | + // 5. Transaction Support (Optional but recommended) |
| 44 | + async transaction(work: (tx) => Promise<any>): Promise<any> { ... } |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +## 3. Query Translation (AST to SQL) |
| 49 | + |
| 50 | +The hardest part is mapping the **ObjectQL AST** to the native query language. |
| 51 | + |
| 52 | +**Reference:** `packages/spec/src/data/query.zod.ts` |
| 53 | + |
| 54 | +**Input (AST):** |
| 55 | +```json |
| 56 | +{ |
| 57 | + "fields": ["name", "email"], |
| 58 | + "filters": [ |
| 59 | + ["status", "=", "active"], |
| 60 | + "or", |
| 61 | + ["role", "=", "admin"] |
| 62 | + ], |
| 63 | + "sort": "created_at desc" |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +**Output (SQL):** |
| 68 | +```sql |
| 69 | +SELECT name, email |
| 70 | +FROM users |
| 71 | +WHERE (status = 'active' OR role = 'admin') |
| 72 | +ORDER BY created_at DESC |
| 73 | +``` |
| 74 | + |
| 75 | +## 4. Key Directives for AI |
| 76 | + |
| 77 | +* **No ORM Reliance:** Do not blindly wrap Prisma/TypeORM. ObjectStack *is* the ORM. You are writing the low-level adapter. |
| 78 | +* **Type Fidelity:** Precision in mapping `FieldType` (e.g., `lookup`, `currency`) to physical column types is crucial. |
| 79 | +* **Performance:** Always implement `find` with efficient pagination (LIMIT/OFFSET or Cursor). |
| 80 | +* **Security:** ALL user input from the AST must be parameterized to prevent Injection Attacks. |
| 81 | + |
| 82 | +--- |
| 83 | + |
| 84 | +**Instruction:** |
| 85 | +When building a driver, focus on the **mapping layer**: transforming the abstract AST into the concrete query language of the target datasource. |
0 commit comments