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

@codestra/next-serverless-mongoose

v1.2.0

Published

Mongoose helper functions to connect with mongodb in nextjs api-routes

Downloads

3

Readme

next-serverless-mongoose

next-serverless-mongoose is a package that provides helper functions to easily connect with a mongodb. This package was mainly build for usage with nextjs-api-routes and mongoose.

Installation

Use npm for installation:

npm install @codestra/next-serverless-mongoose

Or use yarn for installation:

yarn add @codestra/next-serverless-mongoose

Usage

This package provides currently two functions to deal with a mongodb connection. Both these function assume that you have set one of the two following alternatives in your .env.local file:

MONGODB_URI=example-uri.com

Or the following:

MONGODB_HOST=example-host
MONGODB_DATABASE_NAME=example-database-name

If one of these two alternatives have been set, the following functions will automagically make sure that you have a running mongodb-connection and you will be able to do your operations.

For the authentication, you can set the following:

MONGODB_USER=example-user
MONGODB_PASS=example-password

useMongoose

The useMongoose function has to be called before any other mongoose operation in the api-route. It will automatically connect to the mongodb with the provided details from the environment.

Example:

import { NextApiRequest, NextApiResponse } from 'next';
import { useMongoose } from '@codestra/next-serverless-mongoose';
import User from '../../models/User';

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
  const mongoose = await useMongoose();
  console.log(mongoose.connection.readyState);
  // returns 1: connected

  await User.create({
    name: 'Name',
  });

  res.status(200).json({ message: 'created user' });
};

export default handler;

withMongoose

The withMongoose function acts as a wrapper around the handle function of the api-route. It also makes sure that you have a running mongodb-connection based on the environment variables. This function will alter the request from nextjs and add a mongoose field. At this point you will be able to call any other mongoose function as long as you have wrapped the handler with it.

Example:

import { NextApiResponse } from 'next';
import { withMongoose, NextApiRequestWithMongoose } from '@codestra/next-serverless-mongoose';
import User from '../../models/User';

const handler = async (req: NextApiRequestWithMongoose, res: NextApiResponse) => {
  console.log(req.mongoose.connection.readyState);
  // returns 1: connected

  await User.create({
    name: 'Name',
  });

  res.status(200).json({ message: 'created user' });
};

export default withMongoose(handler);

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Credits and Inspiration

License

MIT