Last active
October 21, 2016 13:54
Star
You must be signed in to star a gist
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
for followers, DB.mapping is described here https://github.com/crystal-lang/crystal-db/blob/4f724475e0b929fe028186e7bce8a656fcb4f46d/src/db/mapping.cr and "from_rs" means "from result set" :|
You might be able to avoid having to call close on the resultset by putting it in a block.