Open In App

Mongoose Connection.prototype.plugins Property

Last Updated : 23 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The Mongoose Schema API Connection.prototype.plugins property of the Mongoose API is used on the Connection object. It allows us to determine the details about plugin we define over connection object. The plugins we define using plugin() method will get executed for every model creation. Let us understand plugins property using an example.

Syntax:

connection.plugins;

Parameters: This property does not accepts any parameter.

Return Value: This property will return plugin reference object.

Setting up Node.js Mongoose Module:

Step 1: Create a Node.js application using the following command:

npm init

Step 2: After creating the NodeJS application, Install the required module using the following command:

npm install mongoose

Project Structure: The project structure will look like this: 

 

Example 1: The below example illustrates the basic functionality of the Mongoose Connection plugins property, using callback function.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
  
const connectionObject =
    mongoose.createConnection(URI, {
        useNewUrlParser: true,
        useUnifiedTopology: true,
    });
  
const pluginFunction = () => {
    console.log("Customer Model Created")
    console.log(connectionObject.plugins.length);
}
  
connectionObject.plugin(pluginFunction);
  
const customerSchema = new mongoose.Schema({
    name: String,
    city: String,
    state: String,
    orderNumber: Number
})
  
const Customer =
    connectionObject.model('Customer', customerSchema);


Step to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

Customer Model Created
1

Example 2: The below example illustrates the basic functionality of the Mongoose Connection plugins property. We are converting the plugin function return value into string using toLocaleString() method.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
  
const connectionObject =
    mongoose.createConnection(URI, {
        useNewUrlParser: true,
        useUnifiedTopology: true,
    });
  
function pluginFunction1() {
    console.log("Customer Model Created, Plugin-1")
}
  
connectionObject.plugin(pluginFunction1);
  
console.log(connectionObject.plugins.toLocaleString())
  
const customerSchema = new mongoose.Schema({
    name: String,
    city: String,
    state: String,
    orderNumber: Number
})
  
const Customer =
    connectionObject.model('Customer', customerSchema);


Step to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

function pluginFunction1(){
  console.log("Customer Model Created, Plugin-1")
},
Customer Model Created, Plugin-1

Reference: https://mongoosejs.com/docs/api/connection.html#connection_Connection-plugins



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads