Skip to content

Instantly share code, notes, and snippets.

@rdp
Forked from crisward/mess.cr
Created September 28, 2016 21:25
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 rdp/f099508e26934d20938c4ffe37ee3298 to your computer and use it in GitHub Desktop.
Save rdp/f099508e26934d20938c4ffe37ee3298 to your computer and use it in GitHub Desktop.
kemal mysql
require "./mess/*"
require "kemal"
require "json"
require "mysql"
require "pool/connection"
db = ConnectionPool.new(capacity: 25, timeout: 0.01) do
DB.open(ENV["DATABASE_URL"])
end
class User
DB.mapping({
id: Int32,
first_name: {type: String, nilable: true},
surname: {type: String, nilable: true},
})
JSON.mapping({
id: Int32,
first_name: {type: String, nilable: true},
surname: {type: String, nilable: true},
})
end
get "/" do |env|
db.connection do |conn|
env.response.content_type = "application/json"
rs = conn.query("SELECT id,first_name,surname FROM users")
users = User.from_rs(rs)
rs.close #must do this to allow the connection to be reused
return users.to_json
end
end
Kemal.run
name: mess
version: 0.1.0
authors:
- Cris Ward
license: MIT
dependencies:
kemal:
github: sdogruyol/kemal
branch: master
mysql:
github: crystal-lang/crystal-mysql
branch: master
pool:
github: ysbaddaden/pool
branch: master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment