New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

array support #4

Merged
merged 1 commit into from Jan 23, 2018

Conversation

Projects
None yet
2 participants
@tanner0101
Copy link
Member

tanner0101 commented Jan 23, 2018

Adds array support conveniences to Fluent PostgreSQL.

  • Swift Array automatically becomes T[] column
  • Swift Dictionary automatically becomes JSONB column
  • Custom types (such as Pet below) can be conformed to PostgreSQLType
    • PostgreSQLJSONType for easy JSONB conformance (where Self is Codable)
    • PostgreSQLArrayType for easy T[] conformance (where Element is a PostgreSQLType)
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
)

@tanner0101 tanner0101 added this to the 1.0.0 milestone Jan 23, 2018

@tanner0101 tanner0101 self-assigned this Jan 23, 2018

@tanner0101 tanner0101 merged commit 6cb278d into beta Jan 23, 2018

2 checks passed

ci/circleci: linux Your tests passed on CircleCI!
Details
ci/circleci: macos Your tests passed on CircleCI!
Details

@tanner0101 tanner0101 deleted the array-support branch Jan 23, 2018

@tanner0101 tanner0101 added this to Done in Vapor 3 Feb 12, 2018

@abbasmousavi

This comment has been minimized.

Copy link

abbasmousavi commented Feb 27, 2018

@tanner0101 Please take a look at this: #20

@tanner0101

This comment has been minimized.

Copy link
Member Author

tanner0101 commented Feb 28, 2018

@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

This comment has been minimized.

Copy link

abbasmousavi commented Mar 13, 2018

@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]?
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment