Skip to content

Instantly share code, notes, and snippets.

@eldoy
Created December 8, 2017 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eldoy/deb42f2dd593f7732996e7bacac8c4bc to your computer and use it in GitHub Desktop.
Save eldoy/deb42f2dd593f7732996e7bacac8c4bc to your computer and use it in GitHub Desktop.
# Add rethinkdb to Gemfile
# Add to boot.rb: autoload :RethinkDB, 'rethinkdb'
# Set up RabbitMQ queue as well
# Load database settings
config = (YAML.load_file('./config/database.yml') || {})[App.env]
# Connect database
if config
# Read settings or load defaults
name = "#{config['name'] || App.name}_#{App.env}"
# Connect rethinkdb
include RethinkDB::Shortcuts
r.connect(:host => 'localhost', :port => 28015, :db => name).repl
# Create database unless it exists
r.db_create(name).run unless r.db_list.run.include?(name)
# Create tables
table_list = r.db(name).table_list.run
%w[projects].each do |table|
r.db(name).table_create(table).run unless table_list.include?(table)
end
# Listen for changes
cursor = r.table('projects').changes
Thread.new do
cursor.run.each do |document|
token = 'BNquxwicvgrVJxdBidmiIA'
App.channel.publish(token, document.to_json)
end
end
else
puts "Database settings not found for #{App.env} in config/database.yml"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment