|
6 | 6 | */ |
7 | 7 |
|
8 | 8 | import { ObjectKernel, DriverPlugin, AppPlugin } from '@objectstack/runtime'; |
9 | | -import { ObjectQLPlugin } from '@objectstack/objectql'; |
| 9 | +import { ObjectQLPlugin, SchemaRegistry } from '@objectstack/objectql'; |
10 | 10 | import { InMemoryDriver } from '@objectstack/driver-memory'; |
11 | 11 | import { MSWPlugin } from '@objectstack/plugin-msw'; |
12 | 12 | // import appConfig from '../../objectstack.config'; |
@@ -39,9 +39,98 @@ export async function startMockServer() { |
39 | 39 | baseUrl: '/api/v1', |
40 | 40 | logRequests: true |
41 | 41 | })); |
| 42 | + |
| 43 | + // --- BROKER SHIM START --- |
| 44 | + // HttpDispatcher requires a broker to function. We inject a simple shim. |
| 45 | + (kernel as any).broker = { |
| 46 | + call: async (action: string, params: any, opts: any) => { |
| 47 | + const parts = action.split('.'); |
| 48 | + const service = parts[0]; |
| 49 | + const method = parts[1]; |
| 50 | + |
| 51 | + // Get Engines |
| 52 | + const ql = kernel!.context?.getService<any>('objectql'); |
| 53 | + |
| 54 | + if (service === 'data') { |
| 55 | + if (method === 'create') { |
| 56 | + const res = await ql.insert(params.object, params.data); |
| 57 | + return { ...params.data, ...res }; |
| 58 | + } |
| 59 | + if (method === 'get') { |
| 60 | + // Manual filtering workaround for test/browser environment |
| 61 | + let all = await ql.find(params.object); |
| 62 | + if (!all) all = []; |
| 63 | + const match = all.find((i: any) => i.id === params.id || i._id === params.id); |
| 64 | + return match || null; |
| 65 | + } |
| 66 | + if (method === 'update') { |
| 67 | + // ql.update(obj, data, options) - inject ID into data |
| 68 | + return ql.update(params.object, { ...params.data, id: params.id }); |
| 69 | + } |
| 70 | + if (method === 'delete') { |
| 71 | + // ql.delete(obj, options) - pass ID as filter |
| 72 | + return ql.delete(params.object, { filter: params.id }); |
| 73 | + } |
| 74 | + if (method === 'find' || method === 'query') { |
| 75 | + // Manual filtering workaround |
| 76 | + let all = await ql.find(params.object); |
| 77 | + if (!all) all = []; |
| 78 | + |
| 79 | + const filters = params.filters; |
| 80 | + if (filters && typeof filters === 'object' && !Array.isArray(filters)) { |
| 81 | + const keys = Object.keys(filters); |
| 82 | + if (keys.length > 0) { |
| 83 | + all = all.filter((item: any) => { |
| 84 | + return keys.every(k => item[k] === filters[k]); |
| 85 | + }); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // HttpDispatcher expects { data, count } for query/list |
| 90 | + return { data: all, count: all.length }; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if (service === 'metadata') { |
| 95 | + if (method === 'objects') { |
| 96 | + // Try engine first |
| 97 | + let objs = ql && ql.getObjects ? ql.getObjects() : []; |
| 98 | + // Fallback to Registry if engine returns empty (likely delayed sync) |
| 99 | + if (objs.length === 0) { |
| 100 | + objs = SchemaRegistry.getAllObjects(); |
| 101 | + } |
| 102 | + return objs; |
| 103 | + } |
| 104 | + if (method === 'getObject') { |
| 105 | + // Try Registry first for speed/correctness |
| 106 | + return SchemaRegistry.getObject(params.objectName) || (ql ? ql.getObject(params.objectName) : null); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + console.warn(`[BrokerShim] Action not implemented: ${action}`); |
| 111 | + return null; |
| 112 | + } |
| 113 | + }; |
| 114 | + // --- BROKER SHIM END --- |
42 | 115 |
|
43 | 116 | await kernel.bootstrap(); |
44 | 117 |
|
| 118 | + // --- PROTOCOL SERVICE MOCK --- |
| 119 | + // Overwrite protocol service because the default one might be empty/broken in this env |
| 120 | + if (kernel.services instanceof Map) { |
| 121 | + kernel.services.set('protocol', { |
| 122 | + getUiView: async ({ object, type }: any) => { |
| 123 | + return { |
| 124 | + type: type || 'list', |
| 125 | + name: 'default', |
| 126 | + object: object, |
| 127 | + title: object, |
| 128 | + body: [] |
| 129 | + }; |
| 130 | + } |
| 131 | + }); |
| 132 | + } |
| 133 | + |
45 | 134 | // Initialize default data from manifest if available |
46 | 135 | const manifest = (todoConfig as any).manifest; |
47 | 136 | if (manifest && Array.isArray(manifest.data)) { |
|
0 commit comments