Last active
August 15, 2016 22:29
-
-
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
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
brew install mysql | |
brew link mysql | |
mysql.server start |
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
$ vapor run | |
Running Hello... | |
No command supplied, defaulting to serve... | |
Preparing User | |
Prepared 'User' | |
Database prepared |
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
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) |
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
curl http://localhost:8080/users -X POST -d "name=hoge" |
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
curl http://localhost:8080/users -X POST -d "name=hoge" |
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
↓ID:1のデータを削除 | |
curl http://localhost:8080/users/1 -X DELETE |
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
↓ID:1のデータを削除 | |
curl http://localhost:8080/users/1 -X DELETE |
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
curl http://localhost:8080/users/1 -X PUT -d "name=fuga" |
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
curl http://localhost:8080/users/1 -X PUT -d "name=fuga" |
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
Server error: dispatch(HTTP.ParserError.streamEmpty) |
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
.Package(url: "https://github.com/vapor/mysql-provider.git", majorVersion: 0, minor: 4) |
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
swift build -Xswiftc -I/usr/local/include/mysql -Xlinker -L/usr/local/lib |
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
vapor build --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
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) |
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
$ vapor run | |
Running Hello... | |
No command supplied, defaulting to serve... | |
Preparing User | |
Prepared 'User' | |
Database prepared |
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
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] | |
) | |
// 以下略 |
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
{ | |
"host": "localhost", | |
"user": "root", | |
"password": "", | |
"database": "vapor" | |
} |
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
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", | |
] | |
) | |
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
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") | |
} | |
} |
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
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