Are there any best practices when writing to the db from a different file than where a Collection is defined?
For example, I’m working with a 3rd-party API and would like to write some of data to the db. I’m calling a method from the client, waiting for the result from the API on the server and then inserting.
Another example would be if you have CollectionA and after successful insert, you want to write to CollectionB.
Would it be better to:
1) Import Collection and call insert directly
import { Collection } from '/imports/api/collection.js'
// do stuff
Collection.insert({...})
Pro: Less code
Con: You might have writes like insert / update / upsert
sprinkled in different files. Something about this feels not great but maybe it’s fine.
– OR –
2) Define a function that wraps Collection.insert and import the function into the file
import { insertDocument } from '/imports/api/collection.js'
// do stuff
insertDocument({...})
Pro: All your writes would be in one file. Seems better for validation and maintaining.
Con: More code
Or maybe there’s another way that I’m not even thinking of. Curious to hear your thoughts.