@terreno/admin-backend 57.0.0-beta.1 → 57.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/dist/adminApp.d.ts +11 -4
- package/dist/adminApp.js +859 -263
- package/dist/adminConfigIdentity.d.ts +16 -0
- package/dist/adminConfigIdentity.js +100 -0
- package/dist/adminTextSearch.d.ts +17 -0
- package/dist/adminTextSearch.js +117 -0
- package/dist/adminUiV2.js +1 -1
- package/dist/aggregateAdmin.d.ts +20 -0
- package/dist/aggregateAdmin.js +275 -0
- package/dist/documentStorageApp.d.ts +3 -1
- package/dist/documentStorageApp.js +20 -42
- package/dist/filterParser.d.ts +11 -0
- package/dist/filterParser.js +260 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +15 -1
- package/dist/legacy.d.ts +9 -0
- package/dist/legacy.js +61 -0
- package/dist/resolvedAdminModel.d.ts +23 -0
- package/dist/resolvedAdminModel.js +130 -0
- package/dist/routePath.d.ts +2 -0
- package/dist/routePath.js +18 -0
- package/package.json +4 -3
- package/src/adminApp.models.test.ts +894 -1
- package/src/adminApp.test.ts +35 -1
- package/src/adminApp.ts +599 -98
- package/src/adminConfigIdentity.test.ts +56 -0
- package/src/adminConfigIdentity.ts +87 -0
- package/src/adminTextSearch.test.ts +73 -0
- package/src/adminTextSearch.ts +74 -0
- package/src/adminUiV2.test.ts +1 -0
- package/src/adminUiV2.ts +1 -1
- package/src/aggregateAdmin.test.ts +220 -0
- package/src/aggregateAdmin.ts +206 -0
- package/src/documentStorageApp.test.ts +11 -0
- package/src/documentStorageApp.ts +20 -39
- package/src/filterParser.test.ts +68 -0
- package/src/filterParser.ts +229 -0
- package/src/index.ts +35 -0
- package/src/legacy.test.ts +34 -0
- package/src/legacy.ts +63 -0
- package/src/resolvedAdminModel.ts +114 -0
- package/src/routePath.ts +14 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {afterEach, describe, expect, it} from "bun:test";
|
|
2
|
+
import {FoodModel} from "@terreno/api/testing";
|
|
3
|
+
|
|
4
|
+
import type {AdminModelConfig} from "./adminApp";
|
|
5
|
+
import {convertLegacyModelConfig, resetLegacyDeprecationWarningsForTests} from "./legacy";
|
|
6
|
+
|
|
7
|
+
describe("convertLegacyModelConfig", () => {
|
|
8
|
+
afterEach(() => {
|
|
9
|
+
resetLegacyDeprecationWarningsForTests();
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it("maps legacy fields onto AdminConfig and warns once per model", () => {
|
|
13
|
+
const config: AdminModelConfig = {
|
|
14
|
+
defaultSort: "-created",
|
|
15
|
+
displayName: "Foods",
|
|
16
|
+
hiddenFields: ["secret"],
|
|
17
|
+
listFields: ["name", "calories"],
|
|
18
|
+
model: FoodModel,
|
|
19
|
+
permissions: {create: false, delete: true, update: true},
|
|
20
|
+
routePath: "/foods",
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const first = convertLegacyModelConfig(config);
|
|
24
|
+
const second = convertLegacyModelConfig(config);
|
|
25
|
+
|
|
26
|
+
expect(first.displayName).toBe("Foods");
|
|
27
|
+
expect(first.admin.listFields).toEqual(["name", "calories"]);
|
|
28
|
+
expect(first.admin.hiddenFields).toEqual(["secret"]);
|
|
29
|
+
expect(first.permissions).toEqual({create: false, delete: true, update: true});
|
|
30
|
+
expect(first.source).toBe("legacy");
|
|
31
|
+
expect(second.source).toBe("legacy");
|
|
32
|
+
expect(first.routePath).toBe("/foods");
|
|
33
|
+
});
|
|
34
|
+
});
|
package/src/legacy.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type {AdminConfig} from "@terreno/api";
|
|
2
|
+
import {logger} from "@terreno/api";
|
|
3
|
+
|
|
4
|
+
import type {AdminModelConfig} from "./adminApp";
|
|
5
|
+
import type {ResolvedAdminModel} from "./resolvedAdminModel";
|
|
6
|
+
import {normalizeAdminRoutePath} from "./routePath";
|
|
7
|
+
|
|
8
|
+
const warnedLegacyKeys = new Set<string>();
|
|
9
|
+
|
|
10
|
+
const legacyWarningKey = (config: AdminModelConfig): string => {
|
|
11
|
+
return `${config.model.modelName}:${normalizeAdminRoutePath(config.routePath)}`;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Converts legacy `AdminApp.models[]` entries into resolved admin models and emits a
|
|
16
|
+
* one-time deprecation warning per model/routePath per process.
|
|
17
|
+
*/
|
|
18
|
+
export const convertLegacyModelConfig = (config: AdminModelConfig): ResolvedAdminModel => {
|
|
19
|
+
const key = legacyWarningKey(config);
|
|
20
|
+
if (!warnedLegacyKeys.has(key)) {
|
|
21
|
+
warnedLegacyKeys.add(key);
|
|
22
|
+
logger.warn(
|
|
23
|
+
`[admin] AdminApp.models entry "${config.displayName}" uses the legacy shape (model=${config.model.modelName}, routePath=${config.routePath}). Migrate to modelRouter({admin: {...}}) — see docs/how-to/admin-add-model.md`
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const admin: AdminConfig = {
|
|
28
|
+
actions: config.actions,
|
|
29
|
+
autocompleteFields: undefined,
|
|
30
|
+
bulkPatchAllowlist: config.bulkPatchAllowlist,
|
|
31
|
+
defaultSort: config.defaultSort,
|
|
32
|
+
displayName: config.displayName,
|
|
33
|
+
excludeFields: undefined,
|
|
34
|
+
fieldOrder: config.fieldOrder,
|
|
35
|
+
fieldOverrides: config.fieldOverrides,
|
|
36
|
+
fieldsets: config.fieldsets,
|
|
37
|
+
filters: config.filters,
|
|
38
|
+
group: config.group,
|
|
39
|
+
hiddenFields: config.hiddenFields,
|
|
40
|
+
listDisplay: config.listDisplay,
|
|
41
|
+
listDisplayLinks: config.listDisplayLinks,
|
|
42
|
+
listFields: config.listFields,
|
|
43
|
+
pageSize: config.pageSize,
|
|
44
|
+
readonlyFields: config.readonlyFields,
|
|
45
|
+
realtime: config.realtime,
|
|
46
|
+
recordTitleField: config.recordTitleField,
|
|
47
|
+
searchFields: config.searchFields,
|
|
48
|
+
sortableFields: config.sortableFields,
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
...config,
|
|
53
|
+
admin,
|
|
54
|
+
routePath: normalizeAdminRoutePath(config.routePath),
|
|
55
|
+
source: "legacy",
|
|
56
|
+
sourceLabel: config.displayName,
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/** @internal Test helper — clears one-time legacy deprecation warnings. */
|
|
61
|
+
export const resetLegacyDeprecationWarningsForTests = (): void => {
|
|
62
|
+
warnedLegacyKeys.clear();
|
|
63
|
+
};
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import type {AdminConfig, ModelRouterOptions, PopulatePath} from "@terreno/api";
|
|
2
|
+
import type {Model} from "mongoose";
|
|
3
|
+
|
|
4
|
+
import type {AdminModelConfig} from "./adminApp";
|
|
5
|
+
import type {AdminListFilter, AdminModelPermissionsInput} from "./adminUiV2";
|
|
6
|
+
import {normalizeAdminRoutePath} from "./routePath";
|
|
7
|
+
|
|
8
|
+
export type AdminModelSource = "legacy" | "plugin" | "registered";
|
|
9
|
+
|
|
10
|
+
export interface ResolvedAdminModel extends AdminModelConfig {
|
|
11
|
+
admin: AdminConfig;
|
|
12
|
+
source: AdminModelSource;
|
|
13
|
+
sourceLabel: string;
|
|
14
|
+
registrationPath?: string;
|
|
15
|
+
excludeFields?: string[];
|
|
16
|
+
autocompleteFields?: string[];
|
|
17
|
+
icon?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const adminPermissionsToModelPermissions = (
|
|
21
|
+
permissions?: AdminConfig["adminPermissions"]
|
|
22
|
+
): AdminModelPermissionsInput | undefined => {
|
|
23
|
+
if (!permissions) {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
const has = (methods: typeof permissions.create): boolean => {
|
|
27
|
+
return methods != null && methods.length > 0;
|
|
28
|
+
};
|
|
29
|
+
const mapped: AdminModelPermissionsInput = {};
|
|
30
|
+
if (permissions.create !== undefined) {
|
|
31
|
+
mapped.create = has(permissions.create);
|
|
32
|
+
}
|
|
33
|
+
if (permissions.delete !== undefined) {
|
|
34
|
+
mapped.delete = has(permissions.delete);
|
|
35
|
+
}
|
|
36
|
+
if (permissions.update !== undefined) {
|
|
37
|
+
mapped.update = has(permissions.update);
|
|
38
|
+
}
|
|
39
|
+
return mapped;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const adminFilterToQueryFilter = (
|
|
43
|
+
adminFilter?: AdminConfig["adminFilter"]
|
|
44
|
+
): AdminModelConfig["queryFilter"] | undefined => {
|
|
45
|
+
if (!adminFilter) {
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
return async (user, query) => {
|
|
49
|
+
const stubReq = {user} as import("express").Request;
|
|
50
|
+
const scoped = await adminFilter(stubReq);
|
|
51
|
+
return {...(query ?? {}), ...scoped};
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export const resolvedModelFromAdminConfig = ({
|
|
56
|
+
admin,
|
|
57
|
+
model,
|
|
58
|
+
populatePaths,
|
|
59
|
+
queryFilter,
|
|
60
|
+
registrationPath,
|
|
61
|
+
routePath,
|
|
62
|
+
source,
|
|
63
|
+
sourceLabel,
|
|
64
|
+
}: {
|
|
65
|
+
admin: AdminConfig;
|
|
66
|
+
model: Model<unknown>;
|
|
67
|
+
populatePaths?: PopulatePath[];
|
|
68
|
+
queryFilter?: ModelRouterOptions<unknown>["queryFilter"];
|
|
69
|
+
registrationPath?: string;
|
|
70
|
+
routePath: string;
|
|
71
|
+
source: AdminModelSource;
|
|
72
|
+
sourceLabel: string;
|
|
73
|
+
}): ResolvedAdminModel => {
|
|
74
|
+
const normalizedPath = normalizeAdminRoutePath(routePath);
|
|
75
|
+
const defaultSort = Array.isArray(admin.defaultSort)
|
|
76
|
+
? admin.defaultSort.join(" ")
|
|
77
|
+
: admin.defaultSort;
|
|
78
|
+
|
|
79
|
+
const mergedQueryFilter = queryFilter ?? adminFilterToQueryFilter(admin.adminFilter);
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
actions: admin.actions,
|
|
83
|
+
admin,
|
|
84
|
+
autocompleteFields: admin.autocompleteFields,
|
|
85
|
+
bulkPatchAllowlist: admin.bulkPatchAllowlist,
|
|
86
|
+
defaultSort,
|
|
87
|
+
displayName: admin.displayName,
|
|
88
|
+
excludeFields: admin.excludeFields,
|
|
89
|
+
fieldOrder: admin.fieldOrder,
|
|
90
|
+
fieldOverrides: admin.fieldOverrides,
|
|
91
|
+
fieldsets: admin.fieldsets,
|
|
92
|
+
filters: admin.filters as AdminListFilter[] | undefined,
|
|
93
|
+
group: admin.group,
|
|
94
|
+
hiddenFields: admin.hiddenFields,
|
|
95
|
+
icon: admin.icon,
|
|
96
|
+
listDisplay: admin.listDisplay,
|
|
97
|
+
listDisplayLinks: admin.listDisplayLinks,
|
|
98
|
+
listFields: admin.listFields,
|
|
99
|
+
model,
|
|
100
|
+
pageSize: admin.pageSize,
|
|
101
|
+
permissions: adminPermissionsToModelPermissions(admin.adminPermissions),
|
|
102
|
+
populatePaths,
|
|
103
|
+
queryFilter: mergedQueryFilter,
|
|
104
|
+
readonlyFields: admin.readonlyFields,
|
|
105
|
+
realtime: admin.realtime,
|
|
106
|
+
recordTitleField: admin.recordTitleField,
|
|
107
|
+
registrationPath,
|
|
108
|
+
routePath: normalizedPath,
|
|
109
|
+
searchFields: admin.searchFields,
|
|
110
|
+
sortableFields: admin.sortableFields,
|
|
111
|
+
source,
|
|
112
|
+
sourceLabel,
|
|
113
|
+
};
|
|
114
|
+
};
|
package/src/routePath.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** Normalize admin model route paths for collision detection (`/users` and `/users/` match). */
|
|
2
|
+
export const normalizeAdminRoutePath = (routePath: string): string => {
|
|
3
|
+
if (!routePath) {
|
|
4
|
+
return "/";
|
|
5
|
+
}
|
|
6
|
+
let path = routePath.trim();
|
|
7
|
+
if (!path.startsWith("/")) {
|
|
8
|
+
path = `/${path}`;
|
|
9
|
+
}
|
|
10
|
+
if (path.length > 1 && path.endsWith("/")) {
|
|
11
|
+
path = path.replace(/\/+$/, "");
|
|
12
|
+
}
|
|
13
|
+
return path;
|
|
14
|
+
};
|