Vapor系列教程 - Provider

Swift国内社区: SwiftMic


Provider 使得给 Vapor 添加功能和第三方 Package 变得更容易,只要遵循 Provider 协议即可。

添加 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 的具体使用,请参考相应 Provider 的文档。


Go to Vapor系列教程 - 目录

你可能感兴趣的:(Vapor系列教程 - Provider)