Skip to content

在 plugin-auth 中添加 SysUserPreference 系统对象,支持用户偏好存储 #1103

Closed

Description

@xuyushun441-sys

背景

框架当前缺少用户偏好(theme、locale、timezone、sidebar 状态等)的持久化能力。Config Resolution 文档(content/docs/protocol/objectos/config-resolution.mdx)已定义了 User Preferences 作为配置分层体系的第 2 层(Runtime > User Preferences > Tenant > Env),但代码层面尚未落地对应的 object schema。

方案

直接将 user_preference 作为系统对象放入 plugin-auth——与 SysUser、SysSession 等同属 identity 领域,遵循已有的 sys namespace 规范。
export const SysUserPreference = ObjectSchema.create({
namespace: 'sys',
name: 'user_preference',
label: 'User Preference',
pluralLabel: 'User Preferences',
icon: 'settings',
isSystem: true,
description: 'Per-user key-value preferences (theme, locale, etc.)',
titleFormat: '{key}',
compactLayout: ['user_id', 'key'],

fields: {
id: Field.text({
label: 'Preference 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,
}),

user_id: Field.text({
  label: 'User ID',
  required: true,
  maxLength: 255,
  description: 'Owner user of this preference',
}),

key: Field.text({
  label: 'Key',
  required: true,
  maxLength: 255,
  description: 'Preference key (e.g., theme, locale, plugin.ai.auto_save)',
}),

value: Field.json({
  label: 'Value',
  description: 'Preference value (any JSON-serializable type)',
}),

},

indexes: [
{ fields: ['user_id', 'key'], unique: true },
{ fields: ['user_id'] },
],

enable: {
trackHistory: false,
searchable: false,
apiEnabled: true,
apiMethods: ['get', 'list', 'create', 'update', 'delete'],
trash: false,
mru: false,
},
});

客户端使用方式

import { SystemObjectName } from '@objectstack/spec/system';

// 读取偏好
const theme = await engine.findOne(SystemObjectName.USER_PREFERENCE, {
  where: { user_id: currentUserId, key: 'theme' },
});

// 写入偏好
await engine.upsert(SystemObjectName.USER_PREFERENCE, {
  user_id: currentUserId,
  key: 'theme',
  value: JSON.stringify('dark'),
});

// 批量读取某用户所有偏好
const allPrefs = await engine.find(SystemObjectName.USER_PREFERENCE, {
  where: { user_id: currentUserId },
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions