node-mongodb-fixtures icon indicating copy to clipboard operation
node-mongodb-fixtures copied to clipboard

How to skip load more fixtures without removing existing entries.

Open krzywiecki opened this issue 5 years ago • 3 comments

Hello guys! Thanks a lot for a great job!

I have a question about how can I load some fixtures to my database without removing existing entries from the collection. My use case is that I have a list of categories in my app with some categories defined at the start by myself (these I want to load from fixtures) but users of the app can also create more categories. So what I want to do is to only add entries defined in fixtures but not removing existing ones. I did manage to do it by removing unload() function from my file but then I'm getting an error as it tries to add entries that already exist (the key name in the collection has to be unique).

My file looks like this:

require('dotenv').config();
const Fixtures = require('node-mongodb-fixtures');
const fixtures = new Fixtures({
  dir: './fixtures',
});

fixtures
  .connect(process.env.DB_CONNECTION_URL)
  .then(() => fixtures.load())
  .catch(e => console.error(e))
  .then(() => fixtures.disconnect());

As you can see I'm catching these errors so it works but I'm wondering if there is a better solution for my use case?

Thanks!

krzywiecki avatar Jan 06 '20 12:01 krzywiecki

@krzywiecki thanks a lot. I appreciate the feedback and this issue.

given the current implementation, you are handling the case appropriately. that being said, we could consider providing options to load(options) that effectively enable you to instruct the loader to perform bulk upserts rather than inserts.

This line is responsible for the insert https://github.com/cdimascio/node-mongodb-fixtures/blob/master/lib/index.js#L181

This could be augmented to perform a bulk upsert based on the provide load options

PRs are very much welcome. If you'd like to take the first crack at it, I'm happy to review your changes and help get them integrated into a new version.

if you're up for it, i also have a channel on gitter where we can discuss more or to help with implementation and questions

cdimascio avatar Jan 08 '20 02:01 cdimascio

Sure, I'm happy to help! I'll be very busy for the next few weeks but I think you can assign this issue to me. Before I'll start implementing it I'll reach about to you on the gitter channel you just posted.

krzywiecki avatar Jan 08 '20 10:01 krzywiecki

fantastic! much appreciated and thanks

cdimascio avatar Jan 08 '20 13:01 cdimascio