Skip to content

💫 Enum array support

Pre-release
Pre-release
Compare
Choose a tag to compare
@MihaelIsaev MihaelIsaev released this 26 Oct 06:16
· 13 commits to master since this release

Now you're able to use enum array columns in your tables!

Preparation

enum Permission: String, BridgesEnum {    
    case create, edit, delete
}

final class User: Table, Content {
    @Column("id")
    var id: UUID

    @Column("permissions")
    var permissions: [Permission]

    init () {}
    init(permissions: [Permission] = []) {
        self.id = UUID()
        self.permissions = permissions
    }
}

struct CreateUserMigration: TableMigration {
    typealias Table = User
        
    static func prepare(on conn: BridgeConnection) -> EventLoopFuture<Void> {
        createBuilder
            .column("id", .uuid, .primaryKey)
            .column("permissions", .auto(from: [Permission].self), .default("{}"), .notNull)
            .execute(on: conn)
    }
        
    static func revert(on conn: BridgeConnection) -> EventLoopFuture<Void> {
        dropBuilder.execute(on: conn)
    }
}

Usage examples

Where clause

SELECT * FROM "User" WHERE "permissions" @> '{edit}'
SwifQL.select(User.table.*).from(User.table).where(\User.$permissions ||> [Permission.edit])

Convenient update

// let user: User // already fetched user
user.permissions = [.create, .edit, .delete]
user.update(on: \.$id, on: conn)

Pure update

UPDATE "User" SET "permissions" = '{edit,delete}' WHERE "permissions" @> '{edit}'
SwifQL.update(User.table)
      .set[items: \User.$permissions == [Permission.edit, .delete]]
      .where(\User.$permissions ||> [Permission.edit])

Convenient insert

User(permissions: [.create, .edit, .delete]).insert(on: conn)

Pure insert

INSERT INTO "User" ("id", "permissions") VALUES (..., '{create,edit,delete}')
SwifQL.insertInto(User.table, fields: \User.$id, \User.$permissions)
      .values
      .values(UUID(), [Permission.create, .edit, .delete])
      .execute(on: conn)