npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@awsmag/power-document-db

v1.7.0

Published

A package to connect and work with document db (mongodb compatible)

Downloads

159

Readme

power-document-db

A lightweight utility library to simplify connecting and working with Amazon DocumentDB (with MongoDB compatibility).
Built for real-world usage, including production DocumentDB clusters, containerized local MongoDB, and Koa middleware integration.


📦 NPM Package

npm version MIT License NodeJS Types Downloads


📚 Table of Contents

  1. Features
  2. Installation
  3. Environment Variables
  4. Usage
  5. Koa Middleware
  6. Transactions
  7. API
  8. Local Development
  9. Troubleshooting
  10. Contributing
  11. Maintainers
  12. License

✅ Features

  • Connect easily to DocumentDB or local MongoDB
  • SSL/TLS support with AWS CA certificates
  • Works with Docker-based Mongo
  • Koa middleware support → ctx.db
  • Supports transactions
  • TypeScript friendly
  • Minimal, simple API

🔧 Installation

npm install @awsmag/power-document-db

🌍 Environment Variables

| Variable | Description | Optional | | ---------------- | ---------------------------------- | -------- | | CONNECTION_URI | Mongo/DocumentDB connection string | ✅ | | DB_NAME | Database name | ✅ |

If set → connectDb() can be called without arguments Otherwise → pass parameters explicitly


🚀 Usage

Using Environment Variables

import { connectDb } from "@awsmag/power-document-db";

async function main() {
  const db = await connectDb();
  const users = await db.collection("users").find({}).toArray();
  console.log(users);
}

main();

Passing Parameters Explicitly

import { connectDb } from "@awsmag/power-document-db";

async function main() {
  const uri = "mongodb://localhost:27017";
  const dbName = "test";
  const ssl = false;
  const tlsCAFile = "./certs/global-bundle.pem"; // Required only when ssl=true

  const db = await connectDb(uri, dbName, ssl, tlsCAFile);
}

🧩 Koa Middleware

Attach DB client automatically to ctx.db:

import Koa from "koa";
import { connectDb, getDbClientMw } from "@awsmag/power-document-db";

const app = new Koa();

(async () => {
  await connectDb("mongodb://localhost:27017", "test");
  app.use(getDbClientMw());

  app.use(async ctx => {
    const users = await ctx.db.collection("users").find({}).toArray();
    ctx.body = users;
  });

  app.listen(3000);
})();

🔄 Transactions

import { startSession } from "@awsmag/power-document-db";

async function runWithTransaction(db) {
  const session = await startSession();

  try {
    await session.startTransaction();

    await db.collection("orders").insertOne({ item: "Book" }, { session });

    await session.commitTransaction();
  } catch (err) {
    await session.abortTransaction();
    console.error("Transaction aborted:", err);
  } finally {
    session.endSession();
  }
}

🧠 API

connectDb(uri?, dbName?, ssl?, tlsCAFile?)

Returns: Promise<Db>

If no args → uses env variables


startSession()

Returns: Promise<ClientSession>


getDbClientMw()

Koa middleware → adds ctx.db


🛠 Local Development

Using Docker

docker run \
  -p 27017:27017 \
  --name mongo \
  mongo:latest

Then connect using:

mongodb://localhost:27017

Use ssl=false for local development.


❗ Troubleshooting

| Issue | Fix | | ------------------------------- | ---------------------------------------------- | | SSL enabled without CA bundle | Provide tlsCAFile | | Timeout connecting to AWS DocDB | Check VPC + security group access | | Transaction errors | Ensure cluster supports transactions | | ctx.db undefined | Make sure connectDb() runs before middleware |


🤝 Contributing

  1. Fork the repo
  2. Create branch → feature/my-change
  3. Add tests if needed
  4. Submit a PR

👨‍🔧 Maintainers


📄 License

MIT