-
-
Save rdp/f099508e26934d20938c4ffe37ee3298 to your computer and use it in GitHub Desktop.
kemal mysql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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