@nest-my-admin/core 0.5.0 → 0.7.1
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 +118 -6
- package/dist/api/action.service.js +2 -3
- package/dist/api/action.service.js.map +1 -1
- package/dist/api/admin-api.service.d.ts +17 -2
- package/dist/api/admin-api.service.js +104 -44
- package/dist/api/admin-api.service.js.map +1 -1
- package/dist/api/menu.d.ts +35 -2
- package/dist/api/menu.js +51 -6
- package/dist/api/menu.js.map +1 -1
- package/dist/api/write-runner.d.ts +2 -2
- package/dist/api/write-runner.js +8 -2
- package/dist/api/write-runner.js.map +1 -1
- package/dist/audit/audit-participant.js +2 -1
- package/dist/audit/audit-participant.js.map +1 -1
- package/dist/audit/audit.service.js +5 -3
- package/dist/audit/audit.service.js.map +1 -1
- package/dist/contract.d.ts +8 -0
- package/dist/crud/relation-writes.d.ts +6 -0
- package/dist/crud/relation-writes.js +32 -0
- package/dist/crud/relation-writes.js.map +1 -1
- package/dist/dashboard/dashboard.service.js +1 -2
- package/dist/dashboard/dashboard.service.js.map +1 -1
- package/dist/decorators/admin-action.d.ts +3 -2
- package/dist/decorators/admin-action.js +9 -8
- package/dist/decorators/admin-action.js.map +1 -1
- package/dist/decorators/admin-field.d.ts +5 -0
- package/dist/decorators/admin-field.js.map +1 -1
- package/dist/decorators/admin-print.d.ts +2 -1
- package/dist/decorators/admin-print.js +2 -3
- package/dist/decorators/admin-print.js.map +1 -1
- package/dist/decorators/admin-settings-page.d.ts +2 -2
- package/dist/http/admin-http.server.js +8 -1
- package/dist/http/admin-http.server.js.map +1 -1
- package/dist/http/ui-messages.d.ts +9 -0
- package/dist/http/ui-messages.js +43 -0
- package/dist/http/ui-messages.js.map +1 -0
- package/dist/i18n/texts.d.ts +26 -0
- package/dist/i18n/texts.js +31 -0
- package/dist/i18n/texts.js.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js.map +1 -1
- package/dist/inlines/inline-config.d.ts +6 -2
- package/dist/inlines/inline-config.js +9 -6
- package/dist/inlines/inline-config.js.map +1 -1
- package/dist/inlines/inline-writer.d.ts +1 -1
- package/dist/inlines/inline-writer.js +6 -2
- package/dist/inlines/inline-writer.js.map +1 -1
- package/dist/options.d.ts +28 -5
- package/dist/options.js +15 -3
- package/dist/options.js.map +1 -1
- package/dist/pages/page.service.js +5 -4
- package/dist/pages/page.service.js.map +1 -1
- package/dist/policy/admin-policy.service.d.ts +11 -2
- package/dist/policy/admin-policy.service.js +23 -11
- package/dist/policy/admin-policy.service.js.map +1 -1
- package/dist/policy/explain.js +2 -1
- package/dist/policy/explain.js.map +1 -1
- package/dist/rbac/rbac.service.js +4 -2
- package/dist/rbac/rbac.service.js.map +1 -1
- package/dist/registry/resource-registry.js +27 -3
- package/dist/registry/resource-registry.js.map +1 -1
- package/dist/resource/admin-context.d.ts +13 -1
- package/dist/resource/admin-context.js +10 -0
- package/dist/resource/admin-context.js.map +1 -1
- package/dist/resource/admin-resource-base.d.ts +30 -8
- package/dist/resource/admin-resource-base.js +28 -1
- package/dist/resource/admin-resource-base.js.map +1 -1
- package/dist/schema/build-resource-schema.js +3 -3
- package/dist/schema/build-resource-schema.js.map +1 -1
- package/dist/schema/dto-form.d.ts +2 -2
- package/dist/schema/dto-form.js +2 -2
- package/dist/schema/dto-form.js.map +1 -1
- package/dist/schema/field-config.d.ts +23 -5
- package/dist/schema/field-config.js +33 -18
- package/dist/schema/field-config.js.map +1 -1
- package/package.json +2 -2
|
@@ -29,7 +29,7 @@ const EXPORT_PAGE = 500;
|
|
|
29
29
|
const PRINT_MAX = 100;
|
|
30
30
|
import { ADMIN_OPTIONS, MAX_BULK } from '../constants.js';
|
|
31
31
|
import { parseListQuery } from '../crud/list-query.js';
|
|
32
|
-
import { buildMenu, checkMenu } from './menu.js';
|
|
32
|
+
import { buildMenu, checkMenu, resolveMenu } from './menu.js';
|
|
33
33
|
import { DashboardService } from '../dashboard/dashboard.service.js';
|
|
34
34
|
import { WidgetRegistry } from '../dashboard/widget-registry.js';
|
|
35
35
|
import { loadComputed } from '../crud/computed-columns.js';
|
|
@@ -38,7 +38,7 @@ import { encodeRecordId, parseRecordId, recordIdOf } from '../crud/record-id.js'
|
|
|
38
38
|
import { likePattern } from '../crud/list-query-builder.js';
|
|
39
39
|
import { parseId } from '../crud/list-query.js';
|
|
40
40
|
import { isLoadedField, loadReferences, relationRef } from '../crud/references.js';
|
|
41
|
-
import { relationIdsOf } from '../crud/relation-writes.js';
|
|
41
|
+
import { normalizeRelationValues, relationIdsOf } from '../crud/relation-writes.js';
|
|
42
42
|
import { serializeRecord } from '../crud/serialize.js';
|
|
43
43
|
import { validateWrite } from '../crud/validate-write.js';
|
|
44
44
|
import { auditParticipant } from '../audit/audit-participant.js';
|
|
@@ -47,14 +47,16 @@ import { filesParticipant } from '../files/files-participant.js';
|
|
|
47
47
|
import { FilesService } from '../files/files.service.js';
|
|
48
48
|
import { AdminBadRequestError, AdminConflictError, AdminForbiddenError, AdminNotFoundError, AdminValidationError } from '../errors.js';
|
|
49
49
|
import { AdminPolicy } from '../policy/admin-policy.service.js';
|
|
50
|
-
import {
|
|
50
|
+
import { scopedText } from '../i18n/texts.js';
|
|
51
51
|
import { ResourceRegistry } from '../registry/resource-registry.js';
|
|
52
|
+
import { inAdminTransaction } from '../resource/admin-context.js';
|
|
52
53
|
import { AdminResourceBase } from '../resource/admin-resource-base.js';
|
|
53
54
|
import { PageRegistry } from '../pages/page-registry.js';
|
|
54
55
|
import { PageService } from '../pages/page.service.js';
|
|
55
56
|
import { ActionService } from './action.service.js';
|
|
56
57
|
import { SINGLE_CONNECTION_DRIVERS } from './write-queue.js';
|
|
57
58
|
import { WriteRunner } from './write-runner.js';
|
|
59
|
+
import { getCanRules } from '../decorators/admin-scope.js';
|
|
58
60
|
let AdminApiService = class AdminApiService {
|
|
59
61
|
registry;
|
|
60
62
|
options;
|
|
@@ -102,7 +104,7 @@ let AdminApiService = class AdminApiService {
|
|
|
102
104
|
change: (change, tx, method) => this.change(change, tx, method),
|
|
103
105
|
checkRelationsExist: (entry, ids, ctx, values) => this.checkRelationsExist(entry, ids, ctx, values),
|
|
104
106
|
checkVersion: (entry, id, expected, ctx) => this.checkVersion(entry, id, expected, ctx),
|
|
105
|
-
lockedFields: (entry, entity) => this.lockedFields(entry, entity),
|
|
107
|
+
lockedFields: (entry, entity, ctx) => this.lockedFields(entry, entity, ctx),
|
|
106
108
|
values: async (entry, entity, ctx) => toValues(await this.record(entry, entity, ctx)),
|
|
107
109
|
reloadIfEmpty: (entry, result, id, ctx) => this.reloadIfEmpty(entry, result, id, ctx),
|
|
108
110
|
errorMapper: options.errorMapper,
|
|
@@ -129,7 +131,7 @@ let AdminApiService = class AdminApiService {
|
|
|
129
131
|
}
|
|
130
132
|
/** Sidebar data with labels in `locale` (the default locale when omitted). */
|
|
131
133
|
meta(locale = this.options.locale, ctx) {
|
|
132
|
-
const text = (label) =>
|
|
134
|
+
const text = (label, key) => this.options.texts(label, locale, key);
|
|
133
135
|
// Only resources the user may view (spec §6.5: meta is filtered per user).
|
|
134
136
|
// `nav: false` resources have no entry of their own (spec §5): reachable by URL, not listed.
|
|
135
137
|
const resources = this.registry.list().filter((entry) => entry.nav && (!ctx || this.policy.permissionsOf(ctx).canOn(entry.schema.name, 'view')));
|
|
@@ -140,13 +142,13 @@ let AdminApiService = class AdminApiService {
|
|
|
140
142
|
.groupList()
|
|
141
143
|
.map((group) => ({
|
|
142
144
|
group,
|
|
143
|
-
label: text(group.label),
|
|
145
|
+
label: text(group.label, `group.${group.key}`),
|
|
144
146
|
resources: resources
|
|
145
147
|
.filter((entry) => entry.schema.group === group.key)
|
|
146
148
|
.map((entry) => {
|
|
147
149
|
const { schema, label } = entry;
|
|
148
150
|
const seen = ctx ? this.view(entry, ctx).schema : schema;
|
|
149
|
-
const summary = { name: schema.name, label: text(label), ...(schema.icon ? { icon: schema.icon } : {}), creatable: seen.creatable, searchable: seen.list.search.length > 0 };
|
|
151
|
+
const summary = { name: schema.name, label: text(label, `resource.${schema.name}`), ...(schema.icon ? { icon: schema.icon } : {}), creatable: seen.creatable, searchable: seen.list.search.length > 0 };
|
|
150
152
|
return { order: entry.order, summary };
|
|
151
153
|
})
|
|
152
154
|
.sort(byOrder)
|
|
@@ -167,10 +169,10 @@ let AdminApiService = class AdminApiService {
|
|
|
167
169
|
resources: items,
|
|
168
170
|
pages: groupPages,
|
|
169
171
|
}));
|
|
170
|
-
const menu = this.
|
|
172
|
+
const menu = this.menu && buildMenu(this.menu, text, groups);
|
|
171
173
|
// With widgets in the app, whether this user has a dashboard (it replaces the home page).
|
|
172
174
|
const dashboard = this.widgets.list().length > 0 ? { dashboard: ctx ? this.dashboard.has(ctx) : true } : {};
|
|
173
|
-
return { schemaVersion: 1, title: text(this.options.title), locale, locales: this.options.locales, groups, ...dashboard, ...(menu ? { menu } : {}) };
|
|
175
|
+
return { schemaVersion: 1, title: text(this.options.title, 'title'), locale, locales: this.options.locales, groups, ...dashboard, ...(menu ? { menu } : {}) };
|
|
174
176
|
}
|
|
175
177
|
/**
|
|
176
178
|
* Image cells hold a file key; the page gets its URL (the storage's public one, or the admin's download route, which
|
|
@@ -222,14 +224,22 @@ let AdminApiService = class AdminApiService {
|
|
|
222
224
|
/** Computed columns of some rows of a resource, image cells as URLs. */
|
|
223
225
|
async computedValues(entry, columns, entities, ctx) {
|
|
224
226
|
const locale = ctx.locale ?? this.options.locale;
|
|
225
|
-
const values = await loadComputed(columns, entities, entry.metadata, ctx, this.registry.childQuery, (alias) => entry.resource.readQuery(alias, ctx), (label) =>
|
|
227
|
+
const values = await loadComputed(columns, entities, entry.metadata, ctx, this.registry.childQuery, (alias) => entry.resource.readQuery(alias, ctx), (label) => this.options.texts(label, locale), (target, field) => this.registry.forEntity(target, entry.dataSource)?.schema.fields.find((candidate) => candidate.name === field));
|
|
226
228
|
await this.imageUrls(entry, columns, values, ctx);
|
|
227
229
|
return values;
|
|
228
230
|
}
|
|
229
231
|
/** The menu's references are checked once every resource and settings page is registered. */
|
|
230
232
|
onApplicationBootstrap() {
|
|
231
|
-
if (this.options.menu)
|
|
232
|
-
|
|
233
|
+
if (!this.options.menu)
|
|
234
|
+
return;
|
|
235
|
+
this.menu = resolveMenu(this.options.menu, this.registry, this.pageRegistry);
|
|
236
|
+
checkMenu(this.menu, this.registry, this.pageRegistry);
|
|
237
|
+
}
|
|
238
|
+
/** `options.menu` with its classes and groups resolved (at bootstrap, once everything is registered). */
|
|
239
|
+
menu;
|
|
240
|
+
/** @internal The resolved menu (`@nest-my-admin/testing` crawls its badges). */
|
|
241
|
+
resolvedMenu() {
|
|
242
|
+
return this.menu;
|
|
233
243
|
}
|
|
234
244
|
/**
|
|
235
245
|
* `GET /api/menu/badges`: each menu badge counted for this user through the list's own path (scopes and `query()`
|
|
@@ -238,8 +248,8 @@ let AdminApiService = class AdminApiService {
|
|
|
238
248
|
async badges(ctx) {
|
|
239
249
|
const out = {};
|
|
240
250
|
const perms = this.policy.permissionsOf(ctx);
|
|
241
|
-
for (const section of this.
|
|
242
|
-
for (const item of section.items) {
|
|
251
|
+
for (const section of this.menu?.sections ?? []) {
|
|
252
|
+
for (const item of section.items ?? []) {
|
|
243
253
|
if (!('resource' in item) || !item.badge)
|
|
244
254
|
continue;
|
|
245
255
|
const entry = this.registry.get(item.resource);
|
|
@@ -266,24 +276,25 @@ let AdminApiService = class AdminApiService {
|
|
|
266
276
|
const entry = this.registry.get(name);
|
|
267
277
|
if (ctx)
|
|
268
278
|
this.policy.requireReach(entry, ctx);
|
|
269
|
-
const text = (
|
|
279
|
+
const text = scopedText(this.options.texts, locale, `resource.${name}`);
|
|
270
280
|
const related = entry.schema.related.map((item, index) => {
|
|
271
|
-
const
|
|
281
|
+
const target = this.registry.get(item.resource);
|
|
282
|
+
const base = this.options.texts(target.label, locale, `resource.${target.schema.name}`);
|
|
272
283
|
const fieldLabel = entry.relatedFieldLabels[index];
|
|
273
284
|
return { ...item, label: fieldLabel ? `${base} (${fieldLabel})` : base };
|
|
274
285
|
});
|
|
275
|
-
const fields = entry.schema.fields.map((field) => applyFieldConfig(field, entry.fieldConfig.get(field.name),
|
|
286
|
+
const fields = entry.schema.fields.map((field) => applyFieldConfig(field, entry.fieldConfig.get(field.name), (value, path) => text(value, `fields.${path}`), this.options.files?.maxBytes, this.files.enabled));
|
|
276
287
|
const layout = mainLayout(entry.resource.form?.layout);
|
|
277
|
-
const inline = (node, label) => toLayoutInline(inlineOf(entry, node), label,
|
|
278
|
-
const form = layout ? { ...entry.schema.form, layout: localizeLayout(layout,
|
|
279
|
-
const actions = [...entry.actions.values()].map((action) => toActionSchema(action,
|
|
280
|
-
const prints = [...entry.prints.values()].map((print) => toPrintSchema(print,
|
|
281
|
-
const computed = entry.schema.list.computed && Object.fromEntries(Object.entries(entry.schema.list.computed).map(([key, column]) => [key, { ...column, label: text(entry.computed.get(key).column.label) }]));
|
|
282
|
-
const tabs = entry.schema.list.tabs?.map((tab, index) => ({ ...tab, label: text(entry.resource.list.tabs[index].label) }));
|
|
288
|
+
const inline = (node, label) => toLayoutInline(inlineOf(entry, node), label, text, this.options.texts(inlineOf(entry, node).child.label, locale, `resource.${inlineOf(entry, node).name}`));
|
|
289
|
+
const form = layout ? { ...entry.schema.form, layout: localizeLayout(layout, (value, key) => text(value, key === undefined ? undefined : `form.${key}`), inline) } : entry.schema.form;
|
|
290
|
+
const actions = [...entry.actions.values()].map((action) => toActionSchema(action, text));
|
|
291
|
+
const prints = [...entry.prints.values()].map((print) => toPrintSchema(print, text));
|
|
292
|
+
const computed = entry.schema.list.computed && Object.fromEntries(Object.entries(entry.schema.list.computed).map(([key, column]) => [key, { ...column, label: text(entry.computed.get(key).column.label, `list.computed.${key}.label`) }]));
|
|
293
|
+
const tabs = entry.schema.list.tabs?.map((tab, index) => ({ ...tab, label: text(entry.resource.list.tabs[index].label, `list.tabs.${tab.key}.label`) }));
|
|
283
294
|
const list = { ...entry.schema.list, ...(computed ? { computed } : {}), ...(tabs ? { tabs } : {}) };
|
|
284
295
|
const asideConfig = entry.resource.form?.layout && !Array.isArray(entry.resource.form.layout) ? entry.resource.form.layout.aside : undefined;
|
|
285
|
-
const localizedForm = form.aside && asideConfig ? { ...form, aside: form.aside.map((card, index) => ({ ...card, title: text(asideConfig[index].title) })) } : form;
|
|
286
|
-
const localized = { ...entry.schema, label: text(entry.label), related, fields, form: localizedForm, actions, prints, list };
|
|
296
|
+
const localizedForm = form.aside && asideConfig ? { ...form, aside: form.aside.map((card, index) => ({ ...card, title: text(asideConfig[index].title, asideConfig[index].key === undefined ? undefined : `form.${asideConfig[index].key}`) })) } : form;
|
|
297
|
+
const localized = { ...entry.schema, label: text(entry.label, ''), related, fields, form: localizedForm, actions, prints, list };
|
|
287
298
|
const schema = ctx ? this.policy.view(entry, localized, ctx).schema : localized;
|
|
288
299
|
// The child schema of every inline left in the layout (the policy removed those the user may not view).
|
|
289
300
|
const children = (schema.form.layout ?? []).flatMap((node) => ('tab' in node ? node.sections : [node])).flatMap((node) => ('inline' in node ? [node.inline] : []));
|
|
@@ -316,7 +327,7 @@ let AdminApiService = class AdminApiService {
|
|
|
316
327
|
const computed = [...entry.computed.values()].filter((column) => schema.list.computed?.[column.key] && shown.has(column.key));
|
|
317
328
|
if (computed.length > 0) {
|
|
318
329
|
const locale = ctx.locale ?? this.options.locale;
|
|
319
|
-
const values = await loadComputed(computed, items, entry.metadata, ctx, this.registry.childQuery, (alias) => entry.resource.readQuery(alias, ctx), (label) =>
|
|
330
|
+
const values = await loadComputed(computed, items, entry.metadata, ctx, this.registry.childQuery, (alias) => entry.resource.readQuery(alias, ctx), (label) => this.options.texts(label, locale), (entity, field) => this.registry.forEntity(entity, entry.dataSource)?.schema.fields.find((candidate) => candidate.name === field));
|
|
320
331
|
await this.imageUrls(entry, computed, values, ctx);
|
|
321
332
|
for (const record of records)
|
|
322
333
|
record._computed = values.get(String(record._id)) ?? {};
|
|
@@ -364,7 +375,7 @@ let AdminApiService = class AdminApiService {
|
|
|
364
375
|
return undefined;
|
|
365
376
|
return {
|
|
366
377
|
resource: schema.name,
|
|
367
|
-
label:
|
|
378
|
+
label: this.options.texts(entry.label, locale, `resource.${schema.name}`),
|
|
368
379
|
items: items.map((entity) => {
|
|
369
380
|
const id = recordIdOf(entity, schema.primaryKeys);
|
|
370
381
|
return { _id: id, _title: this.policy.titleVisible(entry, ctx) ? entry.title(entity) : `#${id}` };
|
|
@@ -459,7 +470,7 @@ let AdminApiService = class AdminApiService {
|
|
|
459
470
|
const files = fileFields.length > 0 ? await this.files.refs(this.files.ownerOf(entry), entities, fileFields, ctx) : undefined;
|
|
460
471
|
const out = entities.map((entity, index) => {
|
|
461
472
|
const id = recordIdOf(entity, schema.primaryKeys);
|
|
462
|
-
const locked = this.lockedFields(entry, entity);
|
|
473
|
+
const locked = this.lockedFields(entry, entity, ctx);
|
|
463
474
|
const links = this.recordLinks(entry, entity, locale);
|
|
464
475
|
// TypeORM does not load the discriminator into entities; each row's class says which kind it is.
|
|
465
476
|
const values = discriminator && schema.fields.some((field) => field.name === discriminator) ? { ...loaded?.get(id), [discriminator]: kindOf(entity) } : loaded?.get(id);
|
|
@@ -519,6 +530,7 @@ let AdminApiService = class AdminApiService {
|
|
|
519
530
|
return this.write(entry, ctx, async (tx) => {
|
|
520
531
|
// Every refusal the children can cause is decided before the parent is written.
|
|
521
532
|
const checked = plan ? await this.inlineWriter.check(plan, undefined, tx) : undefined;
|
|
533
|
+
await this.checkParentRules(entry, dto, tx);
|
|
522
534
|
const created = await this.change({ kind: 'create', target: entry, dto }, tx, async () => {
|
|
523
535
|
await this.checkRelationsExist(entry, relationIds, tx, dto);
|
|
524
536
|
const result = await resource.create(dto, tx);
|
|
@@ -532,6 +544,35 @@ let AdminApiService = class AdminApiService {
|
|
|
532
544
|
return this.withInlineRows(entry, created, tx);
|
|
533
545
|
});
|
|
534
546
|
}
|
|
547
|
+
/**
|
|
548
|
+
* `@AdminCan('create')` of an inline child written through its own endpoint: each parent its link field names, as
|
|
549
|
+
* the user may view it, must allow the row (403) — on create, and on an update that moves the row to another parent
|
|
550
|
+
* (`existing`: the stored row; staying under its parent is not adding). Without a link value the rule has nothing to
|
|
551
|
+
* judge; a parent the parent resource does not show the user is a 422 like a missing one.
|
|
552
|
+
*/
|
|
553
|
+
async checkParentRules(entry, dto, ctx, existing) {
|
|
554
|
+
if (getCanRules(entry.resource.constructor, 'create').length === 0)
|
|
555
|
+
return;
|
|
556
|
+
for (const parent of this.registry.list()) {
|
|
557
|
+
for (const inline of parent.inlines) {
|
|
558
|
+
const value = inline.child === entry ? dto[inline.link] : undefined;
|
|
559
|
+
if (value === undefined || value === null)
|
|
560
|
+
continue;
|
|
561
|
+
if (existing) {
|
|
562
|
+
const stored = existing[inline.link];
|
|
563
|
+
const storedId = typeof stored === 'object' && stored !== null ? stored[inline.referenced] : stored;
|
|
564
|
+
if (storedId !== undefined && storedId !== null && String(storedId) === String(value))
|
|
565
|
+
continue;
|
|
566
|
+
}
|
|
567
|
+
const found = await parent.resource.readQuery('nma_parent', ctx).andWhere(`nma_parent.${inline.referenced} = :nmaParent`, { nmaParent: value }).getOne();
|
|
568
|
+
// A parent this resource does not show (another resource on the entity might): as missing, so the rule is never skipped.
|
|
569
|
+
if (!found)
|
|
570
|
+
throw new AdminValidationError({ [inline.link]: ['does not exist'] }, 'A related record does not exist');
|
|
571
|
+
if (!this.policy.createRule(entry, found, ctx))
|
|
572
|
+
throw new AdminForbiddenError(`You may not add ${entry.schema.label} rows here`);
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
}
|
|
535
576
|
async update(name, rawId, body, ctx, version) {
|
|
536
577
|
const entry = this.registry.get(name);
|
|
537
578
|
this.policy.require(entry, 'update', ctx);
|
|
@@ -560,7 +601,8 @@ let AdminApiService = class AdminApiService {
|
|
|
560
601
|
if (!existing)
|
|
561
602
|
throw new AdminNotFoundError(`${schema.label} "${rawId}" not found`);
|
|
562
603
|
this.policy.requireRecordRule(entry, 'update', existing, tx);
|
|
563
|
-
|
|
604
|
+
await this.checkParentRules(entry, dto, tx, existing);
|
|
605
|
+
const locked = this.lockedFields(entry, existing, tx).filter((name) => Object.hasOwn(dto, name));
|
|
564
606
|
if (locked.length > 0)
|
|
565
607
|
throw new AdminValidationError(Object.fromEntries(locked.map((name) => [name, ['is read-only']])), 'Some fields are read-only');
|
|
566
608
|
const checked = plan ? await this.inlineWriter.check(plan, existing, tx) : undefined;
|
|
@@ -642,7 +684,7 @@ let AdminApiService = class AdminApiService {
|
|
|
642
684
|
if (expected === undefined || !version)
|
|
643
685
|
return;
|
|
644
686
|
const manager = ctx.manager ?? entry.dataSource.manager;
|
|
645
|
-
const lock = ctx
|
|
687
|
+
const lock = inAdminTransaction(ctx) && !SINGLE_CONNECTION_DRIVERS.has(entry.dataSource.options.type) ? { mode: 'pessimistic_write' } : undefined;
|
|
646
688
|
const where = typeof id === 'object' ? id : { [entry.schema.primaryKeys[0]]: id };
|
|
647
689
|
const current = await manager.getRepository(entry.metadata.target).findOne({ where, withDeleted, ...(lock ? { lock } : {}) });
|
|
648
690
|
if (!current)
|
|
@@ -655,7 +697,7 @@ let AdminApiService = class AdminApiService {
|
|
|
655
697
|
* A query for the records a relation field may point to (`option`), restricted by the target resource's query() and
|
|
656
698
|
* this resource's relationOptions().
|
|
657
699
|
*/
|
|
658
|
-
optionsQuery(entry, field, ctx, values) {
|
|
700
|
+
async optionsQuery(entry, field, ctx, values) {
|
|
659
701
|
const target = entry.relations.get(field).inverseEntityMetadata;
|
|
660
702
|
const manager = ctx.manager ?? entry.dataSource.manager;
|
|
661
703
|
const qb = manager.getRepository(target.target).createQueryBuilder('option');
|
|
@@ -676,7 +718,7 @@ let AdminApiService = class AdminApiService {
|
|
|
676
718
|
continue;
|
|
677
719
|
const target = entry.relations.get(field).inverseEntityMetadata;
|
|
678
720
|
const key = target.primaryColumns[0].propertyName;
|
|
679
|
-
const rows = await this.optionsQuery(entry, field, ctx, values)
|
|
721
|
+
const rows = await (await this.optionsQuery(entry, field, ctx, values))
|
|
680
722
|
.andWhere(`option.${key} IN (:...nmaIds)`, { nmaIds: ids })
|
|
681
723
|
.select(`option.${key}`, 'id')
|
|
682
724
|
.getRawMany();
|
|
@@ -708,7 +750,7 @@ let AdminApiService = class AdminApiService {
|
|
|
708
750
|
throw new AdminValidationError({ [key]: [known.has(key) ? 'must be given once' : 'is not a supported parameter'] });
|
|
709
751
|
}
|
|
710
752
|
}
|
|
711
|
-
const values = this.parentValues(entry, parseValues(query.get('values')), query.get('parent'), ctx);
|
|
753
|
+
const values = this.parentValues(entry, normalizeRelationValues(parseValues(query.get('values')), entry.schema), query.get('parent'), ctx);
|
|
712
754
|
const relation = entry.relations.get(fieldName);
|
|
713
755
|
const target = relation.inverseEntityMetadata;
|
|
714
756
|
const key = target.primaryColumns[0].propertyName;
|
|
@@ -718,7 +760,7 @@ let AdminApiService = class AdminApiService {
|
|
|
718
760
|
if (targetEntry && !this.policy.permissionsOf(ctx).canOn(targetEntry.schema.name, 'view')) {
|
|
719
761
|
throw new AdminForbiddenError(`You may not view ${targetEntry.schema.label} records`);
|
|
720
762
|
}
|
|
721
|
-
const qb = this.optionsQuery(entry, fieldName, ctx, values);
|
|
763
|
+
const qb = await this.optionsQuery(entry, fieldName, ctx, values);
|
|
722
764
|
const rawIds = query.get('ids');
|
|
723
765
|
const search = query.get('search')?.trim() ?? '';
|
|
724
766
|
if (rawIds !== null) {
|
|
@@ -767,7 +809,7 @@ let AdminApiService = class AdminApiService {
|
|
|
767
809
|
if (typeof sent !== 'object' || sent === null || Array.isArray(sent))
|
|
768
810
|
throw new AdminValidationError({ values: ['_parent must be an object of the parent form\'s values'] });
|
|
769
811
|
const own = Object.fromEntries(Object.entries(sent).filter(([key]) => !key.startsWith('_')));
|
|
770
|
-
return { ...values, _parent: { ...own, _resource: parent.schema.name } };
|
|
812
|
+
return { ...values, _parent: { ...normalizeRelationValues(own, parent.schema), _resource: parent.schema.name } };
|
|
771
813
|
}
|
|
772
814
|
/**
|
|
773
815
|
* Deletes each record on its own (its own transaction, hooks, `query()` and trash), so one failure never undoes the
|
|
@@ -815,7 +857,7 @@ let AdminApiService = class AdminApiService {
|
|
|
815
857
|
if (!print)
|
|
816
858
|
throw new AdminNotFoundError(`${entry.schema.label} has no print template "${printName}"`);
|
|
817
859
|
if (!this.policy.canPrint(entry, printName, ctx))
|
|
818
|
-
throw new AdminForbiddenError(`You may not print "${
|
|
860
|
+
throw new AdminForbiddenError(`You may not print "${this.options.texts(print.label, ctx.locale ?? this.options.locale, `resource.${entry.schema.name}.prints.${print.name}.label`)}"`);
|
|
819
861
|
const ids = [...new Set(rawIds)];
|
|
820
862
|
if (ids.length === 0)
|
|
821
863
|
throw new AdminBadRequestError('id is required');
|
|
@@ -839,7 +881,7 @@ let AdminApiService = class AdminApiService {
|
|
|
839
881
|
throw hostError(error);
|
|
840
882
|
}
|
|
841
883
|
const locale = ctx.locale ?? this.options.locale;
|
|
842
|
-
const label =
|
|
884
|
+
const label = this.options.texts(print.label, locale, `resource.${entry.schema.name}.prints.${print.name}.label`);
|
|
843
885
|
for (const entity of entities) {
|
|
844
886
|
await this.audit.recordAfter({ event: printCode(entry.schema.name, printName), entry, recordId: recordIdOf(entity, entry.schema.primaryKeys), title: entry.title(entity) }, ctx);
|
|
845
887
|
}
|
|
@@ -941,22 +983,40 @@ let AdminApiService = class AdminApiService {
|
|
|
941
983
|
await this.change({ kind: 'purge', target: entry, id }, tx, () => entry.resource.purge(id, tx));
|
|
942
984
|
});
|
|
943
985
|
}
|
|
944
|
-
/**
|
|
945
|
-
|
|
946
|
-
|
|
986
|
+
/**
|
|
987
|
+
* Fields `readonlyIf` and the resource's `readonlyFields` lock on this record, each once. A `readonlyIf` that throws
|
|
988
|
+
* locks its field, a `readonlyFields` that throws locks the update form (each logged once); unknown names are dropped.
|
|
989
|
+
*/
|
|
990
|
+
lockedFields(entry, entity, ctx) {
|
|
991
|
+
const locked = new Set();
|
|
947
992
|
for (const [name, options] of entry.fieldConfig) {
|
|
948
993
|
if (!options.readonlyIf)
|
|
949
994
|
continue;
|
|
950
995
|
try {
|
|
951
996
|
if (options.readonlyIf(entity))
|
|
952
|
-
locked.
|
|
997
|
+
locked.add(name);
|
|
953
998
|
}
|
|
954
999
|
catch (error) {
|
|
955
|
-
locked.
|
|
1000
|
+
locked.add(name);
|
|
956
1001
|
this.warnOnce(`${entry.className}.fields.${name}.readonlyIf threw (${error instanceof Error ? error.message : String(error)}); the field stays read-only`);
|
|
957
1002
|
}
|
|
958
1003
|
}
|
|
959
|
-
|
|
1004
|
+
let more;
|
|
1005
|
+
try {
|
|
1006
|
+
more = entry.resource.readonlyFields(entity, ctx);
|
|
1007
|
+
}
|
|
1008
|
+
catch (error) {
|
|
1009
|
+
more = entry.schema.form.update;
|
|
1010
|
+
this.warnOnce(`${entry.className}.readonlyFields threw (${error instanceof Error ? error.message : String(error)}); the record stays read-only`);
|
|
1011
|
+
}
|
|
1012
|
+
const fields = new Set(entry.schema.fields.map((field) => field.name));
|
|
1013
|
+
for (const name of more) {
|
|
1014
|
+
if (fields.has(name) && !entry.schema.primaryKeys.includes(name))
|
|
1015
|
+
locked.add(name);
|
|
1016
|
+
else if (!fields.has(name))
|
|
1017
|
+
this.warnOnce(`${entry.className}.readonlyFields names "${name}", which is not a field; it is ignored`);
|
|
1018
|
+
}
|
|
1019
|
+
return [...locked];
|
|
960
1020
|
}
|
|
961
1021
|
/** `links()` of the resource with labels in the locale; only http(s) and relative URLs get through. */
|
|
962
1022
|
recordLinks(entry, entity, locale) {
|
|
@@ -970,7 +1030,7 @@ let AdminApiService = class AdminApiService {
|
|
|
970
1030
|
}
|
|
971
1031
|
return (Array.isArray(links) ? links : [])
|
|
972
1032
|
.filter((link) => typeof link?.href === 'string' && (/^https?:\/\//i.test(link.href) || !/^[a-z][a-z0-9+.-]*:/i.test(link.href.trim())))
|
|
973
|
-
.map((link) => ({ label:
|
|
1033
|
+
.map((link) => ({ label: this.options.texts(link.label, locale), href: link.href }));
|
|
974
1034
|
}
|
|
975
1035
|
warned = new Set();
|
|
976
1036
|
warnOnce(message) {
|