Skip to content

Connect

Source: src/AWS/DocDB/Connect.ts

Runtime binding that resolves MongoDB connection settings for a DocumentDB DBCluster from a Secrets Manager secret (the cluster’s managed master user secret by default).

Binding it yields an Effect (not a callable) that resolves a MongoConnectionInfo — host, port, credentials, and a ready-to-use mongodb:// URL — fresh on every execution. No socket is opened; feed the result into mongo (the bundled Effect client over the mongodb driver) or any MongoDB driver. The deploy half grants secretsmanager:GetSecretValue on the secret and publishes the endpoint as environment variables. Provide the implementation with Effect.provide(AWS.DocDB.ConnectHttp).

Query DocumentDB from a Function

export default MyFunction.make(
{ main: import.meta.url, url: true },
Effect.gen(function* () {
// init — bind the cluster's managed master secret; grants
// secretsmanager:GetSecretValue and attaches the function to the
// cluster's VPC subnets/security groups
const connect = yield* AWS.DocDB.Connect(cluster, {
database: "app",
subnetIds: [subnetA.subnetId, subnetB.subnetId],
securityGroupIds: [appSecurityGroup.groupId],
});
// init — build the Effect mongo client (one connection per execution)
const db = yield* AWS.DocDB.mongo(connect);
return {
fetch: Effect.gen(function* () {
const { use } = yield* db;
const orders = yield* use((db) =>
db.collection("orders").find({ open: true }).toArray(),
);
return yield* HttpServerResponse.json({ count: orders.length });
}).pipe(Effect.orDie),
};
}).pipe(Effect.provide(AWS.DocDB.ConnectHttp)),
);

Resolve Raw Connection Info

// init
const connect = yield* AWS.DocDB.Connect(cluster, { database: "app" });
// runtime — host/port/credentials plus a ready-to-use mongodb:// URL
const info = yield* connect;