@happyvertical/smrt-preferences

中文文档
0.62.1发布于 2026/11/17MITTypeScriptES Module近 30 天下载 1.3万

Tenant defaults and per-user overrides for user-interface preferences (overview layouts, shell layout), validated per registered kind

原始使用文档
文档目录5 节
  1. Installation
  2. Permissions
  3. Overview pages
  4. App frame (shell layout)
  5. Other kinds

@happyvertical/smrt-preferences

Saved user-interface preferences for s-m-r-t apps: how a page or the app frame is arranged. An organization sets the default; each person can keep their own version on top of it. Reset returns either one to the layer below.

Two kinds of preference ship with the package:

  • overview: the layout of a customizable overview page from smrt-svelte/overview (which widgets, in what order, how wide, with which options).
  • shell-layout: the app frame's settings from the AdminShell (navigation order and visibility, panel states and widths, keyboard shortcuts).

An app can register more kinds. Every kind checks its own data when it is saved (anything invalid is refused) and again when it is loaded (anything that no longer fits is dropped and reported, the rest still applies).

Use the stores below; the table's model is not an application API (it refuses writes that do not come from the store and never returns another person's preferences).

See AGENTS.md for the storage rules and agents/preference-kinds.md for the kind contract.

Installation

pnpm add @happyvertical/smrt-preferences

Preferences live in the _smrt_ui_preferences table. Create it with your normal migration (smrt db:migrate); the package never creates tables at runtime. smrt db:status --parity reports it like any other table.

Permissions

PermissionLets a person
overviews.customizeChange or reset the organization's default layout of a page
overviews.personalizeKeep and reset their own layout of a page
shell.customizeChange or reset the organization's default app frame
shell.personalizeKeep and reset their own app frame settings

The store reads these from the signed-in person's permission set in the request context. If your app enters the tenant context through the tenancy SvelteKit or Express adapter, configure its resolvePermissions (or let createSessionHandler() from @happyvertical/smrt-users enter the context with enterTenantContext); without it the adapter supplies an empty permission set, and everyone, owners included, is refused.

Owners and admins receive all four when role permissions are seeded. Members and viewers receive the two personalize permissions; for roles created before this package was installed, run seedDefaultRolePersonalizationPermissions() from @happyvertical/smrt-users once.

Overview pages

// +page.server.ts
import { createOverviewStore } from '@happyvertical/smrt-preferences';
import { eventsHome, widgets } from '$lib/overviews';

export const load = async ({ locals }) => {
  const overviews = createOverviewStore({ db: locals.db });
  // Runs inside the request's tenant context; each widget loader receives
  // the capabilities passed here and must authorize through them.
  const overview = await overviews.loadPage(eventsHome, widgets, {
    locale: locals.locale,
    db: locals.db,
  });
  return { overview };
};

Save with the revision the page loaded; a stale revision returns conflict instead of overwriting someone else's change:

const saved = await overviews.save(eventsHome, widgets, {
  scope: 'user',
  override,
  revision,
});
// saved.ok, or saved.reason: 'invalid' (with issues), 'conflict', 'not_allowed'
await overviews.reset(eventsHome, { scope: 'user', revision });
const result = await overviews.loadWidget(eventsHome, widgets, widget, ctx);

App frame (shell layout)

import {
  createPreferenceStore,
  createShellSettingsPreferences,
} from '@happyvertical/smrt-preferences';

const shell = createShellSettingsPreferences(createPreferenceStore({ db }));

// Remote functions the browser's ShellSettingsAdapter calls.
export const readShellSettings = async () => (await shell.read('admin')).delta;
export const writeShellSettings = (delta: unknown) =>
  shell.write('admin', delta);

read merges the organization default with the person's own settings. write without a revision writes over the latest stored value, because the shell's adapter does not carry one; pass { revision } to guard it.

Other kinds

import {
  createPreferenceStore,
  registerPreferenceKind,
} from '@happyvertical/smrt-preferences';

registerPreferenceKind({
  kind: 'board-columns',
  formatVersion: 1,
  permissions: { tenant: 'boards.customize', user: 'boards.personalize' },
  validate: (payload, ctx) => checkColumns(payload, ctx),
});

const preferences = createPreferenceStore({ db });
const state = await preferences.load('board-columns', 'projects.board');
await preferences.save('board-columns', 'projects.board', {
  scope: 'user',
  payload,
  revision: state.user?.revision ?? null,
});

版本与发布记录

@happyvertical/smrt-preferences 共发布 103 个版本,首次发布于 2026/10/15;过去 12 个月发布了 103 个版本。

版本发布日期距今
0.62.1latest2026/11/17今天
0.62.02026/11/17今天
0.61.62026/11/161 天前
0.61.52026/11/161 天前
0.61.42026/11/161 天前
0.61.32026/11/152 天前

关于 @happyvertical/smrt-preferences 的常见问题

@happyvertical/smrt-preferences 是什么?

Tenant defaults and per-user overrides for user-interface preferences (overview layouts, shell layout), validated per registered kind

如何安装 @happyvertical/smrt-preferences?

在项目目录运行 npm install @happyvertical/smrt-preferences,或使用 pnpm add / yarn add / bun add。

@happyvertical/smrt-preferences 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@happyvertical/smrt-preferences 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@happyvertical/smrt-preferences 可以商用吗?

@happyvertical/smrt-preferences 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@happyvertical/smrt-preferences 的下载量有多少?

根据 npm 官方下载统计,@happyvertical/smrt-preferences 在 2026/10/17 – 2026/11/15 这 30 天内被下载 13,293 次,日均约 443 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@happyvertical/smrt-preferences 还在维护吗?

最新版本 0.62.1 发布于 2026/11/17(今天);至今共发布 103 个版本,其中过去 12 个月发布了 103 个;首次发布于 2026/10/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。