Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

insertMany middleware #11531

Open
jayantasamaddar opened this issue Mar 15, 2022 · 3 comments
Open

insertMany middleware #11531

jayantasamaddar opened this issue Mar 15, 2022 · 3 comments

Comments

@jayantasamaddar
Copy link

Dear Mongoose Team,

The post and pre insertMany middleware have the arguments order reversed.

Pre takes in (next, docs),
Post takes in (docs, next)

Please follow standardized API design or it gets confusing.

@IslandRhythms IslandRhythms added needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue docs This issue is due to a mistake or omission in the mongoosejs.com documentation confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Mar 15, 2022
@IslandRhythms
Copy link
Collaborator

IslandRhythms commented Mar 16, 2022

const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({
    name: String
});

testSchema.pre('insertMany', (docs, next) => {
    console.log('==========================PRE===========================')
    console.log('first', docs);
    console.log('second', next);
    if(Array.isArray(docs)) {
        next();
    } else {
        docs();
    }
});

testSchema.post('insertMany', (docs, next) => {
    console.log('==========================POST==========================');
    console.log('first', docs);
    console.log('second', next);
    if(Array.isArray(docs)) {
        next();
    } else {
        docs();
    }
});

const Test = mongoose.model('Test', testSchema);

async function run() {
    await mongoose.connect('mongodb://localhost:27017');
    await mongoose.connection.dropDatabase();

    await Test.insertMany([{name: 'Test'}, {name: 'Testerson'}]);
}

run();

@vkarpov15 vkarpov15 added backwards-breaking and removed docs This issue is due to a mistake or omission in the mongoosejs.com documentation confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. labels Mar 18, 2022
@vkarpov15 vkarpov15 added this to the 7.0 milestone Mar 18, 2022
@vkarpov15
Copy link
Collaborator

Something to consider changing for Mongoose 7. I agree this is very confusing.

@assetcorp
Copy link

This is especially confusing in TypeScript code. For example, I have the following code:

Schema.pre('insertMany', async function (results: ISchema[]) {
  if (Array.isArray(results)) {
    for (const item of results) {
      const finder = await Model.findOne({ someId: item.someId })
      if (!finder) {
        throw new Error("Schema must exist before AnotherSchema can be created")
      }
    }
  }
})

Becausepre takes in (next, docs), TypeScript warns that Types of parameters 'results' and 'next' are incompatible.. Thus, I am forced to do Schema.pre('insertMany', async function (next, results: ISchema[]) { for everything to work correctly.

I assume correcting the order might be a breaking change, but I agree with @vkarpov15 that it should be considered for version 7.

@vkarpov15 vkarpov15 modified the milestones: 7.0, Parking Lot Feb 13, 2023
@vkarpov15 vkarpov15 modified the milestones: Parking Lot, 9.0 Apr 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants