Skip to content

Commit 26ee64a

Browse files
Copilothotlong
andcommitted
refactor: extract mergeViewsIntoObjects to shared utility in @object-ui/core
Address DRY concern from code review by extracting the adapter function to packages/core/src/utils/merge-views-into-objects.ts. Both config files now import from @object-ui/core instead of duplicating the function. Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 2e65af2 commit 26ee64a

File tree

4 files changed

+31
-46
lines changed

4 files changed

+31
-46
lines changed

apps/console/objectstack.shared.ts

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { ObjectStackDefinition } from '@objectstack/spec';
22
import { composeStacks } from '@objectstack/spec';
3+
import { mergeViewsIntoObjects } from '@object-ui/core';
34
import crmConfigImport from '@object-ui/example-crm/objectstack.config';
45
import todoConfigImport from '@object-ui/example-todo/objectstack.config';
56
import kitchenSinkConfigImport from '@object-ui/example-kitchen-sink/objectstack.config';
@@ -13,29 +14,6 @@ function resolveDefault<T>(mod: MaybeDefault<T>): T {
1314
return mod as T;
1415
}
1516

16-
// ---------------------------------------------------------------------------
17-
// Adapter: merge stack-level views (views[].listViews) into object definitions.
18-
// The runtime reads listViews from each object; this bridges the gap until
19-
// the runtime/provider layer handles it natively.
20-
// ---------------------------------------------------------------------------
21-
function mergeViewsIntoObjects(objects: any[], views: any[]): any[] {
22-
const viewsByObject: Record<string, Record<string, any>> = {};
23-
for (const view of views) {
24-
if (!view.listViews) continue;
25-
for (const [viewName, listView] of Object.entries(view.listViews as Record<string, any>)) {
26-
const objectName = listView?.data?.object;
27-
if (!objectName) continue;
28-
if (!viewsByObject[objectName]) viewsByObject[objectName] = {};
29-
viewsByObject[objectName][viewName] = listView;
30-
}
31-
}
32-
return objects.map((obj: any) => {
33-
const v = viewsByObject[obj.name];
34-
if (!v) return obj;
35-
return { ...obj, listViews: { ...(obj.listViews || {}), ...v } };
36-
});
37-
}
38-
3917
const crmConfig = resolveDefault<ObjectStackDefinition>(crmConfigImport);
4018
const todoConfig = resolveDefault<ObjectStackDefinition>(todoConfigImport);
4119
const kitchenSinkConfig = resolveDefault<ObjectStackDefinition>(kitchenSinkConfigImport);

objectstack.config.ts

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,11 @@ import { InMemoryDriver } from '@objectstack/driver-memory';
2020
import { HonoServerPlugin } from '@objectstack/plugin-hono-server';
2121
import { ConsolePlugin } from '@object-ui/console';
2222
import { composeStacks } from '@objectstack/spec';
23+
import { mergeViewsIntoObjects } from '@object-ui/core';
2324
import { CRMPlugin } from './examples/crm/plugin';
2425
import { TodoPlugin } from './examples/todo/plugin';
2526
import { KitchenSinkPlugin } from './examples/kitchen-sink/plugin';
2627

27-
// ---------------------------------------------------------------------------
28-
// Adapter: merge stack-level views (views[].listViews) into object definitions.
29-
// The runtime reads listViews from each object; this bridges the gap until
30-
// the runtime/provider layer handles it natively.
31-
// ---------------------------------------------------------------------------
32-
function mergeViewsIntoObjects(objects: any[], views: any[]): any[] {
33-
const viewsByObject: Record<string, Record<string, any>> = {};
34-
for (const view of views) {
35-
if (!view.listViews) continue;
36-
for (const [viewName, listView] of Object.entries(view.listViews as Record<string, any>)) {
37-
const objectName = listView?.data?.object;
38-
if (!objectName) continue;
39-
if (!viewsByObject[objectName]) viewsByObject[objectName] = {};
40-
viewsByObject[objectName][viewName] = listView;
41-
}
42-
}
43-
return objects.map((obj: any) => {
44-
const v = viewsByObject[obj.name];
45-
if (!v) return obj;
46-
return { ...obj, listViews: { ...(obj.listViews || {}), ...v } };
47-
});
48-
}
49-
5028
// Instantiate example plugins
5129
const plugins = [new CRMPlugin(), new TodoPlugin(), new KitchenSinkPlugin()];
5230

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ export * from './data-scope/index.js';
2626
export * from './errors/index.js';
2727
export * from './utils/debug.js';
2828
export * from './utils/debug-collector.js';
29+
export * from './utils/merge-views-into-objects.js';
2930
export * from './protocols/index.js';
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Adapter: merge stack-level views into object definitions.
3+
*
4+
* Views are defined at the stack level (views[].listViews) but the runtime
5+
* expects listViews on each object definition. This bridges the gap until
6+
* the runtime/provider layer handles it natively.
7+
*
8+
* @param objects - Object definitions from composed stack
9+
* @param views - View definitions containing listViews keyed by object name
10+
* @returns Objects with listViews merged in from matching views
11+
*/
12+
export function mergeViewsIntoObjects(objects: any[], views: any[]): any[] {
13+
const viewsByObject: Record<string, Record<string, any>> = {};
14+
for (const view of views) {
15+
if (!view.listViews) continue;
16+
for (const [viewName, listView] of Object.entries(view.listViews as Record<string, any>)) {
17+
const objectName = listView?.data?.object;
18+
if (!objectName) continue;
19+
if (!viewsByObject[objectName]) viewsByObject[objectName] = {};
20+
viewsByObject[objectName][viewName] = listView;
21+
}
22+
}
23+
return objects.map((obj: any) => {
24+
const v = viewsByObject[obj.name];
25+
if (!v) return obj;
26+
return { ...obj, listViews: { ...(obj.listViews || {}), ...v } };
27+
});
28+
}

0 commit comments

Comments
 (0)