-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsys-setting.object.ts
More file actions
204 lines (186 loc) · 6.94 KB
/
Copy pathsys-setting.object.ts
File metadata and controls
204 lines (186 loc) · 6.94 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
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { ObjectSchema, Field } from '@objectstack/spec/data';
/**
* sys_setting — Generic K/V store backing the SettingsManifest contract
*
* Single physical table that holds *every* value for *every* settings
* namespace declared by a `SettingsManifest`. Plugins MUST NOT define
* per-namespace tables (e.g. `sys_mail_config`); they declare a manifest
* and the value persists here.
*
* Row identity: (namespace, key, scope, user_id?).
*
* Resolution order (handled by `SettingsService.get`):
* 1. process.env override (source='env', locked=true)
* 2. sys_setting WHERE scope='global' (source='global')
* 3. sys_setting WHERE scope='tenant' (source='tenant')
* 4. sys_setting WHERE scope='user' (source='user')
* 5. manifest specifier.default (source='default')
*
* Encryption: rows with `encrypted=true` store ciphertext in `value_enc`
* and leave `value` null. The plain value is never written to audit log
* or history snapshots — only an `'<encrypted>'` placeholder + a digest.
*
* managedBy: 'engine-owned' — the admin grid in Setup is a diagnostic surface
* only; all writes flow through `SettingsService.set()` so the resolver
* stays the single source of truth.
*
* See ADR-0007 (Settings Manifest + K/V Store + Resolver).
*
* @namespace sys
*/
export const SysSetting = ObjectSchema.create({
name: 'sys_setting',
label: 'Setting',
pluralLabel: 'Settings',
icon: 'sliders',
isSystem: true,
managedBy: 'engine-owned',
description: 'Generic K/V store backing the SettingsManifest contract.',
displayNameField: 'key',
nameField: 'key', // [ADR-0079] canonical primary-title pointer (mirrors deprecated displayNameField)
titleFormat: '{namespace}.{key}',
highlightFields: ['namespace', 'key', 'scope', 'updated_at'],
listViews: {
by_namespace: {
type: 'grid',
name: 'by_namespace',
label: 'By Namespace',
data: { provider: 'object', object: 'sys_setting' },
columns: ['namespace', 'key', 'scope', 'encrypted', 'updated_by', 'updated_at'],
sort: [{ field: 'namespace', order: 'asc' }, { field: 'key', order: 'asc' }],
grouping: { fields: [{ field: 'namespace', order: 'asc', collapsed: false }] },
pagination: { pageSize: 200 },
},
tenant_only: {
type: 'grid',
name: 'tenant_only',
label: 'Tenant',
data: { provider: 'object', object: 'sys_setting' },
columns: ['namespace', 'key', 'encrypted', 'updated_by', 'updated_at'],
filter: [{ field: 'scope', operator: 'equals', value: 'tenant' }],
sort: [{ field: 'namespace', order: 'asc' }, { field: 'key', order: 'asc' }],
pagination: { pageSize: 200 },
},
user_only: {
type: 'grid',
name: 'user_only',
label: 'User',
data: { provider: 'object', object: 'sys_setting' },
columns: ['user_id', 'namespace', 'key', 'updated_at'],
filter: [{ field: 'scope', operator: 'equals', value: 'user' }],
sort: [{ field: 'user_id', order: 'asc' }, { field: 'namespace', order: 'asc' }],
pagination: { pageSize: 200 },
},
all_settings: {
type: 'grid',
name: 'all_settings',
label: 'All',
data: { provider: 'object', object: 'sys_setting' },
columns: ['namespace', 'key', 'scope', 'user_id', 'encrypted', 'updated_at'],
sort: [{ field: 'updated_at', order: 'desc' }],
pagination: { pageSize: 100 },
},
},
fields: {
id: Field.text({
label: 'Setting ID',
required: true,
readonly: true,
}),
created_at: Field.datetime({
label: 'Created At',
defaultValue: 'NOW()',
readonly: true,
}),
updated_at: Field.datetime({
label: 'Updated At',
defaultValue: 'NOW()',
readonly: true,
}),
namespace: Field.text({
label: 'Namespace',
required: true,
maxLength: 64,
description: 'Manifest namespace (e.g. mail, branding, feature_flags).',
}),
key: Field.text({
label: 'Key',
required: true,
maxLength: 128,
description: 'Specifier key inside the namespace (snake_case).',
}),
scope: Field.select(
[
{ label: 'Global', value: 'global' },
{ label: 'Tenant', value: 'tenant' },
{ label: 'User', value: 'user' },
{ label: 'Runtime',value: 'runtime' },
],
{
label: 'Scope',
required: true,
defaultValue: 'tenant',
description: 'Which layer of the config-resolution hierarchy this row belongs to.',
},
),
user_id: Field.lookup('sys_user', {
label: 'User',
description: 'Owning user when scope=user; null otherwise.',
}),
value: Field.json({
label: 'Value',
description: 'JSON-encoded value. Null when encrypted=true (see value_enc).',
}),
encrypted: Field.boolean({
label: 'Encrypted',
defaultValue: false,
description: 'When true, the value is stored encrypted-at-rest in value_enc; value column is null.',
}),
locked: Field.boolean({
label: 'Locked',
defaultValue: false,
description:
'When true, lower-scope rows cannot override this value; writes against lower scopes return 409. ' +
'Used by platform administrators to pin a global value for all tenants (Phase 2 cascade).',
}),
locked_reason: Field.text({
label: 'Lock Reason',
description: 'Human-readable explanation surfaced in the UI tooltip when locked=true.',
}),
value_enc: Field.text({
label: 'Encrypted Value',
readonly: true,
description: 'Ciphertext payload (KMS-wrapped). Set only when encrypted=true.',
}),
updated_by: Field.lookup('sys_user', {
label: 'Updated By',
readonly: true,
description: 'Last actor who wrote this row via SettingsService.set().',
}),
},
indexes: [
// Primary lookup path: (namespace, key, scope, user_id?) is what
// SettingsService.get hits on every resolve. The composite UNIQUE
// covers both the row-identity constraint and the read path.
{ fields: ['namespace', 'key', 'scope', 'user_id'], unique: true },
// Common range read: full namespace dump for SettingsService.getNamespace.
{ fields: ['namespace', 'scope'], unique: false },
// Per-user listing on the user-prefs scope.
{ fields: ['user_id', 'namespace'], unique: false },
],
enable: {
// History on settings is opt-in per namespace (handled at service
// layer when needed) to avoid bloating sys_history with churn from
// feature flags and similar high-frequency toggles.
trackHistory: false,
searchable: false,
apiEnabled: true,
// Direct data API exposed for the admin grid view, but writes from
// the UI MUST go through /api/settings/:namespace so the resolver
// and audit hooks fire. The grid is diagnostic-only.
apiMethods: ['get', 'list'],
trash: false,
mru: false,
},
});