Docs/Skills and code/src/

src/pattern-store.js

Code98 lines1.9 KBsrc/

import { ensureAnalysisSchema, db } from "./analysis-schema.js";
import { normalizeText } from "./metadata-utils.js";

ensureAnalysisSchema();

export function upsertPattern({
  ecosystem,
  type,
  name,
  description = null
}) {
  const canonicalName = String(name ?? "").trim();
  const normalizedName = normalizeText(canonicalName);

  if (!canonicalName || !normalizedName || !type) return null;

  const now = new Date().toISOString();

  db.prepare(`
    INSERT INTO patterns (
      ecosystem,
      pattern_type,
      canonical_name,
      normalized_name,
      description,
      created_at,
      updated_at
    )
    VALUES (?, ?, ?, ?, ?, ?, ?)
    ON CONFLICT(ecosystem, pattern_type, normalized_name)
    DO UPDATE SET
      updated_at = excluded.updated_at,
      description = COALESCE(patterns.description, excluded.description)
  `).run(
    ecosystem,
    type,
    canonicalName,
    normalizedName,
    description,
    now,
    now
  );

  return db.prepare(`
    SELECT id, canonical_name, pattern_type
    FROM patterns
    WHERE ecosystem = ?
      AND pattern_type = ?
      AND normalized_name = ?
  `).get(ecosystem, type, normalizedName);
}

export function attachPatternToVideo({
  videoId,
  patternId,
  source,
  confidence = null
}) {
  db.prepare(`
    INSERT OR REPLACE INTO video_patterns (
      video_id,
      pattern_id,
      source,
      confidence,
      created_at
    ) VALUES (?, ?, ?, ?, ?)
  `).run(
    videoId,
    patternId,
    source,
    confidence,
    new Date().toISOString()
  );
}

export function attachPatternToChannel({
  channelUrl,
  patternId,
  source,
  confidence = null
}) {
  db.prepare(`
    INSERT OR REPLACE INTO channel_patterns (
      channel_url,
      pattern_id,
      source,
      confidence,
      created_at
  ) VALUES (?, ?, ?, ?, ?)
  `).run(
    channelUrl,
    patternId,
    source,
    confidence,
    new Date().toISOString()
  );
}

Shown as it is in the repository at build time (2026-11-03); the home directory appears as <home>. If this page and the file differ, the file is right. Back to the catalogue.