Add PostgreSQL to vapor project

Viewed 336

I added .package(url: "https://github.com/vapor-community/postgresql-provider.git", .upToNextMajor(from: "2.1.0")) to my Package.swift file, ran vapor update and let it regenerate the Xcode project. When I then add the import of PostgreSQLProvider Xcode can't find it. I already have postgresql and pkg-config installed via homebrew.

I'm using Xcode 9 and Swift 4

2 Answers

Looks like you are using the Swift 4 package manager. The API for it was updated so you have to explicitly add the dependency to the target:

// swift-tools-version:4.0

import PackageDescription

let package = Package(
    name: "Project",
    products: [
        .library(name: "App", targets: ["App"]),
        .executable(name: "Run", targets: ["Run"])
    ],
    dependencies: [
        .package(url: "https://github.com/vapor/vapor.git", .upToNextMajor(from: "2.1.0")),
        .package(url: "https://github.com/vapor/fluent-provider.git", .upToNextMajor(from: "1.2.0")),
        .package(url: "https://github.com/vapor-community/postgresql-provider.git", .exact("2.1.0"))
    ],
    targets: [
        .target(name: "App", dependencies: ["Vapor", "FluentProvider", "PostgreSQLProvider"],
                exclude: [
                    "Config",
                    "Public",
                    "Resources",
                ]),
        .target(name: "Run", dependencies: ["App"]),
        .testTarget(name: "AppTests", dependencies: ["App", "Testing"])
    ]
)

See above, I added the PostgreSQLProvider package to dependencies array for App target.

I had this problem too and wasn't sufficient with vapor xcode because it only regenerate the project and don't fetch the new dependencies like postgree you're adding, to solve this use vapor fetch and later vapor xcode, both commands with the project closed

Related