· 8 years ago · Dec 11, 2017, 01:38 AM
1# Add rethinkdb to Gemfile
2# Add to boot.rb: autoload :RethinkDB, 'rethinkdb'
3# Set up RabbitMQ queue as well
4
5# Load database settings
6config = (YAML.load_file('./config/database.yml') || {})[App.env]
7
8# Connect database
9if config
10
11 # Read settings or load defaults
12 name = "#{config['name'] || App.name}_#{App.env}"
13
14 # Connect rethinkdb
15 include RethinkDB::Shortcuts
16 r.connect(:host => 'localhost', :port => 28015, :db => name).repl
17
18 # Create database unless it exists
19 r.db_create(name).run unless r.db_list.run.include?(name)
20
21 # Create tables
22 table_list = r.db(name).table_list.run
23 %w[projects].each do |table|
24 r.db(name).table_create(table).run unless table_list.include?(table)
25 end
26
27 # Listen for changes
28 cursor = r.table('projects').changes
29 Thread.new do
30 cursor.run.each do |document|
31 token = 'BNquxwicvgrVJxdBidmiIA'
32 App.channel.publish(token, document.to_json)
33 end
34 end
35
36else
37 puts "Database settings not found for #{App.env} in config/database.yml"
38end