These are chat archives for Automattic/mongoose

13th
May 2017
codingPerfection
@codingPerfection
May 13 2017 11:15
guys i have a question about saving refered models is there a way to automatically save parent model and child refered model smth asked in this question
http://stackoverflow.com/questions/14758761/mongoose-create-reference-on-model-save
basically a populate on save
codingPerfection
@codingPerfection
May 13 2017 11:47
http://mongoosejs.com/docs/populate.html saving ref from here seems like so much pain can't it be automated?
Ankur Anand
@ankur-anand
May 13 2017 16:28

What is the proper way to check that if collection exist then only drop using mongoose

before(async () => {
    const {users} = mongoose.connection.collections;
    try {
         await users.drop();
    } catch(err) {
        throw new Error(err);
    }  
});

I'm tying this way but first run result in before all hook error as the collection is not there. How could i handle such cases

Ankur Anand
@ankur-anand
May 13 2017 20:34
worked around the above like this
before(async () => {

    let res = await mongoose.connection.db.listCollections({name: 'users'}).toArray();
    if (res.length) {
        await mongoose.connection.collections.users.drop();
    }
});
Please let me know your suggestion on this