プラグインはデータベースのマイグレーションを書かずに、独自のデータをドキュメントコレクションに保存できます。プラグイン定義でコレクションとインデックスを宣言すると、EmDash がスキーマを自動的に扱います。
ストレージの宣言
definePlugin() でストレージコレクションを定義します。
import { definePlugin } from "emdash";
export default definePlugin({
id: "forms",
version: "1.0.0",
storage: {
submissions: {
indexes: [
"formId", // Single-field index
"status",
"createdAt",
["formId", "createdAt"], // Composite index
["status", "createdAt"],
],
},
forms: {
indexes: ["slug"],
},
},
// ...
});
storage の各キーはコレクション名です。indexes 配列は効率的にクエリできるフィールドを列挙します。
ストレージコレクション API
フックやルートから ctx.storage 経由でコレクションにアクセスします。
"content:afterSave": async (event, ctx) => {
const { submissions } = ctx.storage;
// CRUD operations
await submissions.put("sub_123", { formId: "contact", email: "[email protected]" });
const item = await submissions.get("sub_123");
const exists = await submissions.exists("sub_123");
await submissions.delete("sub_123");
}
完全な API リファレンス
interface StorageCollection<T = unknown> {
// Basic CRUD
get(id: string): Promise<T | null>;
put(id: string, data: T): Promise<void>;
delete(id: string): Promise<boolean>;
exists(id: string): Promise<boolean>;
// Batch operations
getMany(ids: string[]): Promise<Map<string, T>>;
putMany(items: Array<{ id: string; data: T }>): Promise<void>;
deleteMany(ids: string[]): Promise<number>;
// Query (indexed fields only)
query(options?: QueryOptions): Promise<PaginatedResult<{ id: string; data: T }>>;
count(where?: WhereClause): Promise<number>;
}
データのクエリ
条件に一致するドキュメントを取得するには query() を使います。クエリはページ分割された結果を返します。
const result = await ctx.storage.submissions.query({
where: {
formId: "contact",
status: "pending",
},
orderBy: { createdAt: "desc" },
limit: 20,
});
// result.items - Array of { id, data }
// result.cursor - Pagination cursor (if more results)
// result.hasMore - Boolean indicating more pages
クエリオプション
interface QueryOptions {
where?: WhereClause;
orderBy?: Record<string, "asc" | "desc">;
limit?: number; // Default 50, max 1000
cursor?: string; // For pagination
}
where 句の演算子
インデックス付きフィールドで次の演算子によりフィルタします。
完全一致
where: {
status: "pending", // Exact string match
count: 5, // Exact number match
archived: false // Exact boolean match
} 範囲
where: {
createdAt: { gte: "2024-01-01" }, // Greater than or equal
score: { gt: 50, lte: 100 } // Between (exclusive/inclusive)
}
// Available: gt, gte, lt, lte
In
where: {
status: { in: ["pending", "approved"] }
} 先頭一致
where: {
slug: { startsWith: "blog-" }
} 並び順
インデックス付きフィールドで結果を並べ替えます。
orderBy: {
createdAt: "desc";
} // Newest first
orderBy: {
score: "asc";
} // Lowest first
ページネーション
結果はページ分割されます。追加のページを取得するには cursor を使います。
async function getAllSubmissions(ctx: PluginContext) {
const allItems = [];
let cursor: string | undefined;
do {
const result = await ctx.storage.submissions!.query({
orderBy: { createdAt: "desc" },
limit: 100,
cursor,
});
allItems.push(...result.items);
cursor = result.cursor;
} while (cursor);
return allItems;
}
PaginatedResult
interface PaginatedResult<T> {
items: T[];
cursor?: string; // Pass to next query for more results
hasMore: boolean; // True if more pages exist
}
ドキュメントの件数
条件に一致するドキュメント数を数えます。
// Count all
const total = await ctx.storage.submissions!.count();
// Count with filter
const pending = await ctx.storage.submissions!.count({
status: "pending",
});
バッチ操作
一括操作にはバッチメソッドを使います。
// Get multiple by ID
const items = await ctx.storage.submissions!.getMany(["sub_1", "sub_2", "sub_3"]);
// Returns Map<string, T>
// Put multiple
await ctx.storage.submissions!.putMany([
{ id: "sub_1", data: { formId: "contact", status: "new" } },
{ id: "sub_2", data: { formId: "contact", status: "new" } },
]);
// Delete multiple
const deletedCount = await ctx.storage.submissions!.deleteMany(["sub_1", "sub_2"]);
インデックス設計
クエリパターンに基づいてインデックスを選びます。
| クエリパターン | 必要なインデックス |
|---|---|
formId でフィルタ | "formId" |
formId でフィルタし createdAt で並べ替え | ["formId", "createdAt"] |
createdAt のみで並べ替え | "createdAt" |
status と formId でフィルタ | "status" と "formId"(別々) |
複合インデックスは、先頭フィールドでフィルタし、任意で第2フィールドで並べ替えるクエリをサポートします。
// With index ["formId", "createdAt"]:
// This works:
query({ where: { formId: "contact" }, orderBy: { createdAt: "desc" } });
// This also works (filter only):
query({ where: { formId: "contact" } });
// This does NOT use the composite index (wrong field order):
query({ where: { createdAt: { gte: "2024-01-01" } } });
型安全性
IntelliSense を強化するためにストレージコレクションに型を付けます。
interface Submission {
formId: string;
email: string;
data: Record<string, unknown>;
status: "pending" | "approved" | "spam";
createdAt: string;
}
definePlugin({
id: "forms",
version: "1.0.0",
storage: {
submissions: {
indexes: ["formId", "status", "createdAt"],
},
},
hooks: {
"content:afterSave": async (event, ctx) => {
// Cast to typed collection
const submissions = ctx.storage.submissions as StorageCollection<Submission>;
const submission: Submission = {
formId: "contact",
email: "[email protected]",
data: { message: "Hello" },
status: "pending",
createdAt: new Date().toISOString(),
};
await submissions.put(`sub_${Date.now()}`, submission);
},
},
});
Storage と Content と KV
用途に合った保存方式を使います。
| 用途 | ストレージ |
|---|---|
| プラグインの運用データ(ログ、送信、キャッシュ) | ctx.storage |
| ユーザーが設定可能な設定 | プレフィックス settings: の ctx.kv |
| プラグイン内部の状態 | プレフィックス state: の ctx.kv |
| 管理 UI で編集するコンテンツ | サイトコレクション(プラグインストレージではない) |
実装の詳細
内部ではプラグインストレージは単一のデータベーステーブルを使います。
CREATE TABLE _plugin_storage (
plugin_id TEXT NOT NULL,
collection TEXT NOT NULL,
id TEXT NOT NULL,
data JSON NOT NULL,
created_at TEXT,
updated_at TEXT,
PRIMARY KEY (plugin_id, collection, id)
);
EmDash は宣言されたフィールドに対して式インデックスを作成します。
CREATE INDEX idx_forms_submissions_formId
ON _plugin_storage(json_extract(data, '$.formId'))
WHERE plugin_id = 'forms' AND collection = 'submissions';
この設計により次が得られます。
- マイグレーション不要 — スキーマはプラグインコード内にあります
- 移植性 — D1、libSQL、SQLite で動作します
- 分離 — プラグインは自分のデータにのみアクセスできます
- 安全性 — SQL インジェクションなし、検証済みクエリ
インデックスの追加
プラグインの更新でインデックスを追加すると、EmDash は次回起動時に自動で作成します。データ移行なしでインデックスを追加しても安全です。
インデックスを削除すると、EmDash はそれをドロップします。インデックスのないフィールドへのクエリは検証エラーで失敗します。