Skip to content

Commit b8a22bf

Browse files
Copilothotlong
andcommitted
Update documentation and deprecate ObjectStackKernel
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 8e005e5 commit b8a22bf

File tree

2 files changed

+166
-75
lines changed

2 files changed

+166
-75
lines changed

packages/runtime/README.md

Lines changed: 137 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,51 @@ ObjectStack Core Runtime & Query Engine
44

55
## Overview
66

7-
The runtime package provides the `ObjectStackKernel` - the central orchestrator for ObjectStack applications. It manages the application lifecycle, plugins, and the ObjectQL data engine.
7+
The runtime package provides the `ObjectKernel` (MiniKernel) - a highly modular, plugin-based microkernel that orchestrates ObjectStack applications. It manages the application lifecycle through a standardized plugin system with dependency injection and event hooks.
8+
9+
### Architecture Highlights
10+
11+
- **MiniKernel Design**: Business logic is completely separated into plugins
12+
- **Dependency Injection**: Service registry for inter-plugin communication
13+
- **Event/Hook System**: Publish-subscribe mechanism for loose coupling
14+
- **Lifecycle Management**: Standardized init/start/destroy phases
15+
- **Dependency Resolution**: Automatic topological sorting based on plugin dependencies
816

917
## Installation
1018

1119
```bash
1220
npm install @objectstack/runtime
1321
```
1422

15-
## Usage
23+
## Quick Start
1624

17-
### Basic Setup
25+
### Basic Setup (Recommended)
1826

1927
```typescript
20-
import { ObjectStackKernel, ObjectQLPlugin } from '@objectstack/runtime';
28+
import { ObjectKernel, ObjectQLPlugin, DriverPlugin } from '@objectstack/runtime';
2129
import { InMemoryDriver } from '@objectstack/driver-memory';
2230

23-
const kernel = new ObjectStackKernel([
31+
const kernel = new ObjectKernel();
32+
33+
kernel
2434
// Register ObjectQL engine
25-
new ObjectQLPlugin(),
35+
.use(new ObjectQLPlugin())
2636

2737
// Add database driver
28-
new InMemoryDriver(),
38+
.use(new DriverPlugin(new InMemoryDriver(), 'memory'))
2939

3040
// Add your app configurations
31-
// appConfig,
32-
]);
41+
// .use(new AppManifestPlugin(appConfig));
3342

34-
await kernel.start();
43+
await kernel.bootstrap();
3544
```
3645

3746
### Custom ObjectQL Instance
3847

3948
If you have a separate ObjectQL implementation or need custom configuration:
4049

4150
```typescript
42-
import { ObjectStackKernel, ObjectQLPlugin, ObjectQL } from '@objectstack/runtime';
51+
import { ObjectKernel, ObjectQLPlugin, DriverPlugin, ObjectQL } from '@objectstack/runtime';
4352

4453
// Create custom ObjectQL instance
4554
const customQL = new ObjectQL({
@@ -52,111 +61,167 @@ customQL.registerHook('beforeInsert', async (ctx) => {
5261
console.log(`Inserting into ${ctx.object}`);
5362
});
5463

55-
const kernel = new ObjectStackKernel([
64+
const kernel = new ObjectKernel();
65+
66+
kernel
5667
// Use your custom ObjectQL instance
57-
new ObjectQLPlugin(customQL),
68+
.use(new ObjectQLPlugin(customQL))
5869

59-
// ... other plugins
60-
]);
70+
// Add driver
71+
.use(new DriverPlugin(new InMemoryDriver(), 'memory'));
6172

62-
await kernel.start();
73+
await kernel.bootstrap();
74+
75+
// Access ObjectQL via service registry
76+
const objectql = kernel.getService('objectql');
6377
```
6478

65-
### Backward Compatibility
79+
## Architecture
6680

67-
For backward compatibility, the kernel will automatically initialize ObjectQL if no `ObjectQLPlugin` is provided:
81+
### ObjectKernel (MiniKernel)
82+
83+
The kernel provides:
84+
- **Plugin Lifecycle Management**: init → start → destroy phases
85+
- **Service Registry**: Dependency injection container
86+
- **Event/Hook System**: Inter-plugin communication
87+
- **Dependency Resolution**: Topological sort for plugin dependencies
88+
89+
### Built-in Plugins
90+
91+
#### ObjectQLPlugin
92+
Registers the ObjectQL data engine as a service.
6893

6994
```typescript
70-
// This still works, but will show a deprecation warning
71-
const kernel = new ObjectStackKernel([
72-
new InMemoryDriver(),
73-
// ... other plugins
74-
]);
95+
new ObjectQLPlugin() // Default instance
96+
new ObjectQLPlugin(customQL) // Custom instance
97+
new ObjectQLPlugin(undefined, { env: 'prod' }) // With context
7598
```
7699

77-
## Architecture
100+
**Services**: `'objectql'`
78101

79-
### ObjectStackKernel
102+
#### DriverPlugin
103+
Registers a data driver with ObjectQL.
80104

81-
The kernel is responsible for:
82-
- Orchestrating application lifecycle
83-
- Managing plugins
84-
- Coordinating the ObjectQL engine
85-
- Handling data operations
105+
```typescript
106+
new DriverPlugin(driver, 'driver-name')
107+
```
108+
109+
**Dependencies**: `['com.objectstack.engine.objectql']`
110+
111+
#### AppManifestPlugin
112+
Wraps ObjectStack app manifests (objectstack.config.ts) as plugins.
86113

87-
### ObjectQLPlugin
114+
```typescript
115+
new AppManifestPlugin(appConfig)
116+
```
88117

89-
The `ObjectQLPlugin` provides:
90-
- Explicit ObjectQL engine registration
91-
- Support for custom ObjectQL instances
92-
- Clean separation of concerns
93-
- Better testability
118+
**Dependencies**: `['com.objectstack.engine.objectql']`
94119

95120
## API Reference
96121

97-
### ObjectStackKernel
122+
### ObjectKernel
123+
124+
#### Methods
125+
- `use(plugin: Plugin)`: Register a plugin
126+
- `bootstrap()`: Initialize and start all plugins
127+
- `shutdown()`: Stop all plugins in reverse order
128+
- `getService<T>(name: string)`: Get a service from registry
129+
- `isRunning()`: Check if kernel is running
130+
- `getState()`: Get current kernel state
131+
132+
### Plugin Interface
98133

99-
#### Constructor
100134
```typescript
101-
constructor(plugins: any[] = [])
135+
interface Plugin {
136+
name: string; // Unique identifier
137+
version?: string; // Plugin version
138+
dependencies?: string[]; // Required plugin names
139+
140+
init(ctx: PluginContext): Promise<void>; // Register services
141+
start?(ctx: PluginContext): Promise<void>; // Execute business logic
142+
destroy?(): Promise<void>; // Cleanup
143+
}
102144
```
103145

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
146+
### PluginContext
147+
117148
```typescript
118-
constructor(ql?: ObjectQL, hostContext?: Record<string, any>)
149+
interface PluginContext {
150+
registerService(name: string, service: any): void;
151+
getService<T>(name: string): T;
152+
hook(name: string, handler: Function): void;
153+
trigger(name: string, ...args: any[]): Promise<void>;
154+
logger: Console;
155+
getKernel?(): any;
156+
}
119157
```
120158

121-
#### Parameters
122-
- `ql` (optional): Custom ObjectQL instance
123-
- `hostContext` (optional): Host context configuration
124-
125159
## Examples
126160

127161
See the `examples/` directory for complete examples:
128162
- `examples/host/` - Full server setup with Hono
129163
- `examples/msw-react-crud/` - Browser-based setup with MSW
130164
- `examples/custom-objectql-example.ts` - Custom ObjectQL instance
165+
- `test-mini-kernel.ts` - Comprehensive test suite
131166

132167
## Migration Guide
133168

134-
### From Hardcoded ObjectQL to Plugin-Based
169+
### From ObjectStackKernel to ObjectKernel
135170

136-
**Before:**
171+
**Before (Legacy):**
137172
```typescript
138-
const kernel = new ObjectStackKernel([appConfig, driver]);
139-
```
173+
import { ObjectStackKernel, ObjectQLPlugin } from '@objectstack/runtime';
140174

141-
**After (Recommended):**
142-
```typescript
143175
const kernel = new ObjectStackKernel([
144176
new ObjectQLPlugin(),
145-
appConfig,
177+
appConfig,
146178
driver
147179
]);
180+
181+
await kernel.start();
148182
```
149183

150-
**After (Custom Instance):**
184+
**After (Recommended):**
151185
```typescript
152-
const customQL = new ObjectQL({ /* config */ });
153-
const kernel = new ObjectStackKernel([
154-
new ObjectQLPlugin(customQL),
155-
appConfig,
156-
driver
157-
]);
186+
import { ObjectKernel, ObjectQLPlugin, DriverPlugin, AppManifestPlugin } from '@objectstack/runtime';
187+
188+
const kernel = new ObjectKernel();
189+
190+
kernel
191+
.use(new ObjectQLPlugin())
192+
.use(new DriverPlugin(driver, 'memory'))
193+
.use(new AppManifestPlugin(appConfig));
194+
195+
await kernel.bootstrap();
158196
```
159197

198+
## Benefits of MiniKernel
199+
200+
1. **True Modularity**: Each plugin is independent and reusable
201+
2. **Testability**: Mock services easily in tests
202+
3. **Flexibility**: Load plugins conditionally
203+
4. **Extensibility**: Add new plugins without modifying kernel
204+
5. **Clear Dependencies**: Explicit dependency declarations
205+
6. **Better Architecture**: Separation of concerns
206+
207+
## Best Practices
208+
209+
1. **Keep plugins focused**: One responsibility per plugin
210+
2. **Use services**: Share functionality via service registry
211+
3. **Declare dependencies**: Make plugin requirements explicit
212+
4. **Use hooks**: Decouple plugins with event system
213+
5. **Handle errors**: Implement proper error handling in lifecycle methods
214+
215+
## Documentation
216+
217+
- [MiniKernel Guide](../../MINI_KERNEL_GUIDE.md) - Complete API documentation and patterns
218+
- [MiniKernel Architecture](../../MINI_KERNEL_ARCHITECTURE.md) - Architecture diagrams and flows
219+
- [MiniKernel Implementation](../../MINI_KERNEL_IMPLEMENTATION.md) - Implementation details
220+
221+
## Legacy Support
222+
223+
The `ObjectStackKernel` is still available for backward compatibility but is deprecated. New projects should use `ObjectKernel`.
224+
160225
## License
161226

162-
MIT
227+
Apache-2.0

packages/runtime/src/kernel.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,36 @@ import { ServiceObject } from '@objectstack/spec/data';
22
import { SchemaRegistry, ObjectQL } from '@objectstack/objectql';
33

44
/**
5-
* ObjectStack Kernel (Microkernel)
5+
* ObjectStack Kernel (Legacy)
66
*
7-
* The central container orchestrating the application lifecycle,
8-
* plugins, and the core ObjectQL engine.
7+
* @deprecated Use ObjectKernel (MiniKernel) instead for better modularity and plugin architecture.
8+
* This class is kept for backward compatibility but new projects should use ObjectKernel.
9+
*
10+
* @see ObjectKernel - The new MiniKernel implementation
11+
* @see MINI_KERNEL_GUIDE.md - Complete guide to the new architecture
12+
*
13+
* The legacy kernel orchestrates the application lifecycle,
14+
* plugins, and the core ObjectQL engine, but with less modularity
15+
* than the new ObjectKernel implementation.
16+
*
17+
* Migration example:
18+
* ```typescript
19+
* // Old (deprecated):
20+
* const kernel = new ObjectStackKernel([
21+
* new ObjectQLPlugin(),
22+
* appConfig,
23+
* driver
24+
* ]);
25+
* await kernel.start();
26+
*
27+
* // New (recommended):
28+
* const kernel = new ObjectKernel();
29+
* kernel
30+
* .use(new ObjectQLPlugin())
31+
* .use(new DriverPlugin(driver, 'memory'))
32+
* .use(new AppManifestPlugin(appConfig));
33+
* await kernel.bootstrap();
34+
* ```
935
*/
1036
export class ObjectStackKernel {
1137
public ql?: ObjectQL; // Will be set by ObjectQLPlugin or fallback initialization

0 commit comments

Comments
 (0)