Having a REPL / console to play with your application code is always handy. So here is a little piece of code that enables me to use a mongoose User model inside a node repl:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// load environment variables from .env file | |
require('dotenv').config(); | |
// load mongoose and connect to db using env variable | |
const mongoose = require('mongoose'); | |
mongoose.connect(process.env.MONGODB_CONNECTION, { useNewUrlParser: true, useUnifiedTopology: true }); | |
// start the node repl | |
const repl = require('repl'); | |
const context = repl.start().context; | |
// load mongoose User model | |
const { User } = require('./lib/models/user'); | |
// add mongoose User model to repl context: | |
context.User = User; | |
context.mongoose = mongoose; | |
// start with: node --experimental-repl-await repl.js | |
// use the User model: await User.find({}); |
Top comments (0)