mohamed saad
mohamed saad

Reputation: 95

Get data from another nodejs app to save it in another database

Suppose that a server built with nodejs and express and connected with mySQL Database. This app wants to get data from a MongoDB database and add edits to it.

For example, the server wants to get the users data from another nodejs app connected with mongoDb and add voted field to each user, then save them to mySQL database.

What's the best design and approach for this situation ?

Upvotes: 0

Views: 433

Answers (1)

Ahmed Abdelhak
Ahmed Abdelhak

Reputation: 239

1- Simple "Stupid" solution (with coupling between services):

  • create REST endpoint at the application (A) which returns the necessary data, and from your app (B) call this endpoint directly (http call) (fetch BaseURL from your configs or route if you're using API Gateway), add the votes into your domain(business layer in app B) and save to the MySql DB.

2- a little bit complex one ( which guarantee no coupling between services)

  • is to use a message broker like "RabbitMQ", and depend on the "Pub/Sub" model, where you publish a message to the broker (when app A add a new User to MongoDB), then you subscribe to this from your app B, to fetch the message (this message will include the data you want "the user object" mutate it, then save to Mysql )

  • this simple answer, supposed no data synchronization between the two databases, as you're going to mutate the "User" in one of them, otherwise, if you want synchronization, you need to fire an event after mutation done, and the service A will subscribe to this fetches the message, update its MongoDB

Upvotes: 1

Related Questions