Skip to main content

@happyvertical/smrt-products

Product catalog reference template demonstrating triple-consumption: npm package library, module federation, and standalone REST API server.

Installation

pnpm add @happyvertical/smrt-products

Usage

Import as npm library

import { Product, ProductCollection, Category } from '@happyvertical/smrt-products';
import { startServer } from '@happyvertical/smrt-products';
import { generateMCPServer } from '@happyvertical/smrt-products';
import { AssetCollection } from '@happyvertical/smrt-assets';

// Start standalone REST API server
const { shutdown } = await startServer();

const products = await ProductCollection.create();
const assets = await AssetCollection.create();
const product = await products.create({
name: 'Demo Product',
price: 29.99,
});
const hero = await assets.create({
name: 'demo-product-hero.jpg',
sourceUri: 'file:///tmp/demo-product-hero.jpg',
mimeType: 'image/jpeg',
});

await product.addAsset(hero, 'hero');
await products.addAsset(product.id!, hero, 'gallery', 1);

Three consumption modes

  1. NPM library -- import classes, components, and stores directly
  2. Module federation -- runtime component sharing (experimental)
  3. Standalone API -- startServer() launches Express with auto-generated routes

API

Top-Level Exports

ExportDescription
startServerLaunch standalone REST API server
generateMCPServerGenerate MCP server for AI tool integration
demonstrateClientDemo of auto-generated TypeScript client
startAllStart all services (REST + MCP)

Models (from lib/models)

ExportDescription
ProductSTI-enabled product with specs and tags
CategoryHierarchical category (parentId, level, productCount), STI enabled
ProductAssetDedicated owned-asset join stored in product_assets with relationship and sortOrder; intentionally not tenant-scoped because Product is not tenant-scoped

Collections (from lib/collections)

ExportDescription
ProductCollectionCRUD plus findByManufacturer(), findInStock(), and owned asset wrappers
ProductAssetCollectionDirect access to product_assets rows plus asset helper wrappers

Components (from lib/components)

ExportDescription
ProductCardSvelte 5 product display component
ProductFormSvelte 5 product edit form

Stores (from lib/stores)

ExportDescription
ProductStoreClassSvelte 5 rune-based state management class
productStoreSingleton store instance

Utilities (from lib/utils)

ExportDescription
formatPriceFormat number as USD currency string
formatDateFormat date as human-readable string
slugifyConvert text to URL-friendly slug
generateIdGenerate random ID string

Virtual Modules (Vite plugin)

ExportDescription
createClientAuto-generated TypeScript API client
setupRoutesAuto-generated Express routes
createMCPServerAuto-generated MCP server
manifests-m-r-t object metadata

Owned asset helpers are available on both Product and ProductCollection via getAssets(), addAsset(), and removeAsset(). Common relationships include hero, gallery, attachment, and thumbnail.

Dependencies

PackagePurpose
@happyvertical/smrt-coreSmrtObject/SmrtCollection base classes, REST server, MCP generator
@happyvertical/smrt-assetsShared Asset / AssetCollection types used by product-owned asset helpers
@happyvertical/sqlDatabase operations
@happyvertical/aiAI integration