|
| 1 | +import { ObjectKernel, DriverPlugin, AppPlugin } from '@objectstack/runtime'; |
| 2 | +import { ObjectQLPlugin } from '@objectstack/objectql'; |
| 3 | +import { InMemoryDriver } from '@objectstack/driver-memory'; |
| 4 | +import { MSWPlugin } from '@objectstack/plugin-msw'; |
| 5 | +import appConfig from '../objectstack.config'; |
| 6 | +import { mockData } from '../data'; |
| 7 | +import { http, HttpResponse } from 'msw'; |
| 8 | + |
| 9 | +let kernel: ObjectKernel | null = null; |
| 10 | + |
| 11 | +export async function startMockServer() { |
| 12 | + if (kernel) return kernel; |
| 13 | + |
| 14 | + console.log('[MSW] Starting ObjectStack Runtime (Browser Mode)...'); |
| 15 | + |
| 16 | + const driver = new InMemoryDriver(); |
| 17 | + |
| 18 | + // Create kernel with MiniKernel architecture |
| 19 | + kernel = new ObjectKernel(); |
| 20 | + |
| 21 | + kernel |
| 22 | + // Register ObjectQL engine |
| 23 | + .use(new ObjectQLPlugin()) |
| 24 | + // Register the driver |
| 25 | + .use(new DriverPlugin(driver, 'memory')) |
| 26 | + // Load app config as a plugin |
| 27 | + .use(new AppPlugin(appConfig)) |
| 28 | + // MSW Plugin (intercepts network requests) |
| 29 | + .use(new MSWPlugin({ |
| 30 | + enableBrowser: true, |
| 31 | + baseUrl: '/api/v1', |
| 32 | + logRequests: true, |
| 33 | + customHandlers: [ |
| 34 | + // Custom handlers that are not part of standard CRUD |
| 35 | + http.get('/api/bootstrap', async () => { |
| 36 | + // We can use ObjectStackServer helper which proxies to the kernel if initialized |
| 37 | + // Or better, use the kernel instance we have right here. |
| 38 | + // But MSWPlugin might not expose the kernel globally to the handler context easily without closure |
| 39 | + // So we use the closure 'kernel' variable. |
| 40 | + |
| 41 | + // However, ObjectQL usually requires a 'session'. |
| 42 | + const session = { userId: 'current', isSpaceAdmin: true }; // Mock session |
| 43 | + |
| 44 | + try { |
| 45 | + const objectql = kernel!.getService<any>('objectql'); |
| 46 | + const user = (await objectql.getObject('user').findOne('current', {}, session)) || {}; |
| 47 | + const contacts = await objectql.getObject('contact').find({}, session); |
| 48 | + const opportunities = await objectql.getObject('opportunity').find({}, session); |
| 49 | + const stats = { revenue: 125000, leads: 45, deals: 12 }; |
| 50 | + |
| 51 | + return HttpResponse.json({ |
| 52 | + user, |
| 53 | + stats, |
| 54 | + contacts, |
| 55 | + opportunities |
| 56 | + }); |
| 57 | + } catch (e) { |
| 58 | + console.error(e); |
| 59 | + return new HttpResponse(null, { status: 500 }); |
| 60 | + } |
| 61 | + }) |
| 62 | + ] |
| 63 | + })); |
| 64 | + |
| 65 | + await kernel.bootstrap(); |
| 66 | + |
| 67 | + // Seed Data |
| 68 | + await seedData(kernel); |
| 69 | + |
| 70 | + return kernel; |
| 71 | +} |
| 72 | + |
| 73 | +// Helper to seed data into the in-memory driver |
| 74 | +async function seedData(kernel: ObjectKernel) { |
| 75 | + const session = { userId: 'system' }; |
| 76 | + const objectql = kernel.getService<any>('objectql'); |
| 77 | + |
| 78 | + // Seed User |
| 79 | + if (mockData.user) { |
| 80 | + // ObjectQL insert usually needs specific structure, but we try simplest first |
| 81 | + await objectql.getObject('user').insert({ ...mockData.user, id: 'current', _id: 'current' }, session); |
| 82 | + } |
| 83 | + |
| 84 | + // Seed Contacts |
| 85 | + if (mockData.contacts) { |
| 86 | + for (const contact of mockData.contacts) { |
| 87 | + await objectql.getObject('contact').insert({ ...contact, _id: contact.id }, session); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // Seed Opportunities |
| 92 | + if (mockData.opportunities) { |
| 93 | + for (const opp of mockData.opportunities) { |
| 94 | + await objectql.getObject('opportunity').insert({ ...opp, _id: opp.id }, session); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // Seed Accounts |
| 99 | + const accounts = [ |
| 100 | + { id: '1', name: 'Acme Corp', industry: 'Technology' }, |
| 101 | + { id: '2', name: 'TechStart Inc', industry: 'Startup' }, |
| 102 | + { id: '3', name: 'Global Solutions', industry: 'Consulting' } |
| 103 | + ]; |
| 104 | + |
| 105 | + for (const acc of accounts) { |
| 106 | + await objectql.getObject('account').insert({ ...acc, _id: acc.id }, session); |
| 107 | + } |
| 108 | + |
| 109 | + console.log('[MockServer] Data seeded successfully'); |
| 110 | +} |
0 commit comments