|
| 1 | +--- |
| 2 | +title: Building a Custom Driver |
| 3 | +description: Learn how to implement a custom database driver for ObjectStack using the Driver Interface. |
| 4 | +--- |
| 5 | + |
| 6 | +# Building a Custom Driver |
| 7 | + |
| 8 | +Drivers are the bridge between the **ObjectQL** engine and the underlying data storage (SQL, NoSQL, APIs, etc.). By implementing the `DriverInterface`, you can connect ObjectStack to any data source. |
| 9 | + |
| 10 | +In this guide, we will walk through creating a simple **In-Memory Driver** as an example. |
| 11 | + |
| 12 | +## Prerequisites |
| 13 | + |
| 14 | +- Existing ObjectStack workspace or plugin package. |
| 15 | +- Dependency on `@objectstack/objectql` and `@objectstack/spec`. |
| 16 | + |
| 17 | +## 1. The Driver Interface |
| 18 | + |
| 19 | +All drivers must implement the `DriverInterface` defined in the specification. |
| 20 | + |
| 21 | +```typescript |
| 22 | +import { DriverInterface, DriverOptions } from '@objectstack/spec'; |
| 23 | +import { QueryAST, QueryResult } from '@objectstack/objectql'; |
| 24 | + |
| 25 | +export class MyCustomDriver implements DriverInterface { |
| 26 | + name = 'MyCustomDriver'; |
| 27 | + |
| 28 | + async connect() { |
| 29 | + // Initialize connection |
| 30 | + } |
| 31 | + |
| 32 | + async disconnect() { |
| 33 | + // Close connection |
| 34 | + } |
| 35 | + |
| 36 | + // ... CRUD Methods |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +## 2. Implementing CRUD Operations |
| 41 | + |
| 42 | +You need to implement `find`, `insert`, `update`, and `delete` methods. |
| 43 | + |
| 44 | +### Find (Query) |
| 45 | + |
| 46 | +The `find` method receives a `QueryAST` object, which contains structured query information (filters, sorting, pagination). |
| 47 | + |
| 48 | +```typescript |
| 49 | +async find(object: string, query: QueryAST, options?: DriverOptions): Promise<QueryResult> { |
| 50 | + // 1. Convert QueryAST to your database's query language (e.g., SQL) |
| 51 | + // 2. Execute query |
| 52 | + // 3. Return results as an array of objects |
| 53 | + return []; |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +### Insert (Create) |
| 58 | + |
| 59 | +```typescript |
| 60 | +async insert(object: string, data: Record<string, any>, options?: DriverOptions) { |
| 61 | + // Insert data into the storage |
| 62 | + return data; |
| 63 | +} |
| 64 | +``` |
| 65 | +
|
| 66 | +### Update |
| 67 | +
|
| 68 | +```typescript |
| 69 | +async update(object: string, id: string, data: Record<string, any>, options?: DriverOptions) { |
| 70 | + // Update the record identified by `id` |
| 71 | + return { id, ...data }; |
| 72 | +} |
| 73 | +``` |
| 74 | +
|
| 75 | +### Delete |
| 76 | +
|
| 77 | +```typescript |
| 78 | +async delete(object: string, id: string, options?: DriverOptions) { |
| 79 | + // Remove the record |
| 80 | +} |
| 81 | +``` |
| 82 | +
|
| 83 | +## 3. Handling Types |
| 84 | +
|
| 85 | +Use the types provided by `@objectstack/objectql` to ensure compatibility. |
| 86 | +
|
| 87 | +- `QueryAST`: The universal abstract syntax tree for queries. |
| 88 | +- `QueryResult`: `Record<string, any>[]`. |
| 89 | + |
| 90 | +## 4. Registering the Driver |
| 91 | + |
| 92 | +In your plugin's runtime entry point (e.g., `index.ts`), export the driver so it can be used by the engine. |
| 93 | + |
| 94 | +```typescript |
| 95 | +import { MyCustomDriver } from './my-driver'; |
| 96 | + |
| 97 | +export default { |
| 98 | + drivers: [new MyCustomDriver()] |
| 99 | +}; |
| 100 | +``` |
| 101 | + |
| 102 | +## Example: In-Memory Driver |
| 103 | + |
| 104 | +See the reference implementation in `examples/plugin-driver-memory`. |
| 105 | + |
| 106 | +```typescript |
| 107 | +// examples/plugin-driver-memory/src/memory-driver.ts |
| 108 | +export class InMemoryDriver implements DriverInterface { |
| 109 | + name = 'InMemory'; |
| 110 | + private store = new Map<string, Map<string, any>>(); |
| 111 | + |
| 112 | + async find(object: string, query: QueryAST) { |
| 113 | + const table = this.store.get(object) || new Map(); |
| 114 | + // Simple filter implementation... |
| 115 | + return Array.from(table.values()); |
| 116 | + } |
| 117 | + // ... |
| 118 | +} |
| 119 | +``` |
0 commit comments