-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathobjectos.ts
More file actions
161 lines (142 loc) · 4.82 KB
/
objectos.ts
File metadata and controls
161 lines (142 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { ObjectQL } from '@objectql/core';
import { ObjectQLConfig, ObjectQLPlugin } from '@objectql/types';
import type { PluginDefinition, ObjectStackManifest, KernelContext } from '@objectstack/spec/system';
import { ObjectOSPlugin } from './plugins/objectql';
import { KernelContextManager } from './kernel-context';
import { StorageManager } from './scoped-storage';
import { PluginManager } from './plugin-manager';
import { PluginContextBuilder } from './plugin-context';
import { createLogger, Logger } from './logger';
export interface ObjectOSConfig extends ObjectQLConfig {
/**
* Kernel context configuration
*/
kernelContext?: Partial<KernelContext>;
/**
* Spec-compliant plugins (new style)
*/
specPlugins?: Array<{
manifest: ObjectStackManifest;
definition: PluginDefinition;
}>;
}
/**
* ObjectOS - The Enterprise Operating System
*
* Implements the @objectstack/spec protocol for plugin lifecycle,
* context management, and system orchestration.
*/
export class ObjectOS extends ObjectQL {
private kernelContext: KernelContextManager;
private storageManager: StorageManager;
private pluginManager: PluginManager;
private contextBuilder: PluginContextBuilder;
private logger: Logger;
constructor(config: ObjectOSConfig = {}) {
// Initialize ObjectQL base with ObjectOS plugin
super({
datasources: config.datasources || {},
presets: config.presets || config.packages,
plugins: [ObjectOSPlugin, ...(config.plugins || [])],
source: config.source,
objects: config.objects,
connection: config.connection,
remotes: config.remotes,
});
// Initialize kernel components
this.kernelContext = new KernelContextManager(config.kernelContext);
this.storageManager = new StorageManager();
this.logger = createLogger('ObjectOS');
// Create plugin context builder
this.contextBuilder = new PluginContextBuilder(
this,
(pluginId: string) => this.storageManager.createScopedStorage(pluginId)
);
// Create plugin manager with context builder
this.pluginManager = new PluginManager(
(pluginId: string) => this.contextBuilder.build(pluginId)
);
// Register spec-compliant plugins if provided
if (config.specPlugins) {
for (const { manifest, definition } of config.specPlugins) {
this.registerPlugin(manifest, definition);
}
}
}
/**
* Initialize the ObjectOS kernel.
* Boots ObjectQL and enables all registered plugins.
*/
async init(options?: any): Promise<void> {
this.logger.info('Initializing ObjectOS kernel...');
// Initialize ObjectQL base
await super.init();
// Enable all registered plugins
const plugins = this.pluginManager.getAllPlugins();
for (const [pluginId, entry] of plugins) {
try {
await this.pluginManager.enable(pluginId);
} catch (error) {
this.logger.error(`Failed to enable plugin '${pluginId}'`, error as Error);
// Continue with other plugins
}
}
this.logger.info(`ObjectOS kernel initialized (${plugins.size} plugins)`);
}
/**
* Register a spec-compliant plugin.
*
* @param manifest - Plugin manifest (static configuration)
* @param definition - Plugin definition (lifecycle hooks)
*/
registerPlugin(manifest: ObjectStackManifest, definition: PluginDefinition): void {
this.pluginManager.register(manifest, definition);
}
/**
* Get the kernel context.
*/
getKernelContext(): KernelContext {
return this.kernelContext.getContext();
}
/**
* Get the plugin manager.
*/
getPluginManager(): PluginManager {
return this.pluginManager;
}
/**
* Get the context builder (for advanced use cases).
*/
getContextBuilder(): PluginContextBuilder {
return this.contextBuilder;
}
/**
* Get the storage manager.
*/
getStorageManager(): StorageManager {
return this.storageManager;
}
/**
* Set a database driver for the default datasource.
*
* @deprecated since v0.2.0. Use datasources configuration in constructor instead.
* Will be removed in v1.0.0.
*
* @example
* ```typescript
* // Instead of:
* const os = new ObjectOS();
* os.useDriver(driver);
*
* // Use:
* const os = new ObjectOS({
* datasources: {
* default: driver
* }
* });
* ```
*/
useDriver(driver: any): void {
(this as any).datasources['default'] = driver;
}
}