SoftwareBrothers / adminjs-mongoose

Mongoose adapter for AdminJS
MIT License
14 stars 39 forks source link

feat: use countdocuments method #85

Open itamar-owlytics opened 1 year ago

itamar-owlytics commented 1 year ago

countDocuments method use the pre count hook and then we got the right number of items

For example: I have 'users' collection in mongoDB and I use the next models:

const userSchema = new Schema({
    name: String,
    is_staff: Boolean
});

const Users = mongoose.model('user', userSchema);

const staffSchema = new Schema({
    email: String
}).add(userSchema);

const Staff = mongoose.model('staff', staffSchema, 'users');

const findQueryTypes = ['count', 'find', ...];

findQueryTypes.forEach(type => {
    staffSchema.pre(type, function (next) {
        const query = { is_staff: true };  
        this.where(query);

        next();
    });
});

Both models use the users collection but in the Staff list page I expect to receive only the staff members from the users collection - and it's works - But the count number next to the List label is still same as in the Users page

image

image

itamar-owlytics commented 1 year ago

I know that estimatedDocumentCount method is faster than countDocuments method because the first use the collection metadata but I think that this change is required

https://mongoosejs.com/docs/api/model.html#model_Model-estimatedDocumentCount https://mongoosejs.com/docs/api/model.html#model_Model-countDocuments