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

koa2-session-mongo

v0.1.0

Published

MongoDB storage layer for Koa session middleware

Downloads

9

Readme

koa2-session-mongo

NPM version build status Test coverage David deps Known Vulnerabilities npm download

MongoDB session store for Koa session middleware. Based on connect-mongo.

This repo is forked from hiddentao/Koa session middleware. Go origin repository see preview version.

See Chinses version: 中文

Compatibility

For extended compatibility, see previous versions.

Installation

# for npm
npm install --save koa2-session-mongo
# for yarn
yarn add koa2-session-mongo

Usage

Koa integration

Koa 2.x:

const session = require("koa-session-store");

const MongoStore = require("koa2-session-mongo");
const Koa = require("koa");

const app = new Koa();

app.keys = ["some secret key"]; // needed for cookie-signing

app.use(
  session({
    store: new MongoStore({
      url: "mongodb://127.0.0.1", // requierd
      db: "database_name", // required
    }),
  })
);

app.use(async (ctx) => {
  let n = ctx.session.views || 0;
  ctx.session.views = ++n;
  ctx.body = n + " views";
});

app.listen(3000);
console.log("listening on port 3000");

Connection to MongoDB

In many circumstances, koa2-session-mongo will not be the only part of your application which need a connection to a MongoDB database. It could be interesting to re-use an existing connection.

Alternatively, you can configure koa2-session-mongo to establish a new connection.

Re-use a Mongoose connection

const mongoose = require("mongoose");

// Basic usage
mongoose.connect(connectionOptions);

app.use(
  session({
    store: new MongoStore({ mongooseConnection: mongoose.connection }),
  })
);

// Advanced usage
const connection = mongoose.createConnection(connectionOptions);

app.use(
  session({
    store: new MongoStore({ mongooseConnection: connection }),
  })
);

Re-use a native MongoDB driver connection (or a promise)

In this case, you just have to give your db instance to koa2-session-mongo. If the connection is not opened, koa2-session-mongo will do it for you.

/*
 ** There are many ways to create dbInstance.
 ** You should refer to the driver documentation.
 */

app.use(
  session({
    store: new MongoStore({ db: dbInstance }),
  })
);

Or just give a promise...

app.use(
  session({
    store: new MongoStore({ dbPromise: dbInstancePromise }),
  })
);

Create a new connection from a MongoDB connection string

MongoDB connection strings are the best way to configure a new connection. For advanced usage, more options can be configured with mongoOptions property.

// Basic usage
app.use(
  session({
    store: new MongoStore({ url: "mongodb://localhost/test-app" }),
  })
);

// Advanced usage
app.use(
  session({
    store: new MongoStore({
      url:
        "mongodb://user12345:foobar@localhost/test-app?authSource=admins&w=1",
      mongoOptions: advancedOptions, // See below for details
    }),
  })
);

Options

The following configuration options are available for the new call:

  • db String or Object - db name or instantiated node-mongo-native db object.
  • collection String - collection name. Default is sessions.
  • auto_reconnect Boolean - gets passed to the node-mongo-native constructor as the same option. Default is false.
  • ssl Boolean - use ssl to connect to the server. Default is false.
  • expirationTime Number - time-to-live (TTL) in seconds for any given session data - MongoDB will auto-delete data which hasn't been updated for this amount of time. Default is 2 weeks.
  • url String - connection URL of the form mongodb://user:pass@host:port/database/collection. If provided then this will take precedence over other options except mongoose.
  • mongoose Object - a Mongoose connection, use** mongoose.connection to get the connection out of an existing Mongoose object. If provided then this will take precedence over other options.

License

MIT