vapor / fluent-postgres-driver

🐘 PostgreSQL driver for Fluent.
MIT License
149 stars 53 forks source link

array support #4

Closed tanner0101 closed 6 years ago

tanner0101 commented 6 years ago

Adds array support conveniences to Fluent PostgreSQL.

struct Pet: PostgreSQLJSONType {
    var name: String
}

final class User: PostgreSQLModel, Migration {
    static let idKey = \User.id
    var id: Int?
    var name: String
    var age: Int?
    var favoriteColors: [String]
    var pet: Pet
    var dict: [String: String]

    init(id: Int? = nil, name: String, pet: Pet) {
        self.favoriteColors = []
        self.dict = [:]
        self.id = id
        self.name = name
        self.pet = pet
    }
}

Results in:

CREATE TABLE "users" (
    "id" BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, 
    "name" TEXT NOT NULL, 
    "age" BIGINT, 
    "favoriteColors" TEXT[] NOT NULL, 
    "pet" JSONB NOT NULL, 
    "dict" JSONB NOT NULL
)
abbasmousavi commented 6 years ago

@tanner0101 Please take a look at this: https://github.com/vapor/fluent-postgresql/issues/20

tanner0101 commented 6 years ago

@abbasmousavi take a look at the release notes here: https://github.com/vapor/fluent-postgresql/releases/tag/1.0.0-rc.1.1

It's due to an incomplete implementation of conditional conformance by Swift. Nothing we can do about it :(

abbasmousavi commented 6 years ago

@tanner0101 , thanks for the explanation. What about an array of a custom type, is it possible? for example

struct Pet: PostgreSQLJSONType, Codable {
    var name: String
    var type: String
}

struct Person: PostgreSQLModel, Migration{

    var id: Int?

    var title: String
    var pets: [Pet]?
}