[SOLVED] Transactions with MongoDB + Meteor Methods

Did you already convert your mongodb instance to a replica set? See: Convert a Standalone to a Replica Set

1 Like

Thanks @peterfkruger . I didn’t have it converted, but now it is already working. It was a little complex at the beginning, because I had my mongo server with authentication enabled. So, I had to disable it to facilitate this part of conversion.

Also, I had the doubt if it was possible to have a Replica Set server with only one member (the primary), and effectively it does is possible, on this way, I can develop on localhost database without the need of configure other 2 nodes. more info

I share the following links related to this topic:

Alternatively, there is a global NPM dependency (run-rs) that help to configure a Replica Set in a easy way. This is an article of how to use it.

I wonder why is necessary to have a replica set to run transactions. Hopefully the mongo team will enable transactions for standalone servers.

Fortunately, for cloud database exists Mongo Atlas, which provides replica set mongo servers even in free tier. For other options (e.g. docker), a custom configuration will be needed.

2 Likes

@robfallows , not sure if you ever used Validated Methods. Would you happen to know how to syntax an async Validated Method? There is no reference to async in the MDG page so this package might need un upgrade. GitHub - meteor/validated-method: Meteor methods with better scoping, argument checking, and good defaults.

You can make a Validated Method async either by declaring the run function async or by returning a Promise.

With async:

new ValidatedMethod({
  name: "asyncValidatedMethod",
  validate: new SimpleSchema({
    param: { type: Number }
  }).validator(),
  async run({ param }) {
    return param * 2;
  }
});

Returning a Promise:

new ValidatedMethod({
  name: "asyncValidatedMethod2",
  validate: new SimpleSchema({
    param: { type: Number }
  }).validator(),
  run({ param }) {
    return new Promise(resolve => {
      resolve(param * 2);
    });
  }
});
2 Likes

That’s great. Thank you

TypeScript says these two do not need await. Is it safer to put await there anyway in case of an API change or do we assume these will never change?

Maybe its time for Meteor to officially support transactions?

8 Likes

That would be great!

Yes, would be a killer feature for Meteor as lack of official support for transactions means it is hard to justify using Meteor + MongoDB combination at all.

Transactions can be used but you need to expose raw collections…but yeah I agree official support would be great.

2 Likes

5t15wa

5 Likes