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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nonocast/koa-mongoose

v1.1.0

Published

Mongoose middleware for Koa 2

Downloads

4

Readme

koa-mongoose

🍃 Mongoose middleware for Koa 2

npm CI Status Coveralls Status Dependencies Dev Dependencies


Features

  • Adds model() and document() to the koa ctx
  • Config for automatically loading schemas and events on the mongoose instance

Basic Usage

The middleware config accepts a user and pass or you can use a uri or url as an escape hatch for other url schemes.

Unauthenticated connections will also work by omitting the user and pass and only providing a host, port and db.

Note: If you're using the mongodb+srv syntax to connect to MongoDB Atlas, you should use the mongoose dbName option to specify the database because you currently cannot in the connection string. (source) Be sure to change the default koa-mongoose db option to match as well.

const Koa = require('koa');
const mongoose = require('@south-paw/koa-mongoose');

const app = new Koa();

// middleware config
const config = {
  host: 'localhost',
  port: '27017',
  user: '',
  pass: '',
  db: 'test',
  mongoOptions: {
    // these are the default middleware options, see https://mongoosejs.com/docs/connections.html#options
    useNewUrlParser: true,
    useCreateIndex: true,
    useUnifiedTopology: true,
    reconnectTries: Number.MAX_SAFE_INTEGER, // Never stop trying to reconnect
    reconnectInterval: 500, // Reconnect every 500ms
    poolSize: 10, // Maintain up to 10 socket connections
    bufferMaxEntries: 0, // If not connected, return errors immediately rather than waiting for reconnect
    connectTimeoutMS: 10000, // Give up initial connection after 10 seconds
    socketTimeoutMS: 45000, // Close sockets after 45 seconds of inactivity
  },
  schemas: {
    // schemas recieve the middleware's mongoose instance, see https://mongoosejs.com/docs/schematypes.html
    users: ({ Schema }) =>
      new Schema(
        {
          name: { type: String },
          pets: [Schema.Types.ObjectId],
        },
        { collection: 'users' },
      ),
    pets: ({ Schema }) =>
      new Schema(
        {
          type: { type: String },
          name: { type: String },
        },
        { collection: 'pets' },
      ),
  },
  events: {
    // see https://mongoosejs.com/docs/api.html#connection_Connection-readyState
    error: () => console.error('mongoose: error'),
    connected: () => console.log('mongoose: connected'),
    connecting: () => console.log('mongoose: connecting'),
    disconnecting: () => console.log('mongoose: disconnecting'),
    disconnected: () => console.log('mongoose: disconnected'),
  },
  useDefaultErrorHandler: false, // enable or disable the default error handler
};

// apply the middleware
app.use(mongoose(config));

// and you can now use the middleware via the ctx with
// `ctx.model(modelName)` and `ctx.document(modelName, document)`

Issues and Bugs

If you manage to find any, please report them here so they can be squashed.

Development and Contributing

Pull the repo and install dependencies with yarn

# run tests
yarn test

# lint source
yarn lint

# format source with prettier
yarn format

License

MIT, see the LICENSE file.