Frage

I'm experimenting great Sinatra environment to realize a json API server that access data from a "legacy" db, (an existent database, already in production).

I'm enjoing sinatra-activerecord gem to access my data. as simple as include in the Sinatra app the simple decalration:

class Exam < ActiveRecord::Base
end

I'm enjoing too tux (the rails console equivalent), to query my models (tables).

Now, I have an Architectural doubt about two different scenarios explained here below, in the case of inserting new records via an API call (let say a POST client request to Create/update a record):

Scenario 1. Existent DB as part of (my) Rails project, already in prod

suppose my production database (postgresql instance) is part of a Rails project developed by myself, so I have my Activerecord Models .rb code that contain quite complex validations methods; By example the above mentioned Model exam.rb consiste of a thousand of line of some complex regex validation methods...

In that scenario, what is the best technical solution to avoid Model source code duplication (and so poossible mismatch errors) ? My I have to do in the Sinatra app, an OS symbolik link to the original Rails model exam.rb ... ?

Scenario 2. Existent DB developed by someone else

so suppose I have access at the database tables, but I do not have any source code about applicative data "validations" (maybe developed in Java or any code I do not access for some reasons...)

In that scenario, I think the way is to re-write validations in ruby-activerecord; right ?

sorry for my long post

giorgio about.me/solyaris

War es hilfreich?

Lösung

Scenario 1. Existent DB as part of (my) Rails project, already in prod

You have three options (in my opinion):

  1. Mount the Sinatra app via Rack along with the Rails app (in the config.ru), and then it will be able to access the same code as the Rails app.

  2. Make the database code into its own library, put it in a gem, get both apps to require the gem.

  3. Wrap the existing DB in a data API, one that serves no pages, only data (this may be what you're trying to do with Sinatra anyway). Take the direct access to the database away from the Rails app and get it to make calls to the new API just like any other client.

I believe option 2 is better than option 1, and option 3 is better than option 2. If I was going to move to option 3, then I'd probably do 1 first, then 2, then 3, as each can use the previous option to make it easier.

Scenario 2. Existent DB developed by someone else

I'm not certain I fully understand, but yes, you'd have to re-write validations. A well designed database will mean that some validations should already exist in the form of constraints, but not always.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top