runebook · rune
runebook · rune
No capabilities requested.
manifest: sha256:dac629f8cbcf8477d52b03bed85f17361f50a1818dbd2f47b7034bbb69d6a27e
rune add @rune/mongo@0.1.2Use MongoDB as a database for @rune/storage documents, @rune/web-auth accounts, or a stateful Paper Rune.
rune add @rune/mongo
Use a mongodb: or mongodb+srv: URL with the intended database in its path. The application user needs read and write access to the collection and permission to create the expiration index.
Set DATABASE_URL in the process environment for this example.
Example URL shape:
mongodb://user:password@127.0.0.1:27017/rune
import { connect } from "@rune/mongo";
import { document } from "@rune/storage";
const db = await connect(process.env.DATABASE_URL!, {
name: "shared",
collection: "rune_storage",
});
const visits = document(db.store, "lobby:visits", () => ({ total: 0 }));
await visits.update(({ total }) => ({ total: total + 1 }));
name registers the open connection for other Runes in the same server process.
For example, RUNE_HOMES_STORAGE=reuse:shared makes Homes use this connection.
Omit the name for a generated name or set it to false for a private connection.
Only the connection owner should call await db.close() when consumers have stopped.
collection defaults to rune_storage. The adapter ensures a TTL index on expiresAt. MongoDB's TTL monitor removes expired records asynchronously.
await mongoStore(collection) wraps an existing MongoAuthCollection. db.client exposes the MongoClient created by connect. When you wrap a driver yourself, you own its lifecycle. See the shared storage configuration for plugin environment variables and connection modes.