Skip to content

mongo

Source: src/AWS/DocDB/Mongo.ts

Open an Effect-typed MongoDB client from a DocumentDB connection (the runtime Effect produced by binding AWS.DocDB.Connect).

The connect work is deferred until first use and memoized on the current execution’s Scope (via makeExecutionMemo), so the driver connection is built at most once per execution — a Lambda invocation or Worker event — and its close finalizer fires when the execution settles, never held across events. This is the one legal pooling shape on workerd (sockets are IoContext-pinned) and the correct one on Lambda.

DocumentDB authenticates over the MongoDB wire protocol: database users and their built-in roles (read, readWrite, dbAdmin, clusterAdmin, …) are managed inside the database with db.createUser(...) — IAM only governs the management plane.

Query a Collection inside a Function

// init — bind the cluster, then build the client
const connect = yield* AWS.DocDB.Connect(cluster, { database: "app" });
const db = yield* AWS.DocDB.mongo(connect);
// runtime — one driver connection per execution, closed on settle
const { use } = yield* db;
const open = yield* use((db) =>
db.collection("orders").find({ open: true }).toArray(),
);

Create a Database User (DB-plane auth)

const { use } = yield* db;
yield* use((db) =>
db.admin().command({
createUser: "reporting",
pwd: reportingPassword,
roles: [{ role: "read", db: "app" }],
}),
);