mongoose-live v0.0.2
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
- Install the package:
npm i mongoose-live - In a file (
repl.js, perhaps),requirethe package... - Then invoke the package, providing a
mongoose.connectionobject and amodelsobject (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)- Execute the file using the
--experimental-repl-awaitflag either from the command line or from annpmscript:
node --experimental-repl-await repl.jsor (in package.json.scripts)
"repl": "node --experimental-repl-await repl.js"... and then execute npm run repl.
- 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 REPLIf 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 availableOptions
An options object may also be provided. Available options may be expanded, but currently include:
prompt: Define a custom prompt to use in place ofMongooseLive>.
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.