-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsys-metadata.object.ts
More file actions
278 lines (250 loc) · 9.24 KB
/
Copy pathsys-metadata.object.ts
File metadata and controls
278 lines (250 loc) · 9.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { ObjectSchema, Field } from '@objectstack/spec/data';
/**
* sys_metadata — System Metadata Object
*
* Canonical ObjectStack object definition for the metadata persistence table.
* Stores all platform-scope and user-scope metadata records (Objects, Views,
* Flows, etc.) using the MetadataRecordSchema envelope.
*
* This is a system object (isSystem: true) — protected from deletion and
* automatically provisioned by the DatabaseLoader on first use.
*
* @see MetadataRecordSchema in metadata-persistence.zod.ts
*/
export const SysMetadataObject = ObjectSchema.create({
name: 'sys_metadata',
label: 'System Metadata',
pluralLabel: 'System Metadata',
icon: 'settings',
isSystem: true,
// managedBy: 'engine-owned' — the metadata table backs every other config
// object. Writing rows directly here bypasses the typed Zod APIs and
// would let an admin inject malformed payloads. The "All Metadata"
// menu is therefore a read-only debug surface (Export only); typed
// edits flow through the dedicated per-type pages (Approval Process,
// Sharing Rule, etc.).
managedBy: 'engine-owned',
description: 'Stores platform and user-scope metadata records (objects, views, flows, etc.)',
fields: {
/** Primary Key (UUID) */
id: Field.text({
label: 'ID',
required: true,
readonly: true,
}),
/** Machine name — unique identifier used in code references */
name: Field.text({
label: 'Name',
required: true,
searchable: true,
maxLength: 255,
}),
/** Metadata type (e.g. "object", "view", "flow") */
type: Field.text({
label: 'Metadata Type',
required: true,
searchable: true,
maxLength: 100,
}),
/** Namespace / module grouping (e.g. "crm", "core") */
namespace: Field.text({
label: 'Namespace',
required: false,
defaultValue: 'default',
maxLength: 100,
}),
/** Package that owns/delivered this metadata (legacy string identifier, kept for compat) */
package_id: Field.text({
label: 'Package ID',
required: false,
maxLength: 255,
description: 'Legacy package manifest ID string. Use package_version_id for new records.',
}),
/**
* FK → sys_package_version (UUID). Set for metadata that belongs to a specific
* package release snapshot. NULL = platform-built-in or environment override.
*/
package_version_id: Field.lookup('sys_package_version', {
label: 'Package Version',
required: false,
description:
'Foreign key to sys_package_version (UUID). Null = platform-built-in or env-level override.',
}),
/** Who manages this record: package, platform, or user */
managed_by: Field.select(['package', 'platform', 'user'], {
label: 'Managed By',
required: false,
}),
/** Scope: system (code), platform (admin DB), user (personal DB) */
scope: Field.select(['system', 'platform', 'user'], {
label: 'Scope',
required: true,
defaultValue: 'platform',
}),
/** JSON payload — the actual metadata configuration */
metadata: Field.textarea({
label: 'Metadata',
required: true,
description: 'JSON-serialized metadata payload',
}),
/** Parent metadata name for extension/override */
extends: Field.text({
label: 'Extends',
required: false,
maxLength: 255,
}),
/** Merge strategy when extending parent metadata */
strategy: Field.select(['merge', 'replace'], {
label: 'Strategy',
required: false,
defaultValue: 'merge',
}),
/** Owner user ID (for user-scope items) */
owner: Field.text({
label: 'Owner',
required: false,
maxLength: 255,
}),
/** Lifecycle state */
state: Field.select(['draft', 'active', 'archived', 'deprecated'], {
label: 'State',
required: false,
defaultValue: 'active',
}),
/** Organization ID for multi-tenant isolation */
organization_id: Field.lookup('sys_organization', {
label: 'Organization',
required: false,
description: 'Organization for multi-tenant isolation.',
}),
/**
* @deprecated ADR-0005 (revised 2026-05): per-env DBs replace per-project
* isolation. `environment_id` is no longer written by saveMetaItem and not
* consulted by overlay reads. Kept for legacy rows; new writes leave it
* NULL. Will be dropped in a future schema migration.
*/
environment_id: Field.lookup('sys_environment', {
label: 'Environment (deprecated)',
required: false,
description: 'DEPRECATED. Use organization_id for tenant isolation.',
}),
/** Version number for optimistic concurrency */
version: Field.number({
label: 'Version',
required: false,
defaultValue: 1,
}),
/** Content checksum for change detection (e.g. `sha256:<64 hex>` = 71 chars) */
checksum: Field.text({
label: 'Checksum',
required: false,
maxLength: 71,
}),
/** Origin of this metadata record */
source: Field.select(['filesystem', 'database', 'api', 'migration'], {
label: 'Source',
required: false,
}),
/** Classification tags (JSON array) */
tags: Field.textarea({
label: 'Tags',
required: false,
description: 'JSON-serialized array of classification tags',
}),
/** Audit fields */
created_by: Field.lookup('sys_user', {
label: 'Created By',
required: false,
readonly: true,
}),
created_at: Field.datetime({
label: 'Created At',
required: false,
readonly: true,
}),
updated_by: Field.lookup('sys_user', {
label: 'Updated By',
required: false,
}),
updated_at: Field.datetime({
label: 'Updated At',
required: false,
}),
},
indexes: [
// ADR-0005 (revised 2026-05) + ADR-0048: overlay uniqueness is scoped by
// (type, name, organization_id, package_id), restricted to active rows so
// resets / archived versions don't collide. `package_id` is part of the
// discriminator so two installed packages shipping the same `type`/`name`
// each get their OWN customization row (a package-less / global overlay
// uses NULL). environment_id is deprecated and not part of the
// discriminator. The runtime layer (protocol.ts ensureOverlayIndex) issues
// a DROP-then-CREATE migration that uses `COALESCE(package_id,'')` so the
// package-less rows stay unique among themselves (SQLite treats NULLs as
// distinct in a plain unique index); this declaration is the fallback shape
// for drivers without the runtime migration.
{
name: 'idx_sys_metadata_overlay_active',
fields: ['type', 'name', 'organization_id', 'package_id'],
unique: true,
partial: "state = 'active'",
},
{ name: 'idx_sys_metadata_org_type', fields: ['organization_id', 'type'] },
{ fields: ['type', 'scope'] },
{ fields: ['package_version_id'] },
{ fields: ['state'] },
{ fields: ['namespace'] },
],
enable: {
trackHistory: true,
searchable: false,
apiEnabled: true,
// #3220 — sys_metadata is `managedBy: 'engine-owned'`: customization overlays are
// authored ONLY through the metadata-protocol RPC (its engine writes carry a
// transaction context, not a user session), never the generic /data route.
// Neither the framework nor the Console (objectui) POSTs /data/sys_metadata,
// so the generic write verbs were a latent hole for a user-context raw write
// the bucket forbids. Reads stay open for the Setup "Data Model" grids.
apiMethods: ['get', 'list'],
trash: false,
},
// Named list views — power the Setup App "Data Model" group so admins
// can browse object/field metadata in a typed grid instead of the raw
// `All Metadata` debug surface. Each entry pre-filters by `type` and
// shows the columns that matter for that type. The dedicated visual
// designer (objectui's <ObjectManager> / <FieldDesigner>) deep-links
// from the row's `Edit in Designer` action; the grid stays useful for
// search, audit (state / updated_at) and triage.
listViews: {
only_objects: {
type: 'grid',
name: 'only_objects',
label: 'Objects',
data: { provider: 'object', object: 'sys_metadata' },
columns: ['name', 'namespace', 'scope', 'managed_by', 'state', 'updated_at'],
filter: [{ field: 'type', operator: 'equals', value: 'object' }],
sort: [{ field: 'name', order: 'asc' }],
pagination: { pageSize: 50 },
},
only_fields: {
type: 'grid',
name: 'only_fields',
label: 'Fields',
data: { provider: 'object', object: 'sys_metadata' },
columns: ['name', 'namespace', 'scope', 'managed_by', 'state', 'updated_at'],
filter: [{ field: 'type', operator: 'equals', value: 'field' }],
sort: [{ field: 'name', order: 'asc' }],
pagination: { pageSize: 50 },
},
all_metadata: {
type: 'grid',
name: 'all_metadata',
label: 'All',
data: { provider: 'object', object: 'sys_metadata' },
columns: ['name', 'type', 'namespace', 'scope', 'state', 'updated_at'],
sort: [{ field: 'updated_at', order: 'desc' }],
pagination: { pageSize: 50 },
},
},
});