Swift國內(nèi)社區(qū): SwiftMic
Provider
使得給 Vapor 添加功能和第三方 Package
變得更容易遭赂,只要遵循 Provider
協(xié)議即可。
添加 Package
以 MySQL Provider 為例
編輯 Package.swift
import PackageDescription
let package = Package(
name: "vapor_test",
dependencies: [
.Package(url: "https://github.com/vapor/vapor.git", majorVersion: 0, minor: 16),
.Package(url: "https://github.com/vapor/mysql-provider.git", majorVersion: 0, minor: 4)
],
exclude: [
"Config",
"Database",
"Localization",
"Public",
"Resources",
"Tests",
]
)
使用
編輯 main.swift
import Vapor
import VaporMySQL
let mysql = try VaporMySQL.Provider(host: "localhost", user: "xxx", password: "xxx", database: "xxx")
let drop = Droplet(initializedProviders: [mysql])
let result = try mysql.driver.mysql("select version();")
let mysqlVersion = result[0]?["version()"]?.string
print("mysqlVersion = \(mysqlVersion)")
輸出
mysqlVersion = Optional("5.7.13")
注意: Provider
的具體使用狂鞋,請參考相應(yīng) Provider
的文檔。
Go to Vapor系列教程 - 目錄