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

mongoose-live

v0.0.2

Published

A simple REPL integration for Mongoose

Downloads

8

Readme

Mongoose Live

A simple REPL integration for Mongoose

Why?

Many developers are accustomed to using the Django shell or Rails console to test database interactions via data models. Mongoose does not have a native REPL -- this module provides an easy-to-implement option.

Usage

An automated installation script for this package is also available: npx create-mongoose-live

  1. Install the package: npm i mongoose-live
  2. In a file (repl.js, perhaps), require the package...
  3. Then invoke the package, providing a mongoose.connection object and a models object (optional, see below for details) as arguments:
const live = require('mongoose-live') // requires the package
const db = require('./db') // a mongoose.connection object
const models = require('./models') // must be an object; keys available in REPL context
live(db, models)
  1. Execute the file using the --experimental-repl-await flag either from the command line or from an npm script:
node --experimental-repl-await repl.js

or (in package.json.scripts)

"repl": "node --experimental-repl-await repl.js"

... and then execute npm run repl.

  1. Interact with your Mongoose models using any methods you would use in an API controller or any other Node.js environment.

The Models Object

The models object provided as an argument should be an object with keys matching the names of each Mongoose model. These keys will be available as variables in the REPL context. Example:

const User = require('./models/user.js') // imports the User model
const Task = require('./models/task.js') // imports the Task model
const models = { User, Task } // provided as argument, enables User and Task in REPL

If your project already includes a "models" directory whose index.js exports an object that includes all models, you may require that directory directly.

Context

Additional variables may optionally be added to the REPL context as keys in an object:

const db = require('./db')
const models = require('./models')
const context = {
    searchUsersByName: async function (name) {
        return await models.User.find({name: {$regex: name, $options: "i"}})
    }
}
live(db, models, context) // in addition to models, searchUsersByName will be available

Options

An options object may also be provided. Available options may be expanded, but currently include:

  • prompt: Define a custom prompt to use in place of MongooseLive> .

Known Issues

.then()

Operations that return a Promise chained with .then() may see any resulting logs output immediately after the following prompt. A blinking cursor will be shown with no visible prompt. Hit the RETURN key, and the REPL should behave as normal.

If the provided mongoose.connection object is chained before a .then() method, any logs from the .then may appear immediately after the initial prompt, with no significant operational consequences.