bin/example in rack-queries-0.2.0 vs bin/example in rack-queries-0.2.1

- old
+ new

@@ -64,76 +64,67 @@ stmt = db.prepare(query) stmt.execute(binds) end end -module Queries - class OrgCountQuery - def self.name - 'Organization count' - end +module Rack + module Queries + create do + name 'Organization count' + desc 'The total count of organizations' - def self.desc - 'The total count of organizations in the system' + run { |_opts| Database.value_from('SELECT COUNT(*) FROM orgs') } end - def run(_opts) - Database.value_from('SELECT COUNT(*) FROM orgs') - end - end + create do + name 'User count' + desc 'The total count of users' - class OrgUsersCountQuery - def org_name - Database.values_from('SELECT name FROM orgs') + run { |_opts| Database.value_from('SELECT COUNT(*) FROM users') } end - def run(opts) - query = <<~SQL - SELECT COUNT(*) FROM users - WHERE org_id = (SELECT id FROM orgs WHERE name = :org_name) - SQL + create do + name 'Users' + desc 'The list of all users' - Database.value_from(query, opts) + run do |_opts| + [%w[id org_id active name]] + + Database.rows_from('SELECT id, org_id, active, name FROM users') + end end - end - class OrgActiveUsersCountQuery - def org_name - Database.values_from('SELECT name FROM orgs') - end + create do + name 'Users in organizations count' + desc 'The total count of users in a given organization' - def run(opts) - query = <<~SQL - SELECT COUNT(*) FROM users - WHERE org_id = (SELECT id FROM orgs WHERE name = :org_name) - AND active = 1 - SQL + opt(:org_name) { Database.values_from('SELECT name FROM orgs') } - Database.value_from(query, opts) - end - end + run do |opts| + query = <<~SQL + SELECT COUNT(*) FROM users + WHERE org_id = (SELECT id FROM orgs WHERE name = :org_name) + SQL - class UserCountQuery - def run(_opts) - Database.value_from('SELECT COUNT(*) FROM users') + Database.value_from(query, opts) + end end - end -end -Rack::Queries.add( - Queries::OrgCountQuery, - Queries::OrgUsersCountQuery, - Queries::OrgActiveUsersCountQuery, - Queries::UserCountQuery -) + create do + name 'Active users in organizations count' + desc 'The total count of active users in a given organization' -Rack::Queries.create do - name 'UsersQuery' - desc 'The list of all users' + opt(:org_name) { Database.values_from('SELECT name FROM orgs') } - run do |_opts| - [%w[id org_id active name]] + - Database.rows_from('SELECT id, org_id, active, name FROM users') + run do |opts| + query = <<~SQL + SELECT COUNT(*) FROM users + WHERE org_id = (SELECT id FROM orgs WHERE name = :org_name) + AND active = 1 + SQL + + Database.value_from(query, opts) + end + end end end Rack::Server.start(app: Rack::Queries::App, server: 'webrick')