Skip to content

Instantly share code, notes, and snippets.

@taji-taji
Last active August 15, 2016 22:29
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 taji-taji/733ba1c3609120cf7a3b6aeacf94551e to your computer and use it in GitHub Desktop.
Save taji-taji/733ba1c3609120cf7a3b6aeacf94551e to your computer and use it in GitHub Desktop.
【サーバーサイドSwift】VaporでMySQLと接続してみた【Vapor v0.16.0】 ref: http://qiita.com/taji-taji/items/a5192aa7da319cdc0a17
brew install mysql
brew link mysql
mysql.server start
$ vapor run
Running Hello...
No command supplied, defaulting to serve...
Preparing User
Prepared 'User'
Database prepared
mysql> use vapor
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+-----------------+
| Tables_in_vapor |
+-----------------+
| fluent |
| users |
+-----------------+
2 rows in set (0.00 sec)
mysql> show columns from users;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
+-------+--------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
curl http://localhost:8080/users -X POST -d "name=hoge"
curl http://localhost:8080/users -X POST -d "name=hoge"
↓ID:1のデータを削除
curl http://localhost:8080/users/1 -X DELETE
↓ID:1のデータを削除
curl http://localhost:8080/users/1 -X DELETE
curl http://localhost:8080/users/1 -X PUT -d "name=fuga"
curl http://localhost:8080/users/1 -X PUT -d "name=fuga"
Server error: dispatch(HTTP.ParserError.streamEmpty)
.Package(url: "https://github.com/vapor/mysql-provider.git", majorVersion: 0, minor: 4)
swift build -Xswiftc -I/usr/local/include/mysql -Xlinker -L/usr/local/lib
vapor build --mysql
Linking ./.build/debug/App
ld: library not found for -lmysqlclient for architecture x86_64
<unknown>:0: error: link command failed with exit code 1 (use -v to see invocation)
$ vapor run
Running Hello...
No command supplied, defaulting to serve...
Preparing User
Prepared 'User'
Database prepared
import Vapor
import VaporMustache
import VaporMySQL
import HTTP
/**
Droplets are service containers that make accessing
all of Vapor's features easy. Just call
`drop.serve()` to serve your application
or `drop.client()` to create a client for
request data from other servers.
*/
let drop = Droplet(
preparations: [User.self], // ←追加
providers: [VaporMustache.Provider.self, VaporMySQL.Provider.self]
)
// 以下略
{
"host": "localhost",
"user": "root",
"password": "",
"database": "vapor"
}
import PackageDescription
let package = Package(
name: "Hello",
dependencies: [
.Package(url: "https://github.com/vapor/vapor.git", majorVersion: 0, minor: 16),
.Package(url: "https://github.com/vapor/vapor-mustache.git", majorVersion: 0, minor: 11),
.Package(url: "https://github.com/vapor/mysql-provider.git", majorVersion: 0, minor: 4)
],
exclude: [
"Config",
"Database",
"Localization",
"Public",
"Resources",
"Tests",
]
)
import Vapor
import Fluent
final class User: Model {
var id: Node?
var name: String
init(name: String) {
self.name = name
}
init(node: Node, in context: Context) throws {
id = try node.extract("id")
name = try node.extract("name")
}
func makeNode() throws -> Node {
return try Node(node: [
"id": id, // ←追加! (1)
"name": name
])
}
static func prepare(_ database: Database) throws {
// ↓追加! (2)
try database.create("users") { users in
users.id()
users.string("name")
}
}
static func revert(_ database: Database) throws {
// ↓追加! (3)
try database.delete("users")
}
}
func index(request: Request) throws -> ResponseRepresentable {
let users = try User.query().all()
return try JSON(users)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment