|
| 1 | +# @objectstack/runtime |
| 2 | + |
| 3 | +ObjectStack Core Runtime & Query Engine |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The runtime package provides the `ObjectStackKernel` - the central orchestrator for ObjectStack applications. It manages the application lifecycle, plugins, and the ObjectQL data engine. |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +```bash |
| 12 | +npm install @objectstack/runtime |
| 13 | +``` |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +### Basic Setup |
| 18 | + |
| 19 | +```typescript |
| 20 | +import { ObjectStackKernel, ObjectQLPlugin } from '@objectstack/runtime'; |
| 21 | +import { InMemoryDriver } from '@objectstack/driver-memory'; |
| 22 | + |
| 23 | +const kernel = new ObjectStackKernel([ |
| 24 | + // Register ObjectQL engine |
| 25 | + new ObjectQLPlugin(), |
| 26 | + |
| 27 | + // Add database driver |
| 28 | + new InMemoryDriver(), |
| 29 | + |
| 30 | + // Add your app configurations |
| 31 | + // appConfig, |
| 32 | +]); |
| 33 | + |
| 34 | +await kernel.start(); |
| 35 | +``` |
| 36 | + |
| 37 | +### Custom ObjectQL Instance |
| 38 | + |
| 39 | +If you have a separate ObjectQL implementation or need custom configuration: |
| 40 | + |
| 41 | +```typescript |
| 42 | +import { ObjectStackKernel, ObjectQLPlugin, ObjectQL } from '@objectstack/runtime'; |
| 43 | + |
| 44 | +// Create custom ObjectQL instance |
| 45 | +const customQL = new ObjectQL({ |
| 46 | + env: 'production', |
| 47 | + customConfig: true |
| 48 | +}); |
| 49 | + |
| 50 | +// Pre-configure with custom hooks |
| 51 | +customQL.registerHook('beforeInsert', async (ctx) => { |
| 52 | + console.log(`Inserting into ${ctx.object}`); |
| 53 | +}); |
| 54 | + |
| 55 | +const kernel = new ObjectStackKernel([ |
| 56 | + // Use your custom ObjectQL instance |
| 57 | + new ObjectQLPlugin(customQL), |
| 58 | + |
| 59 | + // ... other plugins |
| 60 | +]); |
| 61 | + |
| 62 | +await kernel.start(); |
| 63 | +``` |
| 64 | + |
| 65 | +### Backward Compatibility |
| 66 | + |
| 67 | +For backward compatibility, the kernel will automatically initialize ObjectQL if no `ObjectQLPlugin` is provided: |
| 68 | + |
| 69 | +```typescript |
| 70 | +// This still works, but will show a deprecation warning |
| 71 | +const kernel = new ObjectStackKernel([ |
| 72 | + new InMemoryDriver(), |
| 73 | + // ... other plugins |
| 74 | +]); |
| 75 | +``` |
| 76 | + |
| 77 | +## Architecture |
| 78 | + |
| 79 | +### ObjectStackKernel |
| 80 | + |
| 81 | +The kernel is responsible for: |
| 82 | +- Orchestrating application lifecycle |
| 83 | +- Managing plugins |
| 84 | +- Coordinating the ObjectQL engine |
| 85 | +- Handling data operations |
| 86 | + |
| 87 | +### ObjectQLPlugin |
| 88 | + |
| 89 | +The `ObjectQLPlugin` provides: |
| 90 | +- Explicit ObjectQL engine registration |
| 91 | +- Support for custom ObjectQL instances |
| 92 | +- Clean separation of concerns |
| 93 | +- Better testability |
| 94 | + |
| 95 | +## API Reference |
| 96 | + |
| 97 | +### ObjectStackKernel |
| 98 | + |
| 99 | +#### Constructor |
| 100 | +```typescript |
| 101 | +constructor(plugins: any[] = []) |
| 102 | +``` |
| 103 | + |
| 104 | +#### Methods |
| 105 | +- `start()`: Initialize and start the kernel |
| 106 | +- `find(objectName, query)`: Query data |
| 107 | +- `get(objectName, id)`: Get single record |
| 108 | +- `create(objectName, data)`: Create record |
| 109 | +- `update(objectName, id, data)`: Update record |
| 110 | +- `delete(objectName, id)`: Delete record |
| 111 | +- `getMetadata(objectName)`: Get object metadata |
| 112 | +- `getView(objectName, viewType)`: Get UI view definition |
| 113 | + |
| 114 | +### ObjectQLPlugin |
| 115 | + |
| 116 | +#### Constructor |
| 117 | +```typescript |
| 118 | +constructor(ql?: ObjectQL, hostContext?: Record<string, any>) |
| 119 | +``` |
| 120 | + |
| 121 | +#### Parameters |
| 122 | +- `ql` (optional): Custom ObjectQL instance |
| 123 | +- `hostContext` (optional): Host context configuration |
| 124 | + |
| 125 | +## Examples |
| 126 | + |
| 127 | +See the `examples/` directory for complete examples: |
| 128 | +- `examples/host/` - Full server setup with Hono |
| 129 | +- `examples/msw-react-crud/` - Browser-based setup with MSW |
| 130 | +- `examples/custom-objectql-example.ts` - Custom ObjectQL instance |
| 131 | + |
| 132 | +## Migration Guide |
| 133 | + |
| 134 | +### From Hardcoded ObjectQL to Plugin-Based |
| 135 | + |
| 136 | +**Before:** |
| 137 | +```typescript |
| 138 | +const kernel = new ObjectStackKernel([appConfig, driver]); |
| 139 | +``` |
| 140 | + |
| 141 | +**After (Recommended):** |
| 142 | +```typescript |
| 143 | +const kernel = new ObjectStackKernel([ |
| 144 | + new ObjectQLPlugin(), |
| 145 | + appConfig, |
| 146 | + driver |
| 147 | +]); |
| 148 | +``` |
| 149 | + |
| 150 | +**After (Custom Instance):** |
| 151 | +```typescript |
| 152 | +const customQL = new ObjectQL({ /* config */ }); |
| 153 | +const kernel = new ObjectStackKernel([ |
| 154 | + new ObjectQLPlugin(customQL), |
| 155 | + appConfig, |
| 156 | + driver |
| 157 | +]); |
| 158 | +``` |
| 159 | + |
| 160 | +## License |
| 161 | + |
| 162 | +MIT |
0 commit comments