Skip to content

Commit 49673fc

Browse files
committed
feat: enhance datasource registration and seeding logic in console plugin
1 parent f12f1b6 commit 49673fc

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

apps/console/objectstack.config.ts

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ const InMemoryDriver = DriverMemoryPkg.InMemoryDriver || (DriverMemoryPkg as any
2525
const DriverPlugin = RuntimePkg.DriverPlugin || (RuntimePkg as any).default?.DriverPlugin || (RuntimePkg as any).default;
2626
// const HonoServerPlugin = HonoServerPluginPkg.HonoServerPlugin || (HonoServerPluginPkg as any).default?.HonoServerPlugin || (HonoServerPluginPkg as any).default;
2727

28+
const memoryDriver = new InMemoryDriver();
29+
2830
import ConsolePluginConfig from './plugin.js';
2931

3032
const FixedConsolePlugin = {
@@ -33,6 +35,75 @@ const FixedConsolePlugin = {
3335
start: async (ctx: any) => {
3436
console.log('DEBUG: FixedConsolePlugin start called');
3537

38+
// Official way: Configure the 'default' datasource manually
39+
try {
40+
const metadataService = ctx.metadata || ctx.getService('metadata');
41+
if (metadataService && metadataService.addDatasource) {
42+
console.log('[Config] Registering default datasource (memory)...');
43+
await metadataService.addDatasource({
44+
name: 'default',
45+
driver: 'in-memory-driver', // Using the registered service name from logs
46+
});
47+
} else {
48+
console.warn('[Config] Metadata service not available for datasource registration');
49+
}
50+
} catch (e: any) {
51+
console.error('[Config] Failed to register default datasource:', e);
52+
}
53+
54+
// --- Data Seeding Logic ---
55+
try {
56+
console.log('[Seeder] Checking for initial data...');
57+
58+
// Resolve the active driver from the runtime context
59+
let activeDriver = null;
60+
61+
try {
62+
// Try getting the service that was explicitly logged as registered
63+
// "Service 'driver.in-memory-driver' registered"
64+
const serviceName = 'driver.in-memory-driver';
65+
const service = ctx.getService(serviceName);
66+
if (service && typeof service.create === 'function') {
67+
activeDriver = service;
68+
} else if (service && service.driver && typeof service.driver.create === 'function') {
69+
activeDriver = service.driver;
70+
}
71+
} catch (err: any) {
72+
console.log(`[Seeder] Driver retrieval error: ${err.message}`);
73+
}
74+
75+
if (!activeDriver) {
76+
console.log("[Seeder] Driver 'driver.in-memory-driver' not found or invalid.");
77+
console.log("[Seeder] Available Services:", ctx.getServiceNames ? ctx.getServiceNames() : 'Unknown');
78+
// Last resort fallback
79+
activeDriver = memoryDriver;
80+
} else {
81+
console.log("[Seeder] Successfully resolved active driver from Runtime.");
82+
}
83+
84+
const manifest = sharedConfig.manifest;
85+
if (manifest && Array.isArray(manifest.data)) {
86+
for (const dataset of manifest.data) {
87+
if (dataset.object && Array.isArray(dataset.records)) {
88+
console.log(`[Seeder] Seeding ${dataset.records.length} records for ${dataset.object}`);
89+
for (const record of dataset.records) {
90+
try {
91+
await activeDriver.create(dataset.object, record);
92+
} catch (err: any) {
93+
console.warn(`[Seeder] Failed to insert ${dataset.object} record:`, err.message);
94+
}
95+
}
96+
}
97+
}
98+
console.log('[Seeder] Data seeding complete.');
99+
} else {
100+
console.log('[Seeder] No initial data found in manifest.');
101+
}
102+
} catch (e: any) {
103+
console.error('[Seeder] Critical error during data seeding:', e);
104+
}
105+
// --------------------------
106+
36107
let app = null;
37108
const staticRoot = './dist';
38109

@@ -106,7 +177,8 @@ const FixedConsolePlugin = {
106177

107178
const plugins: any[] = [
108179
new ObjectQLPlugin(),
109-
new DriverPlugin(new InMemoryDriver(), 'memory'),
180+
// Driver registration
181+
new DriverPlugin(memoryDriver, 'in-memory-driver'),
110182
// new MSWPlugin(), // Disabled in production mode
111183
// HonoServerPlugin is auto-detected
112184
FixedConsolePlugin
@@ -127,11 +199,13 @@ export default defineConfig({
127199
target: 'node18',
128200
},
129201

202+
/*
130203
datasources: {
131204
default: {
132205
driver: 'memory',
133206
},
134207
},
208+
*/
135209

136210
plugins,
137211

apps/console/src/mocks/browser.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { ObjectQLPlugin } from '@objectstack/objectql';
1010
import { InMemoryDriver } from '@objectstack/driver-memory';
1111
import { MSWPlugin } from '@objectstack/plugin-msw';
1212
import { setupWorker } from 'msw/browser';
13+
import { http, HttpResponse } from 'msw';
1314
import appConfig from '../../objectstack.shared';
1415

1516
let kernel: ObjectKernel | null = null;
@@ -74,6 +75,19 @@ export async function startMockServer() {
7475
// Manually start MSW worker with correct service worker path
7576
if (typeof window !== 'undefined') {
7677
const handlers = mswPlugin.getHandlers();
78+
79+
// Manual Fix: Add discovery endpoint handler to prevent 302/404 errors during initialization
80+
handlers.unshift(
81+
http.get('*/.well-known/objectstack', () => {
82+
return HttpResponse.json({
83+
name: "ObjectStack",
84+
version: "1.0.0",
85+
api_base: "/api/v1",
86+
status: "ok"
87+
});
88+
})
89+
);
90+
7791
const worker = setupWorker(...handlers);
7892

7993
// Check if we are served under a base path (e.g. /console/)

0 commit comments

Comments
 (0)