Socket
Book a DemoInstallSign in
Socket

express-mongoose-loader

Package Overview
Dependencies
Maintainers
2
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-mongoose-loader

Create mongodb models loaders scripts for your express app

1.0.3
latest
Source
npmnpm
Version published
Maintainers
2
Created
Source

Express Mongoose Loader

Create MongoDB models loaders scripts for your express app, saving you thoses numerous database requests.

Installation

$ npm install express-mongoose-loader

Usage

First call for express-mongoose-loader to set up loading system.

app.use(require('express-mongoose-loader'));

Then, create a mongoose model loader by requesting mongoose id list to load with the function req.requestList(listName). Add item's data into the object req.db[listName] created by express-mongoose-loader.

import mongoose from 'mongoose';
import Article from './model/article';

let articlesLoaders = (req, res , next) => {
  Article.find(
    {
      _id: { $in: req.requestList('articles') }
    },
    (err, docs) => {
      if (!err && docs) {
        for (const doc of docs) {
          req.db.articles[doc.id] = {
            title: doc.title,
            datetime: doc.datetime,
            content: doc.content
          };
        }
      }
      next();
    }
  );
}

Request model's item loading by calling the function req.load(id).

req.load('57f0c59a779f68ea0a70a4e2');

After requesting a load you must call your model loader middleware.

app.use(articlesLoaders);

Example App

/* Init your express app */
import express from 'express';

import mongooseLoader from 'express-mongoose-loader';

import mongoose from 'mongoose';
mongoose.connect(/* DB config */);

const app = express();
app.listen(8080);

app.use(mongooseLoader);

/* Create a basic mongoose model */
var ArticleSchema = new mongoose.Schema({
  title: String,
  datetime: Date,
  content: String
});
const Article = mongoose.model('article', ArticleSchema);

/* Create a basic mongoose loader */
let articlesLoaders = (req, res, next) => {
  Article.find(
    {
      _id: { $in: req.requestList('articles') }
    },
    (err, docs) => {
      if (!err && docs) {
        for (const doc of docs) {
          req.db.articles[doc.id] = {
          title: doc.title,
          datetime: doc.datetime,
          content: doc.content
          };
        }
      }
      next();
    }
  );
}

/* Create a basic articles loader middleware */
let loadArticles = (req, res, next) => {
  Article.find(
    {
      public: true
    },
    (err, articles) => {
      if (!err && articles) {
        for (const article of articles) {
          /* Ask for article data loading */
          req.load('articles', article.id);
        }
      }
    }
  );
}

/* Basic index page */
app.use([loadArticles, articlesLoaders]);
app.get('/',
  (req, res) => {
    let content = '';
    for (const article of req.db.articles) {
      content += '<h1>' + article.title + '</h1><br />';
    }
    res.send(content);
  }
);

License

This project is licensed under the MIT license.

Keywords

express

FAQs

Package last updated on 18 Sep 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.