question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to Query a soft deleted record within another model with populate

See original GitHub issue

I have a soft delete model where I would like any existence of such model as subquery Model will fetch all records such that A soft delete model Service can be query from Payment model with all records both deleted and undeleted. maybe a config on populate can be used to disable the override deleted: true effect.

const payments: IPayment[] = await Payment.find().populate('service').exec();

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Procaseycashcommented, Feb 6, 2020

Check the one on my repo… I think I merged that and made use of it… once merged to the main repo… you can switch back but right now, you can use the merged one or create a fork to your repo for usage.

Thanks

On Thu, 06 Feb 2020 at 7:12 PM, Rony Vidaur notifications@github.com wrote:

There is a PR that resolved this issue using configuration options in populate. I clone the repo and merge to master but on my personal Repo for now. You can check that and fork from original writer. You can then do “populate({ path: “service”, options: { withDeleted: true}}). “ Thanks. On Thu, 06 Feb 2020 at 6:53 PM, Rony Vidaur @.***> wrote: I’ve worked around this by having a little helper function that does the population manually (not sure how efficient it would be and probably can be improved) With your models it would look something like this, this function can work with any model that has references to your Service model assuming it is stored like service in such model : export const withDeletedServices = async (collection) => { const allServices = await ServiceModel.findWithDeleted(); return collection.map(c => { if (c.service) { const service = allServices.find(s => { return s._id.toString() === c.service.toString(); }); c.service = service; } return c; }); }; Maybe we can include something like that in order for the library to override the populate function Also worth mentioning that we wouldn’t be doing .populate instead you would do payments = Payments.find(); and then something like paymentsWithDeletedServices = withDeleted(payments); — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub <#75 https://github.com/dsanel/mongoose-delete/issues/75?email_source=notifications&email_token=AEVCQEA3IO7Q7CMBX3JTHQDRBRE77A5CNFSM4KO2M4IKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOELAFA4Y#issuecomment-583028851>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEVCQEEFBIIXVPEYQ2LMVE3RBRE77ANCNFSM4KO2M4IA . – Kazeem Olanipekun, Advanced Software Engineer P: +234(0)813 506 1846, W: kdevs.com.ng.

That’s great, I hope it gets merged soon, thanks for the heads up!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/dsanel/mongoose-delete/issues/75?email_source=notifications&email_token=AEVCQEFYPDYNOZDTIFUY4ADRBRHKHA5CNFSM4KO2M4IKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOELAHGIY#issuecomment-583037731, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEVCQEADGRULQVAOHX7QPRTRBRHKHANCNFSM4KO2M4IA .

– Kazeem Olanipekun, Advanced Software Engineer P: +234(0)813 506 1846, W: kdevs.com.ng.

1reaction
Procaseycashcommented, Feb 6, 2020

There is a PR that resolved this issue using configuration options in populate. I clone the repo and merge to master but on my personal Repo for now. You can check that and fork from original writer.

You can then do “populate({ path: “service”, options: { withDeleted: true}}). “

Thanks.

On Thu, 06 Feb 2020 at 6:53 PM, Rony Vidaur notifications@github.com wrote:

I’ve worked around this by having a little helper function that does the population manually (not sure how efficient it would be and probably can be improved)

With your models it would look something like this, this function can work with any model that has references to your Service model assuming it is stored like service in such model :

export const withDeletedServices = async (collection) => { const allServices = await ServiceModel.findWithDeleted(); return collection.map(c => { if (c.service) { const service = allServices.find(s => { return s._id.toString() === c.service.toString(); }); c.service = service; } return c; }); };

Maybe we can include something like that in order for the library to override the populate function

Also worth mentioning that we wouldn’t be doing .populate instead you would do payments = Payments.find(); and then something like paymentsWithDeletedServices = withDeleted(payments);

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/dsanel/mongoose-delete/issues/75?email_source=notifications&email_token=AEVCQEA3IO7Q7CMBX3JTHQDRBRE77A5CNFSM4KO2M4IKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOELAFA4Y#issuecomment-583028851, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEVCQEEFBIIXVPEYQ2LMVE3RBRE77ANCNFSM4KO2M4IA .

– Kazeem Olanipekun, Advanced Software Engineer P: +234(0)813 506 1846, W: kdevs.com.ng.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Re-using soft deleted records - Stack Overflow
Based on first query result, perform an UPDATE if it exists (and reverse isdeleted flag) or perform a true INSERT if it does...
Read more >
A guide to soft-delete models in Laravel - Beyond Code
Add soft delete to the User model with a new column in the up() method of a new migration: Schema::table('users', function (Blueprint $table)...
Read more >
Middleware sample: soft delete (Reference) - Prisma
The following sample uses middleware to perform a soft delete. Soft delete means that a record is marked as deleted by changing a...
Read more >
Working With Soft Deletes in Laravel 8 and 9 (By Example)
The first step is adding the Illuminate\Database\Eloquent\SoftDeletes trait to the models. Below is an example model where we have set it up to...
Read more >
Soft Deletes in Django - DEV Community ‍ ‍
We can query our database filtering by is_deleted in order to exclude records that have been soft-deleted. Note.objects.filter(is_deleted=False) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found