Socket
Socket
Sign inDemoInstall

mongoose-query-logger

Package Overview
Dependencies
240
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    mongoose-query-logger

![CI](https://github.com/marcosfede/mongoose-query-logger/workflows/CI/badge.svg) ![npm version](https://badgen.net/npm/v/mongoose-query-logger) ![types](https://badgen.net/npm/types/mongoose-query-logger) ![license](https://badgen.net/npm/license/mongoos


Version published
Weekly downloads
1.3K
decreased by-2.96%
Maintainers
1
Install size
402 kB
Created
Weekly downloads
 

Readme

Source

Mongoose Query Logger

CI npm version types license bundlephobia

Mongoose middleware to log your mongoose queries and execution timings.

Optionally, it also logs index usage and warns you about full collection scans

Screenshots

Query logging and execution timing

Query

Query Explain

Collscan IXScan

Installation

npm install --save-dev mongoose-query-logger

Usage

Apply the plugin to all schemas:

import { MongooseQueryLogger } from 'mongoose-query-logger';

export const queryLogger = new MongooseQueryLogger();
 
// optionally add custom configuration eg:
// queryLogger
//    .setExplain(true)
//    .setAdditionalLogProperties(true)
//    .setQueryLogger(myCustomQueryLogger)
//    .setExplainLogger(myCustomExplainLogger);

mongoose.plugin(queryLogger.getPlugin());

Apply the plugin to specific schemas:

import { MongooseQueryLogger } from 'mongoose-query-logger';

export const queryLogger = new MongooseQueryLogger();

// optionally add custom configuration eg:
// queryLogger
//    .setExplain(true)
//    .setAdditionalLogProperties(true)
//    .setQueryLogger(myCustomQueryLogger)
//    .setExplainLogger(myCustomExplainLogger);

const schema = new mongoose.Schema({
  /* schema definition */
});

schema.plugin(queryLogger.getPlugin());

// compile the model AFTER registering plugins
const User = mongoose.model('User', schema);

Explain logging

This is turned off by default. It will fire an explain query for supported operations. Turn this on by calling:

plugin.setExplain(true)

warning: don't use explain in production, it will run each query twice.

Supported query logging methods

The following methods are supported for query logging

methodsupported
count:heavy_check_mark:
countDocuments:heavy_check_mark:
estimatedDocumentCount:heavy_check_mark:
find:heavy_check_mark:
findOne:heavy_check_mark:
findOneAndUpdate:heavy_check_mark:
findOneAndRemove:heavy_check_mark:
findOneAndDelete:heavy_check_mark:
findOneAndRemove:heavy_check_mark:
update:heavy_check_mark:
updateOne:heavy_check_mark:
updateMany:heavy_check_mark:
deleteOne:heavy_check_mark:
deleteMany:heavy_check_mark:
aggregate:heavy_check_mark:
remove
insertMany
distinct

If you want only a subset of these to be logged, you can provide an array of supported methods like so:

plugin.setQueryMethods({targetMethods: ['find', 'aggregate']})

Supported explain logging methods

The following methods are supported for query explaining

methodsupported
find:heavy_check_mark:
findOne:heavy_check_mark:
aggregate:heavy_check_mark:

If you want only a subset of these to be logged, you can provide an array of supported methods like so:

plugin.setQueryMethods({explainMethods: ['find', 'aggregate']})

Custom query logger

You can provide a custom logging function by calling plugin.setQueryLogger(myCustomLogger) The logger should be a function that accepts a single argument of type object with the following keys:

keytypedescriptionexample
operationstringexecuted operationfind, aggregate
collectionNamestringcollection nametasks
executionTimeMSnumberquery execution time in ms320ms
filterObject or nullfilter object provided to the query{"name": "john"}
fieldsObject or nullprojection fields{"name": 1}
optionsanyquery options{"sort": "name"}
updateObject or null
additionalLogPropertiesanyadditional log options

Custom explain logger

You can provide a custom explain logging function by calling plugin.setExplainLogger(myCustomExplainLogger) The logger should be a function that accepts a single argument of type object with the following keys:

keytypedescription
queryPlannersany[]array of query execution plans as returned from mongodb

Additional log properties

You can include additional metadata in your queries by turning this on with

plugin.setAdditionalLogProperties(true)

and using it like await User.find({"name": "john"}).additionalLogProperties('something')

Supported versions

This was tested under mongoose 4.4 and node.js >= 12

License

MIT © Federico Marcos

Keywords

FAQs

Last updated on 04 Nov 2020

Did you know?

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc