await storage.run({ action: "createCollection", collection: "invoices" });
// { ok: true, created: "invoices" }storage.run(action="createCollection", collection="invoices")
# {"ok": True, "created": "invoices"}Cuando realmente lo necesitas
Un insertar En un nombre que no lleva ninguna colección, todavía lo crea. Así que esto es para los casos en los que la estructura debe existir antes de que se escriba algo: un paso de configuración, una migración, una interfaz que lista colecciones antes de que haya datos.
Se rechaza un nombre existente, con un mensaje legible
En lugar del código de la base de datos, que nombra una condición sin decir qué hacer. Si crear dos veces es inofensivo para usted, trate la negativa como éxito.
async function ensureCollection(storage, collection) {
const existing = await storage.run({ action: "collections" });
if (existing.some((c) => c.name === collection)) return "already there";
await storage.run({ action: "createCollection", collection });
return "created";
}def ensure_collection(storage, collection):
existing = storage.run(action="collections")
if any(c["name"] == collection for c in existing):
return "already there"
storage.run(action="createCollection", collection=collection)
return "created"Es una escritura, y se pide como una
Los cambios estructurales están en la misma categoría de permisos que los datos — se pregunta a un agente antes de crear una colección, en cada modo menos el más abierto. la categoría de almacenamiento.