# mongoose-execution-time

> Mongoose plugin for measuring and logging query execution time

Latest version **1.1.1** (published 2023-08-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install mongoose-execution-time
pnpm add mongoose-execution-time
yarn add mongoose-execution-time
bun add mongoose-execution-time
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2023-08-11 |
| First published | 2020-08-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 17.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 34 |
| Author | Hamza Sabljakovic |
| Maintainers | sabljakovich |
| Keywords | mongoose, mongodb, database, query execution time, logging, query time |

## Links

- npm: https://www.npmjs.com/package/mongoose-execution-time
- Repository: https://github.com/sabljakovich/mongoose-execution-time
- Homepage: https://github.com/sabljakovich/mongoose-execution-time#readme
- Issues: https://github.com/sabljakovich/mongoose-execution-time/issues
- npm.io page: https://npm.io/package/mongoose-execution-time

## Recent versions

- 1.1.1 (latest) — 2023-08-11
- 1.1.0 — 2023-07-14
- 1.0.2 — 2020-08-31
- 1.0.1 — 2020-08-30
- 1.0.0 — 2020-08-30

## README

# Mongoose execution time

Mongoose plugin for measuring and logging query execution time.

```bash
npm i mongoose-execution-time
```

Output example:

```sh
Query: find in customers completed in: 7 ms { filter: {} }

Query: find in blogposts completed in: 4 ms { filter: { title: 'Post 1' },
  additionalLogProperties: { bruh: 1 } }

Query: findOne in blogposts completed in: 4 ms { filter: {} }

Query: estimatedDocumentCount in blogposts completed in: 3 ms { filter: {} }

Query: aggregate in blogposts completed in: 1 ms { aggregatePipeline: '[{"$match":{"title":"Post 1"}}]' }

Query: aggregate in blogposts completed in: 1 ms {
  aggregatePipeline: '[{"$match":{"title":"Post 1"}},{"$project":{"title":1}}]'
}


```

## How to use

```js
const mongoose = require('mongoose');
const { logExecutionTime } = require('mongoose-execution-time');

mongoose.plugin(logExecutionTime);
```


## Configuration


The plugin can be easily adjusted via the following configuration options.

```ts
interface LogExecutionTimeConfig {
    logger?: any;
    loggerLevel?: string;
    loggerVerbosity?: LoggerVerbosity;
    loggerFunction?: LoggerFunction;
}
```


| Option  | Description  | Default  |
|---|---|---|
| logger  | logger provider  | console   |
| loggerLevel  | logger level used by the logger above  | debug  |
| loggerVerbosity  | controls how much information gets logged  | High  |
| loggerFunction | for full control over how the log gets written | N/A |


Code example:

```js
const mongoose = require('mongoose');
const { logExecutionTime } = require('mongoose-execution-time');

mongoose.plugin(logExecutionTime, {
    loggerLevel: 'info'
});
```
## Do not want filter/aggregatePipeline information logged?

Simply set the `loggerVerbosity` to `LoggerVerbosity.Normal` in the plugin configuration.

```js
const mongoose = require('mongoose');
const { logExecutionTime, LoggerVerbosity } = require('mongoose-execution-time');

mongoose.plugin(logExecutionTime, {
    loggerVerbosity: LoggerVerbosity.Normal,
    loggerLevel: 'info'
});
```

## Logging additional information

The plugin exposes a method for logging additional information in the same log line as the execution time.

Code example:

```ts
await BlogPostModel.find({ title: 'Title' }).additionalLogProperties({ message: 'My custom message'});
```

Output example:

```
Query: find in blogposts completed in: 8 ms { additionalLogProperties: { message: 'My custom message' } }
```

## Log message configuration

If the default logger formatting or style doesn't fit your needs, it can be adjusted by providing a custom logger function.

Code example:

```ts
const mongoose = require('mongoose');
const { logExecutionTime } = require('mongoose-execution-time');

mongoose.plugin(logExecutionTime, {
    loggerFunction: (operation, collectionName, executionTimeMS, filter, update, additionalLogProperties, aggregationPipeline) => {
        console.log(`My custom logger function | ${operation} | ${collectionName} | ${executionTimeMS}`, { filter, update, additionalLogProperties, aggregationPipeline })
    }
});
```

Output example:

```
My custom logger function | find | blogposts | 4 {
  filter: { title: 'Post 1' },
  update: undefined,
  additionalLogProperties: { bruh: 1 }
  aggregationPipeline: null
}
```

---
_Source: https://npm.io/package/mongoose-execution-time · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
