Support *Local
methods in Collections API
#12644
Replies: 3 comments 4 replies
-
I think that 1 could be better handled with a small api change to how these collections are constructed. Ideally, |
Beta Was this translation helpful? Give feedback.
-
I don't think a good eslint rule will be that easy to write. A naive one will be, but that will catch any methods named For this second reason, my codebase would not be able to use this rule. |
Beta Was this translation helpful? Give feedback.
-
I like the idea of having a synchronous way to read Minimongo data on the client since the async api seems unnecessary here. In an ideal world, I wouldn't need to specify I think starting with read operations makes sense. In fact, that may be all that is needed. I'm not sure I understand the use case of having a way to write synchronously to a LocalCollection -- assuming that the async api supports Optimistic UI for write operations inside a Method. |
Beta Was this translation helpful? Give feedback.
-
Background
Currently, Meteor's Collections API has two modes: synchronous one (
fetch
,insert
, etc.) and asynchronous one (fetchAsync
,insertAsync
, etc.). Their detailed behavior in different Meteor versions is described below.fetch
,insert
, etc.):*Async
.*Async
.fetchAsync
,insertAsync
, etc.):Promise
).Promise
; once the Fiber returns).Now, since Meteor v3 cannot support synchronous database operations on the server (#11505, https://radekmie.dev/blog/on-the-road-to-fibers-free-meteor/), we have to make sure the entire ecosystem is ready for that. View libraries were already taken care of (meteor/react-packages#360, meteor/react-packages#385, meteor/react-packages#387, meteor/blaze#412, meteor/blaze#413), but even if these can work with async APIs, it comes with some drawbacks - it requires a lot of changes in the way our apps handle the data.
Proposal
In #12592 (comment), @Grubba27 has proposed
*Local
methods to keep the synchronous API around. It'd be available only for theLocalCollection
instances, i.e., on the client, when usingLocalCollection
directly, and even on the server, if you connect to a DDP server there. In other words: it'd allow you to use Minimongo synchronously.fetchLocal
,insertLocal
, etc.):LocalCollection
: executes synchronously, returns synchronously.LocalCollection
: ???.LocalCollection
: executes synchronously, returns synchronously.LocalCollection
: ???.As you see, we'd need to fill the above gaps. I see a couple of options, each with some drawbacks:
LocalCollection
s at all.Mongo.Collection
is isomorphic and can be either underneath.LocalCollection
s and throw when called.LocalCollection
s and return some dummy values.I personally would vote for 2. It's easy to create an ESLint plugin that allows
*Local
methods to be executed only in certain paths (e.g., containingclient
), and it prevents any unexpected behavior - if it gets called on a non-LocalCollectio
n, there's an error immediately.Problems
While all read operations are trivial (data is there, just return it), I have a problem with handling write operations. Similarly to what we have in Meteor now, if you execute a write operation on the client, it may fail later without you knowing (you can react in the callback, but that's it).
That's why I propose to start off small, only with read operations first. That'd be:
collection.findOneLocal
.collection.countDocumentsLocal
.collection.estimatedDocumentCountLocal
.cursor.fetchLocal
.Note that I skipped a few methods:
cursor.count
as it's deprecated since 2.9cursor.forEach
,cursor.map
, andcursor[Symbol.iterator]
as these have a trivial replacement, i.e.,cursor.fetchLocal().*
.cursor.observe
andcursor.observeChanges
as they remained synchronous (Release 3.0 observe made back to sync #12573).Beta Was this translation helpful? Give feedback.
All reactions