About create enum

First of all, I would like to thank the authors for creating such an excellent framework that gives iOS developers the opportunity to write back-end applications in swift.

Ask a question about creating an enumeration

struct CreateUser: Migration {
    
    func prepare(on database: Database) -> EventLoopFuture<Void> {
        
        database.enum("user_type")
            .case("admin")
            .case("standard")
            .case("restricted")
            .create()
            .flatMap { userType in
                database.schema(User.schema)
                    .id()
                    .field(User.FieldKeys.username, .string, .required)
                    .field(User.FieldKeys.password, .string, .required)
                    .field(User.FieldKeys.deletedAt, .datetime)
                    .field(User.FieldKeys.userType, userType, .required)
                    .unique(on: User.FieldKeys.username)
                    .create()
            }
    }
    
    func revert(on database: Database) -> EventLoopFuture<Void> {
        database.schema(User.schema).delete()
    }
}

Implement it as an AsyncMigration. Am I doing this right?

Screen Shot 2022-10-21 at 13.06.49

How do I remove this warning? Use _ = ?

In addition, can the enumeration be created using a separate file, like this

struct CreateEnum: AsyncMigration {
    
    func prepare(on database: Database) async throws {
        
       _ =  try await database.enum("user_type")
            .case("admin")
            .case("standard")
            .case("restricted")
            .create()
    }
    
    func revert(on database: Database) async throws {
        try await database.enum("user_type").delete()
    }
}

What if the user table has two enumerated values? For example, usertype and gender
Screen Shot 2022-10-21 at 13.15.55

struct CreateUser: Migration {

    func prepare(on database: Database) -> EventLoopFuture<Void> {

       let userType = database.enum("user_type")
                              .case("admin")
                              .case("standard")
                              .case("restricted")
                              .create()
        
        let gender = database.enum("gender")
                             .case("male")
                             .case("female")
                             .case("unknow")
                             .create()
        
        userType.and(gender).flatMap { userType, gender in
            database.schema(User.schema)
                .id()
                .field(User.FieldKeys.username, .string, .required)
                .field(User.FieldKeys.password, .string, .required)
                .field(User.FieldKeys.deletedAt, .datetime)
                .field(User.FieldKeys.userType, userType, .required)
                .field(User.FieldKeys.gender, gender, .required)
                .unique(on: User.FieldKeys.username)
                .create()
        }
    }

    func revert(on database: Database) -> EventLoopFuture<Void> {
        database.schema(User.schema).delete()
    }
}

Is that correct?

Thanks for answering my initial questions

Yes. I would keep the reading of the enums as a checking and clarity.

1 Like

Yeah if you don’t want to use the enum in the same migration as you create it you can safely underscore the returned value. However in the example you show you create it (which returns the same value as read) and then read it straight away. In those instances you can do:

struct CreateUser: Migration {
    
    func prepare(on database: Database) async throws {
        let userType = try await database.enum("user_type")
            .case("admin")
            .case("standard")
            .case("restricted")
            .create()
        try await database.schema(User.schema)
            .id()
            .field(User.FieldKeys.username, .string, .required)
            .field(User.FieldKeys.password, .string, .required)
            .field(User.FieldKeys.deletedAt, .datetime)
            .field(User.FieldKeys.userType, userType, .required)
            .unique(on: User.FieldKeys.username)
            .create()
    }
    
    func revert(on database: Database) async throws {
        try await database.schema(User.schema).delete()
    }
}
2 Likes

Thanks for answering my question
:grinning:

Thanks for answering my question.
I didn’t notice that create and read return the same type.
Once you’ve created it, just use it
thank you
:grinning: