Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

nachocodoner
Copy link
Member

@nachocodoner nachocodoner commented Jan 17, 2024

Reported on slack: https://meteor-community.slack.com/archives/CN350MY1G/p1704695310758029

Some time back, we were tasked with addressing an issue occurring in a specific code scenario that seems to break isomorphism.

The next code behaves differently on the client and the server in Meteor 3.x, disrupting isomorphism. The operation should align on both client and server, the client's minimongo isn't populated with data by default, even though the server inserts data correctly into Mongo.

 const NamedCollection = new Meteor.Collection('namedCollection');
    
 await NamedCollection.insertAsync({ test: 1 });
    
// In the client it doesn't log the data inserted
// In the server it logs the data inserted
console.log("-> isItemCreated?", NamedCollection.find().fetch()?.[0]);

The problem lies in the code behaving differently in Meteor 2.x and 3.x. It populates in the former but not in the latter. See the next picture to clarify:

image

The problem not only happens in insertAsync but also in other operations such as updateAsync and removeAsync.

image

This problem mainly occurs in testing scenarios, not broadly, as minimongo is typically populated and utilized by functions following proper reads from publications or methods, not as a result of collection operations.

A use case where happens is for example on tests: https://github.com/Meteor-Community-Packages/mongo-collection-instances/pull/40/files#diff-bbd9a41e09b247b7b1128731dc48f53d701e08e4ad08bba374523b3f4f23d43c

Fix

Aligning the operations of Meteor 2.x and 3.x is achieved by introducing the resolverType option for collections (new Collection(name, options)). In Meteor 3.x, a distinction exists between stub and server promises. The former handles client simulation and minimongo population, while the latter solely manages success or error on the server call without populating the data in minimongo. The data is persisted on the server in both. The default is the server promise throughout Meteor 3.x codebase and tests, that is why changing to stub is not an easy option, above all being this an edge case.

The resolverType option offers stub and server values. Using stub maintains collection operation behavior to align with Meteor 2.x, addressing the isomorphic challenge.

 const NamedCollection = new Meteor.Collection('namedCollection', { resolverType: 'stub' });
    
 await NamedCollection.insertAsync({ test: 1 });
    
// In the client it logs the data inserted
// In the server it logs the data inserted
console.log("-> isItemCreated?", NamedCollection.find().fetch()?.[0]);

Check the test to see this new option in action:
https://github.com/meteor/meteor/blob/319-fix-client-op--remote-collections/packages/mongo/mongo_livedata_tests.js#L4279-L4304

Test

  • Add a test for this regression

@CLAassistant
Copy link

CLAassistant commented Jan 17, 2024

CLA assistant check
All committers have signed the CLA.

@nachocodoner nachocodoner added the Meteor 3 relates to Meteor 3 label Jan 17, 2024
@StorytellerCZ StorytellerCZ added this to the Release 3.0 milestone Jan 18, 2024
@nachocodoner nachocodoner marked this pull request as ready for review February 13, 2024 14:40
@jankapunkt
Copy link
Contributor

@nachocodoner thanks a lot! is there a way to test this with some of our community packages without locally forking and building this specific branch?

@nachocodoner
Copy link
Member Author

nachocodoner commented Feb 15, 2024

@jankapunkt: I already tested when adding this new option, I just added here a proof on running this locally again.

image

I don't think there is anything more we can do in this moment to run this change on tests without going to a release process. But surely we can include this for the next one.

@jankapunkt
Copy link
Contributor

Thank you, I will update the tests in the community package repos, once the next beta is out 👍

@denihs denihs merged commit 46339a9 into release-3.0 Feb 19, 2024
@nachocodoner nachocodoner deleted the 319-fix-client-op--remote-collections branch February 19, 2024 16:44
@Grubba27
Copy link
Contributor

Hey, @jankapunkt, beta .4 is out with this one!

@Grubba27 Grubba27 mentioned this pull request Feb 22, 2024
@simonwebs
Copy link

Good job done 🙏🏿❤️👍👏Isomorphic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants