Skip to content

motherduckdb/agent-skills / plugins/motherduck-skills-claude/skills/motherduck-build-cfa-app

motherduck-build-cfa-app

Verified

Build MotherDuck analytics into customer-facing applications with tenant isolation, backend routing, and serving APIs.

artifacts/customer_routing_example.ts

View on GitHub
export {};
declare const process: { env: Record<string, string | undefined> };

type EventRow = { event_id: number; event_type: string; revenue: number };

function normalizeMetadataValue(value: string | undefined, fallback: string): string {
  const raw = (value ?? "").trim();
  if (!raw) return fallback;
  const normalized = raw.replace(/[^A-Za-z0-9._-]+/g, "-").replace(/^[-._]+|[-._]+$/g, "");
  return normalized || fallback;
}

function buildUseCaseUserAgent(): string {
  const harness = normalizeMetadataValue(process.env.MOTHERDUCK_AGENT_HARNESS, "unknown");
  const llm = normalizeMetadataValue(process.env.MOTHERDUCK_AGENT_LLM, "unknown");
  return `agent-skills/3.2.0(harness-${harness};llm-${llm})`;
}

function summarizeCustomer(rows: EventRow[]): Array<{ event_type: string; total_revenue: number }> {
  const totals = new Map<string, number>();
  for (const row of rows) {
    totals.set(row.event_type, (totals.get(row.event_type) ?? 0) + row.revenue);
  }
  return Array.from(totals.entries())
    .map(([event_type, total_revenue]) => ({ event_type, total_revenue }))
    .sort((a, b) => b.total_revenue - a.total_revenue);
}

const customerData: Record<string, EventRow[]> = {
  acme: [
    { event_id: 1, event_type: "search", revenue: 12.5 },
    { event_id: 2, event_type: "checkout", revenue: 18.0 },
  ],
  globex: [
    { event_id: 1, event_type: "signup", revenue: 4.0 },
    { event_id: 2, event_type: "invoice_paid", revenue: 9.5 },
  ],
};

const result = {
  backend: {
    mode: "typescript-companion",
    databases: {
      customer_acme: "customer_acme",
      customer_globex: "customer_globex",
    },
    user_agent: buildUseCaseUserAgent(),
  },
  pattern: "3-tier customer-facing analytics",
  routing_mode: "per-customer database namespace",
  customers: {
    acme: summarizeCustomer(customerData.acme),
    globex: summarizeCustomer(customerData.globex),
  },
};

console.log(JSON.stringify(result, null, 2));
deprecation-and-migrationaddyosmani/agent-skillsManages deprecation and migration. Use when removing old systems, APIs, or features. Use when migrating users from one implementation to another. Use when migrating a database schema in production, such as renaming or dropping a column without downtime (expand/contract). Use when deciding whether to maintain or sunset existing code.Stars: 119kUpdated Claude CodeCodexVerifiedInstall
performance-optimizationaddyosmani/agent-skillsOptimizes application performance across frontend, backend, queries, and databases. Use when performance requirements exist, when you suspect performance regressions, when Core Web Vitals or load times need improvement, when N+1 query patterns need fixing, or when profiling reveals bottlenecks.Stars: 119kUpdated Claude CodeCodexVerifiedInstall
agent-cost-reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json.Stars: 118kUpdated Claude CodeCodexVerifiedInstall
agent-cost-reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json.Stars: 118kUpdated Claude CodeCodexVerifiedInstall