Los plugins pueden guardar sus propios datos en colecciones de documentos sin escribir migraciones de base de datos. Declara colecciones e índices en la definición del plugin y EmDash gestiona el esquema automáticamente.
Declarar almacenamiento
Define colecciones de storage en 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"],
},
},
// ...
});
Cada clave en storage es un nombre de colección. El array indexes enumera los campos que se pueden consultar de forma eficiente.
API de colección de storage
Accede a las colecciones mediante ctx.storage en hooks y rutas:
"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");
}
Referencia completa de la 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>;
}
Consultar datos
Usa query() para obtener documentos que cumplan criterios. Las consultas devuelven resultados paginados.
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
Opciones de consulta
interface QueryOptions {
where?: WhereClause;
orderBy?: Record<string, "asc" | "desc">;
limit?: number; // Default 50, max 1000
cursor?: string; // For pagination
}
Operadores de la cláusula where
Filtra por campos indexados con estos operadores:
Coincidencia exacta
where: {
status: "pending", // Exact string match
count: 5, // Exact number match
archived: false // Exact boolean match
} Rango
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"] }
} Empieza por
where: {
slug: { startsWith: "blog-" }
} Ordenación
Ordena por campos indexados:
orderBy: {
createdAt: "desc";
} // Newest first
orderBy: {
score: "asc";
} // Lowest first
Paginación
Los resultados van paginados. Usa cursor para páginas adicionales:
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
}
Contar documentos
Cuenta documentos que cumplan los criterios:
// Count all
const total = await ctx.storage.submissions!.count();
// Count with filter
const pending = await ctx.storage.submissions!.count({
status: "pending",
});
Operaciones por lotes
Para operaciones masivas, usa métodos por lotes:
// 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"]);
Diseño de índices
Elige índices según tus patrones de consulta:
| Patrón de consulta | Índice necesario |
|---|---|
Filtrar por formId | "formId" |
Filtrar por formId, ordenar por createdAt | ["formId", "createdAt"] |
Ordenar solo por createdAt | "createdAt" |
Filtrar por status y formId | "status" y "formId" (por separado) |
Los índices compuestos admiten consultas que filtran por el primer campo y opcionalmente ordenan por el segundo:
// 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" } } });
Seguridad de tipos
Tipa tus colecciones de storage para mejor 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 frente a contenido frente a KV
Usa el mecanismo adecuado para cada caso:
| Caso de uso | Almacenamiento |
|---|---|
| Datos operativos del plugin (logs, envíos, caché) | ctx.storage |
| Ajustes configurables por el usuario | ctx.kv con prefijo settings: |
| Estado interno del plugin | ctx.kv con prefijo state: |
| Contenido editable en el admin | Colecciones del sitio (no storage del plugin) |
Detalles de implementación
Internamente, el storage de plugins usa una sola tabla:
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 crea índices de expresión para los campos declarados:
CREATE INDEX idx_forms_submissions_formId
ON _plugin_storage(json_extract(data, '$.formId'))
WHERE plugin_id = 'forms' AND collection = 'submissions';
Este diseño ofrece:
- Sin migraciones — El esquema vive en el código del plugin
- Portabilidad — Funciona en D1, libSQL, SQLite
- Aislamiento — Los plugins solo acceden a sus propios datos
- Seguridad — Sin inyección SQL, consultas validadas
Añadir índices
Cuando añades índices en una actualización del plugin, EmDash los crea automáticamente en el siguiente arranque. Es seguro: se pueden añadir índices sin migración de datos.
Cuando quitas índices, EmDash los elimina. Las consultas sobre campos ya no indexados fallarán con un error de validación.