Use existing database and tables with Sinatra and Datamapper?

I play with Sinatra on the weekend to rate it for a small wiki site for my company.

The problem I am facing is that we already have an existing MySQL database with all our information in it that other processes use, so I can’t recreate it and seed it with data, because then I will have a mess storing two different databases synchronized with the same data.

Can someone give me an example class for connecting to a MySQL database with Sinatra and how can I pull fields from existing columns?

This is an example of my table (from create commands):

CREATE TABLE `serverinfo` (
  `DB` CHAR(10) NOT NULL,
  `SERVERNM` CHAR(30) NOT NULL,
  `INSTANCE` CHAR(30) NOT NULL,
  `LOCATION` CHAR(2) NOT NULL,
  `ROLE` CHAR(15) NOT NULL,
  `HOST` CHAR(180) NOT NULL,
  `STATUS` CHAR(1) NOT NULL DEFAULT 'A',
  PRIMARY KEY (`DB`, `SERVERNM`, `INSTANCE`, `LOCATION`, `ROLE`, `HOST`)
)

, , , Datamapper, / . , .

+4
1

Sinatra . mysql2, , Sequel, ORM, .

README Cheat Sheet , , .

, , , :

require 'sequel'
DB = Sequel.connect('mysql2://your_DB:credentials@host/table')
foo = DB[:serverinfo].select(:DB, :SERVERNM).where(:STATUS => 'A').first

get post Sinatra, . - :

require 'sequel'
require 'json'

DB = Sequel.connect('mysql2://your_DB:credentials@host/table')

get '/' do
  content_type :json
  foo = DB[:serverinfo].select(:DB, :SERVERNM).where(:STATUS => 'A').first
  foo.to_json # would return the hash to the browser
end

Datamapper, Sequel.

+4

Source: https://habr.com/ru/post/1534089/


All Articles