AGENT SKILL

sqlite

The App's own SQLite database via Node 24's built-in node:sqlite — migrations, the DatabaseService API, transactions, upserts, full-text search (FTS5), JSON and window functions. Use for any data model change, schema work, or SQL query against the App's database.

License 许可
AGPL-3.0
最近更新
2026-11-05
Stars
★ 43
Forks
9

01 前言 Front Matter

name
sqlite
description
The App's own SQLite database via Node 24's built-in node:sqlite — migrations, the DatabaseService API, transactions, upserts, full-text search (FTS5), JSON and window functions. Use for any data model change, schema work, or SQL query against the App's database.

03 SKILL.md 正文

SQLite

The app uses Node 24's built-in node:sqlite (DatabaseSync). Synchronous API, zero dependencies, no native compile step. Imported as import { DatabaseSync } from "node:sqlite". Database file is at data/app.db.

The platform's two read-only databases — the request log (/workspace/data/database.db) and inbound Events (/workspace/data/external-services.db) — are different files with a different owner: the platform skill has their rules, and each External Service's service-<slug> skill has the data of its Events and its queries.

DatabaseService API

Inject in any NestJS service:

import { DatabaseService } from "../database/database.service"

constructor(private readonly db: DatabaseService) {}

Methods — complete reference (do not invent methods not listed here)

Method Returns Use for
queryAll<T>(sql, params?) T[] SELECT multiple rows
queryOne<T>(sql, params?) T | undefined SELECT single row
run(sql, params?) { changes, lastInsertRowid } INSERT, UPDATE, DELETE
exec(sql) void Raw SQL (DDL, multiple statements)

Examples

// SELECT all
const items = this.db.queryAll<Item>("SELECT * FROM items WHERE status = ?", ["active"])

// SELECT one
const item = this.db.queryOne<Item>("SELECT * FROM items WHERE id = ?", [id])

// INSERT
const result = this.db.run("INSERT INTO items (title) VALUES (?)", ["New item"])
const newId = result.lastInsertRowid

// UPDATE
this.db.run("UPDATE items SET title = ?, updated_at = datetime('now') WHERE id = ?", ["Updated", id])

// DELETE
this.db.run("DELETE FROM items WHERE id = ?", [id])

// COUNT
const { count } = this.db.queryOne<{ count: number }>("SELECT COUNT(*) as count FROM items")!

Creating tables — migrations

Create .sql files in backend/src/migrations/:

backend/src/migrations/
  001_create_items.sql     ← existing (do not modify)
  002_create_tasks.sql     ← your new migration
  003_add_categories.sql   ← another migration

Rules

  • Files run in alphabetical order, each only once
  • Migrations run automatically on every boot (dev and production), each in its own transaction (the whole file commits, or rolls back on error). Once a migration has run it is recorded and skipped forever.
  • Migrations are forward-only — the platform skill (§What Publish carries).
  • Never modify the existing items table — create your own tables
  • Naming: NNN_description.sql

Migration template

CREATE TABLE tasks (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  title TEXT NOT NULL,
  description TEXT,
  completed INTEGER NOT NULL DEFAULT 0,
  priority TEXT NOT NULL DEFAULT 'medium',
  due_date TEXT,
  created_at TEXT NOT NULL DEFAULT (datetime('now')),
  updated_at TEXT NOT NULL DEFAULT (datetime('now'))
);

Common patterns

-- Foreign key
CREATE TABLE comments (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  task_id INTEGER NOT NULL REFERENCES tasks(id) ON DELETE CASCADE,
  body TEXT NOT NULL,
  created_at TEXT NOT NULL DEFAULT (datetime('now'))
);

-- Index for performance
CREATE INDEX idx_comments_task_id ON comments(task_id);

-- Unique constraint
CREATE TABLE users (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  email TEXT NOT NULL UNIQUE,
  name TEXT NOT NULL
);

-- Alter table (add column)
ALTER TABLE tasks ADD COLUMN assigned_to TEXT;

Data types

  • TEXT — strings, dates (ISO format via datetime('now'))
  • INTEGER — numbers, booleans (0/1)
  • REAL — floating point
  • BLOB — binary data

Transactions (batch operations — 10-100x faster)

this.db.exec("BEGIN")
try {
  for (const item of items) {
    this.db.run("INSERT INTO items (title) VALUES (?)", [item.title])
  }
  this.db.exec("COMMIT")
} catch (e) {
  this.db.exec("ROLLBACK")
  throw e
}

Always wrap in try/catch with ROLLBACK. Without it, a failed insert leaves the transaction open and blocks writes.

UPSERT (insert or update)

INSERT INTO settings (key, value) VALUES (?, ?)
  ON CONFLICT(key) DO UPDATE SET value = excluded.value, updated_at = datetime('now');

Requires a UNIQUE constraint on the conflict column.

Full-text search (FTS5)

-- Migration: create FTS table
CREATE VIRTUAL TABLE items_fts USING fts5(title, description);

-- Trigger to keep in sync
CREATE TRIGGER items_ai AFTER INSERT ON items BEGIN
  INSERT INTO items_fts(rowid, title, description) VALUES (new.id, new.title, new.description);
END;
CREATE TRIGGER items_ad AFTER DELETE ON items BEGIN
  DELETE FROM items_fts WHERE rowid = old.id;
END;
CREATE TRIGGER items_au AFTER UPDATE ON items BEGIN
  DELETE FROM items_fts WHERE rowid = old.id;
  INSERT INTO items_fts(rowid, title, description) VALUES (new.id, new.title, new.description);
END;
// Search
this.db.queryAll(`
  SELECT i.* FROM items i
  JOIN items_fts fts ON i.id = fts.rowid
  WHERE items_fts MATCH ?
  ORDER BY rank
`, [searchQuery])

JSON support

-- Store JSON in a TEXT column
INSERT INTO items (metadata) VALUES ('{"color":"blue","tags":["work","urgent"]}');

-- Query JSON fields
SELECT *, json_extract(metadata, '$.color') as color FROM items
WHERE json_extract(metadata, '$.active') = 1;

-- JSON array operations
SELECT * FROM items WHERE json_array_length(json_extract(metadata, '$.tags')) > 0;

Window functions

-- Row numbering
SELECT *, ROW_NUMBER() OVER (ORDER BY created_at DESC) as row_num FROM items;

-- Running total
SELECT *, SUM(amount) OVER (ORDER BY created_at) as running_total FROM transactions;

-- Rank within groups
SELECT *, RANK() OVER (PARTITION BY category ORDER BY score DESC) as category_rank FROM items;

Common aggregate patterns

-- Group by with counts
SELECT status, COUNT(*) as count FROM tasks GROUP BY status;

-- Group by date (day)
SELECT date(created_at) as day, COUNT(*) as count FROM items GROUP BY date(created_at) ORDER BY day DESC;

-- Group by month
SELECT strftime('%Y-%m', created_at) as month, COUNT(*) as count FROM items GROUP BY month ORDER BY month DESC;

Performance

  • PRAGMA journal_mode = TRUNCATE — already set for CephFS compatibility
  • PRAGMA synchronous = FULL — already set for durability; avoid long write transactions
  • CREATE INDEX idx_items_status ON items(status) — always index columns used in WHERE/ORDER BY
  • EXPLAIN QUERY PLAN SELECT ... — run to debug slow queries
  • Wrap batch inserts in BEGIN/COMMIT (10-100x faster than individual inserts)
  • Use LIMIT and OFFSET for pagination: SELECT * FROM items ORDER BY id DESC LIMIT 20 OFFSET 40

Common mistakes

  1. Not indexing foreign keys — SQLite doesn't auto-index FK columns. Always CREATE INDEX idx_comments_task_id ON comments(task_id).
  2. Forgetting ROLLBACK — if an error happens mid-transaction without ROLLBACK, the transaction stays open and blocks writes.
  3. Using LIKE '%query%' for search — full table scan. Use FTS5 for text search.

04 同仓库更多 Skills

SimaDevelopment
SimaDevelopment/opsiforce

figma-use

**MANDATORY prerequisite** — you MUST invoke this skill BEFORE every `use_figma` tool call. NEVER call `use_figma` directly without loading this skill first. Skipping it causes common, hard-to-debug failures. Trigger whenever the user wants to perform a write action or a unique read action that requires JavaScript execution in the Figma file context — e.g. create/edit/delete nodes, set up variables or tokens, build components and variants, modify auto-layout or fills, bind variables to properties, or inspect file structure programmatically.

★ 43
SimaDevelopment
SimaDevelopment/opsiforce

figma-use-slides

This skill helps agents use Figma's use_figma MCP tool in the Slides context. Can be used alongside figma-use which has foundational context for using the use_figma tool.

★ 43
SimaDevelopment
SimaDevelopment/opsiforce

figma-use-motion

Motion / animation context for the `use_figma` MCP tool — animating Figma nodes via manual keyframes, animation styles, easing, and timeline duration. Load alongside figma-use whenever a task involves adding, editing, or inspecting animation on a node.

★ 43
SimaDevelopment
SimaDevelopment/opsiforce

figma-use-figjam

This skill helps agents use Figma's use_figma MCP tool in the FigJam context. Can be used alongside figma-use which has foundational context for using the use_figma tool.

★ 43
SimaDevelopment
SimaDevelopment/opsiforce

figma-swiftui

SwiftUI ↔ Figma translation. Use whenever the user mentions Swift, SwiftUI, iOS, iPhone, or iPad — in EITHER direction — translating a Figma design into SwiftUI (design → code), or pushing SwiftUI views / screens / tokens back into a Figma file (code → design). Triggers on phrases like 'implement this Figma design in SwiftUI', 'build this screen in Swift', 'push this SwiftUI view to Figma', 'mirror my Swift code in a Figma file', or whenever a Figma URL appears alongside `.swift` files / an `.xcodeproj`. Routes to a direction-specific reference doc; loads alongside `figma-use` for the code → design path.

★ 43
SimaDevelopment
SimaDevelopment/opsiforce

figma-shaders

**MANDATORY prerequisite** — load this skill before calling `create_shader` or `update_shader`. Use when the user asks to create, author, change, fix, or iterate on a shader effect, shader fill, custom effect, custom fill, or procedural shader in Figma.

★ 43