-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample-spec-plugin.ts
More file actions
204 lines (176 loc) · 7.01 KB
/
example-spec-plugin.ts
File metadata and controls
204 lines (176 loc) · 7.01 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* Example Plugin Using @objectstack/spec Protocol
*
* This demonstrates how to create a plugin that conforms to the
* ObjectStack specification for plugin lifecycle and context.
*/
import type { PluginDefinition, PluginContextData, ObjectStackManifest } from '@objectstack/spec/system';
/**
* Plugin Manifest
* Conforms to @objectstack/spec/system/ManifestSchema
*
* This is typically stored in a package.json or manifest.json file
* and loaded by the kernel. For this example, we define it inline.
*/
export const ExampleCRMManifest: ObjectStackManifest = {
id: 'com.example.crm',
version: '1.0.0',
type: 'plugin',
name: 'Example CRM Plugin',
description: 'Demonstrates ObjectStack spec-compliant plugin development',
permissions: [
'system.user.read',
'system.data.write',
],
// In v0.4.1, objects are defined via glob patterns pointing to object definition files
// Object files should be YAML or TypeScript files following the object schema
objects: ['./objects/*.object.yml'],
// Contribution points for extending the platform
contributes: {
// Register custom actions that can be invoked by flows or API
actions: [
{
name: 'convertLead',
label: 'Convert Lead to Account',
description: 'Converts a lead to an account and contact',
}
],
// Register custom events that this plugin listens to
events: ['crm_lead.created', 'crm_lead.converted'],
// Register custom field types
fieldTypes: [
{
name: 'lead_status',
label: 'Lead Status',
description: 'Special field type for lead status with workflow integration',
}
],
},
};
/**
* Example CRM Plugin
*
* This plugin demonstrates:
* - Plugin lifecycle hooks (onInstall, onEnable, onLoad, onDisable, onUninstall)
* - Using the plugin context (ql, os, logger, storage, etc.)
* - Registering routes and scheduled jobs
*
* Conforms to @objectstack/spec/system/PluginLifecycleSchema
*/
export const ExampleCRMPlugin: PluginDefinition = {
/**
* Called when the plugin is first installed
*/
async onInstall(context: PluginContextData) {
context.logger.info('CRM Plugin: Installing...');
// Create initial data or setup
await context.storage.set('install_date', new Date().toISOString());
context.logger.info('CRM Plugin: Installation complete');
},
/**
* Called when the plugin is enabled
*/
async onEnable(context: PluginContextData) {
context.logger.info('CRM Plugin: Enabling...');
// The metadata from object files (specified in manifest.objects) is loaded by the kernel
// before this hook is called
// Register custom routes
context.app.router.post('/api/crm/leads/convert', async (req: any, res: any) => {
try {
const { leadId } = req.body;
// Use ObjectQL to query data
const lead = await context.ql.query('crm_lead', {
filters: { id: leadId },
});
if (!lead) {
return res.status(404).json({ error: 'Lead not found' });
}
// Update lead status
await context.ql.object('crm_lead').update(leadId, {
status: 'converted',
converted_at: new Date(),
});
context.logger.info(`Lead ${leadId} converted`);
return res.json({ success: true, lead });
} catch (error) {
context.logger.error('Error converting lead', error);
return res.status(500).json({ error: 'Internal server error' });
}
});
// Register scheduled jobs (if scheduler is available)
if (context.app.scheduler) {
context.app.scheduler.schedule(
'clean-old-leads',
'0 0 * * 0', // Every Sunday at midnight
async () => {
context.logger.info('Running weekly lead cleanup...');
// Find leads older than 90 days with no activity
const oldDate = new Date();
oldDate.setDate(oldDate.getDate() - 90);
// This is just an example - actual cleanup logic would go here
context.logger.info('Lead cleanup completed');
}
);
}
context.logger.info('CRM Plugin: Enabled');
},
/**
* Called when the plugin is disabled
*/
async onDisable(context: PluginContextData) {
context.logger.info('CRM Plugin: Disabling...');
// Cleanup any runtime state
await context.storage.set('last_disabled', new Date().toISOString());
context.logger.info('CRM Plugin: Disabled');
},
/**
* Called when the plugin is uninstalled
*/
async onUninstall(context: PluginContextData) {
context.logger.info('CRM Plugin: Uninstalling...');
// Cleanup data and storage
await context.storage.delete('install_date');
await context.storage.delete('last_disabled');
// Note: Object data is NOT automatically deleted
// The administrator should handle data migration
context.logger.warn('CRM Plugin: Uninstalled - Lead data preserved');
},
};
/**
* Usage Example:
*
* ```typescript
* import { ObjectOS } from '@objectos/kernel';
* import { ExampleCRMPlugin, ExampleCRMManifest } from './plugins/example-spec-plugin';
*
* // The manifest is typically loaded from package.json or manifest.json
* // For this example, we use the exported manifest
*
* const os = new ObjectOS({
* plugins: [ExampleCRMPlugin],
* // The kernel would load manifests separately and associate them with plugins
* // ... other config
* });
*
* await os.init();
* ```
*
* ## Plugin Architecture Notes (v0.4.1)
*
* In the ObjectStack spec v0.4.1, plugins are separated into two parts:
*
* 1. **Manifest** (@objectstack/spec/system/ManifestSchema)
* - Static configuration (id, version, name, permissions)
* - Object file glob patterns (objects are defined in separate files)
* - Contribution points (actions, events, field types, etc.)
* - Typically stored in package.json or manifest.json
*
* 2. **Lifecycle Hooks** (@objectstack/spec/system/PluginLifecycleSchema)
* - Runtime behavior (onInstall, onEnable, onLoad, etc.)
* - Executable code
* - Typically exported from index.ts or main.js
*
* The kernel loads manifests first to understand what plugins are available,
* then loads object definitions from the files specified in manifest.objects,
* and finally executes lifecycle hooks at appropriate times.
*/