@happyvertical/smrt-tags

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

Reusable tagging system with hierarchies, contexts, and multi-language support for SMRT framework

原始使用文档
文档目录8 节
  1. Installation
  2. Usage
  3. API
  4. Models (SmrtObject)
  5. Collections (SmrtCollection)
  6. Utilities
  7. Types
  8. Dependencies

@happyvertical/smrt-tags

Hierarchical tagging with context-scoped slugs, multi-language aliases, and slug utilities.

Installation

pnpm add @happyvertical/smrt-tags

Usage

import { Tag, TagCollection, TagAlias, TagAliasCollection } from '@happyvertical/smrt-tags';
import { sanitizeSlug, validateSlug, generateUniqueSlug } from '@happyvertical/smrt-tags';

// Create a tag hierarchy
const collection = await TagCollection.create();

// `collection.create(...)` returns the saved row, so use it directly
// instead of `new Tag()` + a separate `.create(...)` step. The returned
// instance has its persisted `id` set for use as a parent reference.
const tech = await collection.create({
  slug: 'technology',
  name: 'Technology',
  context: 'blog', // defaults to 'global' if omitted
});

// Children reference their parent by UUID (`parentId`). With `tech`
// already saved, pass `tech.id` directly. `level` is recalculated by
// `TagCollection.moveTag` / `mergeTag` for later moves; you can pass it
// explicitly on create or let it stay at 0 and call moveTag later.
const ai = await collection.create({
  slug: 'artificial-intelligence',
  name: 'Artificial Intelligence',
  context: 'blog',
  parentId: tech.id,
  level: 1,
});

// Traverse hierarchy
const children = await tech.getChildren();
const ancestors = await ai.getAncestors();

// Context scoping -- same slug, different contexts
const blogNews = new Tag({ slug: 'news', name: 'News', context: 'blog' });
const forumNews = new Tag({ slug: 'news', name: 'Forum News', context: 'forum' });

// Multi-language aliases
const aliasCollection = await TagAliasCollection.create();
const alias = new TagAlias({
  tagSlug: 'technology',
  alias: 'Tecnologia',
  language: 'es',
  context: 'blog',
});
await aliasCollection.create(alias);

// Collection operations. Slug args resolve through (slug, context) —
// pass an explicit context when the same slug exists in multiple
// contexts, otherwise the resolver throws on ambiguity.
await collection.moveTag('artificial-intelligence', 'technology', 'blog'); // cycle-checked
await collection.mergeTag('source', 'target', 'blog');                     // moves children + aliases, deletes source
await collection.cleanupUnused();                                          // deletes tags with no children and no aliases

// Slug utilities
sanitizeSlug('My Cool Tag!');   // 'my-cool-tag'
validateSlug('my-tag-123');     // true

API

Models (SmrtObject)

ExportDescription
TagIdentified by slug + context. Hierarchical via parentId (UUID, inherited from SmrtHierarchical). Denormalised level recalculated by TagCollection.moveTag / mergeTag. JSON metadata. Hierarchy methods inherited: getParent(), getChildren(), getAncestors(), getDescendants(), getHierarchy(), moveTo(). Own helpers: getMetadata(), setMetadata(), updateMetadata()
TagAliasLanguage-specific translations: tagSlug, alias, language (ISO 639-1), optional context

Collections (SmrtCollection)

ExportDescription
TagCollectionCRUD + getOrCreate(), moveTag(), mergeTag(), cleanupUnused(), findWithGlobals()
TagAliasCollectionCRUD for tag aliases

Utilities

ExportDescription
sanitizeSlug(input)Clean and format slug strings
validateSlug(slug)Validate slug format (lowercase, alphanumeric + hyphens)
generateUniqueSlug(name, context, collection)Auto-numbered unique slug generation
calculateLevel(parentSlug, collection)Determine hierarchy depth from parent
hasCircularReference(slug, parentSlug, collection)Detect circular parent references

Types

ExportDescription
TagOptionsOptions for Tag constructor
TagAliasOptionsOptions for TagAlias constructor
TagMetadataFlexible metadata structure (colors, icons, statistics)
TagHierarchyComplete hierarchy result (ancestors, current, descendants)

Dependencies

PackagePurpose
@happyvertical/smrt-coreORM base (SmrtObject, SmrtCollection)
@happyvertical/smrt-tenancyOptional tenant scoping

版本与发布记录

@happyvertical/smrt-tags 共发布 365 个版本,首次发布于 2026/06/23;过去 12 个月发布了 365 个版本。

版本发布日期距今
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-tags 的常见问题

@happyvertical/smrt-tags 是什么?

Reusable tagging system with hierarchies, contexts, and multi-language support for SMRT framework

如何安装 @happyvertical/smrt-tags?

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

@happyvertical/smrt-tags 支持 TypeScript 吗?

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

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

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

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

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

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

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

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

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