Skip to content

Commit abebba9

Browse files
committed
添加初始数据种子配置,支持在包安装时插入默认记录;更新 Manifest 相关文档和 Zod 验证
1 parent 51f86b6 commit abebba9

File tree

6 files changed

+91
-12
lines changed

6 files changed

+91
-12
lines changed

content/docs/references/system/config/Manifest.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ description: Manifest Schema Reference
1818
| **dependencies** | `Record<string, string>` | optional | Package dependencies |
1919
| **configuration** | `object` | optional | Plugin configuration settings |
2020
| **contributes** | `object` | optional | Platform contributions |
21+
| **data** | `object[]` | optional | Initial seed data |
2122
| **extensions** | `Record<string, any>` | optional | Extension points and contributions |

examples/objectql/src/engine.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ export class ObjectQL {
6464
SchemaRegistry.registerKind(kind);
6565
}
6666
}
67+
68+
// Register Data Seeding (Lazy execution or immediate?)
69+
// We store it in a temporary registry or execute immediately if engine is ready.
70+
// Since `use` is init time, we might need to store it and run later in `seed()`.
71+
// For this MVP, let's attach it to the manifest object in registry so DataEngine can find it.
6772
}
6873

6974
// 2. Execute Runtime

examples/server/src/kernel/engine.ts

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,34 @@ export class DataEngine {
6868
try {
6969
await this.ql.insert('SystemStatus', { status: 'OK', uptime: 0 });
7070

71-
// Seed some Todo Tasks if table is empty
72-
// Use try-catch because find might throw if object not registered (though it should be)
73-
try {
74-
const tasks = await this.ql.find('todo_task', { top: 1 });
75-
if (tasks.length === 0) {
76-
console.log('[DataEngine] Seeding initial Todo Data...');
77-
await this.ql.insert('todo_task', { subject: 'Review PR #102', is_completed: true, priority: 3, due_date: new Date() });
78-
await this.ql.insert('todo_task', { subject: 'Write Documentation', is_completed: false, priority: 2, due_date: new Date(Date.now() + 86400000) });
79-
await this.ql.insert('todo_task', { subject: 'Fix specific Server bug', is_completed: false, priority: 1 });
80-
}
81-
} catch (e) {
82-
console.warn('[DataEngine] Failed to seed todo_task', e);
71+
// Iterate over all registered plugins/apps and check for 'data' property in manifest
72+
const plugins = SchemaRegistry.getRegisteredTypes(); // This returns types like 'plugin', 'app'
73+
74+
// This is a bit hacky because we don't have a direct "getAllManifests" API exposed easily
75+
// We will iterate known apps for now, or improve Registry API later.
76+
// Actually, SchemaRegistry.listItems('app') returns the manifests!
77+
78+
const apps = [...SchemaRegistry.listItems('app'), ...SchemaRegistry.listItems('plugin')];
79+
80+
for (const appItem of apps) {
81+
const app = appItem as any; // Cast to access data prop safely
82+
if (app.data && Array.isArray(app.data)) {
83+
console.log(`[DataEngine] Seeding data for ${app.name || app.id}...`);
84+
for (const seed of app.data) {
85+
try {
86+
// Check if data exists
87+
const existing = await this.ql.find(seed.object, { top: 1 });
88+
if (existing.length === 0) {
89+
console.log(`[DataEngine] Inserting ${seed.records.length} records into ${seed.object}`);
90+
for (const record of seed.records) {
91+
await this.ql.insert(seed.object, record);
92+
}
93+
}
94+
} catch (e) {
95+
console.warn(`[DataEngine] Failed to seed ${seed.object}`, e);
96+
}
97+
}
98+
}
8399
}
84100

85101
} catch(e) {

examples/todo/objectstack.config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,16 @@ export default App.create({
2828
}
2929
]
3030
}
31+
],
32+
data: [
33+
{
34+
object: 'todo_task',
35+
mode: 'upsert',
36+
records: [
37+
{ subject: 'Review PR #102', is_completed: true, priority: 3, due_date: new Date() },
38+
{ subject: 'Write Documentation', is_completed: false, priority: 2, due_date: new Date(Date.now() + 86400000) },
39+
{ subject: 'Fix specific Server bug', is_completed: false, priority: 1 }
40+
]
41+
}
3142
]
3243
});

packages/spec/json-schema/Manifest.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,42 @@
260260
"additionalProperties": false,
261261
"description": "Platform contributions"
262262
},
263+
"data": {
264+
"type": "array",
265+
"items": {
266+
"type": "object",
267+
"properties": {
268+
"object": {
269+
"type": "string",
270+
"description": "Target Object Name"
271+
},
272+
"records": {
273+
"type": "array",
274+
"items": {
275+
"type": "object",
276+
"additionalProperties": {}
277+
},
278+
"description": "List of records to insert"
279+
},
280+
"mode": {
281+
"type": "string",
282+
"enum": [
283+
"upsert",
284+
"insert",
285+
"ignore"
286+
],
287+
"default": "upsert",
288+
"description": "Seeding mode"
289+
}
290+
},
291+
"required": [
292+
"object",
293+
"records"
294+
],
295+
"additionalProperties": false
296+
},
297+
"description": "Initial seed data"
298+
},
263299
"extensions": {
264300
"type": "object",
265301
"additionalProperties": {},

packages/spec/src/system/manifest.zod.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,16 @@ export const ManifestSchema = z.object({
139139
})).optional().describe('Exposed server actions'),
140140
}).optional().describe('Platform contributions'),
141141

142+
/**
143+
* Initial data seeding configuration.
144+
* Defines default records to be inserted when the package is installed.
145+
*/
146+
data: z.array(z.object({
147+
object: z.string().describe('Target Object Name'),
148+
records: z.array(z.record(z.any())).describe('List of records to insert'),
149+
mode: z.enum(['upsert', 'insert', 'ignore']).default('upsert').describe('Seeding mode')
150+
})).optional().describe('Initial seed data'),
151+
142152
/**
143153
* Extension points contributed by this package.
144154
* Allows packages to extend UI components, add functionality, etc.

0 commit comments

Comments
 (0)