1

I have in mind to develop a Ruby on Rails app with two different databases.

In the primary SQL db - let's say MySQL, for instance - I'd keep my app items, e.g. user profiling, user-user interactions and, in general, everything that's bound to a model, anything that I already know how is made.

Now the best part: I'd like to add a secondary No-SQL db - let's say MongoDB, for instance - where I want to put other documents that I don't know which fileds they may contain, not bound to any model. End-users, while interacting with the app, should be able to add their own custom documents and create their collections, making queries and also creating views - I mean views inside the db, I'm not talking about web pages - to aggregate not-so-well-formatted records.

What do I mean for not-so-well-formatted records? For example, let's say that a user inserts a record like this one:

{ "name":"Bill", "surname":"Ball" }

and then another record like this one:

{ "firstname":"Tim", "lastname":"Tam" }

As you can see, the fields name and firstname are meant to be the same field, while they're actually different feilds; at the same time, surname and lastname are meant to be the same, but they're different because the user did a sloppy job while inserting those records.

I'd like the app could notify the discrepancy to the user so he can choose wether to aggregate those two fields or keep them separated; if he chooses to aggregate them, he should be able to define - in a very simple and friendly way - a view into the db, maybe applying some kind of alias to every field. Maybe even defining the type of each field, e.g. strings, dates, integers, etc. So, after the user makes few clicks, the view could look like:

{ "firstname":"Bill", "lastname":"Ball" }
{ "firstname":"Tim",  "lastname":"Tam" }

while preserving the original/raw data inside the collection. I already know how to do this with MongoDB, by the way, but still I don't know if this could be the right approach.

I don't want the user to be obliged to create any model for his data, I'd simply want to let him throw raw documents into the db and eventually autonomously "fixing" the discrepancies after, so he can continue querying his collections without worrying about those discrepancies in naming convention.

So here's my question: is this a good approach to solve my problem? I already know that I can have multiple dbs attached to my Rails app, but is this structure convenient? Or is there something better?

1 Answer 1

1

Having multiple databases to your application can be fine, if you find requirements that are suited to a certain type of database and not the other. And it's good that you have a clear separation of concerns between them.

But, it's adding to your application's complexity and make it hard to maintain or test later, especially if you will find later that you need to integrate between them.

Regarding your requirement to "throw" raw documents to your MongoDB database that ultimately define the structure for later documents added, and throwing errors to your user.

(Based on ActiveRecord) you can use ActiveRecord to create aliases to columns using create_alias, and have a Helper method (using ActiveSupport extensions to NameError) that may ask the user for the proper aliases using data received from your database.

It may require low-level handling with the MongoDB database circumventing the mongoid uniform interface, and dynamic instantiation of new models and aliases (that should be checked). In my opinion, it'll be hard to maintain, the user may insert bad aliases for your keys and you'll have to remember the aliases.

If remembering user aliases isn't a problem, just have an error and inform the user about the current structure so he'll insert the data appropriately.

1
  • 1
    To tell you the truth, having both MySQL and MongoDB was my initial idea, later I realized it would've been better just having MongoDB; I just wanted to point out my need to keep separated "items" that have a model from other "items" that doesn't have one. I'll have to look further into ActiveRecord and mongoid doc to fully understand how they work. Thank you for your suggestions. Jan 9, 2019 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.